viiccwen commented on code in PR #805:
URL: https://github.com/apache/mahout/pull/805#discussion_r2691741479
##########
qdp/qdp-core/tests/preprocessing.rs:
##########
@@ -105,3 +105,48 @@ fn test_calculate_l2_norm_matches_sequential_sum() {
let norm_sequential = data.iter().map(|x| x * x).sum::<f64>().sqrt();
assert!((norm_parallel - norm_sequential).abs() < 1e-10);
}
+
+#[test]
+fn test_calculate_l2_norm_invalid_values() {
+ let cases = [
+ ("NaN", f64::NAN, "NaN"),
+ ("+Inf", f64::INFINITY, "Infinity"),
+ ("-Inf", f64::NEG_INFINITY, "Infinity"),
+ ];
+
+ for (label, bad_value, expected_fragment) in cases {
+ let data = vec![1.0, bad_value, 3.0];
+ let result = Preprocessor::calculate_l2_norm(&data);
+ assert!(
+ matches!(result, Err(MahoutError::InvalidInput(msg)) if
msg.contains(expected_fragment)),
+ "case {label} did not produce expected error"
+ );
+ }
+}
+
+#[test]
+fn test_calculate_batch_l2_norms_invalid_values() {
+ let cases = [
+ ("NaN", f64::NAN, "NaN"),
+ ("+Inf", f64::INFINITY, "Infinity"),
+ ("-Inf", f64::NEG_INFINITY, "Infinity"),
+ ];
+
+ for (label, bad_value, expected_fragment) in cases {
+ let batch_data = vec![1.0, 2.0, bad_value, 4.0];
+ let result = Preprocessor::calculate_batch_l2_norms(&batch_data, 2, 2);
+ assert!(
+ matches!(result, Err(MahoutError::InvalidInput(msg)) if
msg.contains(expected_fragment)),
+ "case {label} did not produce expected error"
+ );
+ }
+}
+
+#[test]
+fn test_calculate_batch_l2_norms_success() {
+ let batch_data = vec![3.0, 4.0, 5.0, 12.0];
+ let norms = Preprocessor::calculate_batch_l2_norms(&batch_data, 2,
2).unwrap();
+ assert_eq!(norms.len(), 2);
+ assert!((norms[0] - 5.0).abs() < 1e-10); // sqrt(3^2 + 4^2) = 5
+ assert!((norms[1] - 13.0).abs() < 1e-10); // sqrt(5^2 + 12^2) = 13
Review Comment:
Thx for the suggestion!
I followed the existing pattern used earlier in the code, but I agree that
using `approx::assert_relative_eq` would improve readability.
I’ll add `approx` as a dev-dependency and update the tests accordingly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]