This is an automated email from the ASF dual-hosted git repository.
guanmingchiu pushed a commit to branch dev-qdp
in repository https://gitbox.apache.org/repos/asf/mahout.git
The following commit(s) were added to refs/heads/dev-qdp by this push:
new c453f5fcb [QDP] add unit test for qdp-core (#659)
c453f5fcb is described below
commit c453f5fcbd354b32da6152cf9f23188bcafc67da
Author: KUAN-HAO HUANG <[email protected]>
AuthorDate: Sat Nov 29 22:50:52 2025 +0800
[QDP] add unit test for qdp-core (#659)
---
qdp/qdp-core/tests/common/mod.rs | 4 +
qdp/qdp-core/tests/validation.rs | 172 +++++++++++++++++++++++++++++++++++++++
2 files changed, 176 insertions(+)
diff --git a/qdp/qdp-core/tests/common/mod.rs b/qdp/qdp-core/tests/common/mod.rs
new file mode 100644
index 000000000..d13e4623d
--- /dev/null
+++ b/qdp/qdp-core/tests/common/mod.rs
@@ -0,0 +1,4 @@
+/// Create test data with normalized values
+pub fn create_test_data(size: usize) -> Vec<f64> {
+ (0..size).map(|i| (i as f64) / (size as f64)).collect()
+}
diff --git a/qdp/qdp-core/tests/validation.rs b/qdp/qdp-core/tests/validation.rs
new file mode 100644
index 000000000..e8a1a3443
--- /dev/null
+++ b/qdp/qdp-core/tests/validation.rs
@@ -0,0 +1,172 @@
+// Input validation and error handling tests
+
+use qdp_core::{QdpEngine, MahoutError};
+
+mod common;
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_input_validation_invalid_strategy() {
+ println!("Testing invalid strategy name rejection...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data = common::create_test_data(100);
+
+ let result = engine.encode(&data, 7, "invalid_strategy");
+ assert!(result.is_err(), "Should reject invalid strategy");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("Unknown encoder"), "Error message should
mention unknown encoder");
+ println!("PASS: Correctly rejected invalid strategy: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for invalid strategy"),
+ }
+}
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_input_validation_qubit_mismatch() {
+ println!("Testing qubit size validation...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data = common::create_test_data(100);
+
+ // 100 elements need 7 qubits (2^7=128), but we request 6 (2^6=64)
+ let result = engine.encode(&data, 6, "amplitude");
+ assert!(result.is_err(), "Should reject data larger than state vector");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("exceeds state vector size"),
+ "Error should mention size mismatch");
+ println!("PASS: Correctly rejected qubit mismatch: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for size mismatch"),
+ }
+}
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_input_validation_zero_qubits() {
+ println!("Testing zero qubits rejection...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data = common::create_test_data(10);
+
+ let result = engine.encode(&data, 0, "amplitude");
+ assert!(result.is_err(), "Should reject zero qubits");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("at least 1"), "Error should mention minimum
qubit requirement");
+ println!("PASS: Correctly rejected zero qubits: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for zero qubits"),
+ }
+}
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_input_validation_max_qubits() {
+ println!("Testing maximum qubit limit (30)...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data = common::create_test_data(100);
+
+ let result = engine.encode(&data, 35, "amplitude");
+ assert!(result.is_err(), "Should reject excessive qubits");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("exceeds") && msg.contains("30"),
+ "Error should mention 30 qubit limit");
+ println!("PASS: Correctly rejected excessive qubits: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for max qubits"),
+ }
+}
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_empty_data() {
+ println!("Testing empty data rejection...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data: Vec<f64> = vec![];
+
+ let result = engine.encode(&data, 5, "amplitude");
+ assert!(result.is_err(), "Should reject empty data");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("empty"), "Error should mention empty data");
+ println!("PASS: Correctly rejected empty data: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for empty data"),
+ }
+}
+
+#[test]
+#[cfg(target_os = "linux")]
+fn test_zero_norm_data() {
+ println!("Testing zero-norm data rejection...");
+
+ let engine = match QdpEngine::new(0) {
+ Ok(e) => e,
+ Err(_) => return,
+ };
+
+ let data = vec![0.0; 128];
+
+ let result = engine.encode(&data, 7, "amplitude");
+ assert!(result.is_err(), "Should reject zero-norm data");
+
+ match result {
+ Err(MahoutError::InvalidInput(msg)) => {
+ assert!(msg.contains("zero norm"), "Error should mention zero
norm");
+ println!("PASS: Correctly rejected zero-norm data: {}", msg);
+ }
+ _ => panic!("Expected InvalidInput error for zero norm"),
+ }
+}
+
+#[test]
+fn test_error_types() {
+ let err1 = MahoutError::InvalidInput("test".to_string());
+ let err2 = MahoutError::Cuda("test cuda error".to_string());
+
+ assert!(format!("{}", err1).contains("Invalid input"));
+ assert!(format!("{}", err2).contains("CUDA error"));
+}
+
+#[test]
+#[cfg(not(target_os = "linux"))]
+fn test_non_linux_graceful_failure() {
+ let result = QdpEngine::new(0);
+ assert!(result.is_err());
+
+ if let Err(e) = result {
+ println!("PASS: Non-Linux platform correctly rejected: {}", e);
+ }
+}