cast.rs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //! Tests of `num_traits::cast`.
  2. #![no_std]
  3. #[cfg(feature = "std")]
  4. #[macro_use]
  5. extern crate std;
  6. extern crate num_traits;
  7. use num_traits::cast::*;
  8. use core::{i8, i16, i32, i64, isize};
  9. use core::{u8, u16, u32, u64, usize};
  10. use core::{f32, f64};
  11. #[cfg(feature = "i128")]
  12. use core::{i128, u128};
  13. use core::mem;
  14. use core::num::Wrapping;
  15. #[test]
  16. fn to_primitive_float() {
  17. let f32_toolarge = 1e39f64;
  18. assert_eq!(f32_toolarge.to_f32(), None);
  19. assert_eq!((f32::MAX as f64).to_f32(), Some(f32::MAX));
  20. assert_eq!((-f32::MAX as f64).to_f32(), Some(-f32::MAX));
  21. assert_eq!(f64::INFINITY.to_f32(), Some(f32::INFINITY));
  22. assert_eq!((f64::NEG_INFINITY).to_f32(), Some(f32::NEG_INFINITY));
  23. assert!((f64::NAN).to_f32().map_or(false, |f| f.is_nan()));
  24. }
  25. #[test]
  26. fn wrapping_to_primitive() {
  27. macro_rules! test_wrapping_to_primitive {
  28. ($($t:ty)+) => {
  29. $({
  30. let i: $t = 0;
  31. let w = Wrapping(i);
  32. assert_eq!(i.to_u8(), w.to_u8());
  33. assert_eq!(i.to_u16(), w.to_u16());
  34. assert_eq!(i.to_u32(), w.to_u32());
  35. assert_eq!(i.to_u64(), w.to_u64());
  36. assert_eq!(i.to_usize(), w.to_usize());
  37. assert_eq!(i.to_i8(), w.to_i8());
  38. assert_eq!(i.to_i16(), w.to_i16());
  39. assert_eq!(i.to_i32(), w.to_i32());
  40. assert_eq!(i.to_i64(), w.to_i64());
  41. assert_eq!(i.to_isize(), w.to_isize());
  42. assert_eq!(i.to_f32(), w.to_f32());
  43. assert_eq!(i.to_f64(), w.to_f64());
  44. })+
  45. };
  46. }
  47. test_wrapping_to_primitive!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
  48. }
  49. #[test]
  50. fn wrapping_is_toprimitive() {
  51. fn require_toprimitive<T: ToPrimitive>(_: &T) {}
  52. require_toprimitive(&Wrapping(42));
  53. }
  54. #[test]
  55. fn wrapping_is_fromprimitive() {
  56. fn require_fromprimitive<T: FromPrimitive>(_: &T) {}
  57. require_fromprimitive(&Wrapping(42));
  58. }
  59. #[test]
  60. fn wrapping_is_numcast() {
  61. fn require_numcast<T: NumCast>(_: &T) {}
  62. require_numcast(&Wrapping(42));
  63. }
  64. #[test]
  65. fn as_primitive() {
  66. let x: f32 = (1.625f64).as_();
  67. assert_eq!(x, 1.625f32);
  68. let x: f32 = (3.14159265358979323846f64).as_();
  69. assert_eq!(x, 3.1415927f32);
  70. let x: u8 = (768i16).as_();
  71. assert_eq!(x, 0);
  72. }
  73. #[test]
  74. fn float_to_integer_checks_overflow() {
  75. // This will overflow an i32
  76. let source: f64 = 1.0e+123f64;
  77. // Expect the overflow to be caught
  78. assert_eq!(cast::<f64, i32>(source), None);
  79. }
  80. #[test]
  81. fn cast_to_int_checks_overflow() {
  82. let big_f: f64 = 1.0e123;
  83. let normal_f: f64 = 1.0;
  84. let small_f: f64 = -1.0e123;
  85. assert_eq!(None, cast::<f64, isize>(big_f));
  86. assert_eq!(None, cast::<f64, i8>(big_f));
  87. assert_eq!(None, cast::<f64, i16>(big_f));
  88. assert_eq!(None, cast::<f64, i32>(big_f));
  89. assert_eq!(None, cast::<f64, i64>(big_f));
  90. assert_eq!(Some(normal_f as isize), cast::<f64, isize>(normal_f));
  91. assert_eq!(Some(normal_f as i8), cast::<f64, i8>(normal_f));
  92. assert_eq!(Some(normal_f as i16), cast::<f64, i16>(normal_f));
  93. assert_eq!(Some(normal_f as i32), cast::<f64, i32>(normal_f));
  94. assert_eq!(Some(normal_f as i64), cast::<f64, i64>(normal_f));
  95. assert_eq!(None, cast::<f64, isize>(small_f));
  96. assert_eq!(None, cast::<f64, i8>(small_f));
  97. assert_eq!(None, cast::<f64, i16>(small_f));
  98. assert_eq!(None, cast::<f64, i32>(small_f));
  99. assert_eq!(None, cast::<f64, i64>(small_f));
  100. }
  101. #[test]
  102. fn cast_to_unsigned_int_checks_overflow() {
  103. let big_f: f64 = 1.0e123;
  104. let normal_f: f64 = 1.0;
  105. let small_f: f64 = -1.0e123;
  106. assert_eq!(None, cast::<f64, usize>(big_f));
  107. assert_eq!(None, cast::<f64, u8>(big_f));
  108. assert_eq!(None, cast::<f64, u16>(big_f));
  109. assert_eq!(None, cast::<f64, u32>(big_f));
  110. assert_eq!(None, cast::<f64, u64>(big_f));
  111. assert_eq!(Some(normal_f as usize), cast::<f64, usize>(normal_f));
  112. assert_eq!(Some(normal_f as u8), cast::<f64, u8>(normal_f));
  113. assert_eq!(Some(normal_f as u16), cast::<f64, u16>(normal_f));
  114. assert_eq!(Some(normal_f as u32), cast::<f64, u32>(normal_f));
  115. assert_eq!(Some(normal_f as u64), cast::<f64, u64>(normal_f));
  116. assert_eq!(None, cast::<f64, usize>(small_f));
  117. assert_eq!(None, cast::<f64, u8>(small_f));
  118. assert_eq!(None, cast::<f64, u16>(small_f));
  119. assert_eq!(None, cast::<f64, u32>(small_f));
  120. assert_eq!(None, cast::<f64, u64>(small_f));
  121. }
  122. #[test]
  123. #[cfg(feature = "i128")]
  124. fn cast_to_i128_checks_overflow() {
  125. let big_f: f64 = 1.0e123;
  126. let normal_f: f64 = 1.0;
  127. let small_f: f64 = -1.0e123;
  128. assert_eq!(None, cast::<f64, i128>(big_f));
  129. assert_eq!(None, cast::<f64, u128>(big_f));
  130. assert_eq!(Some(normal_f as i128), cast::<f64, i128>(normal_f));
  131. assert_eq!(Some(normal_f as u128), cast::<f64, u128>(normal_f));
  132. assert_eq!(None, cast::<f64, i128>(small_f));
  133. assert_eq!(None, cast::<f64, u128>(small_f));
  134. }
  135. #[cfg(feature = "std")]
  136. fn dbg(args: ::core::fmt::Arguments) {
  137. println!("{}", args);
  138. }
  139. #[cfg(not(feature = "std"))]
  140. fn dbg(_: ::core::fmt::Arguments) {}
  141. // Rust 1.8 doesn't handle cfg on macros correctly
  142. macro_rules! dbg { ($($tok:tt)*) => { dbg(format_args!($($tok)*)) } }
  143. macro_rules! float_test_edge {
  144. ($f:ident -> $($t:ident)+) => { $({
  145. dbg!("testing cast edge cases for {} -> {}", stringify!($f), stringify!($t));
  146. let small = if $t::MIN == 0 || mem::size_of::<$t>() < mem::size_of::<$f>() {
  147. $t::MIN as $f - 1.0
  148. } else {
  149. ($t::MIN as $f).raw_offset(1).floor()
  150. };
  151. let fmin = small.raw_offset(-1);
  152. dbg!(" testing min {}\n\tvs. {:.0}\n\tand {:.0}", $t::MIN, fmin, small);
  153. assert_eq!(Some($t::MIN), cast::<$f, $t>($t::MIN as $f));
  154. assert_eq!(Some($t::MIN), cast::<$f, $t>(fmin));
  155. assert_eq!(None, cast::<$f, $t>(small));
  156. let (max, large) = if mem::size_of::<$t>() < mem::size_of::<$f>() {
  157. ($t::MAX, $t::MAX as $f + 1.0)
  158. } else {
  159. let large = $t::MAX as $f; // rounds up!
  160. let max = large.raw_offset(-1) as $t; // the next smallest possible
  161. assert_eq!(max.count_ones(), $f::MANTISSA_DIGITS);
  162. (max, large)
  163. };
  164. let fmax = large.raw_offset(-1);
  165. dbg!(" testing max {}\n\tvs. {:.0}\n\tand {:.0}", max, fmax, large);
  166. assert_eq!(Some(max), cast::<$f, $t>(max as $f));
  167. assert_eq!(Some(max), cast::<$f, $t>(fmax));
  168. assert_eq!(None, cast::<$f, $t>(large));
  169. dbg!(" testing non-finite values");
  170. assert_eq!(None, cast::<$f, $t>($f::NAN));
  171. assert_eq!(None, cast::<$f, $t>($f::INFINITY));
  172. assert_eq!(None, cast::<$f, $t>($f::NEG_INFINITY));
  173. })+}
  174. }
  175. trait RawOffset: Sized {
  176. type Raw;
  177. fn raw_offset(self, offset: Self::Raw) -> Self;
  178. }
  179. impl RawOffset for f32 {
  180. type Raw = i32;
  181. fn raw_offset(self, offset: Self::Raw) -> Self {
  182. unsafe {
  183. let raw: Self::Raw = mem::transmute(self);
  184. mem::transmute(raw + offset)
  185. }
  186. }
  187. }
  188. impl RawOffset for f64 {
  189. type Raw = i64;
  190. fn raw_offset(self, offset: Self::Raw) -> Self {
  191. unsafe {
  192. let raw: Self::Raw = mem::transmute(self);
  193. mem::transmute(raw + offset)
  194. }
  195. }
  196. }
  197. #[test]
  198. fn cast_float_to_int_edge_cases() {
  199. float_test_edge!(f32 -> isize i8 i16 i32 i64);
  200. float_test_edge!(f32 -> usize u8 u16 u32 u64);
  201. float_test_edge!(f64 -> isize i8 i16 i32 i64);
  202. float_test_edge!(f64 -> usize u8 u16 u32 u64);
  203. }
  204. #[test]
  205. #[cfg(feature = "i128")]
  206. fn cast_float_to_i128_edge_cases() {
  207. float_test_edge!(f32 -> i128 u128);
  208. float_test_edge!(f64 -> i128 u128);
  209. }
  210. macro_rules! int_test_edge {
  211. ($f:ident -> { $($t:ident)+ } with $BigS:ident $BigU:ident ) => { $({
  212. fn test_edge() {
  213. dbg!("testing cast edge cases for {} -> {}", stringify!($f), stringify!($t));
  214. match ($f::MIN as $BigS).cmp(&($t::MIN as $BigS)) {
  215. Greater => {
  216. assert_eq!(Some($f::MIN as $t), cast::<$f, $t>($f::MIN));
  217. }
  218. Equal => {
  219. assert_eq!(Some($t::MIN), cast::<$f, $t>($f::MIN));
  220. }
  221. Less => {
  222. let min = $t::MIN as $f;
  223. assert_eq!(Some($t::MIN), cast::<$f, $t>(min));
  224. assert_eq!(None, cast::<$f, $t>(min - 1));
  225. }
  226. }
  227. match ($f::MAX as $BigU).cmp(&($t::MAX as $BigU)) {
  228. Greater => {
  229. let max = $t::MAX as $f;
  230. assert_eq!(Some($t::MAX), cast::<$f, $t>(max));
  231. assert_eq!(None, cast::<$f, $t>(max + 1));
  232. }
  233. Equal => {
  234. assert_eq!(Some($t::MAX), cast::<$f, $t>($f::MAX));
  235. }
  236. Less => {
  237. assert_eq!(Some($f::MAX as $t), cast::<$f, $t>($f::MAX));
  238. }
  239. }
  240. }
  241. test_edge();
  242. })+}
  243. }
  244. #[test]
  245. fn cast_int_to_int_edge_cases() {
  246. use core::cmp::Ordering::*;
  247. macro_rules! test_edge {
  248. ($( $from:ident )+) => { $({
  249. int_test_edge!($from -> { isize i8 i16 i32 i64 } with i64 u64);
  250. int_test_edge!($from -> { usize u8 u16 u32 u64 } with i64 u64);
  251. })+}
  252. }
  253. test_edge!(isize i8 i16 i32 i64);
  254. test_edge!(usize u8 u16 u32 u64);
  255. }
  256. #[test]
  257. #[cfg(feature = "i128")]
  258. fn cast_int_to_128_edge_cases() {
  259. use core::cmp::Ordering::*;
  260. macro_rules! test_edge {
  261. ($( $t:ident )+) => {
  262. $(
  263. int_test_edge!($t -> { i128 u128 } with i128 u128);
  264. )+
  265. int_test_edge!(i128 -> { $( $t )+ } with i128 u128);
  266. int_test_edge!(u128 -> { $( $t )+ } with i128 u128);
  267. }
  268. }
  269. test_edge!(isize i8 i16 i32 i64 i128);
  270. test_edge!(usize u8 u16 u32 u64 u128);
  271. }