Skip to content

Commit 3ff2b06

Browse files
committed
Move impl FromStr and tests to separate module
2 parents 8ebccee + f3406a7 commit 3ff2b06

File tree

2 files changed

+81
-80
lines changed

2 files changed

+81
-80
lines changed

src/impl_trait_from_str.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use crate::*;
2+
use stdlib::str::FromStr;
3+
4+
impl FromStr for BigDecimal {
5+
type Err = ParseBigDecimalError;
6+
7+
#[inline]
8+
fn from_str(s: &str) -> Result<BigDecimal, ParseBigDecimalError> {
9+
// implemented in impl_num.rs
10+
BigDecimal::from_str_radix(s, 10)
11+
}
12+
}
13+
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use super::*;
18+
19+
macro_rules! impl_case {
20+
($name:ident: $input:literal => $int:literal E $exp:literal) => {
21+
#[test]
22+
fn $name() {
23+
let dec = BigDecimal::from_str($input).unwrap();
24+
assert_eq!(dec.int_val, $int.into());
25+
assert_eq!(dec.scale, -$exp);
26+
}
27+
};
28+
}
29+
30+
impl_case!(case_1331d107: "1331.107" => 1331107 E -3 );
31+
impl_case!(case_1d0: "1.0" => 10 E -1 );
32+
impl_case!(case_2e1: "2e1" => 2 E 1 );
33+
impl_case!(case_0d00123: "0.00123" => 123 E -5);
34+
impl_case!(case_n123: "-123" => -123 E -0);
35+
impl_case!(case_n1230: "-1230" => -1230 E -0);
36+
impl_case!(case_12d3: "12.3" => 123 E -1);
37+
impl_case!(case_123en1: "123e-1" => 123 E -1);
38+
impl_case!(case_1d23ep1: "1.23e+1" => 123 E -1);
39+
impl_case!(case_1d23ep3: "1.23E+3" => 123 E 1);
40+
impl_case!(case_1d23en8: "1.23E-8" => 123 E -10);
41+
impl_case!(case_n1d23en10: "-1.23E-10" => -123 E -12);
42+
impl_case!(case_123_: "123_" => 123 E -0);
43+
impl_case!(case_31_862_140d830_686_979: "31_862_140.830_686_979" => 31862140830686979i128 E -9);
44+
impl_case!(case_n1_1d2_2: "-1_1.2_2" => -1122 E -2);
45+
impl_case!(case_999d521_939: "999.521_939" => 999521939 E -6);
46+
impl_case!(case_679d35_84_03en2: "679.35_84_03E-2" => 679358403 E -8);
47+
impl_case!(case_271576662d_e4: "271576662.__E4" => 271576662 E 4);
48+
49+
impl_case!(case_1_d_2: "1_._2" => 12 E -1);
50+
}
51+
52+
53+
#[cfg(test)]
54+
mod test_invalid {
55+
use super::*;
56+
57+
macro_rules! impl_case {
58+
($name:ident: $input:literal => $exp:literal) => {
59+
#[test]
60+
#[should_panic(expected = $exp)]
61+
fn $name() {
62+
BigDecimal::from_str($input).unwrap();
63+
}
64+
};
65+
}
66+
67+
impl_case!(case_bad_string_empty : "" => "Empty");
68+
impl_case!(case_bad_string_empty_exponent : "123.123E" => "Empty");
69+
impl_case!(case_bad_string_only_decimal_point : "." => "Empty");
70+
impl_case!(test_bad_string_only_decimal_and_exponent : ".e4" => "Empty");
71+
72+
impl_case!(test_bad_string_only_decimal_and_underscore : "_._" => "InvalidDigit");
73+
74+
impl_case!(case_bad_string_hello : "hello" => "InvalidDigit");
75+
impl_case!(case_bad_string_nan : "nan" => "InvalidDigit");
76+
impl_case!(case_bad_string_invalid_char : "12z3.12" => "InvalidDigit");
77+
impl_case!(case_bad_string_nan_exponent : "123.123eg" => "InvalidDigit");
78+
impl_case!(case_bad_string_multiple_decimal_points : "123.12.45" => "InvalidDigit");
79+
impl_case!(case_bad_string_hex : "0xCafeBeef" => "InvalidDigit");
80+
}

src/lib.rs

+1-80
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ mod arithmetic;
9797

