1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#![cfg(feature = "power-of-two")]
#![cfg(feature = "parse-floats")]
#![cfg(feature = "write-floats")]
use approx::assert_relative_eq;
const F32_DATA: [f32; 31] = [
0.,
0.1,
1.,
1.1,
12.,
12.1,
123.,
123.1,
1234.,
1234.1,
12345.,
12345.1,
123456.,
123456.1,
1234567.,
1234567.1,
12345678.,
12345678.1,
123456789.,
123456789.1,
123456789.12,
123456789.123,
123456789.1234,
123456789.12345,
1.2345678912345e8,
1.2345e+8,
1.2345e+11,
1.2345e+38,
1.2345e-8,
1.2345e-11,
1.2345e-38,
];
const F64_DATA: [f64; 33] = [
0.,
0.1,
1.,
1.1,
12.,
12.1,
123.,
123.1,
1234.,
1234.1,
12345.,
12345.1,
123456.,
123456.1,
1234567.,
1234567.1,
12345678.,
12345678.1,
123456789.,
123456789.1,
123456789.12,
123456789.123,
123456789.1234,
123456789.12345,
1.2345678912345e8,
1.2345e+8,
1.2345e+11,
1.2345e+38,
1.2345e+308,
1.2345e-8,
1.2345e-11,
1.2345e-38,
1.2345e-299,
];
macro_rules! test_radix {
($f:ident, $radix:expr, $buffer:ident, $data:ident) => {{
use lexical_core::{
FromLexicalWithOptions,
NumberFormatBuilder,
ParseFloatOptions,
ToLexicalWithOptions,
WriteFloatOptions,
};
const FORMAT: u128 = NumberFormatBuilder::from_radix($radix);
println!("Testing radix {} for type {}...", $radix, stringify!($f));
let write_options = WriteFloatOptions::builder().exponent(b'^').build().unwrap();
let parse_options = ParseFloatOptions::builder().exponent(b'^').build().unwrap();
for &float in $data.iter() {
let data = float.to_lexical_with_options::<FORMAT>(&mut $buffer, &write_options);
let roundtrip = $f::from_lexical_with_options::<FORMAT>(data, &parse_options).unwrap();
assert_relative_eq!(float, roundtrip, epsilon = 1e-6, max_relative = 3e-6);
}
}};
}
macro_rules! test_all {
($f:ident, $buffer:ident, $data:ident) => {{
test_radix!($f, 2, $buffer, $data);
test_radix!($f, 4, $buffer, $data);
test_radix!($f, 8, $buffer, $data);
test_radix!($f, 16, $buffer, $data);
test_radix!($f, 32, $buffer, $data);
test_radix!($f, 36, $buffer, $data);
}};
}
#[test]
fn parse_f32_pow2_roundtrip_test() {
let mut buffer = [0u8; 1024];
test_all!(f32, buffer, F32_DATA);
}
#[test]
fn parse_f64_pow2_roundtrip_test() {
let mut buffer = [0u8; 1024];
test_all!(f64, buffer, F64_DATA);
}
|