Skip to content

Commit 25c71e5

Browse files
committed
test some more corner cases in happy float casts
1 parent 30d07c8 commit 25c71e5

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tests/run-pass/float.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ impl FloatToInt<u64> for f64 {
5656
fn cast(self) -> u64 { self as _ }
5757
unsafe fn cast_unchecked(self) -> u64 { self.to_int_unchecked() }
5858
}
59+
impl FloatToInt<i128> for f64 {
60+
fn cast(self) -> i128 { self as _ }
61+
unsafe fn cast_unchecked(self) -> i128 { self.to_int_unchecked() }
62+
}
63+
impl FloatToInt<u128> for f64 {
64+
fn cast(self) -> u128 { self as _ }
65+
unsafe fn cast_unchecked(self) -> u128 { self.to_int_unchecked() }
66+
}
5967

6068
/// Test this cast both via `as` and via `approx_unchecked` (i.e., it must not saturate).
6169
#[track_caller]
@@ -137,6 +145,7 @@ fn casts() {
137145
// f32 -> u32
138146
test_cast::<f32, u32>(0.0, 0);
139147
test_cast::<f32, u32>(-0.0, 0);
148+
test_cast::<f32, u32>(-0.9999999, 0);
140149
test_cast::<f32, u32>(/*0x1p-149*/ f32::from_bits(0x1), 0);
141150
test_cast::<f32, u32>(/*-0x1p-149*/ f32::from_bits(0x80000001), 0);
142151
test_cast::<f32, u32>(/*0x1.19999ap+0*/ f32::from_bits(0x3f8ccccd), 1);
@@ -210,6 +219,7 @@ fn casts() {
210219
// f64 -> u64
211220
test_cast::<f64, u64>(0.0, 0);
212221
test_cast::<f64, u64>(-0.0, 0);
222+
test_cast::<f64, u64>(-0.99999999999, 0);
213223
test_cast::<f64, u64>(5.0, 5);
214224
test_cast::<f64, u64>(1e16, 10000000000000000);
215225
test_cast::<f64, u64>((u64::MAX-1024) as f64, u64::MAX-2047); // rounding loss
@@ -225,6 +235,14 @@ fn casts() {
225235
assert_eq::<u64>(f64::NAN as u64, 0);
226236
assert_eq::<u64>((-f64::NAN) as u64, 0);
227237

238+
// f64 -> i128
239+
assert_eq::<i128>(f64::MAX as i128, i128::MAX);
240+
assert_eq::<i128>(f64::MIN as i128, i128::MIN);
241+
242+
// f64 -> u128
243+
assert_eq::<u128>(f64::MAX as u128, u128::MAX);
244+
assert_eq::<u128>(f64::MIN as u128, 0);
245+
228246
// int -> f32
229247
assert_eq::<f32>(127i8 as f32, 127.0);
230248
assert_eq::<f32>(2147483647i32 as f32, 2147483648.0);
@@ -275,10 +293,8 @@ fn casts() {
275293
assert_eq::<f32>(5.0f64 as f32, 5.0f32);
276294
assert_eq::<f32>(/*0x0.0000000000001p-1022*/ f64::from_bits(0x1) as f32, 0.0);
277295
assert_eq::<f32>(/*-0x0.0000000000001p-1022*/ (-f64::from_bits(0x1)) as f32, -0.0);
278-
279296
assert_eq::<f32>(/*0x1.fffffe0000000p-127*/ f64::from_bits(0x380fffffe0000000) as f32, /*0x1p-149*/ f32::from_bits(0x800000));
280297
assert_eq::<f32>(/*0x1.4eae4f7024c7p+108*/ f64::from_bits(0x46b4eae4f7024c70) as f32, /*0x1.4eae5p+108*/ f32::from_bits(0x75a75728));
281-
282298
assert_eq::<f32>(f64::MAX as f32, f32::INFINITY);
283299
assert_eq::<f32>(f64::MIN as f32, f32::NEG_INFINITY);
284300
assert_eq::<f32>(f64::INFINITY as f32, f32::INFINITY);

0 commit comments

Comments
 (0)