Skip to content

Commit c2c32ba

Browse files
committed
Add a few tests for the high-level API
1 parent a3b8168 commit c2c32ba

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/test_api.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use fast_float::{parse, parse_partial, FastFloat};
2+
3+
macro_rules! check_ok {
4+
($s:expr, $x:expr) => {
5+
let s = $s;
6+
check_ok!(s, $x, f32);
7+
check_ok!(s.as_bytes(), $x, f32);
8+
check_ok!(s, $x, f64);
9+
check_ok!(s.as_bytes(), $x, f64);
10+
};
11+
($s:expr, $x:expr, $ty:ty) => {
12+
assert_eq!(<$ty>::parse_float($s).unwrap(), $x);
13+
assert_eq!(<$ty>::parse_float_partial($s).unwrap(), ($x, $s.len()));
14+
assert_eq!(parse::<$ty, _>($s).unwrap(), $x);
15+
assert_eq!(parse_partial::<$ty, _>($s).unwrap(), ($x, $s.len()));
16+
};
17+
}
18+
19+
macro_rules! check_ok_partial {
20+
($s:expr, $x:expr, $n:expr) => {
21+
let s = $s;
22+
check_ok_partial!(s, $x, $n, f32);
23+
check_ok_partial!(s.as_bytes(), $x, $n, f32);
24+
check_ok_partial!(s, $x, $n, f64);
25+
check_ok_partial!(s.as_bytes(), $x, $n, f64);
26+
};
27+
($s:expr, $x:expr, $n:expr, $ty:ty) => {
28+
assert!(<$ty>::parse_float($s).is_err());
29+
assert_eq!(<$ty>::parse_float_partial($s).unwrap(), ($x, $n));
30+
assert!(parse::<$ty, _>($s).is_err());
31+
assert_eq!(parse_partial::<$ty, _>($s).unwrap(), ($x, $n));
32+
};
33+
}
34+
35+
macro_rules! check_err {
36+
($s:expr) => {
37+
let s = $s;
38+
check_err!(s, f32);
39+
check_err!(s.as_bytes(), f32);
40+
check_err!(s, f64);
41+
check_err!(s.as_bytes(), f64);
42+
};
43+
($s:expr, $ty:ty) => {
44+
assert!(<$ty>::parse_float($s).is_err());
45+
assert!(<$ty>::parse_float_partial($s).is_err());
46+
assert!(parse::<$ty, _>($s).is_err());
47+
assert!(parse_partial::<$ty, _>($s).is_err());
48+
};
49+
}
50+
51+
#[test]
52+
fn test_api() {
53+
check_ok!("1.23", 1.23);
54+
check_ok!("0.", 0.);
55+
check_ok!("-0", 0.);
56+
check_ok!("+00", 0.);
57+
check_ok!("-0001e-02", -0.01);
58+
check_ok!("345", 345.);
59+
60+
check_ok_partial!("1a", 1., 1);
61+
check_ok_partial!("-2e-1x", -0.2, 5);
62+
check_ok_partial!("2e2.", 200., 3);
63+
check_ok_partial!("2ea", 2., 1);
64+
65+
check_err!("");
66+
check_err!(" ");
67+
check_err!(".");
68+
check_err!(".e1");
69+
check_err!("+");
70+
check_err!("-");
71+
check_err!("x");
72+
check_err!("a123");
73+
}

0 commit comments

Comments
 (0)