Modern Apps git

← Modern-Apps.git

vision: add vision_core Rust unit tests

Commit
e8c66a7485706a6852a565962831be15381b1d97
Author
Vayun Mathur <[email protected]>
When
2026-11-17 21:26 UTC (18 days ago)
Parents
2309425
77 new tests (7 -> 84): linalg (vector/matrix3/dense/Jacobi eigen and SVD), Rodrigues and camera intrinsics, homography DLT and RANSAC, Rgba/Gray buffers, and ORB feature detection/matching on synthetic images. Sibling *_tests.rs files only; no production changes. cargo test -p vision_core green; line coverage 93.0%.

Diff

library/vision/src/main/rust/src/camera.rs
diff --git a/library/vision/src/main/rust/src/camera.rs b/library/vision/src/main/rust/src/camera.rsindex 4e93f29f93..466b0bdf0a 100644--- a/library/vision/src/main/rust/src/camera.rs+++ b/library/vision/src/main/rust/src/camera.rs@@ -72,3 +72,5 @@ pub fn mat_to_rodrigues(m: &Matrix3<f64>) -> Vector3<f64> {     let mult = theta / (2.0 * s);     Vector3::new(rx, ry, rz) * mult }++#[cfg(test)] #[path = "camera_tests.rs"] mod camera_tests;
library/vision/src/main/rust/src/camera_tests.rs
diff --git a/library/vision/src/main/rust/src/camera_tests.rs b/library/vision/src/main/rust/src/camera_tests.rsnew file mode 100644index 0000000000..2c72860be0--- /dev/null+++ b/library/vision/src/main/rust/src/camera_tests.rs@@ -0,0 +1,55 @@+use crate::camera::{mat_to_rodrigues, rodrigues_to_mat, CameraParams};+use crate::linalg::{Matrix3, Vector3};+fn close(a: f64, b: f64, t: f64) { assert!((a-b).abs() <= t, "{a} vs {b}"); }+fn mat_close(m: &Matrix3<f64>, w: &[f64;9], t: f64) { for r in 0..3 { for c in 0..3 { close(m[(r,c)], w[r*3+c], t); } } }+#[test] fn intrinsics_k() {+    let cam = CameraParams { focal: 100.0, aspect: 2.0, ppx: 10.0, ppy: 20.0, r: Matrix3::identity() };+    mat_close(&cam.k(), &[100.0,0.0,10.0, 0.0,200.0,20.0, 0.0,0.0,1.0], 0.0);+    let cam = CameraParams { focal: 50.0, aspect: 1.0, ppx: 0.0, ppy: 0.0, r: Matrix3::identity() };+    mat_close(&cam.k(), &[50.0,0.0,0.0, 0.0,50.0,0.0, 0.0,0.0,1.0], 0.0);+}+#[test] fn rodrigues_zero_and_tiny_are_identity() {+    mat_close(&rodrigues_to_mat(Vector3::zeros()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0], 0.0);+    mat_close(&rodrigues_to_mat(Vector3::new(1e-13, 0.0, 0.0)), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0], 0.0);+}+#[test] fn rodrigues_quarter_turns() {+    let h = std::f64::consts::FRAC_PI_2;+    // Rz(90°): x->y, y->-x+    mat_close(&rodrigues_to_mat(Vector3::new(0.0,0.0,h)), &[0.0,-1.0,0.0, 1.0,0.0,0.0, 0.0,0.0,1.0], 1e-12);+    // Rx(90°): y->z, z->-y+    mat_close(&rodrigues_to_mat(Vector3::new(h,0.0,0.0)), &[1.0,0.0,0.0, 0.0,0.0,-1.0, 0.0,1.0,0.0], 1e-12);+    // Ry(90°): z->x, x->-z+    mat_close(&rodrigues_to_mat(Vector3::new(0.0,h,0.0)), &[0.0,0.0,1.0, 0.0,1.0,0.0, -1.0,0.0,0.0], 1e-12);+}+#[test] fn rodrigues_half_turn_and_orthonormality() {+    let m = rodrigues_to_mat(Vector3::new(std::f64::consts::PI, 0.0, 0.0));+    mat_close(&m, &[1.0,0.0,0.0, 0.0,-1.0,0.0, 0.0,0.0,-1.0], 1e-12);+    close(m.determinant(), 1.0, 1e-12);+    let m = rodrigues_to_mat(Vector3::new(0.3, -0.5, 0.8));+    mat_close(&(m * m.transpose()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0], 1e-12);+    close(m.determinant(), 1.0, 1e-12);+}+#[test] fn rodrigues_roundtrip() {+    for v in [Vector3::new(0.1,0.2,0.3), Vector3::new(-0.5,0.4,0.1), Vector3::new(0.0,0.0,1.0), Vector3::new(1.0,-1.0,0.5)] {+        let back = mat_to_rodrigues(&rodrigues_to_mat(v));+        close(back.x, v.x, 1e-9); close(back.y, v.y, 1e-9); close(back.z, v.z, 1e-9);+    }+}+#[test] fn mat_to_rodrigues_identity_and_quarter() {+    let z = mat_to_rodrigues(&Matrix3::identity()); close(z.norm(), 0.0, 1e-12);+    let v = mat_to_rodrigues(&Matrix3::new(0.0,-1.0,0.0, 1.0,0.0,0.0, 0.0,0.0,1.0));+    close(v.x, 0.0, 1e-9); close(v.y, 0.0, 1e-9); close(v.z, std::f64::consts::FRAC_PI_2, 1e-9);+}+#[test] fn mat_to_rodrigues_pi_rotations_reconstruct() {+    for m in [Matrix3::new(1.0,0.0,0.0, 0.0,-1.0,0.0, 0.0,0.0,-1.0), Matrix3::new(-1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,-1.0), Matrix3::new(-1.0,0.0,0.0, 0.0,-1.0,0.0, 0.0,0.0,1.0)] {+        let v = mat_to_rodrigues(&m);+        close(v.norm(), std::f64::consts::PI, 1e-9);+        mat_close(&rodrigues_to_mat(v), &[m[(0,0)],m[(0,1)],m[(0,2)],m[(1,0)],m[(1,1)],m[(1,2)],m[(2,0)],m[(2,1)],m[(2,2)]], 1e-9);+    }+}+#[test] fn mat_to_rodrigues_scaled_input_is_orthonormalized() {+    // 2*Rz(90°) should still yield the Rz(90°) axis/angle via SVD polar factor+    let m = Matrix3::new(0.0,-2.0,0.0, 2.0,0.0,0.0, 0.0,0.0,2.0);+    let v = mat_to_rodrigues(&m);+    close(v.z, std::f64::consts::FRAC_PI_2, 1e-9);+}
library/vision/src/main/rust/src/features.rs
diff --git a/library/vision/src/main/rust/src/features.rs b/library/vision/src/main/rust/src/features.rsindex 708ea1e4d7..f00f77279e 100644--- a/library/vision/src/main/rust/src/features.rs+++ b/library/vision/src/main/rust/src/features.rs@@ -331,3 +331,5 @@ pub fn match_features(a: &Features, b: &Features, ratio: f32) -> Vec<(usize, usi     }     out }++#[cfg(test)] #[path = "features_tests.rs"] mod features_tests;
library/vision/src/main/rust/src/features_tests.rs
diff --git a/library/vision/src/main/rust/src/features_tests.rs b/library/vision/src/main/rust/src/features_tests.rsnew file mode 100644index 0000000000..4eb6cee0bc--- /dev/null+++ b/library/vision/src/main/rust/src/features_tests.rs@@ -0,0 +1,72 @@+use crate::features::{detect_and_describe, detect_and_describe_orb, match_features, Features, ORB_DEFAULT_NFEATURES, ORB_NLEVELS, ORB_PATCH_SIZE, ORB_SCALE_FACTOR};+use crate::imgbuf::Gray;+fn feats(desc: Vec<[u8;32]>) -> Features { Features { kps: Vec::new(), desc, w: 0, h: 0 } }+#[test] fn orb_constants_match_opencv_defaults() {+    assert_eq!(ORB_DEFAULT_NFEATURES, 500); assert_eq!(ORB_NLEVELS, 8); assert_eq!(ORB_PATCH_SIZE, 31);+    assert!((ORB_SCALE_FACTOR - 1.2).abs() < 1e-6);+}+#[test] fn flat_and_tiny_images_yield_no_features() {+    // border = max(16, 32) = 32 (edgeThreshold 31 + ...) so images <=64 wide yield nothing at level 0+    let mut g = Gray::new(100, 100); g.px.iter_mut().for_each(|p| *p = 128);+    let f = detect_and_describe(&g, 500, 20);+    assert!(f.kps.is_empty() && f.desc.is_empty()); assert_eq!((f.w, f.h), (100, 100));+    let tiny = Gray::new(10, 10);+    assert!(detect_and_describe(&tiny, 500, 20).kps.is_empty());+    assert!(detect_and_describe_orb(&Gray::new(1,1), 500, 20).kps.is_empty());+}+#[test] fn white_square_produces_features_with_valid_descriptors() {+    // Isolated bright dots on black: centre 255, all 16 FAST-circle pixels 0 -> FAST-9 corner+    let mut g = Gray::new(100, 100);+    for y in [40usize, 55, 70] { for x in [40usize, 55, 70] { g.px[y*100+x] = 255; } }+    let f = detect_and_describe(&g, 500, 20);+    assert!(!f.kps.is_empty(), "expected corners on dots");+    assert_eq!(f.kps.len(), f.desc.len());+    assert!(f.kps.len() <= 500);+    for kp in &f.kps { assert!(kp.x >= 0.0 && kp.x < 100.0 && kp.y >= 0.0 && kp.y < 100.0, "kp {},{}", kp.x, kp.y); assert!(kp.angle.is_finite()); }+}+#[test] fn detection_is_deterministic() {+    let mut g = Gray::new(100, 100);+    for y in [40usize, 55, 70] { for x in [40usize, 55, 70] { g.px[y*100+x] = 255; } }+    let a = detect_and_describe(&g, 100, 20); let b = detect_and_describe(&g, 100, 20);+    assert_eq!(a.kps.len(), b.kps.len()); assert_eq!(a.desc, b.desc);+    for (x,y) in a.kps.iter().zip(b.kps.iter()) { assert_eq!((x.x,x.y), (y.x,y.y)); }+}+#[test] fn max_features_caps_output() {+    let mut g = Gray::new(120, 120);+    // Checkerboard-ish texture: many corners+    for y in 0..120 { for x in 0..120 { if (x/10 + y/10) % 2 == 0 { g.px[y*120+x] = 255; } } }+    let f = detect_and_describe(&g, 5, 20);+    assert!(f.kps.len() <= 5, "got {}", f.kps.len());+}+#[test] fn match_identical_descriptors() {+    let d = [0xABu8; 32];+    let a = feats(vec![d]); let b = feats(vec![d, [0xFFu8;32]]);+    // best=0, second=256 => 0 < ratio*second for any positive ratio+    assert_eq!(match_features(&a, &b, 0.8), vec![(0,0)]);+}+#[test] fn match_ratio_rejects_ambiguous() {+    // Two identical candidates: best == second, so best < ratio*second fails+    let d = [7u8; 32];+    assert!(match_features(&feats(vec![d]), &feats(vec![d, d]), 0.8).is_empty());+    assert!(match_features(&feats(vec![d]), &feats(vec![d, d]), 1.0).is_empty());+    // best=8 bits away, second=256: 8 < 0.8*256 passes; reversed ratio small fails? 8 < 0.01*256=2.56 fails+    let mut near = [0u8;32]; near[0] = 0xFF; // 8 bits from zero+    let a = feats(vec![[0u8;32]]); let b = feats(vec![near, [0xFFu8;32]]);+    assert_eq!(match_features(&a, &b, 0.8), vec![(0,0)]);+    assert!(match_features(&a, &b, 0.01).is_empty());+}+#[test] fn match_empty_inputs() {+    assert!(match_features(&feats(vec![]), &feats(vec![[0u8;32]]), 0.8).is_empty());+    assert!(match_features(&feats(vec![[0u8;32]]), &feats(vec![]), 0.8).is_empty());+    assert!(match_features(&feats(vec![]), &feats(vec![]), 0.8).is_empty());+}+#[test] fn self_match_detected_features() {+    let mut g = Gray::new(100, 100);+    for y in [40usize, 55, 70] { for x in [40usize, 55, 70] { g.px[y*100+x] = 255; } }+    let f = detect_and_describe(&g, 500, 20);+    if !f.desc.is_empty() {+        // Each descriptor's nearest in itself is itself at distance 0; ratio test needs a distinct second, so just check no panic and matches index validly+        let m = match_features(&f, &f, 0.99);+        for (i,j) in m { assert!(i < f.desc.len() && j < f.desc.len()); }+    }+}
library/vision/src/main/rust/src/geometry.rs
diff --git a/library/vision/src/main/rust/src/geometry.rs b/library/vision/src/main/rust/src/geometry.rsindex b415d94861..14fc0f7214 100644--- a/library/vision/src/main/rust/src/geometry.rs+++ b/library/vision/src/main/rust/src/geometry.rs@@ -161,3 +161,5 @@ pub fn find_homography_ransac(a: &[Pt], b: &[Pt], iters: usize, thresh: f32) ->     let refined = dlt(&ia, &ib).or(best_h)?;     Some((refined, inliers.len())) }++#[cfg(test)] #[path = "geometry_tests.rs"] mod geometry_tests;
library/vision/src/main/rust/src/geometry_tests.rs
diff --git a/library/vision/src/main/rust/src/geometry_tests.rs b/library/vision/src/main/rust/src/geometry_tests.rsnew file mode 100644index 0000000000..57eb2839aa--- /dev/null+++ b/library/vision/src/main/rust/src/geometry_tests.rs@@ -0,0 +1,66 @@+use crate::geometry::{find_homography_ransac, transfer_inliers, Lcg, Pt};+use crate::linalg::Matrix3;+fn close(a: f64, b: f64, t: f64) { assert!((a-b).abs() < t, "{a} vs {b}"); }+fn square() -> Vec<Pt> { vec![(0.0,0.0),(10.0,0.0),(10.0,10.0),(0.0,10.0)] }+#[test] fn lcg_deterministic_bounded() {+    let mut a = Lcg::new(42); let mut b = Lcg::new(42);+    for _ in 0..20 { let (x,y) = (a.next_usize(7), b.next_usize(7)); assert_eq!(x,y); assert!(x < 7); }+    let mut c = Lcg::new(1); assert_eq!(c.next_usize(1), 0);+    let mut d = Lcg::new(0); for _ in 0..10 { assert!(d.next_usize(100) < 100); }+    // Different seeds diverge quickly+    let mut e = Lcg::new(1); let mut f = Lcg::new(2);+    let se: Vec<usize> = (0..8).map(|_| e.next_usize(1000)).collect();+    let sf: Vec<usize> = (0..8).map(|_| f.next_usize(1000)).collect();+    assert_ne!(se, sf);+}+#[test] fn homography_identity_four_points() {+    let a = square();+    let (h, n) = find_homography_ransac(&a, &a.clone(), 20, 1.0).unwrap();+    assert_eq!(n, 4);+    for r in 0..3 { for c in 0..3 { close(h[(r,c)], if r==c {1.0} else {0.0}, 1e-6); } }+}+#[test] fn homography_translation() {+    let a = square(); let b: Vec<Pt> = a.iter().map(|&(x,y)| (x+5.0, y+3.0)).collect();+    let (h, _) = find_homography_ransac(&a, &b, 20, 1.0).unwrap();+    close(h[(0,0)],1.0,1e-6); close(h[(0,2)],5.0,1e-5); close(h[(1,1)],1.0,1e-6); close(h[(1,2)],3.0,1e-5); close(h[(2,2)],1.0,1e-9);+}+#[test] fn homography_uniform_scale() {+    let a = square(); let b: Vec<Pt> = a.iter().map(|&(x,y)| (x*2.0, y*2.0)).collect();+    let (h, _) = find_homography_ransac(&a, &b, 20, 1.0).unwrap();+    close(h[(0,0)],2.0,1e-6); close(h[(1,1)],2.0,1e-6); close(h[(0,2)],0.0,1e-6);+}+#[test] fn homography_too_few_or_mismatched() {+    assert!(find_homography_ransac(&square()[..3], &square()[..3], 10, 1.0).is_none());+    assert!(find_homography_ransac(&square(), &square()[..3], 10, 1.0).is_none());+    assert!(find_homography_ransac(&[], &[], 10, 1.0).is_none());+}+#[test] fn transfer_inliers_threshold() {+    let id = Matrix3::identity();+    let a: Vec<Pt> = vec![(0.0,0.0),(10.0,0.0),(10.0,10.0)];+    let b: Vec<Pt> = vec![(0.0,0.0),(10.5,0.0),(20.0,10.0)];+    assert_eq!(transfer_inliers(&id, &a, &b, 1.0), vec![0,1]);+    assert_eq!(transfer_inliers(&id, &a, &b, 0.4), vec![0]);+    // Shorter b truncates via min len+    assert_eq!(transfer_inliers(&id, &a, &b[..1], 10.0), vec![0]);+    assert!(transfer_inliers(&id, &[], &[], 1.0).is_empty());+}+#[test] fn transfer_inliers_under_translation() {+    let h = Matrix3::new(1.0,0.0,5.0, 0.0,1.0,3.0, 0.0,0.0,1.0);+    let a = square(); let b: Vec<Pt> = a.iter().map(|&(x,y)| (x+5.0, y+3.0)).collect();+    assert_eq!(transfer_inliers(&h, &a, &b, 0.01).len(), 4);+    assert!(transfer_inliers(&h, &a, &a, 0.01).is_empty());+}+#[test] fn ransac_recovers_translation_with_many_points() {+    let mut a = Vec::new(); let mut b = Vec::new();+    for i in 0..6 { for j in 0..4 { let p = (i as f32*10.0, j as f32*10.0); a.push(p); b.push((p.0+4.0, p.1-2.0)); } }+    let (h, n) = find_homography_ransac(&a, &b, 200, 1.0).unwrap();+    assert!(n >= 20, "inliers {n}");+    close(h[(0,2)], 4.0, 0.05); close(h[(1,2)], -2.0, 0.05);+    assert_eq!(transfer_inliers(&h, &a, &b, 1.0).len(), a.len());+}+#[test] fn ransac_deterministic() {+    let a = square(); let b: Vec<Pt> = a.iter().map(|&(x,y)| (x+1.0, y+1.0)).collect();+    let (h1, n1) = find_homography_ransac(&a, &b, 50, 1.0).unwrap();+    let (h2, n2) = find_homography_ransac(&a, &b, 50, 1.0).unwrap();+    assert_eq!(n1, n2); for r in 0..3 { for c in 0..3 { assert_eq!(h1[(r,c)], h2[(r,c)]); } }+}
library/vision/src/main/rust/src/imgbuf.rs
diff --git a/library/vision/src/main/rust/src/imgbuf.rs b/library/vision/src/main/rust/src/imgbuf.rsindex 3e428569bf..6ee8555353 100644--- a/library/vision/src/main/rust/src/imgbuf.rs+++ b/library/vision/src/main/rust/src/imgbuf.rs@@ -282,3 +282,5 @@ pub fn to_gray(img: &Rgba) -> Gray {     }     g }++#[cfg(test)] #[path = "imgbuf_tests.rs"] mod imgbuf_tests;
library/vision/src/main/rust/src/imgbuf_tests.rs
diff --git a/library/vision/src/main/rust/src/imgbuf_tests.rs b/library/vision/src/main/rust/src/imgbuf_tests.rsnew file mode 100644index 0000000000..003ce4dd90--- /dev/null+++ b/library/vision/src/main/rust/src/imgbuf_tests.rs@@ -0,0 +1,98 @@+use crate::imgbuf::{harris_scores, to_gray, Gray, Rgba};+#[test] fn rgba_new_zeroed_and_get_set() {+    let mut img = Rgba::new(3, 2);+    assert_eq!((img.w, img.h, img.px.len()), (3, 2, 24));+    assert_eq!(img.get(2, 1), [0,0,0,0]);+    img.set(2, 1, [10,20,30,40]); assert_eq!(img.get(2,1), [10,20,30,40]);+    assert_eq!(img.get(0,0), [0,0,0,0]);+}+#[test] fn rgba_from_bytes_roundtrip() {+    let px: Vec<u8> = (0..16).collect();+    let img = Rgba::from_bytes(2, 2, px);+    assert_eq!(img.get(1,0), [4,5,6,7]); assert_eq!(img.get(0,1), [8,9,10,11]);+}+#[test] fn sample_exact_pixel_and_bilinear_midpoint() {+    let mut img = Rgba::new(2, 2);+    img.set(0,0,[0,0,0,255]); img.set(1,0,[100,0,0,255]); img.set(0,1,[0,100,0,255]); img.set(1,1,[100,100,100,255]);+    assert_eq!(img.sample(0.0,0.0).unwrap(), [0.0,0.0,0.0,255.0]);+    assert_eq!(img.sample(1.0,1.0).unwrap(), [100.0,100.0,100.0,255.0]);+    // Center = mean of 4 corners per channel: R(0+100+0+100)/4=50, G likewise 50, B 25+    let c = img.sample(0.5,0.5).unwrap();+    assert!((c[0]-50.0).abs()<1e-6 && (c[1]-50.0).abs()<1e-6 && (c[2]-25.0).abs()<1e-6 && (c[3]-255.0).abs()<1e-6, "{c:?}");+    // Quarter along x on top row: R = 25+    let c = img.sample(0.25,0.0).unwrap(); assert!((c[0]-25.0).abs()<1e-6);+}+#[test] fn sample_out_of_bounds_and_transparent() {+    let mut img = Rgba::new(2, 2);+    assert!(img.sample(-0.1, 0.0).is_none()); assert!(img.sample(0.0, -1.0).is_none());+    assert!(img.sample(1.01, 0.0).is_none()); assert!(img.sample(0.0, 2.0).is_none());+    // All alpha 0 => None even in bounds+    assert!(img.sample(0.0, 0.0).is_none());+    img.set(0,0,[10,20,30,255]);+    assert!(img.sample(0.0,0.0).is_some());+}+#[test] fn rgba_resized_identity_down_and_up() {+    let mut img = Rgba::new(2, 2);+    img.set(0,0,[0,0,0,255]); img.set(1,0,[100,0,0,255]); img.set(0,1,[0,100,0,255]); img.set(1,1,[100,100,100,255]);+    let same = img.resized(2, 2);+    assert_eq!(same.get(1,1), [100,100,100,255]);+    // 1x1 downsample samples center (0.5,0.5): [50,50,25], alpha forced 255+    let one = img.resized(1, 1); assert_eq!(one.get(0,0), [50,50,25,255]);+    let big = img.resized(4, 4); assert_eq!((big.w, big.h), (4,4));+    assert_eq!(big.get(0,0), [0,0,0,255]);+    // Zero size clamps to 1x1+    assert_eq!((img.resized(0,0).w, img.resized(0,0).h), (1,1));+}+#[test] fn gray_new_at_and_clone() {+    let mut g = Gray::new(3, 2); assert_eq!(g.px.len(), 6);+    g.px[5] = 99; assert_eq!(g.at(2,1), 99); assert_eq!(g.at(0,0), 0);+}+#[test] fn gray_resized_average_and_identity() {+    let mut g = Gray::new(2, 2); g.px = vec![0, 100, 100, 200];+    assert_eq!(g.resized(1,1).px, vec![100]); // mean of 4+    assert_eq!(g.resized(2,2).px, vec![0,100,100,200]);+    let up = g.resized(4, 4); assert_eq!(up.at(0,0), 0);+    assert_eq!((g.resized(0,5).w, g.resized(0,5).h), (1,5));+}+#[test] fn gray_blur_uniform_impulse_and_tiny() {+    let mut g = Gray::new(5, 5); g.px.iter_mut().for_each(|p| *p = 80);+    assert!(g.gaussian_blur_3x3().px.iter().all(|&p| p == 80));+    // Impulse 160 at center of 3x3: blurred center = 160*4/16 = 40; borders unchanged (0)+    let mut g = Gray::new(3, 3); g.px[4] = 160;+    let b = g.gaussian_blur_3x3(); assert_eq!(b.at(1,1), 40); assert_eq!(b.at(0,0), 0);+    // Tiny images returned as clone+    let mut g = Gray::new(2, 2); g.px = vec![1,2,3,4];+    assert_eq!(g.gaussian_blur_3x3().px, vec![1,2,3,4]);+}+#[test] fn gray_blur_border_preserved_interior_smoothed() {+    // 4x4 ramp: interior (1,1) kernel over neighbourhood; just assert border == input, interior in range+    let mut g = Gray::new(4, 4); for i in 0..16 { g.px[i] = (i*10) as u8; }+    let b = g.gaussian_blur_3x3();+    for x in 0..4 { assert_eq!(b.at(x,0), g.at(x,0)); assert_eq!(b.at(x,3), g.at(x,3)); }+    assert!(b.at(1,1) > 0 && b.at(1,1) < 255);+}+#[test] fn to_gray_known_colors() {+    let mut img = Rgba::new(5, 1);+    img.set(0,0,[255,255,255,255]); img.set(1,0,[255,0,0,255]); img.set(2,0,[0,255,0,255]); img.set(3,0,[0,0,255,255]); img.set(4,0,[0,0,0,255]);+    let g = to_gray(&img);+    // Rec601 >>8: white 255, red 76 (255*77>>8), green 149 (255*150>>8), blue 28 (255*29>>8)+    assert_eq!(g.px, vec![255, 76, 149, 28, 0]);+    // Gray input r=g=b=v maps to v (77+150+29=256)+    let mut img = Rgba::new(1,1); img.set(0,0,[128,128,128,0]); assert_eq!(to_gray(&img).px, vec![128]);+}+#[test] fn harris_flat_is_zero_and_empty() {+    let mut g = Gray::new(16, 16); g.px.iter_mut().for_each(|p| *p = 100);+    let s = harris_scores(&g, &[(8,8),(4,4)]);+    assert_eq!(s.len(), 2); assert!(s.iter().all(|&v| v == 0.0), "{s:?}");+    assert!(harris_scores(&g, &[]).is_empty());+}+#[test] fn harris_corner_exceeds_edge_and_flat() {+    // White bottom-right quadrant => corner near (16,16), vertical edge near (16,8)... compare magnitudes+    let mut g = Gray::new(32, 32);+    for y in 16..32 { for x in 16..32 { g.px[y*32+x] = 255; } }+    let corner = harris_scores(&g, &[(16,16)])[0];+    let flat = harris_scores(&g, &[(8,8)])[0];+    assert_eq!(flat, 0.0);+    assert!(corner > flat, "corner {corner} flat {flat}");+    assert!(corner > 0.0);+}
library/vision/src/main/rust/src/linalg/dense.rs
diff --git a/library/vision/src/main/rust/src/linalg/dense.rs b/library/vision/src/main/rust/src/linalg/dense.rsindex 463a17a8ca..d3ef0e9205 100644--- a/library/vision/src/main/rust/src/linalg/dense.rs+++ b/library/vision/src/main/rust/src/linalg/dense.rs@@ -229,3 +229,5 @@ impl Lu {         Some(DVector { d: x, _p: PhantomData })     } }++#[cfg(test)] #[path = "dense_tests.rs"] mod dense_tests;
library/vision/src/main/rust/src/linalg/dense_tests.rs
diff --git a/library/vision/src/main/rust/src/linalg/dense_tests.rs b/library/vision/src/main/rust/src/linalg/dense_tests.rsnew file mode 100644index 0000000000..8225e8521b--- /dev/null+++ b/library/vision/src/main/rust/src/linalg/dense_tests.rs@@ -0,0 +1,58 @@+use crate::linalg::{DMatrix, DVector};+fn close(a: f64, b: f64) { assert!((a-b).abs() < 1e-12, "{a} vs {b}"); }+fn dm(vals: &[f64], r: usize, c: usize) -> DMatrix<f64> { let mut m = DMatrix::zeros(r,c); for i in 0..r { for j in 0..c { m[(i,j)] = vals[i*c+j]; } } m }+fn dv(vals: &[f64]) -> DVector<f64> { let mut v = DVector::zeros(vals.len()); for (i,&x) in vals.iter().enumerate() { v[i]=x; } v }+#[test] fn zeros_dims_index_row() {+    let mut m = DMatrix::<f64>::zeros(2, 3);+    assert_eq!(m.nrows(), 2);+    assert_eq!(m.row(0), vec![0.0,0.0,0.0]);+    m[(1,2)] = 7.0; assert_eq!(m[(1,2)], 7.0); assert_eq!(m.row(1), vec![0.0,0.0,7.0]);+}+#[test] fn dvector_index_and_ops() {+    let mut v = dv(&[1.0,2.0,3.0]); v[1] = -2.0; assert_eq!(v[1], -2.0);+    let mut a = dv(&[1.0,2.0]); a += dv(&[10.0,20.0]); assert_eq!((a[0],a[1]), (11.0,22.0));+    let n = -&dv(&[1.0,-2.0,3.0]); assert_eq!((n[0],n[1],n[2]), (-1.0,2.0,-3.0));+}+#[test] fn dmatrix_add_assign() { let mut m = dm(&[1.0,2.0,3.0,4.0],2,2); m += dm(&[10.0,20.0,30.0,40.0],2,2); assert_eq!((m[(0,0)],m[(1,1)]), (11.0,44.0)); }+#[test] fn lu_identity_and_diagonal() {+    let x = dm(&[1.0,0.0, 0.0,1.0],2,2).lu().solve(&dv(&[5.0,-3.0])).unwrap(); close(x[0],5.0); close(x[1],-3.0);+    let x = dm(&[2.0,0.0, 0.0,4.0],2,2).lu().solve(&dv(&[6.0,8.0])).unwrap(); close(x[0],3.0); close(x[1],2.0);+}+#[test] fn lu_requires_pivot_swap() {+    // [[0,1],[1,0]] x = [2,3] => x = [3,2]+    let x = dm(&[0.0,1.0, 1.0,0.0],2,2).lu().solve(&dv(&[2.0,3.0])).unwrap(); close(x[0],3.0); close(x[1],2.0);+    // [[0,2,0],[1,0,0],[0,0,4]] x=[?] with x=[1,2,3] => b=[4,1,12]+    let x = dm(&[0.0,2.0,0.0, 1.0,0.0,0.0, 0.0,0.0,4.0],3,3).lu().solve(&dv(&[4.0,1.0,12.0])).unwrap();+    close(x[0],1.0); close(x[1],2.0); close(x[2],3.0);+}+#[test] fn lu_1x1_and_upper_triangular() {+    let x = dm(&[4.0],1,1).lu().solve(&dv(&[9.0])).unwrap(); close(x[0],2.25);+    assert!(dm(&[0.0],1,1).lu().solve(&dv(&[1.0])).is_none());+    // [[1,2],[0,3]] x, x=[1,1] => b=[3,3]+    let x = dm(&[1.0,2.0, 0.0,3.0],2,2).lu().solve(&dv(&[3.0,3.0])).unwrap(); close(x[0],1.0); close(x[1],1.0);+}+#[test] fn dmatrix_svd_nullspace_is_last_row() {+    // A = [[1,0],[0,0]] (2x2): A^T A = diag(1,0); smallest singular vector = (0,+-1)+    let s = dm(&[1.0,0.0, 0.0,0.0],2,2).svd(true, true);+    let vt = s.v_t.unwrap(); assert!(s.u.is_none());+    let last = vt.row(vt.nrows()-1);+    assert!(last[0].abs() < 1e-9 && (last[1].abs()-1.0).abs() < 1e-9, "{last:?}");+    let first = vt.row(0); assert!((first[0].abs()-1.0).abs() < 1e-9 && first[1].abs() < 1e-9);+}+#[test] fn dmatrix_svd_rows_orthonormal_and_ordered() {+    // A^T A for A=diag(1,3) is diag(1,9): descending order puts (0,+-1) first+    let s = dm(&[1.0,0.0, 0.0,3.0],2,2).svd(true, true);+    let vt = s.v_t.unwrap();+    let r0 = vt.row(0); let r1 = vt.row(1);+    assert!((r0[1].abs()-1.0).abs() < 1e-9, "{r0:?}");+    assert!((r0[0]*r1[0]+r0[1]*r1[1]).abs() < 1e-9);+    assert!(((r0[0]*r0[0]+r0[1]*r0[1])-1.0).abs() < 1e-9);+}+#[test] fn dmatrix_svd_rectangular() {+    // 3x2 A with columns (1,0,0),(0,1,0): A^T A = I, rows of Vt orthonormal+    let s = dm(&[1.0,0.0, 0.0,1.0, 0.0,0.0],3,2).svd(true, true);+    let vt = s.v_t.unwrap(); assert_eq!(vt.nrows(), 2);+    let r0 = vt.row(0); let r1 = vt.row(1);+    assert!(((r0[0]*r0[0]+r0[1]*r0[1])-1.0).abs() < 1e-9);+    assert!((r0[0]*r1[0]+r0[1]*r1[1]).abs() < 1e-9);+}
library/vision/src/main/rust/src/linalg/jacobi.rs
diff --git a/library/vision/src/main/rust/src/linalg/jacobi.rs b/library/vision/src/main/rust/src/linalg/jacobi.rsindex 4e5451bdba..17d2475f62 100644--- a/library/vision/src/main/rust/src/linalg/jacobi.rs+++ b/library/vision/src/main/rust/src/linalg/jacobi.rs@@ -70,3 +70,5 @@ pub(crate) fn jacobi_sym(a_in: &[f64], n: usize) -> (Vec<f64>, Vec<f64>) {     let evals: Vec<f64> = (0..n).map(|i| a[idx(i, i)]).collect();     (evals, v) }++#[cfg(test)] #[path = "jacobi_tests.rs"] mod jacobi_tests;
library/vision/src/main/rust/src/linalg/jacobi_tests.rs
diff --git a/library/vision/src/main/rust/src/linalg/jacobi_tests.rs b/library/vision/src/main/rust/src/linalg/jacobi_tests.rsnew file mode 100644index 0000000000..c0d2da32dc--- /dev/null+++ b/library/vision/src/main/rust/src/linalg/jacobi_tests.rs@@ -0,0 +1,27 @@+use super::jacobi_sym;+fn sorted(mut v: Vec<f64>) -> Vec<f64> { v.sort_by(|a,b| a.total_cmp(b)); v }+#[test] fn one_by_one() { let (e, v) = jacobi_sym(&[42.0], 1); assert_eq!(e, vec![42.0]); assert_eq!(v, vec![1.0]); }+#[test] fn diagonal_unchanged() {+    let (e, v) = jacobi_sym(&[3.0,0.0,0.0, 0.0,-1.0,0.0, 0.0,0.0,5.0], 3);+    assert_eq!(sorted(e.clone()), vec![-1.0, 3.0, 5.0]);+    // eigenvectors are signed basis vectors: V^T V = I+    for i in 0..3 { for j in 0..3 { let d: f64 = (0..3).map(|k| v[k*3+i]*v[k*3+j]).sum(); assert!((d - if i==j {1.0} else {0.0}).abs() < 1e-12); } }+    let _ = e;+}+#[test] fn two_by_two_known_eigen() {+    // [[2,1],[1,2]] eigenvalues 1,3+    let a = [2.0,1.0, 1.0,2.0];+    let (e, v) = jacobi_sym(&a, 2);+    let s = sorted(e.clone()); assert!((s[0]-1.0).abs()<1e-12 && (s[1]-3.0).abs()<1e-12);+    // Reconstruct V diag(e) V^T == A+    for r in 0..2 { for c in 0..2 { let rec: f64 = (0..2).map(|k| v[r*2+k]*e[k]*v[c*2+k]).sum(); assert!((rec-a[r*2+c]).abs()<1e-12, "{r}{c} {rec}"); } }+}+#[test] fn identity_all_ones() { let (e, _) = jacobi_sym(&[1.0,0.0, 0.0,1.0], 2); assert!(e.iter().all(|&x| (x-1.0).abs()<1e-12)); }+#[test] fn zero_matrix() { let (e, v) = jacobi_sym(&[0.0;4], 2); assert_eq!(e, vec![0.0,0.0]); assert_eq!(v, vec![1.0,0.0,0.0,1.0]); }+#[test] fn three_by_three_block() {+    // [[2,1,0],[1,2,0],[0,0,5]] eigenvalues 1,3,5+    let a = [2.0,1.0,0.0, 1.0,2.0,0.0, 0.0,0.0,5.0];+    let (e, v) = jacobi_sym(&a, 3);+    let s = sorted(e.clone()); assert!((s[0]-1.0).abs()<1e-9 && (s[1]-3.0).abs()<1e-9 && (s[2]-5.0).abs()<1e-9, "{s:?}");+    for r in 0..3 { for c in 0..3 { let rec: f64 = (0..3).map(|k| v[r*3+k]*e[k]*v[c*3+k]).sum(); assert!((rec-a[r*3+c]).abs()<1e-9); } }+}
library/vision/src/main/rust/src/linalg/matrix3.rs
diff --git a/library/vision/src/main/rust/src/linalg/matrix3.rs b/library/vision/src/main/rust/src/linalg/matrix3.rsindex 3544c61e9f..12fd275577 100644--- a/library/vision/src/main/rust/src/linalg/matrix3.rs+++ b/library/vision/src/main/rust/src/linalg/matrix3.rs@@ -359,3 +359,5 @@ impl DivAssign<f64> for Matrix3<f64> {         }     } }++#[cfg(test)] #[path = "matrix3_tests.rs"] mod matrix3_tests;
library/vision/src/main/rust/src/linalg/matrix3_tests.rs
diff --git a/library/vision/src/main/rust/src/linalg/matrix3_tests.rs b/library/vision/src/main/rust/src/linalg/matrix3_tests.rsnew file mode 100644index 0000000000..fb37ad1a61--- /dev/null+++ b/library/vision/src/main/rust/src/linalg/matrix3_tests.rs@@ -0,0 +1,102 @@+use crate::linalg::{Matrix3, Vector3};+fn close(a: f64, b: f64) { assert!((a-b).abs() < 1e-12, "{a} vs {b}"); }+fn mat_close(m: &Matrix3<f64>, want: &[f64;9]) { for r in 0..3 { for c in 0..3 { close(m[(r,c)], want[r*3+c]); } } }+#[test] fn zeros_identity_new_index() {+    mat_close(&Matrix3::zeros(), &[0.0;9]);+    mat_close(&Matrix3::identity(), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+    let mut m = Matrix3::new(1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0);+    assert_eq!((m[(0,0)], m[(1,2)], m[(2,1)]), (1.0, 6.0, 8.0));+    m[(1,1)] = -5.0; assert_eq!(m[(1,1)], -5.0);+}+#[test] fn transpose_swaps_and_is_involution() {+    let m = Matrix3::new(1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0);+    mat_close(&m.transpose(), &[1.0,4.0,7.0, 2.0,5.0,8.0, 3.0,6.0,9.0]);+    mat_close(&m.transpose().transpose(), &[1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0]);+}+#[test] fn column_extracts() {+    let m = Matrix3::new(1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0);+    let c = m.column(1); assert_eq!((c.x, c.y, c.z), (2.0, 5.0, 8.0));+    let c = m.column(2); assert_eq!((c.x, c.y, c.z), (3.0, 6.0, 9.0));+}+#[test] fn from_rows_builds() {+    let rows = [Vector3::new(1.0,2.0,3.0).transpose(), Vector3::new(4.0,5.0,6.0).transpose(), Vector3::new(7.0,8.0,9.0).transpose()];+    mat_close(&Matrix3::from_rows(&rows), &[1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0]);+}+#[test] fn determinant_hand_computed() {+    close(Matrix3::identity().determinant(), 1.0);+    close(Matrix3::zeros().determinant(), 0.0);+    // 2*(3*4-2*0) - 0 + 1*(1*0-3*1) = 24 - 3 = 21+    close(Matrix3::new(2.0,0.0,1.0, 1.0,3.0,2.0, 1.0,0.0,4.0).determinant(), 21.0);+    close(Matrix3::new(2.0,0.0,0.0, 0.0,3.0,0.0, 0.0,0.0,4.0).determinant(), 24.0);+    // Row swap flips sign: det of [[0,1,0],[1,0,0],[0,0,1]] = -1+    close(Matrix3::new(0.0,1.0,0.0, 1.0,0.0,0.0, 0.0,0.0,1.0).determinant(), -1.0);+    // Singular (row2 = 2*row1)+    close(Matrix3::new(1.0,2.0,3.0, 2.0,4.0,6.0, 1.0,1.0,1.0).determinant(), 0.0);+}+#[test] fn multiply_hand_computed() {+    let a = Matrix3::new(1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0);+    let b = Matrix3::new(9.0,8.0,7.0, 6.0,5.0,4.0, 3.0,2.0,1.0);+    // Row0: 9+12+9=30, 8+10+6=24, 7+8+3=18; Row1: 36+30+18=84, 32+25+12=69, 28+20+6=54; Row2: 63+48+27=138, 56+40+18=114, 49+32+9=90+    mat_close(&(a*b), &[30.0,24.0,18.0, 84.0,69.0,54.0, 138.0,114.0,90.0]);+    mat_close(&(a * Matrix3::identity()), &[1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0]);+    mat_close(&(Matrix3::zeros() * a), &[0.0;9]);+}+#[test] fn multiply_ref_variants_match_owned() {+    let a = Matrix3::new(1.0,0.0,2.0, 0.0,1.0,0.0, 3.0,0.0,1.0);+    let b = Matrix3::new(2.0,1.0,0.0, 0.0,2.0,1.0, 1.0,0.0,2.0);+    let want = a * b;+    mat_close(&(a * &b), &[want[(0,0)],want[(0,1)],want[(0,2)],want[(1,0)],want[(1,1)],want[(1,2)],want[(2,0)],want[(2,1)],want[(2,2)]]);+    mat_close(&(&a * b), &[want[(0,0)],want[(0,1)],want[(0,2)],want[(1,0)],want[(1,1)],want[(1,2)],want[(2,0)],want[(2,1)],want[(2,2)]]);+    mat_close(&(&a * &b), &[want[(0,0)],want[(0,1)],want[(0,2)],want[(1,0)],want[(1,1)],want[(1,2)],want[(2,0)],want[(2,1)],want[(2,2)]]);+}+#[test] fn matrix_times_vector() {+    let m = Matrix3::new(1.0,2.0,3.0, 4.0,5.0,6.0, 7.0,8.0,9.0);+    let v = m * Vector3::new(1.0, 1.0, 1.0); assert_eq!((v.x, v.y, v.z), (6.0, 15.0, 24.0));+    let v = &m * Vector3::new(1.0, 0.0, 0.0); assert_eq!((v.x, v.y, v.z), (1.0, 4.0, 7.0));+}+#[test] fn inverse_known_matrices() {+    mat_close(&Matrix3::new(2.0,0.0,0.0, 0.0,4.0,0.0, 0.0,0.0,5.0).try_inverse().unwrap(), &[0.5,0.0,0.0, 0.0,0.25,0.0, 0.0,0.0,0.2]);+    // Upper triangular [[1,2,0],[0,1,3],[0,0,1]]^-1 = [[1,-2,6],[0,1,-3],[0,0,1]]+    mat_close(&Matrix3::new(1.0,2.0,0.0, 0.0,1.0,3.0, 0.0,0.0,1.0).try_inverse().unwrap(), &[1.0,-2.0,6.0, 0.0,1.0,-3.0, 0.0,0.0,1.0]);+    assert!(Matrix3::zeros().try_inverse().is_none());+    assert!(Matrix3::new(f64::NAN,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0).try_inverse().is_none());+}+#[test] fn add_assign_and_div_assign() {+    let mut m = Matrix3::identity(); m += Matrix3::identity();+    mat_close(&m, &[2.0,0.0,0.0, 0.0,2.0,0.0, 0.0,0.0,2.0]);+    m /= 2.0; mat_close(&m, &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+}+#[test] fn svd_sorted_descending_and_reconstructs() {+    let m = Matrix3::new(1.0,0.0,0.0, 0.0,3.0,0.0, 0.0,0.0,2.0);+    let s = m.svd_sorted();+    close(s.singular_values[0], 3.0); close(s.singular_values[1], 2.0); close(s.singular_values[2], 1.0);+    let u = s.u.unwrap(); let vt = s.v_t.unwrap();+    let sig = Matrix3::new(s.singular_values[0],0.0,0.0, 0.0,s.singular_values[1],0.0, 0.0,0.0,s.singular_values[2]);+    mat_close(&(u*sig*vt), &[1.0,0.0,0.0, 0.0,3.0,0.0, 0.0,0.0,2.0]);+    // U and Vt orthonormal+    mat_close(&(u*u.transpose()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+    mat_close(&(vt*vt.transpose()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+}+#[test] fn svd_zero_matrix_is_orthonormal_with_zero_singulars() {+    let s = Matrix3::zeros().svd(true, true);+    assert_eq!((s.singular_values[0], s.singular_values[1], s.singular_values[2]), (0.0, 0.0, 0.0));+    let u = s.u.unwrap();+    mat_close(&(u*u.transpose()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+}+#[test] fn svd_rank_one() {+    // diag(5,0,0): singular values {5,0,0}, U Vt still orthonormal, reconstructs+    let m = Matrix3::new(5.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0);+    let s = m.svd_sorted();+    close(s.singular_values[0], 5.0); close(s.singular_values[1], 0.0); close(s.singular_values[2], 0.0);+    let u = s.u.unwrap(); let vt = s.v_t.unwrap();+    let sig = Matrix3::new(s.singular_values[0],0.0,0.0, 0.0,s.singular_values[1],0.0, 0.0,0.0,s.singular_values[2]);+    mat_close(&(u*sig*vt), &[5.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0]);+}+#[test] fn symmetric_eigen_identity_and_scaled() {+    let se = Matrix3::identity().symmetric_eigen();+    for i in 0..3 { close(se.eigenvalues[i], 1.0); }+    mat_close(&(se.eigenvectors * se.eigenvectors.transpose()), &[1.0,0.0,0.0, 0.0,1.0,0.0, 0.0,0.0,1.0]);+    let se = Matrix3::new(4.0,0.0,0.0, 0.0,-2.0,0.0, 0.0,0.0,7.0).symmetric_eigen();+    let mut ev = [se.eigenvalues[0], se.eigenvalues[1], se.eigenvalues[2]]; ev.sort_by(|a,b| a.total_cmp(b));+    close(ev[0], -2.0); close(ev[1], 4.0); close(ev[2], 7.0);+}
library/vision/src/main/rust/src/linalg/vector.rs
diff --git a/library/vision/src/main/rust/src/linalg/vector.rs b/library/vision/src/main/rust/src/linalg/vector.rsindex 872598d034..df32140953 100644--- a/library/vision/src/main/rust/src/linalg/vector.rs+++ b/library/vision/src/main/rust/src/linalg/vector.rs@@ -144,3 +144,5 @@ pub struct RowVector3<T = f64> {     pub(crate) d: [f64; 3],     _p: PhantomData<T>, }++#[cfg(test)] #[path = "vector_tests.rs"] mod vector_tests;
library/vision/src/main/rust/src/linalg/vector_tests.rs
diff --git a/library/vision/src/main/rust/src/linalg/vector_tests.rs b/library/vision/src/main/rust/src/linalg/vector_tests.rsnew file mode 100644index 0000000000..5e56730ec6--- /dev/null+++ b/library/vision/src/main/rust/src/linalg/vector_tests.rs@@ -0,0 +1,49 @@+use crate::linalg::{Matrix3, Vector3};+fn eq(v: Vector3<f64>, x: f64, y: f64, z: f64) { assert_eq!((v.x, v.y, v.z), (x, y, z)); }+#[test] fn zeros_and_new() { eq(Vector3::zeros(), 0.0, 0.0, 0.0); eq(Vector3::new(1.0, -2.0, 3.5), 1.0, -2.0, 3.5); }+#[test] fn norm_pythagorean() {+    assert_eq!(Vector3::new(3.0, 4.0, 0.0).norm(), 5.0);+    assert_eq!(Vector3::new(0.0, 0.0, 0.0).norm(), 0.0);+    assert!((Vector3::new(1.0, 2.0, 2.0).norm() - 3.0).abs() < 1e-15);+    assert!((Vector3::new(-2.0, 3.0, 6.0).norm() - 7.0).abs() < 1e-15);+}+#[test] fn dot_hand_computed() {+    assert_eq!(Vector3::new(1.0, 2.0, 3.0).dot(&Vector3::new(4.0, 5.0, 6.0)), 32.0);+    assert_eq!(Vector3::new(1.0, 0.0, 0.0).dot(&Vector3::new(0.0, 1.0, 0.0)), 0.0);+    assert_eq!(Vector3::new(2.0, -1.0, 3.0).dot(&Vector3::new(2.0, -1.0, 3.0)), 14.0);+}+#[test] fn cross_basis_and_general() {+    eq(Vector3::new(1.0,0.0,0.0).cross(&Vector3::new(0.0,1.0,0.0)), 0.0, 0.0, 1.0);+    eq(Vector3::new(0.0,1.0,0.0).cross(&Vector3::new(1.0,0.0,0.0)), 0.0, 0.0, -1.0);+    // (3*7-4*6, 4*5-2*7, 2*6-3*5) = (-3, 6, -3)+    eq(Vector3::new(2.0,3.0,4.0).cross(&Vector3::new(5.0,6.0,7.0)), -3.0, 6.0, -3.0);+    let a = Vector3::new(1.0, 2.0, 3.0); let b = Vector3::new(-2.0, 0.0, 1.0);+    assert_eq!(a.cross(&b).dot(&a), 0.0); assert_eq!(a.cross(&b).dot(&b), 0.0);+}+#[test] fn scalar_ops() {+    eq(Vector3::new(1.0, -2.0, 4.0) * 2.0, 2.0, -4.0, 8.0);+    eq(Vector3::new(2.0, -4.0, 8.0) / 2.0, 1.0, -2.0, 4.0);+    let mut v = Vector3::new(8.0, 4.0, 2.0); v /= 2.0; eq(v, 4.0, 2.0, 1.0);+}+#[test] fn add_sub_neg_assign() {+    eq(Vector3::new(1.0,2.0,3.0) + Vector3::new(4.0,5.0,6.0), 5.0, 7.0, 9.0);+    eq(Vector3::new(4.0,5.0,6.0) - Vector3::new(1.0,2.0,3.0), 3.0, 3.0, 3.0);+    eq(-Vector3::new(1.0,-2.0,3.0), -1.0, 2.0, -3.0);+    let mut v = Vector3::new(1.0,1.0,1.0); v += Vector3::new(1.0,2.0,3.0); eq(v, 2.0, 3.0, 4.0);+}+#[test] fn indexing_read_write() {+    let mut v = Vector3::new(7.0, 8.0, 9.0);+    assert_eq!((v[0], v[1], v[2]), (7.0, 8.0, 9.0));+    v[1] = -8.0; assert_eq!(v.y, -8.0);+}+#[test] #[should_panic] fn indexing_out_of_range_panics() { let _ = Vector3::zeros()[3]; }+#[test] fn transpose_and_outer_product() {+    let v = Vector3::new(1.0, 2.0, 3.0);+    let m: Matrix3<f64> = v * v.transpose();+    // outer(v,v) = [[1,2,3],[2,4,6],[3,6,9]]+    let want = [1.0,2.0,3.0, 2.0,4.0,6.0, 3.0,6.0,9.0];+    for r in 0..3 { for c in 0..3 { assert_eq!(m[(r,c)], want[r*3+c]); } }+    let w: Matrix3<f64> = Vector3::new(1.0,2.0,3.0) * Vector3::new(4.0,5.0,6.0).transpose();+    assert_eq!((w[(0,0)], w[(0,2)], w[(2,0)], w[(2,2)]), (4.0, 6.0, 12.0, 18.0));+}+#[test] fn into_owned_is_identity() { eq(Vector3::new(1.0,2.0,3.0).into_owned(), 1.0, 2.0, 3.0); }