Skip to content

Commit 4f40fb7

Browse files
adpaco-awstedinski
authored andcommitted
Add test for wrapping intrinsics (rust-lang#1070)
1 parent b6d44bb commit 4f40fb7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
//
4+
// Check that the `wrapping_<op>` intrinsics perform wrapping arithmetic
5+
// operations as expected and do not trigger spurious overflow checks.
6+
//
7+
// This test is a modified version of the examples found in
8+
// https://doc.rust-lang.org/std/primitive.u32.html for wrapping operations
9+
#![feature(core_intrinsics)]
10+
use std::intrinsics::{wrapping_add, wrapping_mul, wrapping_sub};
11+
12+
#[kani::proof]
13+
fn test_wrapping_add() {
14+
// The compiler detects overflows at compile time if we use constants so we
15+
// declare a nondet. variable and assume the value to avoid annotations
16+
let x: u32 = kani::any();
17+
kani::assume(x == 200);
18+
assert!(wrapping_add(x, 55) == 255);
19+
assert!(wrapping_add(x, u32::MAX) == 199);
20+
}
21+
22+
#[kani::proof]
23+
fn test_wrapping_sub() {
24+
let x: u32 = kani::any();
25+
kani::assume(x == 100);
26+
assert_eq!(wrapping_sub(x, u32::MAX), 101);
27+
assert_eq!(wrapping_sub(x, 100), 0);
28+
}
29+
30+
#[kani::proof]
31+
fn test_wrapping_mul() {
32+
let x: u8 = kani::any();
33+
kani::assume(x == 12);
34+
assert_eq!(wrapping_mul(10u8, x), 120);
35+
assert_eq!(wrapping_mul(25u8, x), 44);
36+
}

0 commit comments

Comments
 (0)