|
| 1 | +// Test the `float_exact_integer_constants` feature |
| 2 | +macro_rules! test_float_exact_integer_constants { |
| 3 | + ($fn_name:ident, $float:ty, $integer:ty) => { |
| 4 | + #[test] |
| 5 | + fn $fn_name() { |
| 6 | + { |
| 7 | + // The maximum integer that converts to a unique floating point |
| 8 | + // value. |
| 9 | + const MAX_EXACT_INTEGER: $integer = <$float>::MAX_EXACT_INTEGER; |
| 10 | + |
| 11 | + let max_minus_one = (MAX_EXACT_INTEGER - 1) as $float as $integer; |
| 12 | + let max_plus_one = (MAX_EXACT_INTEGER + 1) as $float as $integer; |
| 13 | + let max_plus_two = (MAX_EXACT_INTEGER + 2) as $float as $integer; |
| 14 | + |
| 15 | + // Lossless roundtrips. |
| 16 | + assert_eq!(MAX_EXACT_INTEGER - 1, max_minus_one); |
| 17 | + assert_eq!(MAX_EXACT_INTEGER, MAX_EXACT_INTEGER as $float as $integer); |
| 18 | + assert_eq!(MAX_EXACT_INTEGER + 1, max_plus_one); |
| 19 | + // The first non-unique conversion, which roundtrips to one less |
| 20 | + // than the starting value. |
| 21 | + assert_ne!(MAX_EXACT_INTEGER + 2, max_plus_two); |
| 22 | + |
| 23 | + // max-1 | max+0 | max+1 | max+2 |
| 24 | + // After roundtripping, +1 and +2 will equal each other. |
| 25 | + assert_ne!(max_minus_one, MAX_EXACT_INTEGER); |
| 26 | + assert_ne!(MAX_EXACT_INTEGER, max_plus_one); |
| 27 | + assert_eq!(max_plus_one, max_plus_two); |
| 28 | + } |
| 29 | + |
| 30 | + { |
| 31 | + // The minimum integer that converts to a unique floating point |
| 32 | + // value. |
| 33 | + const MIN_EXACT_INTEGER: $integer = <$float>::MIN_EXACT_INTEGER; |
| 34 | + |
| 35 | + // Same logic as the previous block, but we work our way leftward |
| 36 | + // across the number line from (min_exact + 1) to (min_exact - 2). |
| 37 | + let min_plus_one = (MIN_EXACT_INTEGER + 1) as $float as $integer; |
| 38 | + let min_minus_one = (MIN_EXACT_INTEGER - 1) as $float as $integer; |
| 39 | + let min_minus_two = (MIN_EXACT_INTEGER - 2) as $float as $integer; |
| 40 | + |
| 41 | + // Lossless roundtrips. |
| 42 | + assert_eq!(MIN_EXACT_INTEGER + 1, min_plus_one); |
| 43 | + assert_eq!(MIN_EXACT_INTEGER, MIN_EXACT_INTEGER as $float as $integer); |
| 44 | + assert_eq!(MIN_EXACT_INTEGER - 1, min_minus_one); |
| 45 | + // The first non-unique conversion, which roundtrips to one |
| 46 | + // greater than the starting value. |
| 47 | + assert_ne!(MIN_EXACT_INTEGER - 2, min_minus_two); |
| 48 | + |
| 49 | + // min-2 | min-1 | min | min+1 |
| 50 | + // After roundtripping, -2 and -1 will equal each other. |
| 51 | + assert_ne!(min_plus_one, MIN_EXACT_INTEGER); |
| 52 | + assert_ne!(MIN_EXACT_INTEGER, min_minus_one); |
| 53 | + assert_eq!(min_minus_one, min_minus_two); |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +test_float_exact_integer_constants! { test_f16_min_max_exact_integer, f16, i16 } |
| 60 | +test_float_exact_integer_constants! { test_f32_min_max_exact_integer, f32, i32 } |
| 61 | +test_float_exact_integer_constants! { test_f64_min_max_exact_integer, f64, i64 } |
| 62 | +test_float_exact_integer_constants! { test_f128_min_max_exact_integer, f128, i128 } |
0 commit comments