File: average.rs

package info (click to toggle)
rust-num-integer 0.1.46-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212 kB
  • sloc: makefile: 4
file content (96 lines) | stat: -rw-r--r-- 3,183 bytes parent folder | download | duplicates (14)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
macro_rules! test_average {
    ($I:ident, $U:ident) => {
        mod $I {
            mod ceil {
                use num_integer::Average;

                #[test]
                fn same_sign() {
                    assert_eq!((14 as $I).average_ceil(&16), 15 as $I);
                    assert_eq!((14 as $I).average_ceil(&17), 16 as $I);

                    let max = std::$I::MAX;
                    assert_eq!((max - 3).average_ceil(&(max - 1)), max - 2);
                    assert_eq!((max - 3).average_ceil(&(max - 2)), max - 2);
                }

                #[test]
                fn different_sign() {
                    assert_eq!((14 as $I).average_ceil(&-4), 5 as $I);
                    assert_eq!((14 as $I).average_ceil(&-5), 5 as $I);

                    let min = std::$I::MIN;
                    let max = std::$I::MAX;
                    assert_eq!(min.average_ceil(&max), 0 as $I);
                }
            }

            mod floor {
                use num_integer::Average;

                #[test]
                fn same_sign() {
                    assert_eq!((14 as $I).average_floor(&16), 15 as $I);
                    assert_eq!((14 as $I).average_floor(&17), 15 as $I);

                    let max = std::$I::MAX;
                    assert_eq!((max - 3).average_floor(&(max - 1)), max - 2);
                    assert_eq!((max - 3).average_floor(&(max - 2)), max - 3);
                }

                #[test]
                fn different_sign() {
                    assert_eq!((14 as $I).average_floor(&-4), 5 as $I);
                    assert_eq!((14 as $I).average_floor(&-5), 4 as $I);

                    let min = std::$I::MIN;
                    let max = std::$I::MAX;
                    assert_eq!(min.average_floor(&max), -1 as $I);
                }
            }
        }

        mod $U {
            mod ceil {
                use num_integer::Average;

                #[test]
                fn bounded() {
                    assert_eq!((14 as $U).average_ceil(&16), 15 as $U);
                    assert_eq!((14 as $U).average_ceil(&17), 16 as $U);
                }

                #[test]
                fn overflow() {
                    let max = std::$U::MAX;
                    assert_eq!((max - 3).average_ceil(&(max - 1)), max - 2);
                    assert_eq!((max - 3).average_ceil(&(max - 2)), max - 2);
                }
            }

            mod floor {
                use num_integer::Average;

                #[test]
                fn bounded() {
                    assert_eq!((14 as $U).average_floor(&16), 15 as $U);
                    assert_eq!((14 as $U).average_floor(&17), 15 as $U);
                }

                #[test]
                fn overflow() {
                    let max = std::$U::MAX;
                    assert_eq!((max - 3).average_floor(&(max - 1)), max - 2);
                    assert_eq!((max - 3).average_floor(&(max - 2)), max - 3);
                }
            }
        }
    };
}

test_average!(i8, u8);
test_average!(i16, u16);
test_average!(i32, u32);
test_average!(i64, u64);
test_average!(i128, u128);
test_average!(isize, usize);