File: test_Currency.bas

package info (click to toggle)
libreoffice 4%3A26.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,833,120 kB
  • sloc: cpp: 4,395,780; xml: 499,109; java: 254,438; python: 81,820; ansic: 33,823; perl: 30,297; javascript: 19,722; sh: 12,050; makefile: 10,854; cs: 8,865; yacc: 8,549; objc: 2,131; lex: 1,385; asm: 1,231; awk: 996; pascal: 914; csh: 20; sed: 5
file content (202 lines) | stat: -rw-r--r-- 8,599 bytes parent folder | download | duplicates (8)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
' This file is part of the LibreOffice project.
'
' This Source Code Form is subject to the terms of the Mozilla Public
' License, v. 2.0. If a copy of the MPL was not distributed with this
' file, You can obtain one at http://mozilla.org/MPL/2.0/.
'

Option Explicit

Function doUnitTest() As String
    TestUtil.TestInit
    verify_testCurrency
    doUnitTest = TestUtil.GetResult()
End Function

Sub verify_testCurrency()
    On Error GoTo errorHandler

    Dim x As Currency, y As Currency

    ' Test division

    verify_DivideByZero(15.1@, 0@)
    x = 15.1@
    y = 0.4@
    TestUtil.AssertEqual(x / y, 37.75@, x & " / " & y)
    verify_IDivideByZero(x, y) ' y rounds to 0
    verify_IModByZero(x, y)
    y = 0.8@
    TestUtil.AssertEqual(x / y, 18.875@, x & " / " & y)
    TestUtil.AssertEqual(x \ y, 15, x & " \ " & y) ' y rounds to 1
    TestUtil.AssertEqual(x Mod y, 0, x & " Mod " & y)

    ' Test various operations; the end result is -922337203685477.5808, i.e. minimal representable value
    x = 9223372036.8@
    y = x * 59.000
    x = x * 59.125
    x = x - y
    x = x * 20.48
    x = x / 0.1024
    x = x * 8
    x = x * 0.5
    x = x / 0.001
    Dim [922337203685477.5807 1] As Currency
    [922337203685477.5807 1] = x + 5477.5807
    TestUtil.AssertEqual(CStr([922337203685477.5807 1]), "922337203685477.5807", "CStr([922337203685477.5807 1])")
    Dim [-922337203685477.5808 1] As Currency
    [-922337203685477.5808 1] = -[922337203685477.5807 1] - 0.0001
    TestUtil.AssertEqual(CStr([-922337203685477.5808 1]), "-922337203685477.5808", "CStr([-922337203685477.5808 1])")

    x = -21474836.48@
    y = 42949672.96@
    Dim [-922337203685477.5808 2] As Currency
    [-922337203685477.5808 2] = x * y
    TestUtil.AssertEqual(CStr([-922337203685477.5808 2]), "-922337203685477.5808", "CStr([-922337203685477.5808 2])")

    ' Check huge literals; FIXME: fails yet, because doubles are stored/read in compiler :(
    ' TestUtil.AssertEqualStrict(922337203685477.5807@, [922337203685477.5807 1], "922337203685477.5807@")
    ' x = 922337203685477.5807@
    ' TestUtil.AssertEqualStrict(x, [922337203685477.5807 1], "x = 922337203685477.5807@")

    ' Comparisons
    TestUtil.Assert([-922337203685477.5808 1] = [-922337203685477.5808 2], [-922337203685477.5808 1] & " = " & [-922337203685477.5808 2])
    TestUtil.Assert(Not([-922337203685477.5808 1] <> [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " <> " & [-922337203685477.5808 2] & ")")
    TestUtil.Assert(Not([-922337203685477.5808 1] < [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " < " & [-922337203685477.5808 2] & ")")
    TestUtil.Assert(Not([-922337203685477.5808 1] > [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " > " & [-922337203685477.5808 2] & ")")
    TestUtil.Assert([-922337203685477.5808 1] <= [-922337203685477.5808 2], [-922337203685477.5808 1] & " <= " & [-922337203685477.5808 2])
    TestUtil.Assert([-922337203685477.5808 1] >= [-922337203685477.5808 2], [-922337203685477.5808 1] & " >= " & [-922337203685477.5808 2])

    TestUtil.Assert(Not([-922337203685477.5808 1] = [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " = " & [922337203685477.5807 1] & ")")
    TestUtil.Assert([-922337203685477.5808 1] <> [922337203685477.5807 1], [-922337203685477.5808 1] & " <> " & [922337203685477.5807 1])
    TestUtil.Assert([-922337203685477.5808 1] < [922337203685477.5807 1], [-922337203685477.5808 1] & " < " & [922337203685477.5807 1])
    TestUtil.Assert(Not([-922337203685477.5808 1] > [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " > " & [922337203685477.5807 1] & ")")
    TestUtil.Assert([-922337203685477.5808 1] <= [922337203685477.5807 1], [-922337203685477.5808 1] & " <= " & [922337203685477.5807 1])
    TestUtil.Assert(Not([-922337203685477.5808 1] >= [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " >= " & [922337203685477.5807 1] & ")")

    ' Two close huge negative values
    TestUtil.Assert(Not([-922337203685477.5808 1] = -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " = " & -[922337203685477.5807 1] & ")")
    TestUtil.Assert([-922337203685477.5808 1] <> -[922337203685477.5807 1], [-922337203685477.5808 1] & " <> " & -[922337203685477.5807 1])
    TestUtil.Assert([-922337203685477.5808 1] < -[922337203685477.5807 1], [-922337203685477.5808 1] & " < " & -[922337203685477.5807 1])
    TestUtil.Assert(Not([-922337203685477.5808 1] > -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " > " & -[922337203685477.5807 1] & ")")
    TestUtil.Assert([-922337203685477.5808 1] <= -[922337203685477.5807 1], [-922337203685477.5808 1] & " <= " & -[922337203685477.5807 1])
    TestUtil.Assert(Not([-922337203685477.5808 1] >= -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " >= " & -[922337203685477.5807 1] & ")")

    TestUtil.AssertEqual([-922337203685477.5808 1] + [922337203685477.5807 1], -0.0001@, [-922337203685477.5808 1] & " + " & [922337203685477.5807 1])

    ' It is not possible to negate -922337203685477.5808, because 922337203685477.5808 is not representable (max is 922337203685477.5807)
    verify_NegationOverflow([-922337203685477.5808 1])

    ' Different overflows
    verify_AddOverflow([922337203685477.5807 1], 0.0001@)
    verify_AddOverflow([-922337203685477.5808 1], -0.0001@)

    verify_SubOverflow([922337203685477.5807 1], -0.0001@)
    verify_SubOverflow([-922337203685477.5808 1], 0.0001@)

    verify_MulOverflow([922337203685477.5807 1], 1.1@)
    verify_MulOverflow([-922337203685477.5808 1], 1.1@)

    verify_DivOverflow([922337203685477.5807 1], 0.1@)
    verify_DivOverflow([-922337203685477.5808 1], 0.1@)

    x = 0.1@ ' Must round to 0 in Not, and complement is -1
    TestUtil.AssertEqual(Not x, -1, "Not " & x)
    x = 0.6@ ' Must round to 1 in Not, and complement is -2
    TestUtil.AssertEqual(Not x, -2, "Not " & x)
    ' TODO: fix compile-time constant operations: rounding is wrong
    TestUtil.AssertEqual(Not 0.1@, -1, "Not 0.1@")
    ' TestUtil.AssertEqual(Not 0.6@, -2, "Not 0.6@") ' Fails, gives -1

    Exit Sub
errorHandler:
    TestUtil.ReportErrorHandler("verify_testCurrency", Err, Error$, Erl)
End Sub

Sub verify_DivideByZero(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x / y
    TestUtil.Assert(False, "verify_DivideByZero", x & " / " & y & ": Division by zero expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 11, x & " / " & y & " gave " & Error$)
End Sub

Sub verify_IDivideByZero(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x \ y
    TestUtil.Assert(False, "verify_IDivideByZero", x & " \ " & y & ": Division by zero expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 11, x & " \ " & y & " gave " & Error$)
End Sub

Sub verify_IModByZero(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x Mod y
    TestUtil.Assert(False, "verify_IModByZero", x & " Mod " & y & ": Division by zero expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 11, x & " Mod " & y & " gave " & Error$)
End Sub

Sub verify_NegationOverflow(x As Currency)
    On Error GoTo errorHandler

    x = -x
    TestUtil.Assert(False, "verify_NegationOverflow", "-" & x & ": Overflow expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 6, "-" & x & " gave " & Error$)
End Sub

Sub verify_AddOverflow(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x + y
    TestUtil.Assert(False, "verify_AddOverflow", x & " + " & y & ": Overflow expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 6, x & " + " & y & " gave " & Error$)
End Sub

Sub verify_SubOverflow(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x - y
    TestUtil.Assert(False, "verify_SubOverflow", x & " - " & y & ": Overflow expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 6, x & " - " & y & " gave " & Error$)
End Sub

Sub verify_MulOverflow(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x * y
    TestUtil.Assert(False, "verify_MulOverflow", x & " * " & y & ": Overflow expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 6, x & " * " & y & " gave " & Error$)
End Sub

Sub verify_DivOverflow(x As Currency, y As Currency)
    On Error GoTo errorHandler

    x = x / y
    TestUtil.Assert(False, "verify_DivOverflow", x & " / " & y & ": Overflow expected")

    Exit Sub
errorHandler:
    TestUtil.AssertEqual(Err, 6, x & " / " & y & " gave " & Error$)
End Sub