Skip to content

Commit 783a848

Browse files
adpaco-awstedinski
authored andcommitted
Tests for "absolute" intrinsics (rust-lang#1033)
1 parent da07567 commit 783a848

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/kani/Intrinsics/Math/fabsf32.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `fabsf32` returns the expected results: absolute value if argument
5+
// is not NaN, otherwise NaN
6+
#![feature(core_intrinsics)]
7+
8+
#[kani::proof]
9+
fn test_abs_finite() {
10+
let x: f32 = kani::any();
11+
kani::assume(!x.is_nan());
12+
let abs_x = unsafe { std::intrinsics::fabsf32(x) };
13+
if x < 0.0 {
14+
assert!(-x == abs_x);
15+
} else {
16+
assert!(x == abs_x);
17+
}
18+
}
19+
20+
#[kani::proof]
21+
fn test_abs_nan() {
22+
let x: f32 = kani::any();
23+
kani::assume(x.is_nan());
24+
let abs_x = unsafe { std::intrinsics::fabsf32(x) };
25+
assert!(abs_x.is_nan());
26+
}

tests/kani/Intrinsics/Math/fabsf64.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Check that `fabsf64` returns the expected results: absolute value if argument
5+
// is not NaN, otherwise NaN
6+
#![feature(core_intrinsics)]
7+
8+
#[kani::proof]
9+
fn test_abs_finite() {
10+
let x: f64 = kani::any();
11+
kani::assume(!x.is_nan());
12+
let abs_x = unsafe { std::intrinsics::fabsf64(x) };
13+
if x < 0.0 {
14+
assert!(-x == abs_x);
15+
} else {
16+
assert!(x == abs_x);
17+
}
18+
}
19+
20+
#[kani::proof]
21+
fn test_abs_nan() {
22+
let x: f64 = kani::any();
23+
kani::assume(x.is_nan());
24+
let abs_x = unsafe { std::intrinsics::fabsf64(x) };
25+
assert!(abs_x.is_nan());
26+
}

0 commit comments

Comments
 (0)