Add test for all midpoint expectations · qinheping/verify-rust-std@139f632 (original) (raw)

Original file line number Diff line number Diff 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 +}