tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / basic / qa / basic_coverage / test_Currency.bas
blobdb6e0bfe03d9fc4bab7d6335a5991ffdc4567a62
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/.
8 Option Explicit
10 Function doUnitTest() As String
11 TestUtil.TestInit
12 verify_testCurrency
13 doUnitTest = TestUtil.GetResult()
14 End Function
16 Sub verify_testCurrency()
17 On Error GoTo errorHandler
19 Dim x As Currency, y As Currency
21 ' Test division
23 verify_DivideByZero(15.1@, 0@)
24 x = 15.1@
25 y = 0.4@
26 TestUtil.AssertEqual(x / y, 37.75@, x & " / " & y)
27 verify_IDivideByZero(x, y) ' y rounds to 0
28 verify_IModByZero(x, y)
29 y = 0.8@
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
35 x = 9223372036.8@
36 y = x * 59.000
37 x = x * 59.125
38 x = x - y
39 x = x * 20.48
40 x = x / 0.1024
41 x = x * 8
42 x = x * 0.5
43 x = x / 0.001
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])")
51 x = -21474836.48@
52 y = 42949672.96@
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@")
62 ' Comparisons
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])
90 ' Different overflows
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
111 Exit Sub
112 errorHandler:
113 TestUtil.ReportErrorHandler("verify_testCurrency", Err, Error$, Erl)
114 End Sub
116 Sub verify_DivideByZero(x As Currency, y As Currency)
117 On Error GoTo errorHandler
119 x = x / y
120 TestUtil.Assert(False, "verify_DivideByZero", x & " / " & y & ": Division by zero expected")
122 Exit Sub
123 errorHandler:
124 TestUtil.AssertEqual(Err, 11, x & " / " & y & " gave " & Error$)
125 End Sub
127 Sub verify_IDivideByZero(x As Currency, y As Currency)
128 On Error GoTo errorHandler
130 x = x \ y
131 TestUtil.Assert(False, "verify_IDivideByZero", x & " \ " & y & ": Division by zero expected")
133 Exit Sub
134 errorHandler:
135 TestUtil.AssertEqual(Err, 11, x & " \ " & y & " gave " & Error$)
136 End Sub
138 Sub verify_IModByZero(x As Currency, y As Currency)
139 On Error GoTo errorHandler
141 x = x Mod y
142 TestUtil.Assert(False, "verify_IModByZero", x & " Mod " & y & ": Division by zero expected")
144 Exit Sub
145 errorHandler:
146 TestUtil.AssertEqual(Err, 11, x & " Mod " & y & " gave " & Error$)
147 End Sub
149 Sub verify_NegationOverflow(x As Currency)
150 On Error GoTo errorHandler
152 x = -x
153 TestUtil.Assert(False, "verify_NegationOverflow", "-" & x & ": Overflow expected")
155 Exit Sub
156 errorHandler:
157 TestUtil.AssertEqual(Err, 6, "-" & x & " gave " & Error$)
158 End Sub
160 Sub verify_AddOverflow(x As Currency, y As Currency)
161 On Error GoTo errorHandler
163 x = x + y
164 TestUtil.Assert(False, "verify_AddOverflow", x & " + " & y & ": Overflow expected")
166 Exit Sub
167 errorHandler:
168 TestUtil.AssertEqual(Err, 6, x & " + " & y & " gave " & Error$)
169 End Sub
171 Sub verify_SubOverflow(x As Currency, y As Currency)
172 On Error GoTo errorHandler
174 x = x - y
175 TestUtil.Assert(False, "verify_SubOverflow", x & " - " & y & ": Overflow expected")
177 Exit Sub
178 errorHandler:
179 TestUtil.AssertEqual(Err, 6, x & " - " & y & " gave " & Error$)
180 End Sub
182 Sub verify_MulOverflow(x As Currency, y As Currency)
183 On Error GoTo errorHandler
185 x = x * y
186 TestUtil.Assert(False, "verify_MulOverflow", x & " * " & y & ": Overflow expected")
188 Exit Sub
189 errorHandler:
190 TestUtil.AssertEqual(Err, 6, x & " * " & y & " gave " & Error$)
191 End Sub
193 Sub verify_DivOverflow(x As Currency, y As Currency)
194 On Error GoTo errorHandler
196 x = x / y
197 TestUtil.Assert(False, "verify_DivOverflow", x & " / " & y & ": Overflow expected")
199 Exit Sub
200 errorHandler:
201 TestUtil.AssertEqual(Err, 6, x & " / " & y & " gave " & Error$)
202 End Sub