9898
// From<T>, To<T>, TryFrom<T> impls
9999
mod impl_convert;
100+
mod impl_trait_from_str;
100101

101102
// Add<T>, Sub<T>, etc...
102103
mod impl_ops;
@@ -918,15 +919,6 @@ impl From<ParseBigIntError> for ParseBigDecimalError {
918919
}
919920
}
920921

921-
impl FromStr for BigDecimal {
922-
type Err = ParseBigDecimalError;
923-
924-
#[inline]
925-
fn from_str(s: &str) -> Result<BigDecimal, ParseBigDecimalError> {
926-
BigDecimal::from_str_radix(s, 10)
927-
}
928-
}
929-
930922
#[allow(deprecated)] // trim_right_match -> trim_end_match
931923
impl Hash for BigDecimal {
932924
fn hash<H: Hasher>(&self, state: &mut H) {
@@ -2158,36 +2150,6 @@ mod bigdecimal_tests {
21582150
}
21592151
}
21602152

2161-
#[test]
2162-
fn test_from_str() {
2163-
let vals = vec![
2164-
("1331.107", 1331107, 3),
2165-
("1.0", 10, 1),
2166-
("2e1", 2, -1),
2167-
("0.00123", 123, 5),
2168-
("-123", -123, 0),
2169-
("-1230", -1230, 0),
2170-
("12.3", 123, 1),
2171-
("123e-1", 123, 1),
2172-
("1.23e+1", 123, 1),
2173-
("1.23E+3", 123, -1),
2174-
("1.23E-8", 123, 10),
2175-
("-1.23E-10", -123, 12),
2176-
("123_", 123, 0),
2177-
("31_862_140.830_686_979", 31862140830686979, 9),
2178-
("-1_1.2_2", -1122, 2),
2179-
("999.521_939", 999521939, 6),
2180-
("679.35_84_03E-2", 679358403, 8),
2181-
("271576662.__E4", 271576662, -4),
2182-
];
2183-
2184-
for &(source, val, scale) in vals.iter() {
2185-
let x = BigDecimal::from_str(source).unwrap();
2186-
assert_eq!(x.int_val.to_i64().unwrap(), val);
2187-
assert_eq!(x.scale, scale);
2188-
}
2189-
}
2190-
21912153
#[test]
21922154
fn test_fmt() {
21932155
let vals = vec![
@@ -2266,47 +2228,6 @@ mod bigdecimal_tests {
22662228
}
22672229
}
22682230

2269-
#[test]
2270-
#[should_panic(expected = "InvalidDigit")]
2271-
fn test_bad_string_nan() {
2272-
BigDecimal::from_str("hello").unwrap();
2273-
}
2274-
#[test]
2275-
#[should_panic(expected = "Empty")]
2276-
fn test_bad_string_empty() {
2277-
BigDecimal::from_str("").unwrap();
2278-
}
2279-
#[test]
2280-
#[should_panic(expected = "InvalidDigit")]
2281-
fn test_bad_string_invalid_char() {
2282-
BigDecimal::from_str("12z3.12").unwrap();
2283-
}
2284-
#[test]
2285-
#[should_panic(expected = "InvalidDigit")]
2286-
fn test_bad_string_nan_exponent() {
2287-
BigDecimal::from_str("123.123eg").unwrap();
2288-
}
2289-
#[test]
2290-
#[should_panic(expected = "Empty")]
2291-
fn test_bad_string_empty_exponent() {
2292-
BigDecimal::from_str("123.123E").unwrap();
2293-
}
2294-
#[test]
2295-
#[should_panic(expected = "InvalidDigit")]
2296-
fn test_bad_string_multiple_decimal_points() {
2297-
BigDecimal::from_str("123.12.45").unwrap();
2298-
}
2299-
#[test]
2300-
#[should_panic(expected = "Empty")]
2301-
fn test_bad_string_only_decimal() {
2302-
BigDecimal::from_str(".").unwrap();
2303-
}
2304-
#[test]
2305-
#[should_panic(expected = "Empty")]
2306-
fn test_bad_string_only_decimal_and_exponent() {
2307-
BigDecimal::from_str(".e4").unwrap();
2308-
}
2309-
23102231
#[test]
23112232
fn test_from_i128() {
23122233
let value = BigDecimal::from_i128(-368934881474191032320).unwrap();

0 commit comments

Comments
 (0)