| 1 | use easy_cast::*; |
| 2 | |
| 3 | #[test] |
| 4 | fn int_casts() { |
| 5 | let a: i32 = 213; |
| 6 | let b: u128 = a.cast(); |
| 7 | let c = u8::conv(b); |
| 8 | let d = c.cast(); |
| 9 | assert_eq!(a, d); |
| 10 | } |
| 11 | |
| 12 | #[test] |
| 13 | fn signed_to_unsigned() { |
| 14 | u32::conv(0i32); |
| 15 | u32::conv(1i32); |
| 16 | u32::conv(core::i32::MAX); |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | #[should_panic] |
| 21 | fn signed_to_unsigned_n1() { |
| 22 | u32::conv(-1i32); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn unsigned_to_signed() { |
| 27 | i32::conv(0u32); |
| 28 | i32::conv(1u32); |
| 29 | i16::conv(1usize); |
| 30 | } |
| 31 | |
| 32 | #[test] |
| 33 | #[should_panic] |
| 34 | fn unsigned_to_signed_large() { |
| 35 | i32::conv(0x8000_0000u32); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn int_to_float() { |
| 40 | f32::conv(0); |
| 41 | f32::conv(0x00FF_FFFF); |
| 42 | f32::conv(-64i32); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | #[should_panic] |
| 47 | fn int_to_float_inexact() { |
| 48 | f32::conv(0x01FF_FFFF); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | #[cfg(any(feature = "std", feature = "libm"))] |
| 53 | fn float_casts() { |
| 54 | assert_eq!(u64::conv_nearest(13.2f32), 13); |
| 55 | let x: i128 = 13.5f32.cast_nearest(); |
| 56 | assert_eq!(x, 14); |
| 57 | assert_eq!(u8::conv_floor(13.8f64), 13); |
| 58 | assert_eq!(u32::conv_ceil(13.1f32), 14); |
| 59 | assert_eq!(i64::conv_floor(-3168565.13), -3168566); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | #[cfg(any(feature = "std", feature = "libm"))] |
| 64 | fn float_trunc() { |
| 65 | let xx = [-32768.0f32, -32768.99, -0.99, 0.99, 32767.99]; |
| 66 | let yy = [-32768i16, -32768, 0, 0, 32767]; |
| 67 | for (x, y) in xx[..].iter().zip(yy[..].iter()) { |
| 68 | assert_eq!(i16::conv_trunc(*x), *y); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | #[should_panic] |
| 74 | #[cfg(any(feature = "std", feature = "libm"))] |
| 75 | fn float_trunc_fail1() { |
| 76 | i16::conv_trunc(32768.0f32); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | #[should_panic] |
| 81 | fn u32_max_f32() { |
| 82 | f32::conv(core::u32::MAX); |
| 83 | } |