Skip to content

Commit 139f632

Browse files
committed
Add test for all midpoint expectations
1 parent 4612afa commit 139f632

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

core/tests/num/midpoint.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Test the following expectations:
2+
//! - midpoint(a, b) == (a + b) / 2
3+
//! - midpoint(a, b) == midpoint(b, a)
4+
//! - midpoint(-a, -b) == -midpoint(a, b)
5+
6+
#[test]
7+
#[cfg(not(miri))]
8+
fn midpoint_obvious_impl_i8() {
9+
for a in i8::MIN..=i8::MAX {
10+
for b in i8::MIN..=i8::MAX {
11+
assert_eq!(i8::midpoint(a, b), ((a as i16 + b as i16) / 2) as i8);
12+
}
13+
}
14+
}
15+
16+
#[test]
17+
#[cfg(not(miri))]
18+
fn midpoint_obvious_impl_u8() {
19+
for a in u8::MIN..=u8::MAX {
20+
for b in u8::MIN..=u8::MAX {
21+
assert_eq!(u8::midpoint(a, b), ((a as u16 + b as u16) / 2) as u8);
22+
}
23+
}
24+
}
25+
26+
#[test]
27+
#[cfg(not(miri))]
28+
fn midpoint_order_expectation_i8() {
29+
for a in i8::MIN..=i8::MAX {
30+
for b in i8::MIN..=i8::MAX {
31+
assert_eq!(i8::midpoint(a, b), i8::midpoint(b, a));
32+
}
33+
}
34+
}
35+
36+
#[test]
37+
#[cfg(not(miri))]
38+
fn midpoint_order_expectation_u8() {
39+
for a in u8::MIN..=u8::MAX {
40+
for b in u8::MIN..=u8::MAX {
41+
assert_eq!(u8::midpoint(a, b), u8::midpoint(b, a));
42+
}
43+
}
44+
}
45+
46+
#[test]
47+
#[cfg(not(miri))]
48+
fn midpoint_negative_expectation() {
49+
for a in 0..=i8::MAX {
50+
for b in 0..=i8::MAX {
51+
assert_eq!(i8::midpoint(-a, -b), -i8::midpoint(a, b));
52+
}
53+
}
54+
}

core/tests/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod dec2flt;
2828
mod flt2dec;
2929
mod int_log;
3030
mod int_sqrt;
31+
mod midpoint;
3132
mod ops;
3233
mod wrapping;
3334

0 commit comments

Comments
 (0)