|
| 1 | +macro_rules! check_int_clamp { |
| 2 | + ($t:ty, $ut:ty) => { |
| 3 | + let min = <$t>::MIN; |
| 4 | + let max = <$t>::MAX; |
| 5 | + let max_u = <$ut>::MAX; |
| 6 | + |
| 7 | + // Basic clamping |
| 8 | + assert_eq!((100 as $t).clamp_magnitude(50), 50); |
| 9 | + assert_eq!((-100 as $t).clamp_magnitude(50), -50); |
| 10 | + assert_eq!((30 as $t).clamp_magnitude(50), 30); |
| 11 | + assert_eq!((-30 as $t).clamp_magnitude(50), -30); |
| 12 | + |
| 13 | + // Exact boundary |
| 14 | + assert_eq!((50 as $t).clamp_magnitude(50), 50); |
| 15 | + assert_eq!((-50 as $t).clamp_magnitude(50), -50); |
| 16 | + |
| 17 | + // Zero cases |
| 18 | + assert_eq!((0 as $t).clamp_magnitude(100), 0); |
| 19 | + assert_eq!((0 as $t).clamp_magnitude(0), 0); |
| 20 | + assert_eq!((100 as $t).clamp_magnitude(0), 0); |
| 21 | + assert_eq!((-100 as $t).clamp_magnitude(0), 0); |
| 22 | + |
| 23 | + // MIN/MAX values |
| 24 | + // Symmetric range [-MAX, MAX] |
| 25 | + assert_eq!(max.clamp_magnitude(max as $ut), max); |
| 26 | + assert_eq!(min.clamp_magnitude(max as $ut), -max); |
| 27 | + |
| 28 | + // Full range (limit covers MIN) |
| 29 | + let min_abs = min.unsigned_abs(); |
| 30 | + assert_eq!(min.clamp_magnitude(min_abs), min); |
| 31 | + |
| 32 | + // Limit larger than type max (uN > iN::MAX) |
| 33 | + assert_eq!(max.clamp_magnitude(max_u), max); |
| 34 | + assert_eq!(min.clamp_magnitude(max_u), min); |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn test_clamp_magnitude_i8() { |
| 40 | + check_int_clamp!(i8, u8); |
| 41 | +} |
| 42 | + |
| 43 | +#[test] |
| 44 | +fn test_clamp_magnitude_i16() { |
| 45 | + check_int_clamp!(i16, u16); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn test_clamp_magnitude_i32() { |
| 50 | + check_int_clamp!(i32, u32); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_clamp_magnitude_i64() { |
| 55 | + check_int_clamp!(i64, u64); |
| 56 | +} |
| 57 | + |
| 58 | +#[test] |
| 59 | +fn test_clamp_magnitude_i128() { |
| 60 | + check_int_clamp!(i128, u128); |
| 61 | +} |
| 62 | + |
| 63 | +#[test] |
| 64 | +fn test_clamp_magnitude_isize() { |
| 65 | + check_int_clamp!(isize, usize); |
| 66 | +} |
| 67 | + |
| 68 | +macro_rules! check_float_clamp { |
| 69 | + ($t:ty) => { |
| 70 | + // Basic clamping |
| 71 | + assert_eq!((5.0 as $t).clamp_magnitude(3.0), 3.0); |
| 72 | + assert_eq!((-5.0 as $t).clamp_magnitude(3.0), -3.0); |
| 73 | + assert_eq!((2.0 as $t).clamp_magnitude(3.0), 2.0); |
| 74 | + assert_eq!((-2.0 as $t).clamp_magnitude(3.0), -2.0); |
| 75 | + |
| 76 | + // Exact boundary |
| 77 | + assert_eq!((3.0 as $t).clamp_magnitude(3.0), 3.0); |
| 78 | + assert_eq!((-3.0 as $t).clamp_magnitude(3.0), -3.0); |
| 79 | + |
| 80 | + // Zero cases |
| 81 | + assert_eq!((0.0 as $t).clamp_magnitude(1.0), 0.0); |
| 82 | + assert_eq!((-0.0 as $t).clamp_magnitude(1.0), 0.0); |
| 83 | + assert_eq!((5.0 as $t).clamp_magnitude(0.0), 0.0); |
| 84 | + assert_eq!((-5.0 as $t).clamp_magnitude(0.0), 0.0); |
| 85 | + |
| 86 | + // Special values - Infinity |
| 87 | + let inf = <$t>::INFINITY; |
| 88 | + let neg_inf = <$t>::NEG_INFINITY; |
| 89 | + assert_eq!(inf.clamp_magnitude(100.0), 100.0); |
| 90 | + assert_eq!(neg_inf.clamp_magnitude(100.0), -100.0); |
| 91 | + assert_eq!(inf.clamp_magnitude(inf), inf); |
| 92 | + |
| 93 | + // Value with infinite limit |
| 94 | + assert_eq!((1.0 as $t).clamp_magnitude(inf), 1.0); |
| 95 | + assert_eq!((-1.0 as $t).clamp_magnitude(inf), -1.0); |
| 96 | + |
| 97 | + // MIN and MAX |
| 98 | + let max = <$t>::MAX; |
| 99 | + let min = <$t>::MIN; |
| 100 | + // Large limit |
| 101 | + let huge = 1e30; |
| 102 | + assert_eq!(max.clamp_magnitude(huge), huge); |
| 103 | + assert_eq!(min.clamp_magnitude(huge), -huge); |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +#[test] |
| 108 | +fn test_clamp_magnitude_f32() { |
| 109 | + check_float_clamp!(f32); |
| 110 | +} |
| 111 | + |
| 112 | +#[test] |
| 113 | +fn test_clamp_magnitude_f64() { |
| 114 | + check_float_clamp!(f64); |
| 115 | +} |
| 116 | + |
| 117 | +#[test] |
| 118 | +#[should_panic(expected = "limit must be non-negative")] |
| 119 | +fn test_clamp_magnitude_f32_panic_negative_limit() { |
| 120 | + let _ = 1.0f32.clamp_magnitude(-1.0); |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +#[should_panic(expected = "limit must be non-negative")] |
| 125 | +fn test_clamp_magnitude_f64_panic_negative_limit() { |
| 126 | + let _ = 1.0f64.clamp_magnitude(-1.0); |
| 127 | +} |
| 128 | + |
| 129 | +#[test] |
| 130 | +#[should_panic] |
| 131 | +fn test_clamp_magnitude_f32_panic_nan_limit() { |
| 132 | + let _ = 1.0f32.clamp_magnitude(f32::NAN); |
| 133 | +} |
| 134 | + |
| 135 | +#[test] |
| 136 | +#[should_panic] |
| 137 | +fn test_clamp_magnitude_f64_panic_nan_limit() { |
| 138 | + let _ = 1.0f64.clamp_magnitude(f64::NAN); |
| 139 | +} |
0 commit comments