Debian Codegraph @

debian/rust-vendor/bigdecimal/src/parsing.tests.parse_from_f32.rs

raw
1 // tests for function bigdecimal::parsing::parse_from_f32
2
3 use paste::paste;
4
5 use stdlib::f32;
6
7 macro_rules! impl_test {
8 ($name:ident : $input:literal == $expected:literal) => {
9 paste! {
10 #[test]
11 fn [< case $name >]() {
12 let n = $input as f32;
13 let d = parse_from_f32(n);
14 let expected: BigDecimal = $expected.parse().unwrap();
15 assert_eq!(d, expected);
16 }
17
18 #[test]
19 fn [< case_neg $name >]() {
20 let n = -($input as f32);
21 let d = parse_from_f32(n);
22 let expected: BigDecimal = concat!("-", $expected).parse().unwrap();
23 assert_eq!(d, expected);
24 }
25 }
26 };
27 }
28
29
30 impl_test!(_0 : 0.0 == "0");
31 impl_test!(_1 : 1.0 == "1");
32 impl_test!(_5en1 : 0.5 == "0.5");
33 impl_test!(_25en2 : 0.25 == "0.25");
34 impl_test!(_50 : 50. == "50");
35 impl_test!(_1en3 : 0.001 == "0.001000000047497451305389404296875");
36 impl_test!(_033203125en8 : 0.033203125 == "0.033203125");
37
38 impl_test!(_45En1 : 4.5 == "4.5");
39 impl_test!(_15625En5 : 0.15625 == "0.15625");
40 impl_test!(_1192092896En7 : 1.192092896e-7 == "1.1920928955078125E-7");
41 impl_test!(_1401757440 : 1401757440. == "1401757440");
42 impl_test!(_215092En1 : 21509.2 == "21509.19921875");
43 impl_test!(_2289620000 : 2289620000.0 == "2289619968");
44 impl_test!(_10000000 : 10000000. == "10000000");
45 impl_test!(_1en05 : 1e-5 == "0.00000999999974737875163555145263671875");
46 impl_test!(_1en1 : 1e-1 == "0.100000001490116119384765625");
47 impl_test!(_2en1 : 2e-1 == "0.20000000298023223876953125");
48 impl_test!(_80000197 : 80000197e0 == "80000200");
49 impl_test!(_23283064En16 : 2.3283064e-10 == "0.00000000023283064365386962890625");
50 impl_test!(_14693861798803098En17 : 0.14693861798803098 == "0.146938621997833251953125");
51 impl_test!(_1e20 : 1e20 == "100000002004087734272");
52 impl_test!(_1e30 : 1e30 == "1000000015047466219876688855040");
53 impl_test!(_1e38 : 1e38 == "99999996802856924650656260769173209088");
54 impl_test!(_317e36 : 317e36 == "317000006395220278118691742155288870912");
55 impl_test!(_23509889819en48 : 2.3509889819e-38 == "2.35098898190426788090088725919040801362055736959656341832065776397049129686767088287524529732763767242431640625E-38");
56 impl_test!(_235098744048en49 : 2.35098744048e-38 == "2.350987440475957123602109243087866394712812961308427354153308831195379018097479928428583662025630474090576171875E-38");
57 impl_test!(_6_99999952316 : 6.99999952316 == "6.999999523162841796875");
58 impl_test!(_317en40 : 317e-40 == "3.1700000098946435501119816090716154772221806896649747100732700841687651538425285480116144753992557525634765625E-38");
59 impl_test!(_4294967295 : 4294967295. == "4294967296");
60 impl_test!(_158456325029e18 : 1.58456325029e+29 == "158456325028528675187087900672");
61
62
63
64 impl_test!(_1_40129846432e_45 : 1.40129846432e-45 == "1.40129846432481707092372958328991613128026194187651577175706828388979108268586060148663818836212158203125E-45");
65 impl_test!(_1_e42 : 1e-42 == "1.0005271035279193886395429224690001177341070264998322610345467546973108330377044694614596664905548095703125e-42");
66 impl_test!(_3_92E_n39 : 3.92E-39 == "3.91999933059456489828739575494312783522406115751507460249208160269472102366083987590172910131514072418212890625E-39");
67 impl_test!(_2_81341650018752E_n308 : 2.81341650018752E-308 == "0");
68
69 #[test]
70 fn case_f32_min() {
71 let n = f32::MIN;
72 let d = parse_from_f32(n);
73 assert_eq!(d, "-340282346638528859811704183484516925440".parse::<BigDecimal>().unwrap());
74 }
75
76 #[test]
77 fn case_f32_max() {
78 let n = f32::MAX;
79 let d = parse_from_f32(n);
80 assert_eq!(d, "340282346638528859811704183484516925440".parse::<BigDecimal>().unwrap());
81 }
82
83 #[test]
84 fn case_f32_epsilon() {
85 let n = f32::EPSILON;
86 let d = parse_from_f32(n);
87 assert_eq!(d, "1.1920928955078125E-7".parse::<BigDecimal>().unwrap());
88 }
89
90 #[test]
91 fn case_f32_pi() {
92 let n = f32::consts::PI;
93 let d = parse_from_f32(n);
94 assert_eq!(d, "3.1415927410125732421875".parse::<BigDecimal>().unwrap());
95 }
96
97 #[test]
98 fn case_nan() {
99 let n = f32::from_bits(0b01111111110000000000000000000000);
100 assert!(n.is_nan());
101
102 let d = parse_from_f32(n);
103 assert_eq!(d, "510423550381407695195061911147652317184".parse::<BigDecimal>().unwrap());
104 }
105
106 #[test]
107 fn case_try_from_nan() {
108 let n = f32::NAN;
109 let d = try_parse_from_f32(n);
110 assert!(d.is_err());
111 }
112
113 #[test]
114 fn case_try_from_infinity() {
115 let n = f32::INFINITY;
116 let d = try_parse_from_f32(n);
117 assert!(d.is_err());
118 }