Skip to main content

vectors/
vectors.rs

1// examples/vectors.rs
2
3use test_helpers::{
4    assert_vector_eq_approx,
5    assert_vector_ne_approx,
6    margin,
7    multiplier,
8};
9
10use std::panic as std_panic;
11
12
13fn main() {
14    {
15        println!();
16        println!("compare two vectors (README-style, with `multiplier()`):");
17
18        let expected = &[3.0, -40404.0, 1.23456];
19        let actual = Vec::from([3.0, -40410.0, 1.234567]);
20
21        assert_vector_eq_approx!(expected, actual, multiplier(0.00015));
22    }
23
24    {
25        println!();
26        println!("compare two vectors (with `margin()`):");
27
28        let expected = &[1.0, 2.0, 3.0];
29        let actual = Vec::from([1.0, 2.001, 3.0]);
30
31        // this one passes
32        assert_vector_ne_approx!(expected, actual, margin(0.0));
33
34        // this one does not
35        let failed = std_panic::catch_unwind(|| {
36            assert_vector_eq_approx!(expected, actual, margin(0.0));
37        })
38        .is_err();
39        assert!(failed, "expected assert_vector_eq_approx! to fail");
40    }
41
42    {
43        println!();
44        println!("compare two vectors (with `multiplier()`):");
45
46        let expected = &[1.0, 2.0, 3.0];
47        let actual = Vec::from([1.0, 2.001, 3.0]);
48
49        // this one passes
50        assert_vector_ne_approx!(expected, actual, multiplier(0.0));
51
52        // this one does not
53        let failed = std_panic::catch_unwind(|| {
54            assert_vector_eq_approx!(expected, actual, multiplier(0.0));
55        })
56        .is_err();
57        assert!(failed, "expected assert_vector_eq_approx! to fail");
58    }
59}
60
61
62// ///////////////////////////// end of file //////////////////////////// //