@@ -17,42 +17,6 @@ macro_rules! int_module {
17
17
num:: test_num( 10 as $T, 2 as $T) ;
18
18
}
19
19
20
- #[ test]
21
- fn test_rem_euclid( ) {
22
- assert_eq!( ( -1 as $T) . rem_euclid( MIN ) , MAX ) ;
23
- }
24
-
25
- #[ test]
26
- pub fn test_abs( ) {
27
- assert_eq!( ( 1 as $T) . abs( ) , 1 as $T) ;
28
- assert_eq!( ( 0 as $T) . abs( ) , 0 as $T) ;
29
- assert_eq!( ( -1 as $T) . abs( ) , 1 as $T) ;
30
- }
31
-
32
- #[ test]
33
- fn test_signum( ) {
34
- assert_eq!( ( 1 as $T) . signum( ) , 1 as $T) ;
35
- assert_eq!( ( 0 as $T) . signum( ) , 0 as $T) ;
36
- assert_eq!( ( -0 as $T) . signum( ) , 0 as $T) ;
37
- assert_eq!( ( -1 as $T) . signum( ) , -1 as $T) ;
38
- }
39
-
40
- #[ test]
41
- fn test_is_positive( ) {
42
- assert!( ( 1 as $T) . is_positive( ) ) ;
43
- assert!( !( 0 as $T) . is_positive( ) ) ;
44
- assert!( !( -0 as $T) . is_positive( ) ) ;
45
- assert!( !( -1 as $T) . is_positive( ) ) ;
46
- }
47
-
48
- #[ test]
49
- fn test_is_negative( ) {
50
- assert!( !( 1 as $T) . is_negative( ) ) ;
51
- assert!( !( 0 as $T) . is_negative( ) ) ;
52
- assert!( !( -0 as $T) . is_negative( ) ) ;
53
- assert!( ( -1 as $T) . is_negative( ) ) ;
54
- }
55
-
56
20
#[ test]
57
21
fn test_bitwise_operators( ) {
58
22
assert_eq!( 0b1110 as $T, ( 0b1100 as $T) . bitor( 0b1010 as $T) ) ;
@@ -63,141 +27,167 @@ macro_rules! int_module {
63
27
assert_eq!( -( 0b11 as $T) - ( 1 as $T) , ( 0b11 as $T) . not( ) ) ;
64
28
}
65
29
30
+ test_runtime_and_compiletime! {
31
+
32
+ fn test_rem_euclid( ) {
33
+ assert_eq_const_safe!( ( -1 as $T) . rem_euclid( MIN ) , MAX ) ;
34
+ }
35
+
36
+ fn test_abs( ) {
37
+ assert_eq_const_safe!( ( 1 as $T) . abs( ) , 1 as $T) ;
38
+ assert_eq_const_safe!( ( 0 as $T) . abs( ) , 0 as $T) ;
39
+ assert_eq_const_safe!( ( -1 as $T) . abs( ) , 1 as $T) ;
40
+ }
41
+
42
+ fn test_signum( ) {
43
+ assert_eq_const_safe!( ( 1 as $T) . signum( ) , 1 as $T) ;
44
+ assert_eq_const_safe!( ( 0 as $T) . signum( ) , 0 as $T) ;
45
+ assert_eq_const_safe!( ( -0 as $T) . signum( ) , 0 as $T) ;
46
+ assert_eq_const_safe!( ( -1 as $T) . signum( ) , -1 as $T) ;
47
+ }
48
+
49
+ fn test_is_positive( ) {
50
+ assert!( ( 1 as $T) . is_positive( ) ) ;
51
+ assert!( !( 0 as $T) . is_positive( ) ) ;
52
+ assert!( !( -0 as $T) . is_positive( ) ) ;
53
+ assert!( !( -1 as $T) . is_positive( ) ) ;
54
+ }
55
+
56
+ fn test_is_negative( ) {
57
+ assert!( !( 1 as $T) . is_negative( ) ) ;
58
+ assert!( !( 0 as $T) . is_negative( ) ) ;
59
+ assert!( !( -0 as $T) . is_negative( ) ) ;
60
+ assert!( ( -1 as $T) . is_negative( ) ) ;
61
+ }
62
+ }
63
+
66
64
const A : $T = 0b0101100 ;
67
65
const B : $T = 0b0100001 ;
68
66
const C : $T = 0b1111001 ;
69
67
70
68
const _0: $T = 0 ;
71
69
const _1: $T = !0 ;
72
70
73
- # [ test ]
74
- fn test_count_ones( ) {
75
- assert_eq !( A . count_ones( ) , 3 ) ;
76
- assert_eq !( B . count_ones( ) , 2 ) ;
77
- assert_eq !( C . count_ones( ) , 5 ) ;
78
- }
71
+ test_runtime_and_compiletime! {
72
+ fn test_count_ones( ) {
73
+ assert_eq_const_safe !( A . count_ones( ) , 3 ) ;
74
+ assert_eq_const_safe !( B . count_ones( ) , 2 ) ;
75
+ assert_eq_const_safe !( C . count_ones( ) , 5 ) ;
76
+ }
79
77
80
- #[ test]
81
- fn test_count_zeros( ) {
82
- assert_eq!( A . count_zeros( ) , $T:: BITS - 3 ) ;
83
- assert_eq!( B . count_zeros( ) , $T:: BITS - 2 ) ;
84
- assert_eq!( C . count_zeros( ) , $T:: BITS - 5 ) ;
85
- }
78
+ fn test_count_zeros( ) {
79
+ assert_eq_const_safe!( A . count_zeros( ) , $T:: BITS - 3 ) ;
80
+ assert_eq_const_safe!( B . count_zeros( ) , $T:: BITS - 2 ) ;
81
+ assert_eq_const_safe!( C . count_zeros( ) , $T:: BITS - 5 ) ;
82
+ }
86
83
87
- #[ test]
88
- fn test_leading_trailing_ones( ) {
89
- let a: $T = 0b0101_1111 ;
90
- assert_eq!( a. trailing_ones( ) , 5 ) ;
91
- assert_eq!( ( !a) . leading_ones( ) , $T:: BITS - 7 ) ;
84
+ fn test_leading_trailing_ones( ) {
85
+ const A : $T = 0b0101_1111 ;
86
+ assert_eq_const_safe!( A . trailing_ones( ) , 5 ) ;
87
+ assert_eq_const_safe!( ( !A ) . leading_ones( ) , $T:: BITS - 7 ) ;
92
88
93
- assert_eq! ( a . reverse_bits( ) . leading_ones( ) , 5 ) ;
89
+ assert_eq_const_safe! ( A . reverse_bits( ) . leading_ones( ) , 5 ) ;
94
90
95
- assert_eq !( _1. leading_ones( ) , $T:: BITS ) ;
96
- assert_eq !( _1. trailing_ones( ) , $T:: BITS ) ;
91
+ assert_eq_const_safe !( _1. leading_ones( ) , $T:: BITS ) ;
92
+ assert_eq_const_safe !( _1. trailing_ones( ) , $T:: BITS ) ;
97
93
98
- assert_eq !( ( _1 << 1 ) . trailing_ones( ) , 0 ) ;
99
- assert_eq !( MAX . leading_ones( ) , 0 ) ;
94
+ assert_eq_const_safe !( ( _1 << 1 ) . trailing_ones( ) , 0 ) ;
95
+ assert_eq_const_safe !( MAX . leading_ones( ) , 0 ) ;
100
96
101
- assert_eq !( ( _1 << 1 ) . leading_ones( ) , $T:: BITS - 1 ) ;
102
- assert_eq !( MAX . trailing_ones( ) , $T:: BITS - 1 ) ;
97
+ assert_eq_const_safe !( ( _1 << 1 ) . leading_ones( ) , $T:: BITS - 1 ) ;
98
+ assert_eq_const_safe !( MAX . trailing_ones( ) , $T:: BITS - 1 ) ;
103
99
104
- assert_eq !( _0. leading_ones( ) , 0 ) ;
105
- assert_eq !( _0. trailing_ones( ) , 0 ) ;
100
+ assert_eq_const_safe !( _0. leading_ones( ) , 0 ) ;
101
+ assert_eq_const_safe !( _0. trailing_ones( ) , 0 ) ;
106
102
107
- let x : $T = 0b0010_1100 ;
108
- assert_eq! ( x . leading_ones( ) , 0 ) ;
109
- assert_eq! ( x . trailing_ones( ) , 0 ) ;
110
- }
103
+ const X : $T = 0b0010_1100 ;
104
+ assert_eq_const_safe! ( X . leading_ones( ) , 0 ) ;
105
+ assert_eq_const_safe! ( X . trailing_ones( ) , 0 ) ;
106
+ }
111
107
112
- #[ test]
113
- fn test_rotate( ) {
114
- assert_eq!( A . rotate_left( 6 ) . rotate_right( 2 ) . rotate_right( 4 ) , A ) ;
115
- assert_eq!( B . rotate_left( 3 ) . rotate_left( 2 ) . rotate_right( 5 ) , B ) ;
116
- assert_eq!( C . rotate_left( 6 ) . rotate_right( 2 ) . rotate_right( 4 ) , C ) ;
117
-
118
- // Rotating these should make no difference
119
- //
120
- // We test using 124 bits because to ensure that overlong bit shifts do
121
- // not cause undefined behaviour. See #10183.
122
- assert_eq!( _0. rotate_left( 124 ) , _0) ;
123
- assert_eq!( _1. rotate_left( 124 ) , _1) ;
124
- assert_eq!( _0. rotate_right( 124 ) , _0) ;
125
- assert_eq!( _1. rotate_right( 124 ) , _1) ;
126
-
127
- // Rotating by 0 should have no effect
128
- assert_eq!( A . rotate_left( 0 ) , A ) ;
129
- assert_eq!( B . rotate_left( 0 ) , B ) ;
130
- assert_eq!( C . rotate_left( 0 ) , C ) ;
131
- // Rotating by a multiple of word size should also have no effect
132
- assert_eq!( A . rotate_left( 128 ) , A ) ;
133
- assert_eq!( B . rotate_left( 128 ) , B ) ;
134
- assert_eq!( C . rotate_left( 128 ) , C ) ;
135
- }
108
+ fn test_rotate( ) {
109
+ assert_eq_const_safe!( A . rotate_left( 6 ) . rotate_right( 2 ) . rotate_right( 4 ) , A ) ;
110
+ assert_eq_const_safe!( B . rotate_left( 3 ) . rotate_left( 2 ) . rotate_right( 5 ) , B ) ;
111
+ assert_eq_const_safe!( C . rotate_left( 6 ) . rotate_right( 2 ) . rotate_right( 4 ) , C ) ;
112
+
113
+ // Rotating these should make no difference
114
+ //
115
+ // We test using 124 bits because to ensure that overlong bit shifts do
116
+ // not cause undefined behaviour. See #10183.
117
+ assert_eq_const_safe!( _0. rotate_left( 124 ) , _0) ;
118
+ assert_eq_const_safe!( _1. rotate_left( 124 ) , _1) ;
119
+ assert_eq_const_safe!( _0. rotate_right( 124 ) , _0) ;
120
+ assert_eq_const_safe!( _1. rotate_right( 124 ) , _1) ;
121
+
122
+ // Rotating by 0 should have no effect
123
+ assert_eq_const_safe!( A . rotate_left( 0 ) , A ) ;
124
+ assert_eq_const_safe!( B . rotate_left( 0 ) , B ) ;
125
+ assert_eq_const_safe!( C . rotate_left( 0 ) , C ) ;
126
+ // Rotating by a multiple of word size should also have no effect
127
+ assert_eq_const_safe!( A . rotate_left( 128 ) , A ) ;
128
+ assert_eq_const_safe!( B . rotate_left( 128 ) , B ) ;
129
+ assert_eq_const_safe!( C . rotate_left( 128 ) , C ) ;
130
+ }
136
131
137
- #[ test]
138
- fn test_swap_bytes( ) {
139
- assert_eq!( A . swap_bytes( ) . swap_bytes( ) , A ) ;
140
- assert_eq!( B . swap_bytes( ) . swap_bytes( ) , B ) ;
141
- assert_eq!( C . swap_bytes( ) . swap_bytes( ) , C ) ;
142
-
143
- // Swapping these should make no difference
144
- assert_eq!( _0. swap_bytes( ) , _0) ;
145
- assert_eq!( _1. swap_bytes( ) , _1) ;
146
- }
132
+ fn test_swap_bytes( ) {
133
+ assert_eq_const_safe!( A . swap_bytes( ) . swap_bytes( ) , A ) ;
134
+ assert_eq_const_safe!( B . swap_bytes( ) . swap_bytes( ) , B ) ;
135
+ assert_eq_const_safe!( C . swap_bytes( ) . swap_bytes( ) , C ) ;
147
136
148
- #[ test]
149
- fn test_le( ) {
150
- assert_eq!( $T:: from_le( A . to_le( ) ) , A ) ;
151
- assert_eq!( $T:: from_le( B . to_le( ) ) , B ) ;
152
- assert_eq!( $T:: from_le( C . to_le( ) ) , C ) ;
153
- assert_eq!( $T:: from_le( _0) , _0) ;
154
- assert_eq!( $T:: from_le( _1) , _1) ;
155
- assert_eq!( _0. to_le( ) , _0) ;
156
- assert_eq!( _1. to_le( ) , _1) ;
157
- }
137
+ // Swapping these should make no difference
138
+ assert_eq_const_safe!( _0. swap_bytes( ) , _0) ;
139
+ assert_eq_const_safe!( _1. swap_bytes( ) , _1) ;
140
+ }
158
141
159
- #[ test]
160
- fn test_be( ) {
161
- assert_eq!( $T:: from_be( A . to_be( ) ) , A ) ;
162
- assert_eq!( $T:: from_be( B . to_be( ) ) , B ) ;
163
- assert_eq!( $T:: from_be( C . to_be( ) ) , C ) ;
164
- assert_eq!( $T:: from_be( _0) , _0) ;
165
- assert_eq!( $T:: from_be( _1) , _1) ;
166
- assert_eq!( _0. to_be( ) , _0) ;
167
- assert_eq!( _1. to_be( ) , _1) ;
168
- }
142
+ fn test_le( ) {
143
+ assert_eq_const_safe!( $T:: from_le( A . to_le( ) ) , A ) ;
144
+ assert_eq_const_safe!( $T:: from_le( B . to_le( ) ) , B ) ;
145
+ assert_eq_const_safe!( $T:: from_le( C . to_le( ) ) , C ) ;
146
+ assert_eq_const_safe!( $T:: from_le( _0) , _0) ;
147
+ assert_eq_const_safe!( $T:: from_le( _1) , _1) ;
148
+ assert_eq_const_safe!( _0. to_le( ) , _0) ;
149
+ assert_eq_const_safe!( _1. to_le( ) , _1) ;
150
+ }
169
151
170
- #[ test]
171
- fn test_signed_checked_div( ) {
172
- assert_eq!( ( 10 as $T) . checked_div( 2 ) , Some ( 5 ) ) ;
173
- assert_eq!( ( 5 as $T) . checked_div( 0 ) , None ) ;
174
- assert_eq!( isize :: MIN . checked_div( -1 ) , None ) ;
175
- }
152
+ fn test_be( ) {
153
+ assert_eq_const_safe!( $T:: from_be( A . to_be( ) ) , A ) ;
154
+ assert_eq_const_safe!( $T:: from_be( B . to_be( ) ) , B ) ;
155
+ assert_eq_const_safe!( $T:: from_be( C . to_be( ) ) , C ) ;
156
+ assert_eq_const_safe!( $T:: from_be( _0) , _0) ;
157
+ assert_eq_const_safe!( $T:: from_be( _1) , _1) ;
158
+ assert_eq_const_safe!( _0. to_be( ) , _0) ;
159
+ assert_eq_const_safe!( _1. to_be( ) , _1) ;
160
+ }
176
161
177
- #[ test]
178
- fn test_saturating_abs( ) {
179
- assert_eq!( ( 0 as $T) . saturating_abs( ) , 0 ) ;
180
- assert_eq!( ( 123 as $T) . saturating_abs( ) , 123 ) ;
181
- assert_eq!( ( -123 as $T) . saturating_abs( ) , 123 ) ;
182
- assert_eq!( ( MAX - 2 ) . saturating_abs( ) , MAX - 2 ) ;
183
- assert_eq!( ( MAX - 1 ) . saturating_abs( ) , MAX - 1 ) ;
184
- assert_eq!( MAX . saturating_abs( ) , MAX ) ;
185
- assert_eq!( ( MIN + 2 ) . saturating_abs( ) , MAX - 1 ) ;
186
- assert_eq!( ( MIN + 1 ) . saturating_abs( ) , MAX ) ;
187
- assert_eq!( MIN . saturating_abs( ) , MAX ) ;
188
- }
162
+ fn test_signed_checked_div( ) {
163
+ assert_eq_const_safe!( ( 10 as $T) . checked_div( 2 ) , Some ( 5 ) ) ;
164
+ assert_eq_const_safe!( ( 5 as $T) . checked_div( 0 ) , None ) ;
165
+ assert_eq_const_safe!( isize :: MIN . checked_div( -1 ) , None ) ;
166
+ }
189
167
190
- #[ test]
191
- fn test_saturating_neg( ) {
192
- assert_eq!( ( 0 as $T) . saturating_neg( ) , 0 ) ;
193
- assert_eq!( ( 123 as $T) . saturating_neg( ) , -123 ) ;
194
- assert_eq!( ( -123 as $T) . saturating_neg( ) , 123 ) ;
195
- assert_eq!( ( MAX - 2 ) . saturating_neg( ) , MIN + 3 ) ;
196
- assert_eq!( ( MAX - 1 ) . saturating_neg( ) , MIN + 2 ) ;
197
- assert_eq!( MAX . saturating_neg( ) , MIN + 1 ) ;
198
- assert_eq!( ( MIN + 2 ) . saturating_neg( ) , MAX - 1 ) ;
199
- assert_eq!( ( MIN + 1 ) . saturating_neg( ) , MAX ) ;
200
- assert_eq!( MIN . saturating_neg( ) , MAX ) ;
168
+ fn test_saturating_abs( ) {
169
+ assert_eq_const_safe!( ( 0 as $T) . saturating_abs( ) , 0 ) ;
170
+ assert_eq_const_safe!( ( 123 as $T) . saturating_abs( ) , 123 ) ;
171
+ assert_eq_const_safe!( ( -123 as $T) . saturating_abs( ) , 123 ) ;
172
+ assert_eq_const_safe!( ( MAX - 2 ) . saturating_abs( ) , MAX - 2 ) ;
173
+ assert_eq_const_safe!( ( MAX - 1 ) . saturating_abs( ) , MAX - 1 ) ;
174
+ assert_eq_const_safe!( MAX . saturating_abs( ) , MAX ) ;
175
+ assert_eq_const_safe!( ( MIN + 2 ) . saturating_abs( ) , MAX - 1 ) ;
176
+ assert_eq_const_safe!( ( MIN + 1 ) . saturating_abs( ) , MAX ) ;
177
+ assert_eq_const_safe!( MIN . saturating_abs( ) , MAX ) ;
178
+ }
179
+
180
+ fn test_saturating_neg( ) {
181
+ assert_eq_const_safe!( ( 0 as $T) . saturating_neg( ) , 0 ) ;
182
+ assert_eq_const_safe!( ( 123 as $T) . saturating_neg( ) , -123 ) ;
183
+ assert_eq_const_safe!( ( -123 as $T) . saturating_neg( ) , 123 ) ;
184
+ assert_eq_const_safe!( ( MAX - 2 ) . saturating_neg( ) , MIN + 3 ) ;
185
+ assert_eq_const_safe!( ( MAX - 1 ) . saturating_neg( ) , MIN + 2 ) ;
186
+ assert_eq_const_safe!( MAX . saturating_neg( ) , MIN + 1 ) ;
187
+ assert_eq_const_safe!( ( MIN + 2 ) . saturating_neg( ) , MAX - 1 ) ;
188
+ assert_eq_const_safe!( ( MIN + 1 ) . saturating_neg( ) , MAX ) ;
189
+ assert_eq_const_safe!( MIN . saturating_neg( ) , MAX ) ;
190
+ }
201
191
}
202
192
203
193
#[ test]
@@ -222,173 +212,173 @@ macro_rules! int_module {
222
212
assert_eq!( from_str:: <$T>( "x" ) , None ) ;
223
213
}
224
214
225
- # [ test ]
226
- fn test_from_str_radix( ) {
227
- assert_eq !( $T:: from_str_radix( "123" , 10 ) , Ok ( 123 as $T) ) ;
228
- assert_eq !( $T:: from_str_radix( "1001" , 2 ) , Ok ( 9 as $T) ) ;
229
- assert_eq !( $T:: from_str_radix( "123" , 8 ) , Ok ( 83 as $T) ) ;
230
- assert_eq !( i32 :: from_str_radix( "123" , 16 ) , Ok ( 291 as i32 ) ) ;
231
- assert_eq !( i32 :: from_str_radix( "ffff" , 16 ) , Ok ( 65535 as i32 ) ) ;
232
- assert_eq !( i32 :: from_str_radix( "FFFF" , 16 ) , Ok ( 65535 as i32 ) ) ;
233
- assert_eq !( $T:: from_str_radix( "z" , 36 ) , Ok ( 35 as $T) ) ;
234
- assert_eq !( $T:: from_str_radix( "Z" , 36 ) , Ok ( 35 as $T) ) ;
235
-
236
- assert_eq !( $T:: from_str_radix( "-123" , 10 ) , Ok ( -123 as $T) ) ;
237
- assert_eq !( $T:: from_str_radix( "-1001" , 2 ) , Ok ( -9 as $T) ) ;
238
- assert_eq !( $T:: from_str_radix( "-123" , 8 ) , Ok ( -83 as $T) ) ;
239
- assert_eq !( i32 :: from_str_radix( "-123" , 16 ) , Ok ( -291 as i32 ) ) ;
240
- assert_eq !( i32 :: from_str_radix( "-ffff" , 16 ) , Ok ( -65535 as i32 ) ) ;
241
- assert_eq !( i32 :: from_str_radix( "-FFFF" , 16 ) , Ok ( -65535 as i32 ) ) ;
242
- assert_eq !( $T:: from_str_radix( "-z" , 36 ) , Ok ( -35 as $T) ) ;
243
- assert_eq !( $T:: from_str_radix( "-Z" , 36 ) , Ok ( -35 as $T) ) ;
244
-
245
- assert_eq !( $T:: from_str_radix( "Z" , 35 ) . ok ( ) , None :: <$T> ) ;
246
- assert_eq !( $T:: from_str_radix( "-9" , 2 ) . ok ( ) , None :: <$T> ) ;
247
- assert_eq !( $T:: from_str_radix( "10_0" , 10 ) . ok ( ) , None :: <$T> ) ;
248
- assert_eq !( u32 :: from_str_radix( "-9" , 10 ) . ok ( ) , None :: < u32 > ) ;
249
- }
215
+ test_runtime_and_compiletime! {
216
+ fn test_from_str_radix( ) {
217
+ assert_eq_const_safe !( $T:: from_str_radix( "123" , 10 ) , Ok ( 123 as $T) ) ;
218
+ assert_eq_const_safe !( $T:: from_str_radix( "1001" , 2 ) , Ok ( 9 as $T) ) ;
219
+ assert_eq_const_safe !( $T:: from_str_radix( "123" , 8 ) , Ok ( 83 as $T) ) ;
220
+ assert_eq_const_safe !( i32 :: from_str_radix( "123" , 16 ) , Ok ( 291 as i32 ) ) ;
221
+ assert_eq_const_safe !( i32 :: from_str_radix( "ffff" , 16 ) , Ok ( 65535 as i32 ) ) ;
222
+ assert_eq_const_safe !( i32 :: from_str_radix( "FFFF" , 16 ) , Ok ( 65535 as i32 ) ) ;
223
+ assert_eq_const_safe !( $T:: from_str_radix( "z" , 36 ) , Ok ( 35 as $T) ) ;
224
+ assert_eq_const_safe !( $T:: from_str_radix( "Z" , 36 ) , Ok ( 35 as $T) ) ;
225
+
226
+ assert_eq_const_safe !( $T:: from_str_radix( "-123" , 10 ) , Ok ( -123 as $T) ) ;
227
+ assert_eq_const_safe !( $T:: from_str_radix( "-1001" , 2 ) , Ok ( -9 as $T) ) ;
228
+ assert_eq_const_safe !( $T:: from_str_radix( "-123" , 8 ) , Ok ( -83 as $T) ) ;
229
+ assert_eq_const_safe !( i32 :: from_str_radix( "-123" , 16 ) , Ok ( -291 as i32 ) ) ;
230
+ assert_eq_const_safe !( i32 :: from_str_radix( "-ffff" , 16 ) , Ok ( -65535 as i32 ) ) ;
231
+ assert_eq_const_safe !( i32 :: from_str_radix( "-FFFF" , 16 ) , Ok ( -65535 as i32 ) ) ;
232
+ assert_eq_const_safe !( $T:: from_str_radix( "-z" , 36 ) , Ok ( -35 as $T) ) ;
233
+ assert_eq_const_safe !( $T:: from_str_radix( "-Z" , 36 ) , Ok ( -35 as $T) ) ;
234
+
235
+ assert !( $T:: from_str_radix( "Z" , 35 ) . is_err ( ) ) ;
236
+ assert !( $T:: from_str_radix( "-9" , 2 ) . is_err ( ) ) ;
237
+ assert !( $T:: from_str_radix( "10_0" , 10 ) . is_err ( ) ) ;
238
+ assert !( u32 :: from_str_radix( "-9" , 10 ) . is_err ( ) ) ;
239
+ }
250
240
251
- #[ test]
252
- fn test_pow( ) {
253
- let mut r = 2 as $T;
254
- assert_eq!( r. pow( 2 ) , 4 as $T) ;
255
- assert_eq!( r. pow( 0 ) , 1 as $T) ;
256
- assert_eq!( r. wrapping_pow( 2 ) , 4 as $T) ;
257
- assert_eq!( r. wrapping_pow( 0 ) , 1 as $T) ;
258
- assert_eq!( r. checked_pow( 2 ) , Some ( 4 as $T) ) ;
259
- assert_eq!( r. checked_pow( 0 ) , Some ( 1 as $T) ) ;
260
- assert_eq!( r. overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
261
- assert_eq!( r. overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
262
- assert_eq!( r. saturating_pow( 2 ) , 4 as $T) ;
263
- assert_eq!( r. saturating_pow( 0 ) , 1 as $T) ;
264
-
265
- r = MAX ;
266
- // use `^` to represent .pow() with no overflow.
267
- // if itest::MAX == 2^j-1, then itest is a `j` bit int,
268
- // so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
269
- // thussaturating_pow the overflowing result is exactly 1.
270
- assert_eq!( r. wrapping_pow( 2 ) , 1 as $T) ;
271
- assert_eq!( r. checked_pow( 2 ) , None ) ;
272
- assert_eq!( r. overflowing_pow( 2 ) , ( 1 as $T, true ) ) ;
273
- assert_eq!( r. saturating_pow( 2 ) , MAX ) ;
274
- //test for negative exponent.
275
- r = -2 as $T;
276
- assert_eq!( r. pow( 2 ) , 4 as $T) ;
277
- assert_eq!( r. pow( 3 ) , -8 as $T) ;
278
- assert_eq!( r. pow( 0 ) , 1 as $T) ;
279
- assert_eq!( r. wrapping_pow( 2 ) , 4 as $T) ;
280
- assert_eq!( r. wrapping_pow( 3 ) , -8 as $T) ;
281
- assert_eq!( r. wrapping_pow( 0 ) , 1 as $T) ;
282
- assert_eq!( r. checked_pow( 2 ) , Some ( 4 as $T) ) ;
283
- assert_eq!( r. checked_pow( 3 ) , Some ( -8 as $T) ) ;
284
- assert_eq!( r. checked_pow( 0 ) , Some ( 1 as $T) ) ;
285
- assert_eq!( r. overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
286
- assert_eq!( r. overflowing_pow( 3 ) , ( -8 as $T, false ) ) ;
287
- assert_eq!( r. overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
288
- assert_eq!( r. saturating_pow( 2 ) , 4 as $T) ;
289
- assert_eq!( r. saturating_pow( 3 ) , -8 as $T) ;
290
- assert_eq!( r. saturating_pow( 0 ) , 1 as $T) ;
291
- }
241
+ fn test_pow( ) {
242
+ {
243
+ const R : $T = 2 ;
244
+ assert_eq_const_safe!( R . pow( 2 ) , 4 as $T) ;
245
+ assert_eq_const_safe!( R . pow( 0 ) , 1 as $T) ;
246
+ assert_eq_const_safe!( R . wrapping_pow( 2 ) , 4 as $T) ;
247
+ assert_eq_const_safe!( R . wrapping_pow( 0 ) , 1 as $T) ;
248
+ assert_eq_const_safe!( R . checked_pow( 2 ) , Some ( 4 as $T) ) ;
249
+ assert_eq_const_safe!( R . checked_pow( 0 ) , Some ( 1 as $T) ) ;
250
+ assert_eq_const_safe!( R . overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
251
+ assert_eq_const_safe!( R . overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
252
+ assert_eq_const_safe!( R . saturating_pow( 2 ) , 4 as $T) ;
253
+ assert_eq_const_safe!( R . saturating_pow( 0 ) , 1 as $T) ;
254
+ }
255
+
256
+ {
257
+ const R : $T = MAX ;
258
+ // use `^` to represent .pow() with no overflow.
259
+ // if itest::MAX == 2^j-1, then itest is a `j` bit int,
260
+ // so that `itest::MAX*itest::MAX == 2^(2*j)-2^(j+1)+1`,
261
+ // thussaturating_pow the overflowing result is exactly 1.
262
+ assert_eq_const_safe!( R . wrapping_pow( 2 ) , 1 as $T) ;
263
+ assert_eq_const_safe!( R . checked_pow( 2 ) , None ) ;
264
+ assert_eq_const_safe!( R . overflowing_pow( 2 ) , ( 1 as $T, true ) ) ;
265
+ assert_eq_const_safe!( R . saturating_pow( 2 ) , MAX ) ;
266
+ }
267
+
268
+ {
269
+ // test for negative exponent.
270
+ const R : $T = -2 ;
271
+ assert_eq_const_safe!( R . pow( 2 ) , 4 as $T) ;
272
+ assert_eq_const_safe!( R . pow( 3 ) , -8 as $T) ;
273
+ assert_eq_const_safe!( R . pow( 0 ) , 1 as $T) ;
274
+ assert_eq_const_safe!( R . wrapping_pow( 2 ) , 4 as $T) ;
275
+ assert_eq_const_safe!( R . wrapping_pow( 3 ) , -8 as $T) ;
276
+ assert_eq_const_safe!( R . wrapping_pow( 0 ) , 1 as $T) ;
277
+ assert_eq_const_safe!( R . checked_pow( 2 ) , Some ( 4 as $T) ) ;
278
+ assert_eq_const_safe!( R . checked_pow( 3 ) , Some ( -8 as $T) ) ;
279
+ assert_eq_const_safe!( R . checked_pow( 0 ) , Some ( 1 as $T) ) ;
280
+ assert_eq_const_safe!( R . overflowing_pow( 2 ) , ( 4 as $T, false ) ) ;
281
+ assert_eq_const_safe!( R . overflowing_pow( 3 ) , ( -8 as $T, false ) ) ;
282
+ assert_eq_const_safe!( R . overflowing_pow( 0 ) , ( 1 as $T, false ) ) ;
283
+ assert_eq_const_safe!( R . saturating_pow( 2 ) , 4 as $T) ;
284
+ assert_eq_const_safe!( R . saturating_pow( 3 ) , -8 as $T) ;
285
+ assert_eq_const_safe!( R . saturating_pow( 0 ) , 1 as $T) ;
286
+ }
287
+ }
292
288
293
- #[ test]
294
- fn test_div_floor( ) {
295
- let a: $T = 8 ;
296
- let b = 3 ;
297
- assert_eq!( a. div_floor( b) , 2 ) ;
298
- assert_eq!( a. div_floor( -b) , -3 ) ;
299
- assert_eq!( ( -a) . div_floor( b) , -3 ) ;
300
- assert_eq!( ( -a) . div_floor( -b) , 2 ) ;
301
- }
289
+ fn test_div_floor( ) {
290
+ const A : $T = 8 ;
291
+ const B : $T = 3 ;
292
+ assert_eq_const_safe!( A . div_floor( B ) , 2 ) ;
293
+ assert_eq_const_safe!( A . div_floor( -B ) , -3 ) ;
294
+ assert_eq_const_safe!( ( -A ) . div_floor( B ) , -3 ) ;
295
+ assert_eq_const_safe!( ( -A ) . div_floor( -B ) , 2 ) ;
296
+ }
302
297
303
- #[ test]
304
- fn test_div_ceil( ) {
305
- let a: $T = 8 ;
306
- let b = 3 ;
307
- assert_eq!( a. div_ceil( b) , 3 ) ;
308
- assert_eq!( a. div_ceil( -b) , -2 ) ;
309
- assert_eq!( ( -a) . div_ceil( b) , -2 ) ;
310
- assert_eq!( ( -a) . div_ceil( -b) , 3 ) ;
311
- }
298
+ fn test_div_ceil( ) {
299
+ const A : $T = 8 ;
300
+ const B : $T = 3 ;
301
+ assert_eq_const_safe!( A . div_ceil( B ) , 3 ) ;
302
+ assert_eq_const_safe!( A . div_ceil( -B ) , -2 ) ;
303
+ assert_eq_const_safe!( ( -A ) . div_ceil( B ) , -2 ) ;
304
+ assert_eq_const_safe!( ( -A ) . div_ceil( -B ) , 3 ) ;
305
+ }
312
306
313
- #[ test]
314
- fn test_next_multiple_of( ) {
315
- assert_eq!( ( 16 as $T) . next_multiple_of( 8 ) , 16 ) ;
316
- assert_eq!( ( 23 as $T) . next_multiple_of( 8 ) , 24 ) ;
317
- assert_eq!( ( 16 as $T) . next_multiple_of( -8 ) , 16 ) ;
318
- assert_eq!( ( 23 as $T) . next_multiple_of( -8 ) , 16 ) ;
319
- assert_eq!( ( -16 as $T) . next_multiple_of( 8 ) , -16 ) ;
320
- assert_eq!( ( -23 as $T) . next_multiple_of( 8 ) , -16 ) ;
321
- assert_eq!( ( -16 as $T) . next_multiple_of( -8 ) , -16 ) ;
322
- assert_eq!( ( -23 as $T) . next_multiple_of( -8 ) , -24 ) ;
323
- assert_eq!( MIN . next_multiple_of( -1 ) , MIN ) ;
324
- }
307
+ fn test_next_multiple_of( ) {
308
+ assert_eq_const_safe!( ( 16 as $T) . next_multiple_of( 8 ) , 16 ) ;
309
+ assert_eq_const_safe!( ( 23 as $T) . next_multiple_of( 8 ) , 24 ) ;
310
+ assert_eq_const_safe!( ( 16 as $T) . next_multiple_of( -8 ) , 16 ) ;
311
+ assert_eq_const_safe!( ( 23 as $T) . next_multiple_of( -8 ) , 16 ) ;
312
+ assert_eq_const_safe!( ( -16 as $T) . next_multiple_of( 8 ) , -16 ) ;
313
+ assert_eq_const_safe!( ( -23 as $T) . next_multiple_of( 8 ) , -16 ) ;
314
+ assert_eq_const_safe!( ( -16 as $T) . next_multiple_of( -8 ) , -16 ) ;
315
+ assert_eq_const_safe!( ( -23 as $T) . next_multiple_of( -8 ) , -24 ) ;
316
+ assert_eq_const_safe!( MIN . next_multiple_of( -1 ) , MIN ) ;
317
+ }
325
318
326
- #[ test]
327
- fn test_checked_next_multiple_of( ) {
328
- assert_eq!( ( 16 as $T) . checked_next_multiple_of( 8 ) , Some ( 16 ) ) ;
329
- assert_eq!( ( 23 as $T) . checked_next_multiple_of( 8 ) , Some ( 24 ) ) ;
330
- assert_eq!( ( 16 as $T) . checked_next_multiple_of( -8 ) , Some ( 16 ) ) ;
331
- assert_eq!( ( 23 as $T) . checked_next_multiple_of( -8 ) , Some ( 16 ) ) ;
332
- assert_eq!( ( -16 as $T) . checked_next_multiple_of( 8 ) , Some ( -16 ) ) ;
333
- assert_eq!( ( -23 as $T) . checked_next_multiple_of( 8 ) , Some ( -16 ) ) ;
334
- assert_eq!( ( -16 as $T) . checked_next_multiple_of( -8 ) , Some ( -16 ) ) ;
335
- assert_eq!( ( -23 as $T) . checked_next_multiple_of( -8 ) , Some ( -24 ) ) ;
336
- assert_eq!( ( 1 as $T) . checked_next_multiple_of( 0 ) , None ) ;
337
- assert_eq!( MAX . checked_next_multiple_of( 2 ) , None ) ;
338
- assert_eq!( MIN . checked_next_multiple_of( -3 ) , None ) ;
339
- assert_eq!( MIN . checked_next_multiple_of( -1 ) , Some ( MIN ) ) ;
340
- }
319
+ fn test_checked_next_multiple_of( ) {
320
+ assert_eq_const_safe!( ( 16 as $T) . checked_next_multiple_of( 8 ) , Some ( 16 ) ) ;
321
+ assert_eq_const_safe!( ( 23 as $T) . checked_next_multiple_of( 8 ) , Some ( 24 ) ) ;
322
+ assert_eq_const_safe!( ( 16 as $T) . checked_next_multiple_of( -8 ) , Some ( 16 ) ) ;
323
+ assert_eq_const_safe!( ( 23 as $T) . checked_next_multiple_of( -8 ) , Some ( 16 ) ) ;
324
+ assert_eq_const_safe!( ( -16 as $T) . checked_next_multiple_of( 8 ) , Some ( -16 ) ) ;
325
+ assert_eq_const_safe!( ( -23 as $T) . checked_next_multiple_of( 8 ) , Some ( -16 ) ) ;
326
+ assert_eq_const_safe!( ( -16 as $T) . checked_next_multiple_of( -8 ) , Some ( -16 ) ) ;
327
+ assert_eq_const_safe!( ( -23 as $T) . checked_next_multiple_of( -8 ) , Some ( -24 ) ) ;
328
+ assert_eq_const_safe!( ( 1 as $T) . checked_next_multiple_of( 0 ) , None ) ;
329
+ assert_eq_const_safe!( MAX . checked_next_multiple_of( 2 ) , None ) ;
330
+ assert_eq_const_safe!( MIN . checked_next_multiple_of( -3 ) , None ) ;
331
+ assert_eq_const_safe!( MIN . checked_next_multiple_of( -1 ) , Some ( MIN ) ) ;
332
+ }
341
333
342
- #[ test]
343
- fn test_carrying_add( ) {
344
- assert_eq!( $T:: MAX . carrying_add( 1 , false ) , ( $T:: MIN , true ) ) ;
345
- assert_eq!( $T:: MAX . carrying_add( 0 , true ) , ( $T:: MIN , true ) ) ;
346
- assert_eq!( $T:: MAX . carrying_add( 1 , true ) , ( $T:: MIN + 1 , true ) ) ;
347
- assert_eq!( $T:: MAX . carrying_add( -1 , false ) , ( $T:: MAX - 1 , false ) ) ;
348
- assert_eq!( $T:: MAX . carrying_add( -1 , true ) , ( $T:: MAX , false ) ) ; // no intermediate overflow
349
- assert_eq!( $T:: MIN . carrying_add( -1 , false ) , ( $T:: MAX , true ) ) ;
350
- assert_eq!( $T:: MIN . carrying_add( -1 , true ) , ( $T:: MIN , false ) ) ; // no intermediate overflow
351
- assert_eq!( ( 0 as $T) . carrying_add( $T:: MAX , true ) , ( $T:: MIN , true ) ) ;
352
- assert_eq!( ( 0 as $T) . carrying_add( $T:: MIN , true ) , ( $T:: MIN + 1 , false ) ) ;
353
- }
334
+ fn test_carrying_add( ) {
335
+ assert_eq_const_safe!( MAX . carrying_add( 1 , false ) , ( MIN , true ) ) ;
336
+ assert_eq_const_safe!( MAX . carrying_add( 0 , true ) , ( MIN , true ) ) ;
337
+ assert_eq_const_safe!( MAX . carrying_add( 1 , true ) , ( MIN + 1 , true ) ) ;
338
+ assert_eq_const_safe!( MAX . carrying_add( -1 , false ) , ( MAX - 1 , false ) ) ;
339
+ assert_eq_const_safe!( MAX . carrying_add( -1 , true ) , ( MAX , false ) ) ; // no intermediate overflow
340
+ assert_eq_const_safe!( MIN . carrying_add( -1 , false ) , ( MAX , true ) ) ;
341
+ assert_eq_const_safe!( MIN . carrying_add( -1 , true ) , ( MIN , false ) ) ; // no intermediate overflow
342
+ assert_eq_const_safe!( ( 0 as $T) . carrying_add( MAX , true ) , ( MIN , true ) ) ;
343
+ assert_eq_const_safe!( ( 0 as $T) . carrying_add( MIN , true ) , ( MIN + 1 , false ) ) ;
344
+ }
354
345
355
- #[ test]
356
- fn test_borrowing_sub( ) {
357
- assert_eq!( $T:: MIN . borrowing_sub( 1 , false ) , ( $T:: MAX , true ) ) ;
358
- assert_eq!( $T:: MIN . borrowing_sub( 0 , true ) , ( $T:: MAX , true ) ) ;
359
- assert_eq!( $T:: MIN . borrowing_sub( 1 , true ) , ( $T:: MAX - 1 , true ) ) ;
360
- assert_eq!( $T:: MIN . borrowing_sub( -1 , false ) , ( $T:: MIN + 1 , false ) ) ;
361
- assert_eq!( $T:: MIN . borrowing_sub( -1 , true ) , ( $T:: MIN , false ) ) ; // no intermediate overflow
362
- assert_eq!( $T:: MAX . borrowing_sub( -1 , false ) , ( $T:: MIN , true ) ) ;
363
- assert_eq!( $T:: MAX . borrowing_sub( -1 , true ) , ( $T:: MAX , false ) ) ; // no intermediate overflow
364
- assert_eq!( ( 0 as $T) . borrowing_sub( $T:: MIN , false ) , ( $T:: MIN , true ) ) ;
365
- assert_eq!( ( 0 as $T) . borrowing_sub( $T:: MIN , true ) , ( $T:: MAX , false ) ) ;
366
- }
346
+ fn test_borrowing_sub( ) {
347
+ assert_eq_const_safe!( MIN . borrowing_sub( 1 , false ) , ( MAX , true ) ) ;
348
+ assert_eq_const_safe!( MIN . borrowing_sub( 0 , true ) , ( MAX , true ) ) ;
349
+ assert_eq_const_safe!( MIN . borrowing_sub( 1 , true ) , ( MAX - 1 , true ) ) ;
350
+ assert_eq_const_safe!( MIN . borrowing_sub( -1 , false ) , ( MIN + 1 , false ) ) ;
351
+ assert_eq_const_safe!( MIN . borrowing_sub( -1 , true ) , ( MIN , false ) ) ; // no intermediate overflow
352
+ assert_eq_const_safe!( MAX . borrowing_sub( -1 , false ) , ( MIN , true ) ) ;
353
+ assert_eq_const_safe!( MAX . borrowing_sub( -1 , true ) , ( MAX , false ) ) ; // no intermediate overflow
354
+ assert_eq_const_safe!( ( 0 as $T) . borrowing_sub( MIN , false ) , ( MIN , true ) ) ;
355
+ assert_eq_const_safe!( ( 0 as $T) . borrowing_sub( MIN , true ) , ( MAX , false ) ) ;
356
+ }
367
357
368
- # [ test ]
369
- fn test_midpoint ( ) {
370
- assert_eq !( <$T>:: midpoint( 1 , 3 ) , 2 ) ;
371
- assert_eq! ( <$T> :: midpoint ( 3 , 1 ) , 2 ) ;
372
-
373
- assert_eq !( <$T>:: midpoint( 0 , 0 ) , 0 ) ;
374
- assert_eq !( <$T>:: midpoint( 0 , 2 ) , 1 ) ;
375
- assert_eq !( <$T>:: midpoint( 2 , 0 ) , 1 ) ;
376
- assert_eq! ( <$T> :: midpoint ( 2 , 2 ) , 2 ) ;
377
-
378
- assert_eq !( <$T>:: midpoint( 1 , 4 ) , 2 ) ;
379
- assert_eq !( <$T>:: midpoint( 4 , 1 ) , 2 ) ;
380
- assert_eq !( <$T>:: midpoint( 3 , 4 ) , 3 ) ;
381
- assert_eq! ( <$T> :: midpoint ( 4 , 3 ) , 3 ) ;
382
-
383
- assert_eq !( <$T>:: midpoint( <$T>:: MIN , <$T>:: MAX ) , -1 ) ;
384
- assert_eq !( <$T>:: midpoint( <$T>:: MAX , <$T>:: MIN ) , - 1 ) ;
385
- assert_eq !( <$T>:: midpoint( <$T>:: MIN , <$T>:: MIN ) , <$T>:: MIN ) ;
386
- assert_eq! ( <$T> :: midpoint ( <$T> :: MAX , <$T> :: MAX ) , <$T> :: MAX ) ;
387
-
388
- assert_eq !( <$T>:: midpoint( <$T>:: MIN , 6 ) , <$T>:: MIN / 2 + 3 ) ;
389
- assert_eq !( <$T>:: midpoint( 6 , <$T>:: MIN ) , <$T>:: MIN / 2 + 3 ) ;
390
- assert_eq !( <$T>:: midpoint( <$T>:: MAX , 6 ) , <$T>:: MAX / 2 + 3 ) ;
391
- assert_eq! ( <$T> :: midpoint ( 6 , <$T> :: MAX ) , <$T> :: MAX / 2 + 3 ) ;
358
+ fn test_midpoint ( ) {
359
+ assert_eq_const_safe! ( <$T> :: midpoint ( 1 , 3 ) , 2 ) ;
360
+ assert_eq_const_safe !( <$T>:: midpoint( 3 , 1 ) , 2 ) ;
361
+
362
+ assert_eq_const_safe! ( <$T> :: midpoint ( 0 , 0 ) , 0 ) ;
363
+ assert_eq_const_safe !( <$T>:: midpoint( 0 , 2 ) , 1 ) ;
364
+ assert_eq_const_safe !( <$T>:: midpoint( 2 , 0 ) , 1 ) ;
365
+ assert_eq_const_safe !( <$T>:: midpoint( 2 , 2 ) , 2 ) ;
366
+
367
+ assert_eq_const_safe! ( <$T> :: midpoint ( 1 , 4 ) , 2 ) ;
368
+ assert_eq_const_safe !( <$T>:: midpoint( 4 , 1 ) , 2 ) ;
369
+ assert_eq_const_safe !( <$T>:: midpoint( 3 , 4 ) , 3 ) ;
370
+ assert_eq_const_safe !( <$T>:: midpoint( 4 , 3 ) , 3 ) ;
371
+
372
+ assert_eq_const_safe! ( <$T> :: midpoint ( <$T> :: MIN , <$T> :: MAX ) , - 1 ) ;
373
+ assert_eq_const_safe !( <$T>:: midpoint( <$T>:: MAX , <$T>:: MIN ) , -1 ) ;
374
+ assert_eq_const_safe !( <$T>:: midpoint( <$T>:: MIN , <$T>:: MIN ) , <$T> :: MIN ) ;
375
+ assert_eq_const_safe !( <$T>:: midpoint( <$T>:: MAX , <$T>:: MAX ) , <$T>:: MAX ) ;
376
+
377
+ assert_eq_const_safe! ( <$T> :: midpoint ( <$T> :: MIN , 6 ) , <$T> :: MIN / 2 + 3 ) ;
378
+ assert_eq_const_safe !( <$T>:: midpoint( 6 , <$T>:: MIN ) , <$T>:: MIN / 2 + 3 ) ;
379
+ assert_eq_const_safe !( <$T>:: midpoint( <$T>:: MAX , 6 ) , <$T>:: MAX / 2 + 3 ) ;
380
+ assert_eq_const_safe !( <$T>:: midpoint( 6 , <$T>:: MAX ) , <$T>:: MAX / 2 + 3 ) ;
381
+ }
392
382
}
393
383
} ;
394
384
}
0 commit comments