1
use super::*;
2

            
3
#[test]
4
1
fn accepts_decimals() {
5
1
    assert_eq!(parse_amount("10.5"), Ok((21, 2)));
6
1
    assert_eq!(parse_amount("-3"), Ok((-3, 1)));
7
1
    assert_eq!(parse_amount("+3"), Ok((3, 1)));
8
1
    assert_eq!(parse_amount("  153.81 "), Ok((15381, 100)));
9
1
    assert_eq!(parse_amount(".5"), Ok((1, 2)));
10
1
    assert_eq!(parse_amount("5."), Ok((5, 1)));
11
1
    assert_eq!(
12
1
        parse_amount("0.000001907348632813"),
13
1
        Ok((1907348632813, 1_000_000_000_000_000_000))
14
    );
15
1
}
16

            
17
#[test]
18
1
fn reduces_before_the_storage_check() {
19
1
    assert_eq!(parse_amount("10.000003814697265625"), Ok((2621441, 262144)));
20
1
    assert_eq!(
21
1
        parse_amount("1.0000000000000000000000000000000000000000"),
22
1
        Ok((1, 1))
23
    );
24
1
}
25

            
26
#[test]
27
1
fn accepts_fractions() {
28
1
    assert_eq!(parse_amount("1/3"), Ok((1, 3)));
29
1
    assert_eq!(parse_amount("-2/4"), Ok((-1, 2)));
30
1
    assert_eq!(parse_amount(" 6 / 3 "), Ok((2, 1)));
31
1
    assert_eq!(parse_amount("+7/1"), Ok((7, 1)));
32
1
    assert_eq!(parse_amount("0/5"), Ok((0, 1)));
33
1
}
34

            
35
#[test]
36
1
fn rejects_malformed_input() {
37
1
    for bad in [
38
1
        "abc", "1.2.3", "1e3", "inf", "nan", "-", ".", "1/", "/2", "1/2/3", "1/-2", "1/+2",
39
1
        "1.5/2", "1/2.5", "--1", "- 1", "0x10",
40
1
    ] {
41
17
        assert_eq!(parse_amount(bad), Err(AmountError::Syntax), "{bad}");
42
    }
43
1
}
44

            
45
#[test]
46
1
fn rejects_empty_and_zero_denominator() {
47
1
    assert_eq!(parse_amount(""), Err(AmountError::Empty));
48
1
    assert_eq!(parse_amount("   "), Err(AmountError::Empty));
49
1
    assert_eq!(parse_amount("1/0"), Err(AmountError::ZeroDenominator));
50
1
}
51

            
52
#[test]
53
1
fn rejects_values_that_do_not_fit() {
54
1
    let huge = "9".repeat(40);
55
1
    assert_eq!(parse_amount(&huge), Err(AmountError::Overflow));
56
1
    assert_eq!(
57
1
        parse_amount(&format!("1/{huge}")),
58
1
        Err(AmountError::Overflow)
59
    );
60
1
    assert_eq!(
61
1
        parse_amount("9223372036854775808"),
62
1
        Err(AmountError::Overflow)
63
    );
64
1
    assert_eq!(
65
1
        parse_amount("-9223372036854775808"),
66
1
        Err(AmountError::Overflow)
67
    );
68
1
    assert_eq!(
69
1
        parse_amount("1/9223372036854775808"),
70
1
        Err(AmountError::Overflow)
71
    );
72
1
    assert_eq!(
73
1
        parse_amount("0.1234567890123456789012345678901234567890123"),
74
1
        Err(AmountError::Overflow)
75
    );
76
1
    assert_eq!(parse_amount("9223372036854775807"), Ok((i64::MAX, 1)));
77
1
    assert_eq!(parse_amount("-9223372036854775807"), Ok((-i64::MAX, 1)));
78
1
    assert_eq!(parse_amount("1/9223372036854775807"), Ok((1, i64::MAX)));
79
1
}
80

            
81
#[test]
82
1
fn error_messages_are_human_readable() {
83
1
    assert!(AmountError::Syntax.to_string().contains("fraction"));
84
1
    assert!(AmountError::Empty.to_string().contains("empty"));
85
1
}
86

            
87
#[test]
88
1
fn zero_decimals_are_zero() {
89
1
    for text in [".0", "+.00", "-.0", "0.0", "0.", "-0"] {
90
6
        assert_eq!(parse_amount(text), Ok((0, 1)), "{text}");
91
    }
92
1
}
93

            
94
#[test]
95
1
fn reduce_to_i64_reduces_and_bounds() {
96
1
    assert_eq!(reduce_to_i64(6, 4), Ok((3, 2)));
97
1
    assert_eq!(reduce_to_i64(-6, 4), Ok((-3, 2)));
98
1
    assert_eq!(
99
1
        reduce_to_i64(i128::from(i64::MAX) * 2, 4),
100
1
        Ok((i64::MAX, 2))
101
    );
102
1
    assert_eq!(
103
1
        reduce_to_i64(i128::from(i64::MAX) * 3, 2),
104
1
        Err(AmountError::Overflow)
105
    );
106
1
    assert_eq!(
107
1
        reduce_to_i64(i128::from(i64::MIN), 1),
108
1
        Err(AmountError::Overflow)
109
    );
110
1
}