tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / basic / qa / basic_coverage / test_optional_paramters_compatible.bas
blob78d14eb152eb3cdca2be06eb1998821ddc96aed8
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 Compatible
9 Option Explicit
11 Type testObject
12 testInt As Integer
13 End Type
15 Function doUnitTest() As String
16 TestUtil.TestInit
17 verify_testOptionalsCompatible
18 doUnitTest = TestUtil.GetResult()
19 End Function
21 ' tdf#36737 - Test optionals with different datatypes. In LO Basic
22 ' with option Compatible, optional parameters are allowed with default values.
23 ' Missing optional parameters that don't have explicit default values will
24 ' not be initialized to their default values of its datatype.
25 Sub verify_testOptionalsCompatible()
27 On Error GoTo errorHandler
29 ' optionals with variant datatypes
30 TestUtil.AssertEqual(TestOptVariant(), 123, "TestOptVariant()")
31 TestUtil.AssertEqual(TestOptVariant(123), 246, "TestOptVariant(123)")
32 TestUtil.AssertEqual(TestOptVariant(, 456), 456, "TestOptVariant(, 456)")
33 TestUtil.AssertEqual(TestOptVariant(123, 456), 579, "TestOptVariant(123, 456)")
35 ' optionals with variant datatypes (ByRef and ByVal)
36 TestUtil.AssertEqual(TestOptVariantByRefByVal(), 123, "TestOptVariantByRefByVal()")
37 TestUtil.AssertEqual(TestOptVariantByRefByVal(123), 246, "TestOptVariantByRefByVal(123)")
38 TestUtil.AssertEqual(TestOptVariantByRefByVal(, 456), 456, "TestOptVariantByRefByVal(, 456)")
39 TestUtil.AssertEqual(TestOptVariantByRefByVal(123, 456), 579, "TestOptVariantByRefByVal(123, 456)")
41 ' optionals with double datatypes
42 TestUtil.AssertEqualApprox(TestOptDouble(), 123.4, 1E-5, "TestOptDouble()")
43 TestUtil.AssertEqualApprox(TestOptDouble(123.4), 246.8, 1E-5, "TestOptDouble(123.4)")
44 TestUtil.AssertEqualApprox(TestOptDouble(, 567.8), 567.8, 1E-5, "TestOptDouble(, 567.8)")
45 TestUtil.AssertEqualApprox(TestOptDouble(123.4, 567.8), 691.2, 1E-5, "TestOptDouble(123.4, 567.8)")
47 ' optionals with double datatypes (ByRef and ByVal)
48 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(), 123.4, 1E-5, "TestOptDoubleByRefByVal()")
49 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4), 246.8, 1E-5, "TestOptDoubleByRefByVal(123.4)")
50 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(, 567.8), 567.8, 1E-5, "TestOptDoubleByRefByVal(, 567.8)")
51 TestUtil.AssertEqualApprox(TestOptDoubleByRefByVal(123.4, 567.8), 691.2, 1E-5, "TestOptDoubleByRefByVal(123.4, 567.8)")
53 ' optionals with integer datatypes
54 TestUtil.AssertEqual(TestOptInteger(), 123, "TestOptInteger()")
55 TestUtil.AssertEqual(TestOptInteger(123), 246, "TestOptInteger(123)")
56 TestUtil.AssertEqual(TestOptInteger(, 456), 456, "TestOptInteger(, 456)")
57 TestUtil.AssertEqual(TestOptInteger(123, 456), 579, "TestOptInteger(123, 456)")
59 ' optionals with integer datatypes (ByRef and ByVal)
60 TestUtil.AssertEqual(TestOptIntegerByRefByVal(), 123, "TestOptIntegerByRefByVal()")
61 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123), 246, "TestOptIntegerByRefByVal(123)")
62 TestUtil.AssertEqual(TestOptIntegerByRefByVal(, 456), 456, "TestOptIntegerByRefByVal(, 456)")
63 TestUtil.AssertEqual(TestOptIntegerByRefByVal(123, 456), 579, "TestOptIntegerByRefByVal(123, 456)")
65 ' optionals with string datatypes
66 TestUtil.AssertEqual(TestOptString(), "123", "TestOptString()")
67 TestUtil.AssertEqual(TestOptString("123"), "123123", "TestOptString(""123"")")
68 TestUtil.AssertEqual(TestOptString(, "456"), "456", "TestOptString(, ""456"")")
69 TestUtil.AssertEqual(TestOptString("123", "456"), "123456", "TestOptString(""123"", ""456"")")
71 ' optionals with string datatypes (ByRef and ByVal)
72 TestUtil.AssertEqual(TestOptStringByRefByVal(), "123", "TestOptStringByRefByVal()")
73 TestUtil.AssertEqual(TestOptStringByRefByVal("123"), "123123", "TestOptStringByRefByVal(""123"")")
74 TestUtil.AssertEqual(TestOptStringByRefByVal(, "456"), "456", "TestOptStringByRefByVal(, ""456"")")
75 TestUtil.AssertEqual(TestOptStringByRefByVal("123", "456"), "123456", "TestOptStringByRefByVal(""123"", ""456"")")
77 ' optionals with object datatypes
78 Dim cA As New Collection
79 cA.Add (123)
80 cA.Add (456)
81 Dim cB As New Collection
82 cB.Add (123.4)
83 cB.Add (567.8)
84 TestUtil.AssertEqual(TestOptObject(), 0, "TestOptObject()")
85 TestUtil.AssertEqual(TestOptObject(cA), 579, "TestOptObject(A)")
86 TestUtil.AssertEqualApprox(TestOptObject(, cB), 691.2, 1E-5, "TestOptObject(, B)")
87 TestUtil.AssertEqualApprox(TestOptObject(cA, cB), 1270.2, 1E-5, "TestOptObject(A, B)")
89 ' optionals with object datatypes (ByRef and ByVal)
90 TestUtil.AssertEqual(TestOptObjectByRefByVal(), 0, "TestOptObjectByRefByVal()")
91 TestUtil.AssertEqual(TestOptObjectByRefByVal(cA), 579, "TestOptObjectByRefByVal(A)")
92 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(, cB), 691.2, 1E-5, "TestOptObjectByRefByVal(, B)")
93 TestUtil.AssertEqualApprox(TestOptObjectByRefByVal(cA, cB), 1270.2, 1E-5, "TestOptObjectByRefByVal(A, B)")
95 ' optionals with array datatypes
96 Dim aA(0 To 1) As Integer
97 aA(0) = 123
98 aA(1) = 456
99 Dim aB(0 To 1) As Variant
100 aB(0) = 123.4
101 aB(1) = 567.8
102 TestUtil.AssertEqual(TestOptArray(), 0, "TestOptArray()")
103 TestUtil.AssertEqual(TestOptArray(aA), 579, "TestOptArray(A)")
104 TestUtil.AssertEqualApprox(TestOptArray(, aB), 691.2, 1E-5, "TestOptArray(, B)")
105 TestUtil.AssertEqualApprox(TestOptArray(aA, aB), 1270.2, 1E-5, "TestOptArray(A, B)")
107 ' optionals with array datatypes (ByRef and ByVal)
108 TestUtil.AssertEqual(TestOptArrayByRefByVal(), 0, "TestOptArrayByRefByVal()")
109 TestUtil.AssertEqual(TestOptArrayByRefByVal(aA), 579, "TestOptArrayByRefByVal(A)")
110 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(, aB), 691.2, 1E-5, "TestOptArrayByRefByVal(, B)")
111 TestUtil.AssertEqualApprox(TestOptArrayByRefByVal(aA, aB), 1270.2, 1E-5, "TestOptArrayByRefByVal(A, B)")
113 ' tdf#144353 - error handling of missing optional parameters (arithmetic operator)
114 ' Without the fix in place, this test would have failed with:
115 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
116 ' - Actual : 549 (Actual value of the variable)
117 TestUtil.AssertEqual(TestArithmeticOperator, 449, "TestArithmeticOperator")
119 ' tdf#144353 - error handling of missing optional parameters (unary operator)
120 ' Without the fix in place, this test would have failed with:
121 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
122 ' - Actual : 100 (Actual value of the variable)
123 TestUtil.AssertEqual(TestUnaryOperator, 449, "TestUnaryOperator")
125 ' tdf#144353 - error handling of missing optional parameters (assigning to a collection)
126 ' Without the fix in place, this test would have failed with:
127 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
128 ' - Actual : 549 (Actual value of the variable)
129 TestUtil.AssertEqual(TestCollection, 449, "TestCollection")
131 ' tdf#144353 - error handling of missing optional parameters (assigning to an object)
132 ' Without the fix in place, this test would have failed with:
133 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
134 ' - Actual : 448 (Actual value of the variable)
135 TestUtil.AssertEqual(TestObjectError, 449, "TestObjectError")
137 ' tdf#151503 - error handling of missing optional parameters (boolean operations)
138 ' Without the fix in place, this test would have failed with:
139 ' - Expected: 449 (ERRCODE_BASIC_NOT_OPTIONAL - Argument not optional)
140 ' - Actual : 0 (No error code since a missing parameter evaluates to true)
141 TestUtil.AssertEqual(TestBooleanOperations, 449, "TestBooleanOperations")
143 Exit Sub
144 errorHandler:
145 TestUtil.ReportErrorHandler("verify_testOptionalsCompatible", Err, Error$, Erl)
146 End Sub
148 Function TestOptVariant(Optional A, Optional B As Variant = 123)
149 TestOptVariant = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
150 End Function
152 Function TestOptVariantByRefByVal(Optional ByRef A, Optional ByVal B As Variant = 123)
153 TestOptVariantByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
154 End Function
156 Function TestOptDouble(Optional A As Double, Optional B As Double = 123.4)
157 TestOptDouble = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
158 End Function
160 Function TestOptDoubleByRefByVal(Optional ByRef A As Double, Optional ByVal B As Double = 123.4)
161 TestOptDoubleByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
162 End Function
164 Function TestOptInteger(Optional A As Integer, Optional B As Integer = 123)
165 TestOptInteger = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
166 End Function
168 Function TestOptIntegerByRefByVal(Optional ByRef A As Integer, Optional ByVal B As Integer = 123)
169 TestOptIntegerByRefByVal = OptNumberSum(IsMissing(A), A, IsMissing(B), B)
170 End Function
172 Function TestOptString(Optional A As String, Optional B As String = "123")
173 TestOptString = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
174 End Function
176 Function TestOptStringByRefByVal(Optional ByRef A As String, Optional ByVal B As String = "123")
177 TestOptStringByRefByVal = OptStringConcat(IsMissing(A), A, IsMissing(B), B)
178 End Function
180 Function TestOptObject(Optional A As Collection, Optional B As Collection)
181 TestOptObject = 0
182 If Not IsMissing(A) Then TestOptObject = CollectionSum(A)
183 If Not IsMissing(B) Then TestOptObject = TestOptObject + CollectionSum(B)
184 End Function
186 Function TestOptObjectByRefByVal(Optional ByRef A As Collection, Optional ByVal B As Collection)
187 TestOptObjectByRefByVal = 0
188 If Not IsMissing(A) Then TestOptObjectByRefByVal = CollectionSum(A)
189 If Not IsMissing(B) Then TestOptObjectByRefByVal = TestOptObjectByRefByVal + CollectionSum(B)
190 End Function
192 Function TestOptArray(Optional A() As Integer, Optional B() As Variant)
193 TestOptArray = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
194 End Function
196 Function TestOptArrayByRefByVal(Optional ByRef A() As Integer, Optional ByVal B() As Variant)
197 TestOptArrayByRefByVal = ArraySum(IsMissing(A), A) + ArraySum(IsMissing(B), B)
198 End Function
200 Function OptNumberSum(is_missingA As Boolean, A, is_missingB As Boolean, B)
201 OptNumberSum = 0
202 If Not is_missingA Then OptNumberSum = A
203 If Not is_missingB Then OptNumberSum = OptNumberSum + B
204 End Function
206 Function OptStringConcat(is_missingA As Boolean, A, is_missingB As Boolean, B)
207 OptStringConcat = ""
208 If Not is_missingA Then OptStringConcat = A
209 If Not is_missingB Then OptStringConcat = OptStringConcat & B
210 End Function
212 Function TestArithmeticOperator(Optional optInt)
213 On Error GoTo errorHandler
214 optInt = optInt + 100
215 TestArithmeticOperator = optInt
216 errorHandler:
217 TestArithmeticOperator = Err()
218 End Function
220 Function TestUnaryOperator(Optional optInt)
221 On Error GoTo errorHandler
222 If (Not optInt) Then optInt = 100
223 TestUnaryOperator = optInt
224 errorHandler:
225 TestUnaryOperator = Err()
226 End Function
228 Function TestCollection(Optional optInt)
229 On Error GoTo errorHandler
230 Dim cA As New Collection
231 cA.Add(optInt)
232 TestCollection = cA.Item(1) + 100
233 errorHandler:
234 TestCollection = Err()
235 End Function
237 Function TestObjectError(Optional optInt)
238 On Error GoTo errorHandler
239 Dim aTestObject As Variant
240 aTestObject = CreateObject("testObject")
241 aTestObject.testInt = optInt
242 TestObjectError = optInt
243 errorHandler:
244 TestObjectError = Err()
245 End Function
247 Function TestBooleanOperations(Optional optBool As Boolean)
248 On Error GoTo errorHandler
249 if optBool then
250 TestBooleanOperations = 0
251 end if
252 errorHandler:
253 TestBooleanOperations = Err()
254 End Function
256 Function CollectionSum(C)
257 Dim idx As Integer
258 CollectionSum = 0
259 For idx = 1 To C.Count
260 CollectionSum = CollectionSum + C.Item(idx)
261 Next idx
262 End Function
264 Function ArraySum(is_missingC As Boolean, C)
265 Dim idx As Integer
266 ArraySum = 0
267 If Not is_missingC Then
268 For idx = LBound(C) To UBound(C)
269 ArraySum = ArraySum + C(idx)
270 Next idx
271 End If
272 End Function