Skip to content

Commit 12481f0

Browse files
authored
Merge pull request rust-lang#221 from Lokathor/tests
slightly improve spec and sanity check coverage
2 parents 32a6a99 + cd32f26 commit 12481f0

File tree

8 files changed

+171
-5
lines changed

8 files changed

+171
-5
lines changed

src/math/ceil.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,22 @@ pub fn ceil(x: f64) -> f64 {
4242

4343
#[cfg(test)]
4444
mod tests {
45+
use super::*;
46+
use core::f64::*;
47+
4548
#[test]
4649
fn sanity_check() {
47-
assert_eq!(super::ceil(1.1), 2.0);
48-
assert_eq!(super::ceil(2.9), 3.0);
50+
assert_eq!(ceil(1.1), 2.0);
51+
assert_eq!(ceil(2.9), 3.0);
52+
}
53+
54+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil
55+
#[test]
56+
fn spec_tests() {
57+
// Not Asserted: that the current rounding mode has no effect.
58+
assert!(ceil(NAN).is_nan());
59+
for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() {
60+
assert_eq!(ceil(f), f);
61+
}
4962
}
5063
}

src/math/ceilf.rs

+22
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,25 @@ pub fn ceilf(x: f32) -> f32 {
3939
}
4040
f32::from_bits(ui)
4141
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
use core::f32::*;
47+
48+
#[test]
49+
fn sanity_check() {
50+
assert_eq!(ceilf(1.1), 2.0);
51+
assert_eq!(ceilf(2.9), 3.0);
52+
}
53+
54+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/ceil
55+
#[test]
56+
fn spec_tests() {
57+
// Not Asserted: that the current rounding mode has no effect.
58+
assert!(ceilf(NAN).is_nan());
59+
for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() {
60+
assert_eq!(ceilf(f), f);
61+
}
62+
}
63+
}

src/math/fabs.rs

+24
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,27 @@ pub fn fabs(x: f64) -> f64 {
1515
}
1616
f64::from_bits(x.to_bits() & (u64::MAX / 2))
1717
}
18+
19+
#[cfg(test)]
20+
mod tests {
21+
use super::*;
22+
use core::f64::*;
23+
24+
#[test]
25+
fn sanity_check() {
26+
assert_eq!(fabs(-1.0), 1.0);
27+
assert_eq!(fabs(2.8), 2.8);
28+
}
29+
30+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
31+
#[test]
32+
fn spec_tests() {
33+
assert!(fabs(NAN).is_nan());
34+
for f in [0.0, -0.0].iter().copied() {
35+
assert_eq!(fabs(f), 0.0);
36+
}
37+
for f in [INFINITY, NEG_INFINITY].iter().copied() {
38+
assert_eq!(fabs(f), INFINITY);
39+
}
40+
}
41+
}

src/math/fabsf.rs

+24
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,27 @@ pub fn fabsf(x: f32) -> f32 {
1313
}
1414
f32::from_bits(x.to_bits() & 0x7fffffff)
1515
}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use super::*;
20+
use core::f32::*;
21+
22+
#[test]
23+
fn sanity_check() {
24+
assert_eq!(fabsf(-1.0), 1.0);
25+
assert_eq!(fabsf(2.8), 2.8);
26+
}
27+
28+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/fabs
29+
#[test]
30+
fn spec_tests() {
31+
assert!(fabsf(NAN).is_nan());
32+
for f in [0.0, -0.0].iter().copied() {
33+
assert_eq!(fabsf(f), 0.0);
34+
}
35+
for f in [INFINITY, NEG_INFINITY].iter().copied() {
36+
assert_eq!(fabsf(f), INFINITY);
37+
}
38+
}
39+
}

src/math/floor.rs

+22
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,25 @@ pub fn floor(x: f64) -> f64 {
3838
x + y
3939
}
4040
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
use core::f64::*;
46+
47+
#[test]
48+
fn sanity_check() {
49+
assert_eq!(floor(1.1), 1.0);
50+
assert_eq!(floor(2.9), 2.0);
51+
}
52+
53+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/floor
54+
#[test]
55+
fn spec_tests() {
56+
// Not Asserted: that the current rounding mode has no effect.
57+
assert!(floor(NAN).is_nan());
58+
for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() {
59+
assert_eq!(floor(f), f);
60+
}
61+
}
62+
}

src/math/floorf.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,23 @@ pub fn floorf(x: f32) -> f32 {
4242

4343
#[cfg(test)]
4444
mod tests {
45+
use super::*;
46+
use core::f32::*;
47+
4548
#[test]
46-
fn no_overflow() {
47-
assert_eq!(super::floorf(0.5), 0.0);
49+
fn sanity_check() {
50+
assert_eq!(floorf(0.5), 0.0);
51+
assert_eq!(floorf(1.1), 1.0);
52+
assert_eq!(floorf(2.9), 2.0);
53+
}
54+
55+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/floor
56+
#[test]
57+
fn spec_tests() {
58+
// Not Asserted: that the current rounding mode has no effect.
59+
assert!(floorf(NAN).is_nan());
60+
for f in [0.0, -0.0, INFINITY, NEG_INFINITY].iter().copied() {
61+
assert_eq!(floorf(f), f);
62+
}
4863
}
4964
}

src/math/sqrt.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* If (2) is false, then q = q ; otherwise q = q + 2 .
3838
* i+1 i i+1 i
3939
*
40-
* With some algebric manipulation, it is not difficult to see
40+
* With some algebraic manipulation, it is not difficult to see
4141
* that (2) is equivalent to
4242
* -(i+1)
4343
* s + 2 <= y (3)
@@ -239,3 +239,26 @@ pub fn sqrt(x: f64) -> f64 {
239239
f64::from_bits((ix0 as u64) << 32 | ix1.0 as u64)
240240
}
241241
}
242+
243+
#[cfg(test)]
244+
mod tests {
245+
use super::*;
246+
use core::f64::*;
247+
248+
#[test]
249+
fn sanity_check() {
250+
assert_eq!(sqrt(100.0), 10.0);
251+
assert_eq!(sqrt(4.0), 2.0);
252+
}
253+
254+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/sqrt
255+
#[test]
256+
fn spec_tests() {
257+
// Not Asserted: FE_INVALID exception is raised if argument is negative.
258+
assert!(sqrt(-1.0).is_nan());
259+
assert!(sqrt(NAN).is_nan());
260+
for f in [0.0, -0.0, INFINITY].iter().copied() {
261+
assert_eq!(sqrt(f), f);
262+
}
263+
}
264+
}

src/math/sqrtf.rs

+23
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,26 @@ pub fn sqrtf(x: f32) -> f32 {
127127
f32::from_bits(ix as u32)
128128
}
129129
}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use super::*;
134+
use core::f32::*;
135+
136+
#[test]
137+
fn sanity_check() {
138+
assert_eq!(sqrtf(100.0), 10.0);
139+
assert_eq!(sqrtf(4.0), 2.0);
140+
}
141+
142+
/// The spec: https://en.cppreference.com/w/cpp/numeric/math/sqrt
143+
#[test]
144+
fn spec_tests() {
145+
// Not Asserted: FE_INVALID exception is raised if argument is negative.
146+
assert!(sqrtf(-1.0).is_nan());
147+
assert!(sqrtf(NAN).is_nan());
148+
for f in [0.0, -0.0, INFINITY].iter().copied() {
149+
assert_eq!(sqrtf(f), f);
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)