Skip to content

Commit eae97ba

Browse files
committed
Create fuzzing tests for overflowing multiplication
1 parent b595fa6 commit eae97ba

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

testcrate/tests/overflowing_mul.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use compiler_builtins::int::mul::{
2+
__mulodi4, __mulosi4, __muloti4, __rust_i128_mulo, __rust_u128_mulo,
3+
};
4+
5+
#[test]
6+
fn overflowing_mul_test() {
7+
testcrate::fuzz_2(10_000, |x: i32, y: i32| {
8+
let (mul0, o0) = x.overflowing_mul(y);
9+
let mut o1 = 0i32;
10+
let mul1 = __mulosi4(x, y, &mut o1);
11+
if mul0 != mul1 || o0 != (o1 != 0) {
12+
panic!(
13+
"__mulosi4({}, {}): expected: {} (oflow = {}), found: {} (oflow = {})",
14+
x, y, mul0, o0, mul1, o1
15+
);
16+
}
17+
});
18+
testcrate::fuzz_2(10_000, |x: i64, y: i64| {
19+
let (mul0, o0) = x.overflowing_mul(y);
20+
let mut o1 = 0i32;
21+
let mul1 = __mulodi4(x, y, &mut o1);
22+
if mul0 != mul1 || o0 != (o1 != 0) {
23+
panic!(
24+
"__mulodi4({}, {}): expected: {} (oflow = {}), found: {} (oflow = {})",
25+
x, y, mul0, o0, mul1, o1
26+
);
27+
}
28+
});
29+
testcrate::fuzz_2(10_000, |x: i128, y: i128| {
30+
let (mul0, o0) = x.overflowing_mul(y);
31+
let mut o1 = 0i32;
32+
let mul1 = __muloti4(x, y, &mut o1);
33+
if mul0 != mul1 || o0 != (o1 != 0) {
34+
panic!(
35+
"__mulodi4({}, {}): expected: {} (oflow = {}), found: {} (oflow = {})",
36+
x, y, mul0, o0, mul1, o1
37+
);
38+
}
39+
let (mul2, o2) = __rust_i128_mulo(x, y);
40+
if mul0 != mul2 || o0 != o2 {
41+
panic!(
42+
"__rust_i128_mulo({}, {}): expected: {} (oflow = {}), found: {} (oflow = {})",
43+
x, y, mul0, o0, mul2, o2
44+
);
45+
}
46+
});
47+
testcrate::fuzz_2(10_000, |x: u128, y: u128| {
48+
let (mul0, o0) = x.overflowing_mul(y);
49+
let (mul1, o1) = __rust_u128_mulo(x, y);
50+
if mul0 != mul1 || o0 != o1 {
51+
panic!(
52+
"__rust_u128_mulo({}, {}): expected: {} (oflow = {}), found: {} (oflow = {})",
53+
x, y, mul0, o0, mul1, o1
54+
);
55+
}
56+
});
57+
}

0 commit comments

Comments
 (0)