Skip to content

Commit 85fd7c8

Browse files
committed
Add proper unit tests for average_ceil and average_floor
1 parent 67fca34 commit 85fd7c8

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

benches/average.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ where
135135
for &(i, j) in v {
136136
let rt = f(&i, &j);
137137
let (a, b) = (min(i, j), max(i, j));
138-
println!("( {:?} + {:?} )/ 2 = {:?}", a, b, rt);
139138
// if both number are the same sign, check rt is in the middle
140139
if (a < T::zero()) == (b < T::zero()) {
141140
if (b - a).is_even() {
@@ -163,7 +162,6 @@ where
163162
for &(i, j) in v {
164163
let rt = f(&i, &j);
165164
let (a, b) = (min(i, j), max(i, j));
166-
println!("{:?} + {:?} / 2 = {:?}", a, b, rt);
167165
// if both number are the same sign, check rt is in the middle
168166
if (a < T::zero()) == (b < T::zero()) {
169167
if (b - a).is_even() {

tests/average.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
extern crate num_integer;
2+
extern crate num_traits;
3+
4+
macro_rules! test_average {
5+
($I:ident, $U:ident) => {
6+
mod $I {
7+
mod ceil {
8+
use num_integer::Average;
9+
10+
#[test]
11+
fn same_sign() {
12+
assert_eq!((14 as $I).average_ceil(&16), 15 as $I);
13+
assert_eq!((14 as $I).average_ceil(&17), 16 as $I);
14+
15+
let max = $crate::std::$I::MAX;
16+
assert_eq!((max - 3).average_ceil(&(max - 1)), max - 2);
17+
assert_eq!((max - 3).average_ceil(&(max - 2)), max - 2);
18+
}
19+
20+
#[test]
21+
fn different_sign() {
22+
assert_eq!((14 as $I).average_ceil(&-4), 5 as $I);
23+
assert_eq!((14 as $I).average_ceil(&-5), 5 as $I);
24+
25+
let min = $crate::std::$I::MIN;
26+
let max = $crate::std::$I::MAX;
27+
assert_eq!(min.average_ceil(&max), 0 as $I);
28+
}
29+
}
30+
31+
mod floor {
32+
use num_integer::Average;
33+
34+
#[test]
35+
fn same_sign() {
36+
assert_eq!((14 as $I).average_floor(&16), 15 as $I);
37+
assert_eq!((14 as $I).average_floor(&17), 15 as $I);
38+
39+
let max = $crate::std::$I::MAX;
40+
assert_eq!((max - 3).average_floor(&(max - 1)), max - 2);
41+
assert_eq!((max - 3).average_floor(&(max - 2)), max - 3);
42+
}
43+
44+
#[test]
45+
fn different_sign() {
46+
assert_eq!((14 as $I).average_floor(&-4), 5 as $I);
47+
assert_eq!((14 as $I).average_floor(&-5), 4 as $I);
48+
49+
let min = $crate::std::$I::MIN;
50+
let max = $crate::std::$I::MAX;
51+
assert_eq!(min.average_floor(&max), -1 as $I);
52+
}
53+
}
54+
}
55+
56+
mod $U {
57+
mod ceil {
58+
use num_integer::Average;
59+
60+
#[test]
61+
fn bounded() {
62+
assert_eq!((14 as $U).average_ceil(&16), 15 as $U);
63+
assert_eq!((14 as $U).average_ceil(&17), 16 as $U);
64+
}
65+
66+
#[test]
67+
fn overflow() {
68+
let max = $crate::std::$U::MAX;
69+
assert_eq!((max - 3).average_ceil(&(max - 1)), max - 2);
70+
assert_eq!((max - 3).average_ceil(&(max - 2)), max - 2);
71+
}
72+
}
73+
74+
mod floor {
75+
use num_integer::Average;
76+
77+
#[test]
78+
fn bounded() {
79+
assert_eq!((14 as $U).average_floor(&16), 15 as $U);
80+
assert_eq!((14 as $U).average_floor(&17), 15 as $U);
81+
}
82+
83+
#[test]
84+
fn overflow() {
85+
let max = $crate::std::$U::MAX;
86+
assert_eq!((max - 3).average_floor(&(max - 1)), max - 2);
87+
assert_eq!((max - 3).average_floor(&(max - 2)), max - 3);
88+
}
89+
}
90+
}
91+
};
92+
}
93+
94+
test_average!(i8, u8);
95+
test_average!(i16, u16);
96+
test_average!(i32, u32);
97+
test_average!(i64, u64);
98+
#[cfg(has_i128)]
99+
test_average!(i128, u128);
100+
test_average!(isize, usize);

0 commit comments

Comments
 (0)