1 #![cfg(not(target_arch = "wasm32"))]
3 use std::mem::MaybeUninit;
6 use core::num::flt2dec::strategy::grisu::format_exact_opt;
7 use core::num::flt2dec::strategy::grisu::format_shortest_opt;
8 use core::num::flt2dec::MAX_SIG_DIGITS;
9 use core::num::flt2dec::{decode, DecodableFloat, Decoded, FullDecoded};
11 use rand::distributions::{Distribution, Uniform};
13 pub fn decode_finite<T: DecodableFloat>(v: T) -> Decoded {
15 FullDecoded::Finite(decoded) => decoded,
16 full_decoded => panic!("expected finite, got {full_decoded:?} instead"),
20 fn iterate<F, G, V>(func: &str, k: usize, n: usize, mut f: F, mut g: G, mut v: V) -> (usize, usize)
22 F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
23 G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
24 V: FnMut(usize) -> Decoded,
28 let mut npassed = 0; // f(x) = Some(g(x))
29 let mut nignored = 0; // f(x) = None
32 if (i & 0xfffff) == 0 {
34 "in progress, {:x}/{:x} (ignored={} passed={} failed={})",
39 i - nignored - npassed
44 let mut buf1 = [MaybeUninit::new(0); 1024];
45 if let Some((buf1, e1)) = f(&decoded, &mut buf1[..k]) {
46 let mut buf2 = [MaybeUninit::new(0); 1024];
47 let (buf2, e2) = g(&decoded, &mut buf2[..k]);
48 if e1 == e2 && buf1 == buf2 {
52 "equivalence test failed, {:x}/{:x}: {:?} f(i)={}e{} g(i)={}e{}",
56 str::from_utf8(buf1).unwrap(),
58 str::from_utf8(buf2).unwrap(),
67 "{}({}): done, ignored={} passed={} failed={}",
72 n - nignored - npassed
75 nignored + npassed == n,
76 "{}({}): {} out of {} values returns an incorrect value!",
79 n - nignored - npassed,
85 pub fn f32_random_equivalence_test<F, G>(f: F, g: G, k: usize, n: usize)
87 F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
88 G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
90 if cfg!(target_os = "emscripten") {
91 return; // using rng pulls in i128 support, which doesn't work
93 let mut rng = crate::test_rng();
94 let f32_range = Uniform::new(0x0000_0001u32, 0x7f80_0000);
95 iterate("f32_random_equivalence_test", k, n, f, g, |_| {
96 let x = f32::from_bits(f32_range.sample(&mut rng));
101 pub fn f64_random_equivalence_test<F, G>(f: F, g: G, k: usize, n: usize)
103 F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
104 G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
106 if cfg!(target_os = "emscripten") {
107 return; // using rng pulls in i128 support, which doesn't work
109 let mut rng = crate::test_rng();
110 let f64_range = Uniform::new(0x0000_0000_0000_0001u64, 0x7ff0_0000_0000_0000);
111 iterate("f64_random_equivalence_test", k, n, f, g, |_| {
112 let x = f64::from_bits(f64_range.sample(&mut rng));
117 pub fn f32_exhaustive_equivalence_test<F, G>(f: F, g: G, k: usize)
119 F: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> Option<(&'a [u8], i16)>,
120 G: for<'a> FnMut(&Decoded, &'a mut [MaybeUninit<u8>]) -> (&'a [u8], i16),
122 // we have only 2^23 * (2^8 - 1) - 1 = 2,139,095,039 positive finite f32 values,
123 // so why not simply testing all of them?
125 // this is of course very stressful (and thus should be behind an `#[ignore]` attribute),
126 // but with `-C opt-level=3 -C lto` this only takes about an hour or so.
128 // iterate from 0x0000_0001 to 0x7f7f_ffff, i.e., all finite ranges
129 let (npassed, nignored) =
130 iterate("f32_exhaustive_equivalence_test", k, 0x7f7f_ffff, f, g, |i: usize| {
131 let x = f32::from_bits(i as u32 + 1);
134 assert_eq!((npassed, nignored), (2121451881, 17643158));
138 fn shortest_random_equivalence_test() {
139 use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
141 let n = if cfg!(miri) { 10 } else { 10_000 };
143 f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, n);
144 f32_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, n);
148 #[ignore] // it is too expensive
149 fn shortest_f32_exhaustive_equivalence_test() {
150 // it is hard to directly test the optimality of the output, but we can at least test if
151 // two different algorithms agree to each other.
153 // this reports the progress and the number of f32 values returned `None`.
154 // with `--nocapture` (and plenty of time and appropriate rustc flags), this should print:
155 // `done, ignored=17643158 passed=2121451881 failed=0`.
157 use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
158 f32_exhaustive_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS);
162 #[ignore] // it is too expensive
163 fn shortest_f64_hard_random_equivalence_test() {
164 // this again probably has to use appropriate rustc flags.
166 use core::num::flt2dec::strategy::dragon::format_shortest as fallback;
167 f64_random_equivalence_test(format_shortest_opt, fallback, MAX_SIG_DIGITS, 100_000_000);
171 fn exact_f32_random_equivalence_test() {
172 use core::num::flt2dec::strategy::dragon::format_exact as fallback;
174 let n = if cfg!(miri) { 3 } else { 1_000 };
177 f32_random_equivalence_test(
178 |d, buf| format_exact_opt(d, buf, i16::MIN),
179 |d, buf| fallback(d, buf, i16::MIN),
187 fn exact_f64_random_equivalence_test() {
188 use core::num::flt2dec::strategy::dragon::format_exact as fallback;
190 let n = if cfg!(miri) { 2 } else { 1_000 };
193 f64_random_equivalence_test(
194 |d, buf| format_exact_opt(d, buf, i16::MIN),
195 |d, buf| fallback(d, buf, i16::MIN),