summaryrefslogtreecommitdiff
path: root/taler-swift/Tests/taler-swiftTests/AmountTests.swift
blob: 5bcae57ec5df917fa990857cf0fd11ea32a3e1e9 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * This file is part of GNU Taler
 * (C) 2022 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

import XCTest
@testable import taler_swift

class AmountTests: XCTestCase {
    func testParsing() {
        var str = "TESTKUDOS:23.42"
        var amt = try! Amount(fromString: str)
        XCTAssert(str == amt.description)
        XCTAssert("TESTKUDOS" == amt.currency)
        XCTAssert(23 == amt.value)
        XCTAssert(UInt64(0.42 * 1e8) == amt.fraction)
        
        str = "EUR:500000000.00000001"
        amt = try! Amount(fromString: str)
        XCTAssert(str == amt.description)
        XCTAssert("EUR" == amt.currency)
        XCTAssert(500000000 == amt.value)
        XCTAssert(1 == amt.fraction)
        
        str = "EUR:1500000000.00000003"
        amt = try! Amount(fromString: str)
        XCTAssert(str == amt.description)
        XCTAssert("EUR" == amt.currency)
        XCTAssert(1500000000 == amt.value)
        XCTAssert(3 == amt.fraction)
        
        let maxValue = 4503599627370496
        str = "TESTKUDOS123:\(maxValue).99999999"
        amt = try! Amount(fromString: str)
        XCTAssert(str == amt.description)
        XCTAssert("TESTKUDOS123" == amt.currency)
        XCTAssert(maxValue == amt.value)
        
        XCTAssertThrowsError(try Amount(fromString: "TESTKUDOS1234:\(maxValue).99999999"))
        XCTAssertThrowsError(try Amount(fromString: "TESTKUDOS123:\(maxValue + 1).99999999"))
        XCTAssertThrowsError(try Amount(fromString: "TESTKUDOS123:\(maxValue).999999990"))
        XCTAssertThrowsError(try Amount(fromString: "TESTKUDOS:0,5"))
        XCTAssertThrowsError(try Amount(fromString: "+TESTKUDOS:0.5"))
        XCTAssertThrowsError(try Amount(fromString: "0.5"))
        XCTAssertThrowsError(try Amount(fromString: ":0.5"))
        XCTAssertThrowsError(try Amount(fromString: "EUR::0.5"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:.5"))
    }
    
    func testAddition() {
        XCTAssert(try Amount(fromString: "EUR:1") + Amount(fromString: "EUR:1") == Amount(fromString: "EUR:2"))
        XCTAssert(try Amount(fromString: "EUR:1.5") + Amount(fromString: "EUR:1.5") == Amount(fromString: "EUR:3"))
        XCTAssert(try Amount(fromString: "EUR:500000000.00000001") + Amount(fromString: "EUR:0.00000001") == Amount(fromString: "EUR:500000000.00000002"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:1") + Amount(fromString: "USD:1"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:4503599627370496.99999999") + Amount(fromString: "EUR:4503599627370496.99999999"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:4000000000000000") + Amount(fromString: "EUR:4000000000000000"))
    }
    
    func testSubtraction() {
        XCTAssert(try Amount(fromString: "EUR:1") - Amount(fromString: "EUR:1") == Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:3") - Amount(fromString: "EUR:1.5") == Amount(fromString: "EUR:1.5"))
        XCTAssert(try Amount(fromString: "EUR:500000000.00000002") - Amount(fromString: "EUR:0.00000001") == Amount(fromString: "EUR:500000000.00000001"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:3") - Amount(fromString: "USD:1.5"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:23.42") - Amount(fromString: "EUR:42.23"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:0.5") - Amount(fromString: "EUR:0.50000001"))
    }
    
    func testMultiplication() {
        XCTAssert(try Amount(fromString: "EUR:2") * 1 == Amount(fromString: "EUR:2"))
        XCTAssert(try Amount(fromString: "EUR:1") * 2 == Amount(fromString: "EUR:2"))
        XCTAssert(try Amount(fromString: "EUR:1.5") * 3 == Amount(fromString: "EUR:4.5"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 0 == Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 1 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 2 == Amount(fromString: "EUR:2.22"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 3 == Amount(fromString: "EUR:3.33"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 4 == Amount(fromString: "EUR:4.44"))
        XCTAssert(try Amount(fromString: "EUR:1.11") * 5 == Amount(fromString: "EUR:5.55"))
        XCTAssert(try Amount(fromString: "EUR:500000000.00000001") * 3 == Amount(fromString: "EUR:1500000000.00000003"))
        XCTAssertThrowsError(try Amount(fromString: "4000000000000000") * 2)
    }
    
    func testDivision() {
        XCTAssert(try Amount(fromString: "EUR:2") / 1 == Amount(fromString: "EUR:2"))
        XCTAssert(try Amount(fromString: "EUR:2") / 2 == Amount(fromString: "EUR:1"))
        XCTAssert(try Amount(fromString: "EUR:4.5") / 3 == Amount(fromString: "EUR:1.5"))
        XCTAssert(try Amount(fromString: "EUR:0") / 5 == Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:1.11") / 1 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:2.22") / 2 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:3.33") / 3 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:4.44") / 4 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:5.55") / 5 == Amount(fromString: "EUR:1.11"))
        XCTAssert(try Amount(fromString: "EUR:1500000000.00000003") / 3 == Amount(fromString: "EUR:500000000.00000001"))
        XCTAssert(try Amount(fromString: "EUR:0.00000003") / 2 == Amount(fromString: "EUR:0.00000001"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:1") / 0)
    }
    
    func testZero() {
        XCTAssert(try Amount(fromString: "EUR") == Amount.zero(currency: "EUR"))
        XCTAssert(Amount.zero(currency: "EUR").isZero)
        XCTAssert(try Amount(fromString: "EUR:0").isZero)
        XCTAssert(try Amount(fromString: "EUR:0.0").isZero)
        XCTAssert(try Amount(fromString: "EUR:0.00000").isZero)
        XCTAssert(try (Amount(fromString: "EUR:1.001") - Amount(fromString: "EUR:1.001")).isZero)
        XCTAssert(try !Amount(fromString: "EUR:0.00000001").isZero)
        XCTAssert(try !Amount(fromString: "EUR:1.0").isZero)
        XCTAssert(try !Amount(fromString: "EUR:0001.0").isZero)
    }
    
    func testComparison() {
        XCTAssert(try Amount(fromString: "EUR:0") <= Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:0") <= Amount(fromString: "EUR:0.00000001"))
        XCTAssert(try Amount(fromString: "EUR:0") < Amount(fromString: "EUR:0.00000001"))
        XCTAssert(try Amount(fromString: "EUR:0") < Amount(fromString: "EUR:1"))
        XCTAssert(try Amount(fromString: "EUR:0") == Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:42") == Amount(fromString: "EUR:42"))
        XCTAssert(try Amount(fromString: "EUR:42.00000001") == Amount(fromString: "EUR:42.00000001"))
        XCTAssert(try Amount(fromString: "EUR:42.00000001") >= Amount(fromString: "EUR:42.00000001"))
        XCTAssert(try Amount(fromString: "EUR:42.00000002") >= Amount(fromString: "EUR:42.00000001"))
        XCTAssert(try Amount(fromString: "EUR:42.00000002") > Amount(fromString: "EUR:42.00000001"))
        XCTAssert(try Amount(fromString: "EUR:0.00000002") > Amount(fromString: "EUR:0.00000001"))
        XCTAssert(try Amount(fromString: "EUR:0.00000001") > Amount(fromString: "EUR:0"))
        XCTAssert(try Amount(fromString: "EUR:2") > Amount(fromString: "EUR:1"))
        XCTAssertThrowsError(try Amount(fromString: "EUR:0.5") < Amount(fromString: "USD:0.50000001"))
    }
}