]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | use core::num::dec2flt::parse::ParseResult::{Invalid, Valid}; |
2 | use core::num::dec2flt::parse::{parse_decimal, Decimal}; | |
e9174d1e SL |
3 | |
4 | #[test] | |
5 | fn missing_pieces() { | |
6 | let permutations = &[".e", "1e", "e4", "e", ".12e", "321.e", "32.12e+", "12.32e-"]; | |
7 | for &s in permutations { | |
8 | assert_eq!(parse_decimal(s), Invalid); | |
9 | } | |
10 | } | |
11 | ||
12 | #[test] | |
13 | fn invalid_chars() { | |
14 | let invalid = "r,?<j"; | |
15 | let valid_strings = &["123", "666.", ".1", "5e1", "7e-3", "0.0e+1"]; | |
16 | for c in invalid.chars() { | |
17 | for s in valid_strings { | |
18 | for i in 0..s.len() { | |
19 | let mut input = String::new(); | |
20 | input.push_str(s); | |
21 | input.insert(i, c); | |
22 | assert!(parse_decimal(&input) == Invalid, "did not reject invalid {:?}", input); | |
23 | } | |
24 | } | |
25 | } | |
26 | } | |
27 | ||
28 | #[test] | |
29 | fn valid() { | |
30 | assert_eq!(parse_decimal("123.456e789"), Valid(Decimal::new(b"123", b"456", 789))); | |
31 | assert_eq!(parse_decimal("123.456e+789"), Valid(Decimal::new(b"123", b"456", 789))); | |
32 | assert_eq!(parse_decimal("123.456e-789"), Valid(Decimal::new(b"123", b"456", -789))); | |
33 | assert_eq!(parse_decimal(".050"), Valid(Decimal::new(b"", b"050", 0))); | |
34 | assert_eq!(parse_decimal("999"), Valid(Decimal::new(b"999", b"", 0))); | |
35 | assert_eq!(parse_decimal("1.e300"), Valid(Decimal::new(b"1", b"", 300))); | |
36 | assert_eq!(parse_decimal(".1e300"), Valid(Decimal::new(b"", b"1", 300))); | |
37 | assert_eq!(parse_decimal("101e-33"), Valid(Decimal::new(b"101", b"", -33))); | |
8faf50e0 | 38 | let zeros = "0".repeat(25); |
e9174d1e SL |
39 | let s = format!("1.5e{}", zeros); |
40 | assert_eq!(parse_decimal(&s), Valid(Decimal::new(b"1", b"5", 0))); | |
41 | } |