Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for wrapping intrinsics #1070

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/kani/Intrinsics/Math/Arith/wrapping_ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//
// Check that the `wrapping_<op>` intrinsics perform wrapping arithmetic
// operations as expected and do not trigger spurious overflow checks.
//
// This test is a modified version of the examples found in
// https://doc.rust-lang.org/std/primitive.u32.html for wrapping operations
#![feature(core_intrinsics)]
use std::intrinsics::{wrapping_add, wrapping_mul, wrapping_sub};

#[kani::proof]
fn test_wrapping_add() {
// The compiler detects overflows at compile time if we use constants so we
// declare a nondet. variable and assume the value to avoid annotations
let x: u32 = kani::any();
kani::assume(x == 200);
assert!(wrapping_add(x, 55) == 255);
assert!(wrapping_add(x, u32::MAX) == 199);
}

#[kani::proof]
fn test_wrapping_sub() {
let x: u32 = kani::any();
kani::assume(x == 100);
assert_eq!(wrapping_sub(x, u32::MAX), 101);
assert_eq!(wrapping_sub(x, 100), 0);
}

#[kani::proof]
fn test_wrapping_mul() {
let x: u8 = kani::any();
kani::assume(x == 12);
assert_eq!(wrapping_mul(10u8, x), 120);
assert_eq!(wrapping_mul(25u8, x), 44);
}