1 ' This file is part of the LibreOffice project.
3 ' This Source Code Form is subject to the terms of the Mozilla Public
4 ' License, v. 2.0. If a copy of the MPL was not distributed with this
5 ' file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 Function doUnitTest() As String
13 doUnitTest
= TestUtil
.GetResult()
16 Sub verify_testCurrency()
17 On Error GoTo errorHandler
19 Dim x
As Currency, y
As Currency
23 verify_DivideByZero(15.1@
, 0@
)
26 TestUtil
.AssertEqual(x
/ y
, 37.75@
, x
& " / " & y
)
27 verify_IDivideByZero(x
, y
) ' y rounds to 0
28 verify_IModByZero(x
, y
)
30 TestUtil
.AssertEqual(x
/ y
, 18.875@
, x
& " / " & y
)
31 TestUtil
.AssertEqual(x \ y
, 15, x
& " \ " & y
) ' y rounds to 1
32 TestUtil
.AssertEqual(x Mod y
, 0, x
& " Mod " & y
)
34 ' Test various operations; the end result is -922337203685477.5808, i.e. minimal representable value
44 Dim [922337203685477.5807 1] As Currency
45 [922337203685477.5807 1] = x
+ 5477.5807
46 TestUtil
.AssertEqual(CStr([922337203685477.5807 1]), "922337203685477.5807", "CStr([922337203685477.5807 1])")
47 Dim [-922337203685477.5808 1] As Currency
48 [-922337203685477.5808 1] = -[922337203685477.5807 1] - 0.0001
49 TestUtil
.AssertEqual(CStr([-922337203685477.5808 1]), "-922337203685477.5808", "CStr([-922337203685477.5808 1])")
53 Dim [-922337203685477.5808 2] As Currency
54 [-922337203685477.5808 2] = x
* y
55 TestUtil
.AssertEqual(CStr([-922337203685477.5808 2]), "-922337203685477.5808", "CStr([-922337203685477.5808 2])")
57 ' Check huge literals; FIXME: fails yet, because doubles are stored/read in compiler :(
58 ' TestUtil.AssertEqualStrict(922337203685477.5807@, [922337203685477.5807 1], "922337203685477.5807@")
59 ' x = 922337203685477.5807@
60 ' TestUtil.AssertEqualStrict(x, [922337203685477.5807 1], "x = 922337203685477.5807@")
63 TestUtil
.Assert([-922337203685477.5808 1] = [-922337203685477.5808 2], [-922337203685477.5808 1] & " = " & [-922337203685477.5808 2])
64 TestUtil
.Assert(Not([-922337203685477.5808 1] <> [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " <> " & [-922337203685477.5808 2] & ")")
65 TestUtil
.Assert(Not([-922337203685477.5808 1] < [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " < " & [-922337203685477.5808 2] & ")")
66 TestUtil
.Assert(Not([-922337203685477.5808 1] > [-922337203685477.5808 2]), "Not(" & [-922337203685477.5808 1] & " > " & [-922337203685477.5808 2] & ")")
67 TestUtil
.Assert([-922337203685477.5808 1] <= [-922337203685477.5808 2], [-922337203685477.5808 1] & " <= " & [-922337203685477.5808 2])
68 TestUtil
.Assert([-922337203685477.5808 1] >= [-922337203685477.5808 2], [-922337203685477.5808 1] & " >= " & [-922337203685477.5808 2])
70 TestUtil
.Assert(Not([-922337203685477.5808 1] = [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " = " & [922337203685477.5807 1] & ")")
71 TestUtil
.Assert([-922337203685477.5808 1] <> [922337203685477.5807 1], [-922337203685477.5808 1] & " <> " & [922337203685477.5807 1])
72 TestUtil
.Assert([-922337203685477.5808 1] < [922337203685477.5807 1], [-922337203685477.5808 1] & " < " & [922337203685477.5807 1])
73 TestUtil
.Assert(Not([-922337203685477.5808 1] > [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " > " & [922337203685477.5807 1] & ")")
74 TestUtil
.Assert([-922337203685477.5808 1] <= [922337203685477.5807 1], [-922337203685477.5808 1] & " <= " & [922337203685477.5807 1])
75 TestUtil
.Assert(Not([-922337203685477.5808 1] >= [922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " >= " & [922337203685477.5807 1] & ")")
77 ' Two close huge negative values
78 TestUtil
.Assert(Not([-922337203685477.5808 1] = -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " = " & -[922337203685477.5807 1] & ")")
79 TestUtil
.Assert([-922337203685477.5808 1] <> -[922337203685477.5807 1], [-922337203685477.5808 1] & " <> " & -[922337203685477.5807 1])
80 TestUtil
.Assert([-922337203685477.5808 1] < -[922337203685477.5807 1], [-922337203685477.5808 1] & " < " & -[922337203685477.5807 1])
81 TestUtil
.Assert(Not([-922337203685477.5808 1] > -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " > " & -[922337203685477.5807 1] & ")")
82 TestUtil
.Assert([-922337203685477.5808 1] <= -[922337203685477.5807 1], [-922337203685477.5808 1] & " <= " & -[922337203685477.5807 1])
83 TestUtil
.Assert(Not([-922337203685477.5808 1] >= -[922337203685477.5807 1]), "Not(" & [-922337203685477.5808 1] & " >= " & -[922337203685477.5807 1] & ")")
85 TestUtil
.AssertEqual([-922337203685477.5808 1] + [922337203685477.5807 1], -0.0001@
, [-922337203685477.5808 1] & " + " & [922337203685477.5807 1])
87 ' It is not possible to negate -922337203685477.5808, because 922337203685477.5808 is not representable (max is 922337203685477.5807)
88 verify_NegationOverflow([-922337203685477.5808 1])
91 verify_AddOverflow([922337203685477.5807 1], 0.0001@
)
92 verify_AddOverflow([-922337203685477.5808 1], -0.0001@
)
94 verify_SubOverflow([922337203685477.5807 1], -0.0001@
)
95 verify_SubOverflow([-922337203685477.5808 1], 0.0001@
)
97 verify_MulOverflow([922337203685477.5807 1], 1.1@
)
98 verify_MulOverflow([-922337203685477.5808 1], 1.1@
)
100 verify_DivOverflow([922337203685477.5807 1], 0.1@
)
101 verify_DivOverflow([-922337203685477.5808 1], 0.1@
)
103 x
= 0.1@
' Must round to 0 in Not, and complement is -1
104 TestUtil
.AssertEqual(Not x
, -1, "Not " & x
)
105 x
= 0.6@
' Must round to 1 in Not, and complement is -2
106 TestUtil
.AssertEqual(Not x
, -2, "Not " & x
)
107 ' TODO: fix compile-time constant operations: rounding is wrong
108 TestUtil
.AssertEqual(Not 0.1@
, -1, "Not 0.1@")
109 ' TestUtil.AssertEqual(Not 0.6@, -2, "Not 0.6@") ' Fails, gives -1
113 TestUtil
.ReportErrorHandler("verify_testCurrency", Err
, Error$, Erl
)
116 Sub verify_DivideByZero(x
As Currency, y
As Currency)
117 On Error GoTo errorHandler
120 TestUtil
.Assert(False, "verify_DivideByZero", x
& " / " & y
& ": Division by zero expected")
124 TestUtil
.AssertEqual(Err
, 11, x
& " / " & y
& " gave " & Error$)
127 Sub verify_IDivideByZero(x
As Currency, y
As Currency)
128 On Error GoTo errorHandler
131 TestUtil
.Assert(False, "verify_IDivideByZero", x
& " \ " & y
& ": Division by zero expected")
135 TestUtil
.AssertEqual(Err
, 11, x
& " \ " & y
& " gave " & Error$)
138 Sub verify_IModByZero(x
As Currency, y
As Currency)
139 On Error GoTo errorHandler
142 TestUtil
.Assert(False, "verify_IModByZero", x
& " Mod " & y
& ": Division by zero expected")
146 TestUtil
.AssertEqual(Err
, 11, x
& " Mod " & y
& " gave " & Error$)
149 Sub verify_NegationOverflow(x
As Currency)
150 On Error GoTo errorHandler
153 TestUtil
.Assert(False, "verify_NegationOverflow", "-" & x
& ": Overflow expected")
157 TestUtil
.AssertEqual(Err
, 6, "-" & x
& " gave " & Error$)
160 Sub verify_AddOverflow(x
As Currency, y
As Currency)
161 On Error GoTo errorHandler
164 TestUtil
.Assert(False, "verify_AddOverflow", x
& " + " & y
& ": Overflow expected")
168 TestUtil
.AssertEqual(Err
, 6, x
& " + " & y
& " gave " & Error$)
171 Sub verify_SubOverflow(x
As Currency, y
As Currency)
172 On Error GoTo errorHandler
175 TestUtil
.Assert(False, "verify_SubOverflow", x
& " - " & y
& ": Overflow expected")
179 TestUtil
.AssertEqual(Err
, 6, x
& " - " & y
& " gave " & Error$)
182 Sub verify_MulOverflow(x
As Currency, y
As Currency)
183 On Error GoTo errorHandler
186 TestUtil
.Assert(False, "verify_MulOverflow", x
& " * " & y
& ": Overflow expected")
190 TestUtil
.AssertEqual(Err
, 6, x
& " * " & y
& " gave " & Error$)
193 Sub verify_DivOverflow(x
As Currency, y
As Currency)
194 On Error GoTo errorHandler
197 TestUtil
.Assert(False, "verify_DivOverflow", x
& " / " & y
& ": Overflow expected")
201 TestUtil
.AssertEqual(Err
, 6, x
& " / " & y
& " gave " & Error$)