File tree 8 files changed +171
-5
lines changed
8 files changed +171
-5
lines changed Original file line number Diff line number Diff line change @@ -42,9 +42,22 @@ pub fn ceil(x: f64) -> f64 {
42
42
43
43
#[ cfg( test) ]
44
44
mod tests {
45
+ use super :: * ;
46
+ use core:: f64:: * ;
47
+
45
48
#[ test]
46
49
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
+ }
49
62
}
50
63
}
Original file line number Diff line number Diff line change @@ -39,3 +39,25 @@ pub fn ceilf(x: f32) -> f32 {
39
39
}
40
40
f32:: from_bits ( ui)
41
41
}
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
+ }
Original file line number Diff line number Diff line change @@ -15,3 +15,27 @@ pub fn fabs(x: f64) -> f64 {
15
15
}
16
16
f64:: from_bits ( x. to_bits ( ) & ( u64:: MAX / 2 ) )
17
17
}
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
+ }
Original file line number Diff line number Diff line change @@ -13,3 +13,27 @@ pub fn fabsf(x: f32) -> f32 {
13
13
}
14
14
f32:: from_bits ( x. to_bits ( ) & 0x7fffffff )
15
15
}
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
+ }
Original file line number Diff line number Diff line change @@ -38,3 +38,25 @@ pub fn floor(x: f64) -> f64 {
38
38
x + y
39
39
}
40
40
}
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
+ }
Original file line number Diff line number Diff line change @@ -42,8 +42,23 @@ pub fn floorf(x: f32) -> f32 {
42
42
43
43
#[ cfg( test) ]
44
44
mod tests {
45
+ use super :: * ;
46
+ use core:: f32:: * ;
47
+
45
48
#[ 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
+ }
48
63
}
49
64
}
Original file line number Diff line number Diff line change 37
37
* If (2) is false, then q = q ; otherwise q = q + 2 .
38
38
* i+1 i i+1 i
39
39
*
40
- * With some algebric manipulation, it is not difficult to see
40
+ * With some algebraic manipulation, it is not difficult to see
41
41
* that (2) is equivalent to
42
42
* -(i+1)
43
43
* s + 2 <= y (3)
@@ -239,3 +239,26 @@ pub fn sqrt(x: f64) -> f64 {
239
239
f64:: from_bits ( ( ix0 as u64 ) << 32 | ix1. 0 as u64 )
240
240
}
241
241
}
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
+ }
Original file line number Diff line number Diff line change @@ -127,3 +127,26 @@ pub fn sqrtf(x: f32) -> f32 {
127
127
f32:: from_bits ( ix as u32 )
128
128
}
129
129
}
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
+ }
You can’t perform that action at this time.
0 commit comments