Skip to content

Commit f0ad56b

Browse files
authored
cmov: have test macro write modules (#1294)
Collapses the module structure by having the test macro declare the module as well
1 parent 99d6472 commit f0ad56b

File tree

2 files changed

+161
-200
lines changed

2 files changed

+161
-200
lines changed

cmov/tests/core_impls.rs

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/// Write the tests for an integer type, given two unequal integers
2+
macro_rules! int_tests {
3+
($int:ident, $a:expr, $b:expr) => {
4+
mod $int {
5+
use cmov::{Cmov, CmovEq};
6+
7+
#[test]
8+
fn cmovz_works() {
9+
let mut n: $int = $a;
10+
11+
for cond in 1..0xFF {
12+
n.cmovz(&$b, cond);
13+
assert_eq!(n, $a);
14+
}
15+
16+
n.cmovz(&$b, 0);
17+
assert_eq!(n, $b);
18+
19+
n.cmovz(&<$int>::MAX, 0);
20+
assert_eq!(n, <$int>::MAX);
21+
}
22+
23+
#[test]
24+
fn cmovnz_works() {
25+
let mut n = $a;
26+
n.cmovnz(&$b, 0);
27+
assert_eq!(n, $a);
28+
29+
for cond in 1..0xFF {
30+
let mut n = $a;
31+
n.cmovnz(&$b, cond);
32+
assert_eq!(n, $b);
33+
}
34+
}
35+
36+
#[test]
37+
fn cmoveq_works() {
38+
let mut o = 0u8;
39+
40+
// compare to zero (a and b should be non-zero)
41+
$a.cmoveq(&0, 1, &mut o);
42+
assert_eq!(o, 0);
43+
0.cmoveq(&$a, 1, &mut o);
44+
assert_eq!(o, 0);
45+
46+
for cond in 1..0xFFi64 {
47+
cond.cmoveq(&cond, cond as u8, &mut o);
48+
assert_eq!(o, cond as u8);
49+
cond.cmoveq(&0, 0, &mut o);
50+
assert_eq!(o, cond as u8);
51+
}
52+
53+
// equal so we move
54+
$a.cmoveq(&$a, 43u8, &mut o);
55+
assert_eq!(o, 43u8);
56+
57+
// non-equal so we don't move
58+
$a.cmoveq(&$b, 55u8, &mut o);
59+
assert_eq!(o, 43u8);
60+
<$int>::MAX.cmoveq(&$a, 55u8, &mut o);
61+
assert_eq!(o, 43u8);
62+
63+
// equal so we move
64+
<$int>::MAX.cmoveq(&<$int>::MAX, 55u8, &mut o);
65+
assert_eq!(o, 55u8);
66+
}
67+
68+
#[test]
69+
fn cmovne_works() {
70+
let mut o = 0u8;
71+
72+
// compare to zero (a and b should be non-zero)
73+
$a.cmovne(&0, 1, &mut o);
74+
assert_eq!(o, 1);
75+
o = 0;
76+
0.cmovne(&$a, 1, &mut o);
77+
assert_eq!(o, 1);
78+
o = 0;
79+
80+
for cond in 1..0xFFi64 {
81+
cond.cmovne(&0, cond as u8, &mut o);
82+
assert_eq!(o, cond as u8);
83+
cond.cmovne(&cond, 0, &mut o);
84+
assert_eq!(o, cond as u8);
85+
}
86+
87+
// non-equal so we move
88+
o = 0;
89+
$a.cmovne(&$b, 55u8, &mut o);
90+
assert_eq!(o, 55u8);
91+
92+
// equal so we don't move
93+
$a.cmovne(&$a, 66u8, &mut o);
94+
assert_eq!(o, 55u8);
95+
<$int>::MAX.cmovne(&<$int>::MAX, 66u8, &mut o);
96+
assert_eq!(o, 55u8);
97+
98+
// non-equal so we move
99+
<$int>::MAX.cmovne(&$a, 66u8, &mut o);
100+
assert_eq!(o, 66u8);
101+
}
102+
}
103+
};
104+
}
105+
106+
int_tests!(i8, 0x11i8, -0x22i8);
107+
int_tests!(i16, 0x1111i16, -0x2222i16);
108+
int_tests!(i32, 0x1111_1111i32, -0x2222_2222i32);
109+
int_tests!(i64, 0x1111_1111_1111_1111i64, -0x2222_2222_2222_2222i64);
110+
int_tests!(
111+
i128,
112+
0x1111_1111_1111_1111_1111_1111_1111_1111i128,
113+
-0x2222_2222_2222_2222_2222_2222_2222_2222i128
114+
);
115+
int_tests!(u8, 0x11u8, 0x22u8);
116+
int_tests!(u16, 0x1111u16, 0x2222u16);
117+
int_tests!(u32, 0x1111_1111u32, 0x2222_2222u32);
118+
int_tests!(u64, 0x1111_1111_1111_1111u64, 0x2222_2222_2222_2222u64);
119+
int_tests!(
120+
u128,
121+
0x1111_1111_1111_1111_2222_2222_2222_2222u128,
122+
0x2222_2222_2222_2222_3333_3333_3333_3333u128
123+
);
124+
125+
mod slices {
126+
use cmov::CmovEq;
127+
128+
#[test]
129+
fn cmoveq_works() {
130+
let mut o = 0u8;
131+
132+
// Same slices.
133+
[1u8, 2, 3].cmoveq(&[1, 2, 3], 43, &mut o);
134+
assert_eq!(o, 43);
135+
136+
// Different lengths.
137+
[1u8, 2, 3].cmoveq(&[1, 2], 44, &mut o);
138+
assert_ne!(o, 44);
139+
140+
// Different contents.
141+
[1u8, 2, 3].cmoveq(&[1, 2, 4], 45, &mut o);
142+
assert_ne!(o, 45);
143+
}
144+
145+
#[test]
146+
fn cmovne_works() {
147+
let mut o = 0u8;
148+
149+
// Same slices.
150+
[1u8, 2, 3].cmovne(&[1, 2, 3], 43, &mut o);
151+
assert_ne!(o, 43);
152+
153+
// Different lengths.
154+
[1u8, 2, 3].cmovne(&[1, 2], 44, &mut o);
155+
assert_eq!(o, 44);
156+
157+
// Different contents.
158+
[1u8, 2, 3].cmovne(&[1, 2, 4], 45, &mut o);
159+
assert_eq!(o, 45);
160+
}
161+
}

cmov/tests/lib.rs

Lines changed: 0 additions & 200 deletions
This file was deleted.

0 commit comments

Comments
 (0)