1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -22,15 +22,28 @@ macro_rules! test {
22
22
( $val: expr, $src_ty: ident -> $dest_ty: ident, $expected: expr) => (
23
23
// black_box disables constant evaluation to test run-time conversions:
24
24
assert_eq!( black_box:: <$src_ty>( $val) as $dest_ty, $expected,
25
- "run time {} -> {}" , stringify!( $src_ty) , stringify!( $dest_ty) ) ;
26
- // ... whereas this variant triggers constant evaluation:
25
+ "run-time {} -> {}" , stringify!( $src_ty) , stringify!( $dest_ty) ) ;
26
+ ) ;
27
+
28
+ ( $fval: expr, f* -> $ity: ident, $ival: expr) => (
29
+ test!( $fval, f32 -> $ity, $ival) ;
30
+ test!( $fval, f64 -> $ity, $ival) ;
31
+ )
32
+ }
33
+
34
+ // This macro tests const eval in addition to run-time evaluation.
35
+ // If and when saturating casts are adopted, this macro should be merged with test!() to ensure
36
+ // that run-time and const eval agree on inputs that currently trigger a const eval error.
37
+ macro_rules! test_c {
38
+ ( $val: expr, $src_ty: ident -> $dest_ty: ident, $expected: expr) => ( {
39
+ test!( $val, $src_ty -> $dest_ty, $expected) ;
27
40
{
28
41
const X : $src_ty = $val;
29
42
const Y : $dest_ty = X as $dest_ty;
30
43
assert_eq!( Y , $expected,
31
44
"const eval {} -> {}" , stringify!( $src_ty) , stringify!( $dest_ty) ) ;
32
45
}
33
- ) ;
46
+ } ) ;
34
47
35
48
( $fval: expr, f* -> $ity: ident, $ival: expr) => (
36
49
test!( $fval, f32 -> $ity, $ival) ;
@@ -48,11 +61,11 @@ macro_rules! common_fptoi_tests {
48
61
// as well, the test is just slightly misplaced.
49
62
test!( $ity:: MIN as $fty, $fty -> $ity, $ity:: MIN ) ;
50
63
test!( $ity:: MAX as $fty, $fty -> $ity, $ity:: MAX ) ;
51
- test !( 0. , $fty -> $ity, 0 ) ;
52
- test !( $fty:: MIN_POSITIVE , $fty -> $ity, 0 ) ;
64
+ test_c !( 0. , $fty -> $ity, 0 ) ;
65
+ test_c !( $fty:: MIN_POSITIVE , $fty -> $ity, 0 ) ;
53
66
test!( -0.9 , $fty -> $ity, 0 ) ;
54
- test !( 1. , $fty -> $ity, 1 ) ;
55
- test !( 42. , $fty -> $ity, 42 ) ;
67
+ test_c !( 1. , $fty -> $ity, 1 ) ;
68
+ test_c !( 42. , $fty -> $ity, 42 ) ;
56
69
) + } ) ;
57
70
58
71
( f* -> $( $ity: ident) +) => ( {
@@ -84,58 +97,58 @@ pub fn main() {
84
97
85
98
// The following tests cover edge cases for some integer types.
86
99
87
- // u8
88
- test ! ( 254. , f* -> u8 , 254 ) ;
100
+ // # u8
101
+ test_c ! ( 254. , f* -> u8 , 254 ) ;
89
102
test ! ( 256. , f* -> u8 , 255 ) ;
90
103
91
- // i8
92
- test ! ( -127. , f* -> i8 , -127 ) ;
104
+ // # i8
105
+ test_c ! ( -127. , f* -> i8 , -127 ) ;
93
106
test ! ( -129. , f* -> i8 , -128 ) ;
94
- test ! ( 126. , f* -> i8 , 126 ) ;
107
+ test_c ! ( 126. , f* -> i8 , 126 ) ;
95
108
test ! ( 128. , f* -> i8 , 127 ) ;
96
109
97
- // i32
110
+ // # i32
98
111
// -2147483648. is i32::MIN (exactly)
99
- test ! ( -2147483648. , f* -> i32 , i32 :: MIN ) ;
112
+ test_c ! ( -2147483648. , f* -> i32 , i32 :: MIN ) ;
100
113
// 2147483648. is i32::MAX rounded up
101
114
test ! ( 2147483648. , f32 -> i32 , 2147483647 ) ;
102
115
// With 24 significand bits, floats with magnitude in [2^30 + 1, 2^31] are rounded to
103
116
// multiples of 2^7. Therefore, nextDown(round(i32::MAX)) is 2^31 - 128:
104
- test ! ( 2147483520. , f32 -> i32 , 2147483520 ) ;
117
+ test_c ! ( 2147483520. , f32 -> i32 , 2147483520 ) ;
105
118
// Similarly, nextUp(i32::MIN) is i32::MIN + 2^8 and nextDown(i32::MIN) is i32::MIN - 2^7
106
119
test ! ( -2147483904. , f* -> i32 , i32 :: MIN ) ;
107
- test ! ( -2147483520. , f* -> i32 , -2147483520 ) ;
120
+ test_c ! ( -2147483520. , f* -> i32 , -2147483520 ) ;
108
121
109
- // u32 -- round(MAX) and nextUp(round(MAX))
110
- test ! ( 4294967040. , f* -> u32 , 4294967040 ) ;
122
+ // # u32
123
+ // round(MAX) and nextUp(round(MAX))
124
+ test_c ! ( 4294967040. , f* -> u32 , 4294967040 ) ;
111
125
test ! ( 4294967296. , f* -> u32 , 4294967295 ) ;
112
126
113
- // u128
114
- // # float->int
115
- test ! ( f32 :: MAX , f32 -> u128 , 0xffffff00000000000000000000000000 ) ;
127
+ // # u128
128
+ // float->int:
129
+ test_c ! ( f32 :: MAX , f32 -> u128 , 0xffffff00000000000000000000000000 ) ;
116
130
// nextDown(f32::MAX) = 2^128 - 2 * 2^104
117
131
const SECOND_LARGEST_F32 : f32 = 340282326356119256160033759537265639424. ;
118
- test ! ( SECOND_LARGEST_F32 , f32 -> u128 , 0xfffffe00000000000000000000000000 ) ;
119
- // # int->float
132
+ test_c ! ( SECOND_LARGEST_F32 , f32 -> u128 , 0xfffffe00000000000000000000000000 ) ;
133
+
134
+ // int->float:
120
135
// f32::MAX - 0.5 ULP and smaller should be rounded down
121
- test ! ( 0xfffffe00000000000000000000000000 , u128 -> f32 , SECOND_LARGEST_F32 ) ;
122
- test ! ( 0xfffffe7fffffffffffffffffffffffff , u128 -> f32 , SECOND_LARGEST_F32 ) ;
123
- test ! ( 0xfffffe80000000000000000000000000 , u128 -> f32 , SECOND_LARGEST_F32 ) ;
136
+ test_c ! ( 0xfffffe00000000000000000000000000 , u128 -> f32 , SECOND_LARGEST_F32 ) ;
137
+ test_c ! ( 0xfffffe7fffffffffffffffffffffffff , u128 -> f32 , SECOND_LARGEST_F32 ) ;
138
+ test_c ! ( 0xfffffe80000000000000000000000000 , u128 -> f32 , SECOND_LARGEST_F32 ) ;
124
139
// numbers within < 0.5 ULP of f32::MAX it should be rounded to f32::MAX
125
- test ! ( 0xfffffe80000000000000000000000001 , u128 -> f32 , f32 :: MAX ) ;
126
- test ! ( 0xfffffeffffffffffffffffffffffffff , u128 -> f32 , f32 :: MAX ) ;
127
- test ! ( 0xffffff00000000000000000000000000 , u128 -> f32 , f32 :: MAX ) ;
128
- test ! ( 0xffffff00000000000000000000000001 , u128 -> f32 , f32 :: MAX ) ;
129
- test ! ( 0xffffff7fffffffffffffffffffffffff , u128 -> f32 , f32 :: MAX ) ;
140
+ test_c ! ( 0xfffffe80000000000000000000000001 , u128 -> f32 , f32 :: MAX ) ;
141
+ test_c ! ( 0xfffffeffffffffffffffffffffffffff , u128 -> f32 , f32 :: MAX ) ;
142
+ test_c ! ( 0xffffff00000000000000000000000000 , u128 -> f32 , f32 :: MAX ) ;
143
+ test_c ! ( 0xffffff00000000000000000000000001 , u128 -> f32 , f32 :: MAX ) ;
144
+ test_c ! ( 0xffffff7fffffffffffffffffffffffff , u128 -> f32 , f32 :: MAX ) ;
130
145
// f32::MAX + 0.5 ULP and greater should be rounded to infinity
131
- test ! ( 0xffffff80000000000000000000000000 , u128 -> f32 , f32 :: INFINITY ) ;
132
- test ! ( 0xffffff80000000f00000000000000000 , u128 -> f32 , f32 :: INFINITY ) ;
133
- test ! ( 0xffffff87ffffffffffffffff00000001 , u128 -> f32 , f32 :: INFINITY ) ;
134
-
135
- test ! ( !0 , u128 -> f32 , f32 :: INFINITY ) ;
146
+ test_c ! ( 0xffffff80000000000000000000000000 , u128 -> f32 , f32 :: INFINITY ) ;
147
+ test_c ! ( 0xffffff80000000f00000000000000000 , u128 -> f32 , f32 :: INFINITY ) ;
148
+ test_c ! ( 0xffffff87ffffffffffffffff00000001 , u128 -> f32 , f32 :: INFINITY ) ;
136
149
137
150
// u128->f64 should not be affected by the u128->f32 checks
138
- test ! ( 0xffffff80000000000000000000000000 , u128 -> f64 ,
151
+ test_c ! ( 0xffffff80000000000000000000000000 , u128 -> f64 ,
139
152
340282356779733661637539395458142568448.0 ) ;
140
- test ! ( u128 :: MAX , u128 -> f64 , 340282366920938463463374607431768211455.0 ) ;
153
+ test_c ! ( u128 :: MAX , u128 -> f64 , 340282366920938463463374607431768211455.0 ) ;
141
154
}
0 commit comments