File: types.rs

package info (click to toggle)
rust-dasp-sample 0.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,721 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// Expands to a unique module with a variety of tests for the given sample newtype.
///
/// Tests include basic operations and over/underflow checks.
macro_rules! test_type {
    ($T:ident, $mod_name:ident) => {
        mod $mod_name {
            #[test]
            fn ops() {
                use dasp_sample::types::$mod_name::$T;
                assert_eq!(
                    $T::new(8).unwrap() + $T::new(12).unwrap(),
                    $T::new(20).unwrap()
                );
                assert_eq!(
                    $T::new(12).unwrap() - $T::new(4).unwrap(),
                    $T::new(8).unwrap()
                );
                assert_eq!(
                    $T::new(2).unwrap() * $T::new(2).unwrap(),
                    $T::new(4).unwrap()
                );
                assert_eq!(
                    $T::new(3).unwrap() * $T::new(3).unwrap(),
                    $T::new(9).unwrap()
                );
                assert_eq!(
                    $T::new(5).unwrap() * $T::new(10).unwrap(),
                    $T::new(50).unwrap()
                );
                assert_eq!(
                    $T::new(16).unwrap() / $T::new(8).unwrap(),
                    $T::new(2).unwrap()
                );
                assert_eq!(
                    $T::new(8).unwrap() % $T::new(3).unwrap(),
                    $T::new(2).unwrap()
                );
            }

            #[cfg(debug_assertions)]
            #[test]
            #[should_panic]
            fn add_panic_debug() {
                use dasp_sample::types::$mod_name::{self, $T};
                let _ = $mod_name::MAX + $T::new(1).unwrap();
            }

            #[cfg(debug_assertions)]
            #[test]
            #[should_panic]
            fn sub_panic_debug() {
                use dasp_sample::types::$mod_name::{self, $T};
                let _ = $mod_name::MIN - $T::new(1).unwrap();
            }

            #[cfg(debug_assertions)]
            #[test]
            #[should_panic]
            fn mul_panic_debug() {
                use dasp_sample::types::$mod_name::{self, $T};
                let _ = $mod_name::MAX * $T::new(2).unwrap();
            }

            #[cfg(not(debug_assertions))]
            #[test]
            fn release_wrapping() {
                use dasp_sample::types::$mod_name::{self, $T};
                assert_eq!($mod_name::MIN - $T::new(1).unwrap(), $mod_name::MAX);
                assert_eq!($mod_name::MAX + $T::new(1).unwrap(), $mod_name::MIN);
            }
        }
    };
}

test_type!(I11, i11);
test_type!(U11, u11);
test_type!(I20, i20);
test_type!(U20, u20);
test_type!(I24, i24);
test_type!(U24, u24);
test_type!(I48, i48);
test_type!(U48, u48);