-
Notifications
You must be signed in to change notification settings - Fork 11
/
CollectionsTesting.ns
1012 lines (885 loc) · 27.6 KB
/
CollectionsTesting.ns
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Newspeak3
'NSCollections'
class CollectionsTesting usingCollections: c minitest: m = (|
private TestContext = m TestContext.
private List = c List.
private Map = c Map.
private Set = c Set.
|) (
public class CollectionTests = TestContext () (
public testArrayList = (
| list = List new. |
assert: list size equals: 0.
assert: list isEmpty.
should: [list at: 0] signal: rangeError.
should: [list at: -1] signal: rangeError.
list addLast: 10.
assert: list size equals: 1.
deny: list isEmpty.
should: [list at: 0] signal: rangeError.
assert: (list at: 1) equals: 10.
should: [list at: 2] signal: rangeError.
list addFirst: 20.
assert: list size equals: 2.
should: [list at: 0] signal: rangeError.
assert: (list at: 1) equals: 20.
assert: (list at: 2) equals: 10.
should: [list at: 3] signal: rangeError.
list include: 20.
assert: list size equals: 2.
should: [list at: 0] signal: rangeError.
assert: (list at: 1) equals: 20.
assert: (list at: 2) equals: 10.
should: [list at: 3] signal: rangeError.
assert: (list at: 2 put: 30) equals: 30.
should: [list at: 0 put: nil] signal: rangeError.
should: [list at: 3 put: nil] signal: rangeError.
assert: (list includes: 30).
deny: (list includes: 10).
assert: list removeLast equals: 30.
assert: list size equals: 1.
should: [list at: 0] signal: rangeError.
assert: (list at: 1) equals: 20.
should: [list at: 2] signal: rangeError.
)
public testHashMap = (
| map = Map new. |
assert: map size equals: 0.
assert: map isEmpty.
should: [map at: 'A'] signal: rangeError.
assert: (map at: 'A' put: 'AA') equals: 'AA'.
assert: (map at: 'B' put: 'BB') equals: 'BB'.
assert: (map at: 'C' put: 'CC') equals: 'CC'.
assert: map size equals: 3.
deny: map isEmpty.
assert: (map at: 'A') equals: 'AA'.
assert: (map at: 'B') equals: 'BB'.
assert: (map at: 'C') equals: 'CC'.
should: [map at: 'D'] signal: rangeError.
assert: (map at: 'D' ifAbsent: ['None']) equals: 'None'.
deny: (map includesKey: 'D').
assert: (map at: 'D' ifAbsentPut: ['DD']) equals: 'DD'.
assert: (map at: 'D' ifAbsentPut: [fail]) equals: 'DD'.
assert: (map includesKey: 'D').
assert: (map removeKey: 'B') equals: 'BB'.
should: [map removeKey: 'B'] signal: rangeError.
assert: (map removeKey: 'B' ifAbsent: ['None']) equals: 'None'.
assert: (map at: 'A' put: 'newAA') equals: 'newAA'.
assert: (map at: 'A') equals: 'newAA'.
assert: map size equals: 3.
)
public testHashMap2 = (
| map = Map new. |
map at: 'A' put: 'newAA'.
map at: 'C' put: 'CCC'.
map at: 'D' ifAbsentPut: ['DD'].
assert: [map values isKindOfList].
assert: [map values size = 3].
assert: [map keys size = 3].
assert: [map keys isKindOfSet].
assert: [map includes: 'newAA'].
assert: [map includes: 'CCC'].
assert: [map includes: 'DD'].
assert: [(map includes: 'B') not].
map do: [:e | assert: [e size > 1]].
assert: (map detect: [:e | e = 'DD']) equals: 'DD'.
)
public testHashSet = (
| set = Set new. |
assert: set size equals: 0.
assert: set isEmpty.
should: [set at: 0] signal: rangeError.
assert: (set add: 2) equals: 2.
assert: set size equals: 1.
deny: set isEmpty.
assert: (set add: 2) equals: 2.
assert: set size equals: 1.
set add: 4; include: 6; add: 8.
set addAll: {1. 2. 3. 4}.
assert: set size equals: 6.
assert: (set remove: 3) equals: 3.
assert: set size equals: 5.
set removeAllSuchThat: [:e | e \\ 2 = 0].
assert: set size equals: 1.
)
public testIsKindOfCollection = (
assert: {} isKindOfCollection.
assert: (Array new: 0) isKindOfCollection.
assert: List new isKindOfCollection.
assert: Map new isKindOfCollection.
assert: Set new isKindOfCollection.
deny: nil isKindOfCollection.
deny: false isKindOfCollection.
deny: true isKindOfCollection.
deny: 0 isKindOfCollection.
)
) : (
TEST_CONTEXT = ()
)
public class ListTests = TestContext () (
public testIsKindOfList = (
deny: {} isKindOfList.
deny: (Array new: 0) isKindOfList.
assert: List new isKindOfList.
deny: Map new isKindOfList.
deny: Set new isKindOfList.
deny: nil isKindOfList.
deny: false isKindOfList.
deny: true isKindOfList.
deny: 0 isKindOfList.
)
public testListAdd = (
| list = List new: 0. |
assert: list size equals: 0.
list add: 'apple'.
assert: list size equals: 1.
assert: (list at: 1) equals: 'apple'.
list add: 'banana'.
assert: list size equals: 2.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
list add: 'orange'.
assert: list size equals: 3.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
list add: 'strawberry'.
assert: list size equals: 4.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
assert: (list at: 4) equals: 'strawberry'.
list add: 'watermelon'.
assert: list size equals: 5.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
assert: (list at: 4) equals: 'strawberry'.
assert: (list at: 5) equals: 'watermelon'.
)
public testListAddAll = (
| list = List new: 0. |
assert: list size equals: 0.
list addAll: {'apple'. 'banana'}.
assert: list size equals: 2.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
list addAll: {}.
assert: list size equals: 2.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
list addAll: {'orange'. 'strawberry'. 'watermelon'}.
assert: list size equals: 5.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
assert: (list at: 4) equals: 'strawberry'.
assert: (list at: 5) equals: 'watermelon'.
)
public testListAddFirst = (
| list = List new: 0. |
assert: list size equals: 0.
list addFirst: 'apple'.
assert: list size equals: 1.
assert: (list at: 1) equals: 'apple'.
list addFirst: 'banana'.
assert: list size equals: 2.
assert: (list at: 1) equals: 'banana'.
assert: (list at: 2) equals: 'apple'.
list addFirst: 'orange'.
assert: list size equals: 3.
assert: (list at: 1) equals: 'orange'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'apple'.
list addFirst: 'strawberry'.
assert: list size equals: 4.
assert: (list at: 1) equals: 'strawberry'.
assert: (list at: 2) equals: 'orange'.
assert: (list at: 3) equals: 'banana'.
assert: (list at: 4) equals: 'apple'.
list addFirst: 'watermelon'.
assert: list size equals: 5.
assert: (list at: 1) equals: 'watermelon'.
assert: (list at: 2) equals: 'strawberry'.
assert: (list at: 3) equals: 'orange'.
assert: (list at: 4) equals: 'banana'.
assert: (list at: 5) equals: 'apple'.
)
public testListAsArray = (
| list = List new. array |
list add: 'blueberry'.
list add: 'blackberry'.
array:: list asArray.
assert: array isKindOfArray.
assert: array size equals: 2.
assert: (array at: 1) equals: 'blueberry'.
assert: (array at: 2) equals: 'blackberry'.
array at: 1 put: 'cantalope'.
array at: 2 put: 'watermelon'.
assert: (list at: 1) equals: 'blueberry'.
assert: (list at: 2) equals: 'blackberry'.
)
public testListAt = (
| list empty |
list:: List new.
list add: 'apple'.
list add: 'banana'.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
should: [list at: 0] signal: rangeError.
should: [list at: 3] signal: rangeError.
should: [list at: -3] signal: rangeError.
should: [list at: nil] signal: Error.
empty:: List new.
should: [empty at: 1] signal: rangeError.
)
public testListAtPut = (
| list empty |
list:: List new.
list add: 'apple'.
list add: 'banana'.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: list size equals: 2.
assert: (list at: 1 put: 'orange') equals: 'orange'.
assert: (list at: 1) equals: 'orange'.
assert: (list at: 2) equals: 'banana'.
assert: list size equals: 2.
assert: (list at: 2 put: 'kiwi') equals: 'kiwi'.
assert: (list at: 1) equals: 'orange'.
assert: (list at: 2) equals: 'kiwi'.
assert: list size equals: 2.
should: [list at: 0 put: 'pineapple'] signal: rangeError.
should: [list at: 3 put: 'pineapple'] signal: rangeError.
should: [list at: -3 put: 'pineapple'] signal: rangeError.
should: [list at: nil put: 'pineapple'] signal: Error.
empty:: List new.
should: [empty at: 1 put: 'orange'] signal: Error.
)
public testListConcatenation = (
|
a = List new.
b = List new.
c
|
a add: 'apple'.
b add: 'banana'.
b add: 'orange'.
b add: 'strawberry'.
c:: a, b.
assert: c size equals: 4.
assert: (c at: 1) equals: 'apple'.
assert: (c at: 2) equals: 'banana'.
assert: (c at: 3) equals: 'orange'.
assert: (c at: 4) equals: 'strawberry'.
c add: 'pineapple'.
assert: c size equals: 5.
assert: (c at: 5) equals: 'pineapple'.
assert: a size equals: 1.
assert: (a at: 1) equals: 'apple'.
assert: b size equals: 3.
assert: (b at: 1) equals: 'banana'.
assert: (b at: 2) equals: 'orange'.
assert: (b at: 3) equals: 'strawberry'.
)
public testListEnumeration = (
| list evens odds sum |
list:: List withAll: {1. 2. 3. 4. 5. 6. 7. 8. 9}.
evens:: list select: [:e | e \\ 2 = 0].
assert: evens size equals: 4.
odds:: list reject: [:e | e \\ 2 = 0].
assert: odds size equals: 5.
sum:: list inject: 0 into: [:a :b | a + b].
assert: sum equals: 45.
sum:: list reduce: [:a :b | a + b] ifEmpty: [fail].
assert: sum equals: 45.
sum:: list reduce: [:a :b | a + b].
assert: sum equals: 45.
assert: (list anySatisfy: [:e | e > 0]).
assert: (list allSatisfy: [:e | e > 0]).
deny: (list noneSatisfy: [:e | e > 0]).
deny: (list anySatisfy: [:e | e < 0]).
deny: (list allSatisfy: [:e | e < 0]).
assert: (list noneSatisfy: [:e | e < 0]).
assert: (list detect: [:e | e > 5]) equals: 6.
assert: (list detect: [:e | e > 55] ifNone: ['None']) equals: 'None'.
)
public testListEqualityIsIdentity = (
|
empty1 = List new.
empty2 = List new.
simple1 = List new.
simple2 = List new.
cycle1 = List new.
cycle2 = List new.
|
simple1 add: 'strawberry'.
simple2 add: 'strawberry'.
cycle1 add: cycle1.
cycle2 add: cycle2.
assert: empty1 equals: empty1.
assert: empty2 equals: empty2.
deny: empty1 equals: empty2.
deny: empty2 equals: empty1.
assert: simple1 equals: simple1.
assert: simple2 equals: simple2.
deny: simple1 equals: simple2.
deny: simple2 equals: simple1.
assert: cycle1 equals: cycle1.
assert: cycle2 equals: cycle2.
deny: cycle1 equals: cycle2.
deny: cycle2 equals: cycle1.
)
public testListFirstLast = (
| list = List new. empty = List new. |
list add: 'apple'.
list add: 'banana'.
assert: (list first) equals: 'apple'.
assert: (list last) equals: 'banana'.
should: [empty first] signal: Error.
should: [empty last] signal: Error.
)
public testListFloatIndex = (
| list = List new. |
list add: 'apple'.
should: [list at: 1 asFloat] signal: Error.
should: [list at: 1 asFloat put: 'banana'] signal: Error.
)
public testListIndexOf = (
| list = List new. empty = List new. |
list add: 42.
list add: nil.
list add: 'apple'.
list add: 'banana'.
list add: 'apple'.
list add: 'banana'.
assert: (list indexOf: 42) equals: 1.
assert: (list indexOf: nil) equals: 2.
assert: (list indexOf: 'apple') equals: 3.
assert: (list indexOf: 'banana') equals: 4.
assert: (list indexOf: 'noSuchElement') equals: 0.
assert: (empty indexOf: 'noSuchElement') equals: 0.
)
public testListIsEmpty = (
| list |
assert: (List new: 0) isEmpty.
assert: (List new: 16) isEmpty.
list:: List new.
assert: list isEmpty.
list add: #element.
deny: list isEmpty.
list removeLast.
assert: list isEmpty.
)
public testListNew = (
assert: (List new) size equals: 0.
assert: (List new: 0) size equals: 0.
assert: (List new: 16) size equals: 0.
should: [List new: -1] signal: Error.
should: [List new: '10'] signal: Error.
should: [List new: nil] signal: Error.
)
public testListRemove = (
| list = List new: 0. |
list add: 'apple'.
list add: 'banana'.
list add: 'orange'.
list add: 'strawberry'.
list add: 'watermelon'.
assert: list size equals: 5.
assert: (list remove: 'orange') equals: 'orange'.
assert: list size equals: 4.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'strawberry'.
assert: (list at: 4) equals: 'watermelon'.
should: [list remove: 'orange'] signal: Error.
assert: (list remove: 'orange' ifAbsent: ['missing']) equals: 'missing'.
assert: (list remove: 'apple') equals: 'apple'.
assert: list size equals: 3.
assert: (list at: 1) equals: 'banana'.
assert: (list at: 2) equals: 'strawberry'.
assert: (list at: 3) equals: 'watermelon'.
assert: (list remove: 'watermelon') equals: 'watermelon'.
assert: list size equals: 2.
assert: (list at: 1) equals: 'banana'.
assert: (list at: 2) equals: 'strawberry'.
assert: (list remove: 'banana') equals: 'banana'.
assert: list size equals: 1.
assert: (list at: 1) equals: 'strawberry'.
assert: (list remove: 'strawberry') equals: 'strawberry'.
assert: list size equals: 0.
)
public testListRemoveAllSuchThat = (
| list = List new: 0. |
list add: 'apple'.
list add: 'banana'.
list add: 'orange'.
list add: 'strawberry'.
list add: 'watermelon'.
assert: list size equals: 5.
list removeAllSuchThat: [:fruit | (fruit indexOf: "n") > 0].
assert: list size equals: 2.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'strawberry'.
)
public testListRemoveFirst = (
| list = List new: 0. |
list add: 'apple'.
list add: 'banana'.
list add: 'orange'.
list add: 'strawberry'.
list add: 'watermelon'.
assert: list size equals: 5.
assert: list removeFirst equals: 'apple'.
assert: list size equals: 4.
assert: (list at: 1) equals: 'banana'.
assert: (list at: 2) equals: 'orange'.
assert: (list at: 3) equals: 'strawberry'.
assert: (list at: 4) equals: 'watermelon'.
assert: list removeFirst equals: 'banana'.
assert: list size equals: 3.
assert: (list at: 1) equals: 'orange'.
assert: (list at: 2) equals: 'strawberry'.
assert: (list at: 3) equals: 'watermelon'.
assert: list removeFirst equals: 'orange'.
assert: list size equals: 2.
assert: (list at: 1) equals: 'strawberry'.
assert: (list at: 2) equals: 'watermelon'.
assert: list removeFirst equals: 'strawberry'.
assert: list size equals: 1.
assert: (list at: 1) equals: 'watermelon'.
assert: list removeFirst equals: 'watermelon'.
assert: list size equals: 0.
should: [list removeFirst] signal: Error.
)
public testListRemoveLast = (
| list = List new: 0. |
list add: 'apple'.
list add: 'banana'.
list add: 'orange'.
list add: 'strawberry'.
list add: 'watermelon'.
assert: list size equals: 5.
assert: list removeLast equals: 'watermelon'.
assert: list size equals: 4.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
assert: (list at: 4) equals: 'strawberry'.
assert: list removeLast equals: 'strawberry'.
assert: list size equals: 3.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: (list at: 3) equals: 'orange'.
assert: list removeLast equals: 'orange'.
assert: list size equals: 2.
assert: (list at: 1) equals: 'apple'.
assert: (list at: 2) equals: 'banana'.
assert: list removeLast equals: 'banana'.
assert: list size equals: 1.
assert: (list at: 1) equals: 'apple'.
assert: list removeLast equals: 'apple'.
assert: list size equals: 0.
should: [list removeLast] signal: Error.
)
public testListSort = (
| list |
list:: List withAll: {12. 15. 3. 5. 10. 6. 8. 1. 13. 11. 16. 4. 14. 9. 2. 7}.
assert: list size equals: 16.
list sort: [:a :b | a < b].
assert: list size equals: 16.
1 to: 16 do: [:index | assert: (list at: index) equals: index].
list:: List withAll: {12. 8. 9. 7. 2. 10. 4. 6. 5. 3. 13. 15. 14. 1. 11}.
assert: list size equals: 15.
list sort: [:a :b | a < b].
assert: list size equals: 15.
1 to: 15 do: [:index | assert: (list at: index) equals: index].
list:: List withAll: {10. 12. 2. 11. 14. 4. 13. 3. 9. 8. 7. 5. 6. 1}.
assert: list size equals: 14.
list sort: [:a :b | a < b].
assert: list size equals: 14.
1 to: 14 do: [:index | assert: (list at: index) equals: index].
list:: List withAll: {12. 6. 10. 5. 7. 9. 2. 4. 1. 11. 3. 8. 13}.
assert: list size equals: 13.
list sort: [:a :b | a < b].
assert: list size equals: 13.
1 to: 13 do: [:index | assert: (list at: index) equals: index].
)
public testListSortEmpty = (
| list |
list:: List new.
assert: list size equals: 0.
list sort: [:a :b | a < b].
assert: list size equals: 0.
)
public testListWithAll = (
| array bytearray list result |
array:: Array new: 2.
array at: 1 put: 'A'.
array at: 2 put: 'B'.
result:: List withAll: array.
assert: result size equals: 2.
assert: (result at: 1) equals: 'A'.
assert: (result at: 2) equals: 'B'.
bytearray:: ByteArray new: 2.
bytearray at: 1 put: 67.
bytearray at: 2 put: 68.
result:: List withAll: bytearray.
assert: result size equals: 2.
assert: (result at: 1) equals: 67.
assert: (result at: 2) equals: 68.
result:: List withAll: 'EF'.
assert: result size equals: 2.
assert: (result at: 1) equals: 69.
assert: (result at: 2) equals: 70.
list:: List new.
list add: 'G'.
list add: 'H'.
result:: List withAll: list.
assert: result size equals: 2.
assert: (result at: 1) equals: 'G'.
assert: (result at: 2) equals: 'H'.
should: [List withAll: -1] signal: Error.
should: [List withAll: 16r110000] signal: Error.
should: [List withAll: nil] signal: Error.
should: [List withAll: 122 asFloat] signal: Error.
)
) : (
TEST_CONTEXT = ()
)
public class MapTests = TestContext () (
public testIsKindOfMap = (
deny: {} isKindOfMap.
deny: (Array new: 0) isKindOfMap.
deny: List new isKindOfMap.
assert: Map new isKindOfMap.
deny: Set new isKindOfMap.
deny: nil isKindOfMap.
deny: false isKindOfMap.
deny: true isKindOfMap.
deny: 0 isKindOfMap.
)
public testMapCollect = (
| map result |
map:: Map new.
map at: 'red' put: 'rouge'.
map at: 'green' put: 'vert'.
map at: 'blue' put: 'bleu'.
result:: map collect: [:each | each size].
assert: result size equals: 3.
assert: (result at: 'red') equals: 5.
assert: (result at: 'green') equals: 4.
assert: (result at: 'blue') equals: 4.
)
public testMapIsEmpty = (
| map |
assert: (Map new: 0) isEmpty.
assert: (Map new: 16) isEmpty.
map:: Map new.
assert: map isEmpty.
map at: #key put: #value.
deny: map isEmpty.
map removeKey: #key.
assert: map isEmpty.
)
public testMapLowCapacity = (
(* Regression test against naively taking the capacity hint, which can break the invariant of having at least one free slot. *)
0 to: 3 do:
[:capacityHint | | map |
map:: Map new: capacityHint.
map at: 'roses' put: 'red'.
map at: 'violets' put: 'blue'.
assert: map size equals: 2.
assert: (map at: 'roses') equals: 'red'.
assert: (map at: 'violets') equals: 'blue'].
)
public testMapNew = (
assert: (Map new) size equals: 0.
assert: (Map new: 0) size equals: 0.
assert: (Map new: 16) size equals: 0.
should: [Map new: -1] signal: Error.
should: [Map new: '10'] signal: Error.
should: [Map new: nil] signal: Error.
)
public testMapNilKey = (
| map = Map new. |
assert: (map at: nil put: 'pear') equals: 'pear'.
assert: map size equals: 1.
assert: (map includesKey: nil).
assert: (map at: nil) equals: 'pear'.
(* Try to trigger growth/rehashing. *)
assert: (map at: 1 put: 'apricot') equals: 'apricot'.
assert: (map at: 2 put: 'cherry') equals: 'cherry'.
assert: (map at: 3 put: 'mango') equals: 'mango'.
assert: (map at: 4 put: 'nectarine') equals: 'nectarine'.
assert: (map at: 5 put: 'lemon') equals: 'lemon'.
assert: (map at: 6 put: 'lime') equals: 'lime'.
assert: (map at: 7 put: 'peach') equals: 'peach'.
assert: (map at: 8 put: 'plum') equals: 'plum'.
assert: map size equals: 9.
assert: (map includesKey: nil).
assert: (map at: nil) equals: 'pear'.
assert: (map at: 1) equals: 'apricot'.
assert: (map at: 2) equals: 'cherry'.
assert: (map at: 3) equals: 'mango'.
assert: (map at: 4) equals: 'nectarine'.
assert: (map at: 5) equals: 'lemon'.
assert: (map at: 6) equals: 'lime'.
assert: (map at: 7) equals: 'peach'.
assert: (map at: 8) equals: 'plum'.
assert: (map removeKey: nil) equals: 'pear'.
assert: map size equals: 8.
deny: (map includes: nil).
assert: (map at: nil put: 'pineapple') equals: 'pineapple'.
assert: map size equals: 9.
assert: (map includesKey: nil).
assert: (map at: nil) equals: 'pineapple'.
)
public testMapReject = (
| map result |
map:: Map new.
map at: 'red' put: 'rouge'.
map at: 'green' put: 'vert'.
map at: 'blue' put: 'bleu'.
result:: map reject: [:each | each size > 4].
assert: result size equals: 2.
assert: (result at: 'green') equals: 'vert'.
assert: (result at: 'blue') equals: 'bleu'.
)
public testMapSelect = (
| map result |
map:: Map new.
map at: 'red' put: 'rouge'.
map at: 'green' put: 'vert'.
map at: 'blue' put: 'bleu'.
result:: map select: [:each | each size > 4].
assert: result size equals: 1.
assert: (result at: 'red') equals: 'rouge'.
)
public testMapSelfKey = (
| map = Map new. |
assert: (map at: map put: 'pear') equals: 'pear'.
assert: map size equals: 1.
assert: (map includesKey: map).
assert: (map at: map) equals: 'pear'.
(* Try to trigger growth/rehashing. *)
assert: (map at: 1 put: 'apricot') equals: 'apricot'.
assert: (map at: 2 put: 'cherry') equals: 'cherry'.
assert: (map at: 3 put: 'mango') equals: 'mango'.
assert: (map at: 4 put: 'nectarine') equals: 'nectarine'.
assert: (map at: 5 put: 'lemon') equals: 'lemon'.
assert: (map at: 6 put: 'lime') equals: 'lime'.
assert: (map at: 7 put: 'peach') equals: 'peach'.
assert: (map at: 8 put: 'plum') equals: 'plum'.
assert: map size equals: 9.
assert: (map includesKey: map).
assert: (map at: map) equals: 'pear'.
assert: (map at: 1) equals: 'apricot'.
assert: (map at: 2) equals: 'cherry'.
assert: (map at: 3) equals: 'mango'.
assert: (map at: 4) equals: 'nectarine'.
assert: (map at: 5) equals: 'lemon'.
assert: (map at: 6) equals: 'lime'.
assert: (map at: 7) equals: 'peach'.
assert: (map at: 8) equals: 'plum'.
assert: (map removeKey: map) equals: 'pear'.
assert: map size equals: 8.
deny: (map includes: map).
assert: (map at: map put: 'pineapple') equals: 'pineapple'.
assert: map size equals: 9.
assert: (map includesKey: map).
assert: (map at: map) equals: 'pineapple'.
)
) : (
TEST_CONTEXT = ()
)
public class SetTests = TestContext () (
public testIsKindOfSet = (
deny: {} isKindOfSet.
deny: (Array new: 0) isKindOfSet.
deny: List new isKindOfSet.
deny: Map new isKindOfSet.
assert: Set new isKindOfSet.
deny: nil isKindOfSet.
deny: false isKindOfSet.
deny: true isKindOfSet.
deny: 0 isKindOfSet.
)
public testSetAsArray = (
| set array |
set:: Set new.
set add: 10.
array:: set asArray.
assert: array isKindOfArray.
assert: array size equals: 1.
assert: (array at: 1) equals: 10.
array at: 1 put: 20.
assert: (set includes: 10).
deny: (set includes: 20).
set:: Set new.
set add: 30.
set add: 40.
set add: 50.
array:: set asArray.
assert: array isKindOfArray.
assert: array size equals: 3.
array sort: [:a :b | a <= b].
assert: (array at: 1) equals: 30.
assert: (array at: 2) equals: 40.
assert: (array at: 3) equals: 50.
)
public testSetEnumeration = (
| set evens odds sum |
set:: Set withAll: {1. 2. 3. 4. 5. 6. 7. 8. 9}.
evens:: set select: [:e | e \\ 2 = 0].
assert: evens size equals: 4.
odds:: set reject: [:e | e \\ 2 = 0].
assert: odds size equals: 5.
sum:: set inject: 0 into: [:a :b | a + b].
assert: sum equals: 45.
sum:: set reduce: [:a :b | a + b] ifEmpty: [fail].
assert: sum equals: 45.
sum:: set reduce: [:a :b | a + b].
assert: sum equals: 45.
assert: (set anySatisfy: [:e | e > 0]).
assert: (set allSatisfy: [:e | e > 0]).
deny: (set noneSatisfy: [:e | e > 0]).
deny: (set anySatisfy: [:e | e < 0]).
deny: (set allSatisfy: [:e | e < 0]).
assert: (set noneSatisfy: [:e | e < 0]).
assert: (set detect: [:e | e > 5]) equals: 6.
assert: (set detect: [:e | e > 55] ifNone: ['None']) equals: 'None'.
)
public testSetIsEmpty = (
| set |
assert: (Set new: 0) isEmpty.
assert: (Set new: 16) isEmpty.
set:: Set new.
assert: set isEmpty.
set add: #element.
deny: set isEmpty.
set remove: #element.
assert: set isEmpty.
)
public testSetLowCapacity = (
(* Regression test against naively taking the capacity hint, which can break the invariant of having at least one free slot. *)
0 to: 3 do:
[:capacityHint | | set |
set:: Set new: capacityHint.
set add: 'roses'.
set add: 'violets'.
assert: set size equals: 2.
assert: (set includes: 'roses').
assert: (set includes: 'violets')].
)
public testSetNew = (
assert: (Set new) size equals: 0.
assert: (Set new: 16) size equals: 0.
should: [Set new: -1] signal: Error.
should: [Set new: '10'] signal: Error.
should: [Set new: nil] signal: Error.
)
public testSetNilElement = (
| set = Set new. |
assert: (set add: nil) equals: nil.
assert: set size equals: 1.
assert: (set includes: nil).
(* Try to trigger growth/rehashing. *)
assert: (set add: 'apricot') equals: 'apricot'.
assert: (set add: 'cherry') equals: 'cherry'.
assert: (set add: 'mango') equals: 'mango'.
assert: (set add: 'nectarine') equals: 'nectarine'.
assert: (set add: 'lemon') equals: 'lemon'.
assert: (set add: 'lime') equals: 'lime'.
assert: (set add: 'peach') equals: 'peach'.
assert: (set add: 'plum') equals: 'plum'.
assert: set size equals: 9.
assert: (set includes: nil).
assert: (set includes: 'apricot').
assert: (set includes: 'cherry').
assert: (set includes: 'mango').
assert: (set includes: 'nectarine').
assert: (set includes: 'lemon').
assert: (set includes: 'lime').
assert: (set includes: 'peach').
assert: (set includes: 'plum').
assert: (set remove: nil) equals: nil.
assert: set size equals: 8.
deny: (set includes: nil).
assert: (set add: nil) equals: nil.
assert: set size equals: 9.
assert: (set includes: nil).
)
public testSetSelfElement = (
| set = Set new. |
assert: (set add: self) equals: self.
assert: set size equals: 1.
assert: (set includes: self).
(* Try to trigger growth/rehashing. *)
assert: (set add: 'apricot') equals: 'apricot'.
assert: (set add: 'cherry') equals: 'cherry'.
assert: (set add: 'mango') equals: 'mango'.
assert: (set add: 'nectarine') equals: 'nectarine'.
assert: (set add: 'lemon') equals: 'lemon'.
assert: (set add: 'lime') equals: 'lime'.
assert: (set add: 'peach') equals: 'peach'.
assert: (set add: 'plum') equals: 'plum'.
assert: set size equals: 9.
assert: (set includes: self).
assert: (set includes: 'apricot').
assert: (set includes: 'cherry').
assert: (set includes: 'mango').
assert: (set includes: 'nectarine').
assert: (set includes: 'lemon').
assert: (set includes: 'lime').
assert: (set includes: 'peach').
assert: (set includes: 'plum').
assert: (set remove: self) equals: self.
assert: set size equals: 8.
deny: (set includes: self).
assert: (set add: self) equals: self.
assert: set size equals: 9.
assert: (set includes: self).
)
public testSetWithAll = (
| array bytearray list result |
array:: Array new: 2.
array at: 1 put: 'A'.
array at: 2 put: 'B'.
result:: Set withAll: array.
assert: result size equals: 2.
assert: (result includes: 'A').
assert: (result includes: 'B').
bytearray:: ByteArray new: 2.
bytearray at: 1 put: 67.
bytearray at: 2 put: 68.
result:: Set withAll: bytearray.
assert: result size equals: 2.
assert: (result includes: 67).
assert: (result includes: 68).
result:: Set withAll: 'EF'.
assert: result size equals: 2.
assert: (result includes: 69).
assert: (result includes: 70).
list:: List new.
list add: 'G'.
list add: 'H'.
result:: Set withAll: list.
assert: result size equals: 2.
assert: (result includes: 'G').
assert: (result includes: 'H').
should: [Set withAll: -1] signal: Error.
should: [Set withAll: 16r110000] signal: Error.