-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTests.xojo_code
More file actions
616 lines (469 loc) · 15.7 KB
/
UnitTests.xojo_code
File metadata and controls
616 lines (469 loc) · 15.7 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#tag Module
Protected Module UnitTests
#tag CompatibilityFlags = TargetHasGUI
#tag Method, Flags = &h1, CompatibilityFlags = TargetHasGUI
Protected Sub DetailedErrorIf(errCondition As Boolean, details As String)
// Unit testing code calls this function to check if an error has
// occurred. If so, report it to the user and then break into
// the debugger so he can do something about it.
if not errCondition then return
MainWindow.Print "Unit test failure: " + details
MessageBox( "Unit test failure." + EndOfLine + EndOfLine + details )
break // OK, now look at the stack to see what went wrong!
End Sub
#tag EndMethod
#tag Method, Flags = &h1, CompatibilityFlags = TargetHasGUI
Protected Sub ErrorIf(errCondition As Boolean)
// Unit testing code calls this function to check if an error has
// occurred. If so, report it to the user and then break into
// the debugger so he can do something about it.
if not errCondition then return
Var msg As String
msg = "Unit test failure."
#if DebugBuild
msg = msg + EndOfLine + EndOfLine _
+ "Click OK to drop into the debugger and examine the stack."
#endif
MainWindow.Print msg
MessageBox( msg )
break // OK, now look at the stack to see what went wrong!
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub ErrorIfNotEqual(arr1() As Double, arr2() As Double)
if arr1.LastIndex <> arr2.LastIndex then
ErrorIf true
return
end if
Var i As Integer
for i = 0 to arr1.LastIndex
ErrorIf arr1(i) <> arr2(i)
next
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub ErrorIfNotEqual(actual As Double, expected As Double)
DetailedErrorIf Abs(actual - expected) > 1E-12, _
"Expected " + str(expected) + ", got " + str(actual) + "."
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub ErrorIfNotEqual(arr1() As Integer, arr2() As Integer)
if arr1.LastIndex <> arr2.LastIndex then
ErrorIf true
return
end if
Var i As Integer
for i = 0 to arr1.LastIndex
ErrorIf arr1(i) <> arr2(i)
next
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub ErrorIfNotEqual(actual As Integer, expected As Integer)
DetailedErrorIf actual <> expected, "Expected " + str(expected) + ", got " + str(actual) + "."
End Sub
#tag EndMethod
#tag Method, Flags = &h1
Protected Sub ErrorIfNotEqual(arr1() As String, arr2() As String)
if arr1.LastIndex <> arr2.LastIndex then
ErrorIf true
return
end if
Var i As Integer
for i = 0 to arr1.LastIndex
ErrorIf arr1(i) <> arr2(i)
next
End Sub
#tag EndMethod
#tag Method, Flags = &h1, CompatibilityFlags = TargetHasGUI
Protected Sub Run()
// Run all our unit tests.
TestAdd
TestAverage
TestConcat
TestClone
TestMaxAndMin
TestRemoveSlice
TestReverse
TestSlice
TestSplice
TestSum
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestAdd()
Var iempty() As Integer
Var ia() As Integer = Array( 1, 2, 3 )
Var ib() As Integer = Array( 4, 5 )
Var itest() As Integer
itest = iempty.Clone
itest.Add iempty
ErrorIfNotEqual itest, iempty
itest = ia.Clone
itest.Add iempty
ErrorIfNotEqual itest, ia
itest = iempty.Clone
itest.Add ia
ErrorIfNotEqual itest, ia
itest = ia.Clone
itest.Add( ib )
ErrorIfNotEqual itest, Array( 1, 2, 3, 4, 5 )
Var dempty() As Double
Var da() As Double = Array( 1.1, 2.2, 3.3 )
Var db() As Double = Array( 4.4, 5.5 )
Var dtest() As Double
dtest = dempty.Clone
dtest.Add( dempty )
ErrorIfNotEqual dtest, dempty
dtest = da.Clone
dtest.Add( dempty )
ErrorIfNotEqual dtest, da
dtest = dempty.Clone
dtest.Add( da )
ErrorIfNotEqual dtest, da
dtest = da.Clone
dtest.Add( db )
ErrorIfNotEqual dtest, Array( 1.1, 2.2, 3.3, 4.4, 5.5 )
Var sempty() As String
Var sa() As String = Array( "a", "b", "c" )
Var sb() As String = Array( "d", "e" )
Var stest() As String
stest = sempty.Clone
stest.Add( sempty )
ErrorIfNotEqual stest, sempty
stest = sa.Clone
stest.Add( sempty )
ErrorIfNotEqual stest, sa
stest = sempty.Clone
stest.Add( sa )
ErrorIfNotEqual stest, sa
stest = sa.Clone
stest.Add( sb )
ErrorIfNotEqual stest, Array( "a", "b", "c", "d", "e" )
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestAverage()
Var iarr() As Integer
ErrorIfNotEqual iarr.Average, 0.0
iarr = Array(42)
ErrorIfNotEqual iarr.Average, 42.0
iarr = Array(-5, 5, 3, -3)
ErrorIfNotEqual iarr.Average, 0.0
iarr = Array(3, 2)
ErrorIfNotEqual iarr.Average, 2.5
Var darr() As Double
ErrorIfNotEqual darr.Average, 0.0
darr = Array(40.2)
ErrorIfNotEqual darr.Average, 40.2
darr = Array(-4.9, 5.1, 3.1, -2.9)
ErrorIfNotEqual darr.Average, 0.1
darr = Array(3.5, 2.5)
ErrorIfNotEqual darr.Average, 3.0
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestClone()
Var ia() As Integer = Array( 1, 3, 5 )
Var ib() As Integer = ia.Clone
ErrorIfNotEqual ia, ib
ia(0) = 42
ErrorIf ib(0) = 42
Var da() As Double = Array( 1.1, 3.2, 5.3 )
Var db() As Double = da.Clone
ErrorIfNotEqual da, db
da(0) = 42
ErrorIf db(0) = 42
Var sa() As String = Array( "a", "b", "c" )
Var sb() As String = sa.Clone
ErrorIfNotEqual sa, sb
sa(0) = "foo"
ErrorIf sb(0) = "foo"
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestConcat()
Var iempty() As Integer
Var ia() As Integer = Array( 1, 2, 3 )
Var ib() As Integer = Array( 4, 5 )
ErrorIfNotEqual iempty.Concat( iempty ), iempty
ErrorIfNotEqual ia.Concat( iempty ), ia
ErrorIfNotEqual iempty.Concat( ia ), ia
ErrorIfNotEqual ia.Concat( ib ), Array( 1, 2, 3, 4, 5 )
Var dempty() As Double
Var da() As Double = Array( 1.1, 2.2, 3.3 )
Var db() As Double = Array( 4.4, 5.5 )
ErrorIfNotEqual dempty.Concat( dempty ), dempty
ErrorIfNotEqual da.Concat( dempty ), da
ErrorIfNotEqual dempty.Concat( da ), da
ErrorIfNotEqual da.Concat( db ), Array( 1.1, 2.2, 3.3, 4.4, 5.5 )
Var sempty() As String
Var sa() As String = Array( "1", "2", "3" )
Var sb() As String = Array( "4", "5" )
ErrorIfNotEqual sempty.Concat( sempty ), sempty
ErrorIfNotEqual sa.Concat( sempty ), sa
ErrorIfNotEqual sempty.Concat( sa ), sa
ErrorIfNotEqual sa.Concat( sb ), Array( "1", "2", "3", "4", "5" )
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestMaxAndMin()
Var iarr() As Integer
ErrorIfNotEqual iarr.Max, 0
ErrorIfNotEqual iarr.Min, 0
iarr = Array(42)
ErrorIfNotEqual iarr.Max, 42
ErrorIfNotEqual iarr.Min, 42
iarr = Array(-1, 2, -3, 4, 5)
ErrorIfNotEqual iarr.Max, 5
ErrorIfNotEqual iarr.Min, -3
Var darr() As Double
ErrorIfNotEqual darr.Max, 0.0
ErrorIfNotEqual darr.Min, 0.0
darr = Array(42.0)
ErrorIfNotEqual darr.Max, 42.0
ErrorIfNotEqual darr.Min, 42.0
darr = Array(-1.1, 2.2, -3.3, 4.4, 5.5)
ErrorIfNotEqual darr.Max, 5.5
ErrorIfNotEqual darr.Min, -3.3
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestRemoveSlice()
// Integers...
Var ia() As Integer = Array( 10, 11, 12, 13, 14, 15 )
Var ib() As Integer = ia.Clone
ib.RemoveSlice 1,4 // remove items 1 through 3
ErrorIfNotEqual ib, Array( 10, 14, 15 )
ib = ia.Clone
ib.RemoveSlice 3 // remove from 3 to the end
ErrorIfNotEqual ib, Array( 10, 11, 12 )
ib = ia.Clone
ib.RemoveSlice -2 // remove the last 2 items
ErrorIfNotEqual ib, Array( 10, 11, 12, 13 )
ib = ia.Clone
ib.RemoveSlice -4, -1 // remove the 4th through 2nd items from the end
ErrorIfNotEqual ib, Array( 10, 11, 15 )
ib = ia.Clone
ib.RemoveSlice 0, 1 // remove just the 1st item
ErrorIfNotEqual ib, Array( 11, 12, 13, 14, 15 )
ib = ia.Clone
ib.RemoveSlice -1 // remove just the last item
ErrorIfNotEqual ib, Array( 10, 11, 12, 13, 14 )
ib = ia.Clone
ib.RemoveSlice 2, -3 // remove just item 2 (specified two ways)
ErrorIfNotEqual ib, Array( 10, 11, 13, 14, 15 )
// Strings...
Var sa() As String = Array( "10", "11", "12", "13", "14", "15" )
Var sb() As String = sa.Clone
sb.RemoveSlice 1,4 // remove items 1 through 3
ErrorIfNotEqual sb, Array( "10", "14", "15" )
sb = sa.Clone
sb.RemoveSlice 3 // remove from 3 to the end
ErrorIfNotEqual sb, Array( "10", "11", "12" )
sb = sa.Clone
sb.RemoveSlice -2 // remove the last 2 items
ErrorIfNotEqual sb, Array( "10", "11", "12", "13" )
sb = sa.Clone
sb.RemoveSlice -4, -1 // remove the 4th through 2nd items from the end
ErrorIfNotEqual sb, Array( "10", "11", "15" )
sb = sa.Clone
sb.RemoveSlice 0, 1 // remove just the 1st item
ErrorIfNotEqual sb, Array( "11", "12", "13", "14", "15" )
sb = sa.Clone
sb.RemoveSlice -1 // remove just the last item
ErrorIfNotEqual sb, Array( "10", "11", "12", "13", "14" )
sb = sa.Clone
sb.RemoveSlice 2, -3 // remove just item 2 (specified two ways)
ErrorIfNotEqual sb, Array( "10", "11", "13", "14", "15" )
// Doubles...
Var da() As Double = Array( 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 )
Var db() As Double = da.Clone
db.RemoveSlice 1,4 // remove items 1 through 3
ErrorIfNotEqual db, Array( 1.0, 1.4, 1.5 )
db = da.Clone
db.RemoveSlice 3 // remove from 3 to the end
ErrorIfNotEqual db, Array( 1.0, 1.1, 1.2 )
db = da.Clone
db.RemoveSlice -2 // remove the last 2 items
ErrorIfNotEqual db, Array( 1.0, 1.1, 1.2, 1.3 )
db = da.Clone
db.RemoveSlice -4, -1 // remove the 4th through 2nd items from the end
ErrorIfNotEqual db, Array( 1.0, 1.1, 1.5 )
db = da.Clone
db.RemoveSlice 0, 1 // remove just the 1st item
ErrorIfNotEqual db, Array( 1.1, 1.2, 1.3, 1.4, 1.5 )
db = da.Clone
db.RemoveSlice -1 // remove just the last item
ErrorIfNotEqual db, Array( 1.0, 1.1, 1.2, 1.3, 1.4 )
db = da.Clone
db.RemoveSlice 2, -3 // remove just item 2 (specified two ways)
ErrorIfNotEqual db, Array( 1.0, 1.1, 1.3, 1.4, 1.5 )
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestReverse()
Var sa() As String = Array( "a" )
sa.Reverse
ErrorIfNotEqual sa, Array( "a" )
sa() = Array( "a", "b" )
sa.Reverse
ErrorIfNotEqual sa, Array( "b", "a" )
sa() = Array( "a", "b", "c" )
sa.Reverse
ErrorIfNotEqual sa, Array( "c", "b", "a" )
sa() = Array( "a", "b", "c", "d" )
sa.Reverse
ErrorIfNotEqual sa, Array( "d", "c", "b", "a" )
Var da() As Double = Array( 1.1 )
da.Reverse
ErrorIfNotEqual da, Array( 1.1 )
da() = Array( 1.1, 2.2 )
da.Reverse
ErrorIfNotEqual da, Array( 2.2, 1.1 )
da() = Array( 1.1, 2.2, 3.3 )
da.Reverse
ErrorIfNotEqual da, Array( 3.3, 2.2, 1.1 )
da() = Array( 1.1, 2.2, 3.3, 4.4 )
da.Reverse
ErrorIfNotEqual da, Array( 4.4, 3.3, 2.2, 1.1 )
Var ia() As Integer = Array( 1 )
ia.Reverse
ErrorIfNotEqual ia, Array( 1 )
ia() = Array( 1, 2 )
ia.Reverse
ErrorIfNotEqual ia, Array( 2, 1 )
ia() = Array( 1, 2, 3 )
ia.Reverse
ErrorIfNotEqual ia, Array( 3, 2, 1 )
ia() = Array( 1, 2, 3, 4 )
ia.Reverse
ErrorIfNotEqual ia, Array( 4, 3, 2, 1 )
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestSlice()
Var ia() As Integer = Array( 1, 2, 3, 4, 5 )
ErrorIfNotEqual ia.Slice(2), Array( 3, 4, 5 )
ErrorIfNotEqual ia.Slice(-2), Array( 4, 5 )
ErrorIfNotEqual ia.Slice(0, 2), Array( 1, 2 )
ErrorIfNotEqual ia.Slice, ia
ErrorIfNotEqual ia.Slice(0, -3), Array( 1, 2 )
Var da() As Double = Array( 1.1, 2.2, 3.3, 4.4, 5.5 )
ErrorIfNotEqual da.Slice(2), Array( 3.3, 4.4, 5.5 )
ErrorIfNotEqual da.Slice(-2), Array( 4.4, 5.5 )
ErrorIfNotEqual da.Slice(0, 2), Array( 1.1, 2.2 )
ErrorIfNotEqual da.Slice, da
ErrorIfNotEqual da.Slice(0, -3), Array( 1.1, 2.2 )
Var sa() As String = Array( "a", "b", "c", "d", "e" )
ErrorIfNotEqual sa.Slice(2), Array( "c", "d", "e" )
ErrorIfNotEqual sa.Slice(-2), Array( "d", "e" )
ErrorIfNotEqual sa.Slice(0, 2), Array( "a", "b" )
ErrorIfNotEqual sa.Slice, sa
ErrorIfNotEqual sa.Slice(0, -3), Array( "a", "b" )
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestSplice()
Var ia() As Integer = Array(0,1,2,3,4,5)
Var ib() As Integer = Array(10,11,12)
ia.Splice 2,4, ib
ErrorIfNotEqual ia, Array(0, 1, 10, 11, 12, 4, 5)
ia = Array(0,1,2,3,4,5)
ia.Splice 2,4, ib, -2
ErrorIfNotEqual ia, Array(0, 1, 11, 12, 4, 5)
ia = Array(0,1,2,3,4,5)
ia.Splice 2,3, ib
ErrorIfNotEqual ia, Array(0, 1, 10, 11, 12, 3, 4, 5)
ia = Array(0,1,2,3,4,5)
ia.Splice 2,2, ib
ErrorIfNotEqual ia, Array(0, 1, 10, 11, 12, 2, 3, 4, 5)
Var da() As Double = Array(0.0,1,2,3,4,5)
Var db() As Double = Array(1.0,1.1,1.2)
da.Splice 2,4, db
ErrorIfNotEqual da, Array(0, 1, 1.0, 1.1, 1.2, 4, 5)
da = Array(0.0,1,2,3,4,5)
da.Splice 2,4, db, -2
ErrorIfNotEqual da, Array(0, 1, 1.1, 1.2, 4, 5)
da = Array(0.0,1,2,3,4,5)
da.Splice 2,3, db
ErrorIfNotEqual da, Array(0, 1, 1.0, 1.1, 1.2, 3, 4, 5)
da = Array(0.0,1,2,3,4,5)
da.Splice 2,2, db
ErrorIfNotEqual da, Array(0, 1, 1.0, 1.1, 1.2, 2, 3, 4, 5)
Var sa() As String = Array("a","b","c","d","e","f")
Var sb() As String = Array("foo","bar","baz")
sa.Splice 2,4, sb
ErrorIfNotEqual sa, Array("a", "b", "foo", "bar", "baz", "e", "f")
sa = Array("a","b","c","d","e","f")
sa.Splice 2,4, sb, -2
ErrorIfNotEqual sa, Array("a", "b", "bar", "baz", "e", "f")
sa = Array("a","b","c","d","e","f")
sa.Splice 2,3, sb
ErrorIfNotEqual sa, Array("a", "b", "foo", "bar", "baz", "d", "e", "f")
sa = Array("a","b","c","d","e","f")
sa.Splice 2,2, sb
ErrorIfNotEqual sa, Array("a", "b", "foo", "bar", "baz", "c", "d", "e", "f")
End Sub
#tag EndMethod
#tag Method, Flags = &h21
Private Sub TestSum()
Var iarr() As Integer
ErrorIfNotEqual iarr.Total, 0
iarr = Array(42)
ErrorIfNotEqual iarr.Total, 42
iarr = Array(-1, 2, -3, 4, 5)
ErrorIfNotEqual iarr.Total, 7
Var darr() As Double
ErrorIfNotEqual darr.Total, 0.0
darr = Array(42.0)
ErrorIfNotEqual darr.Total, 42.0
darr = Array(-1.1, 2.2, -3.3, 4.4, 5.5)
ErrorIfNotEqual darr.Total, 7.7
End Sub
#tag EndMethod
#tag ViewBehavior
#tag ViewProperty
Name="Name"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Index"
Visible=true
Group="ID"
InitialValue="-2147483648"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Super"
Visible=true
Group="ID"
InitialValue=""
Type="String"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Left"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag ViewProperty
Name="Top"
Visible=true
Group="Position"
InitialValue="0"
Type="Integer"
EditorType=""
#tag EndViewProperty
#tag EndViewBehavior
End Module
#tag EndModule