-
Notifications
You must be signed in to change notification settings - Fork 11
/
KernelTests.ns
5895 lines (5262 loc) · 206 KB
/
KernelTests.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
'Root'
class KernelTests usingPlatform: p testFramework: m = (|
private TestContext = m TestContext.
private MessageNotUnderstood = p kernel MessageNotUnderstood.
private Exception = p kernel Exception.
private Stopwatch = p kernel Stopwatch.
private StringBuilder = p kernel StringBuilder.
private List = p collections List.
|) (
public class ArrayTests = TestContext () (
public testArrayAsArray = (
| array = Array new: 3. |
assert: array asArray equals: array.
)
public testArrayAt = (
| array = Array new: 2. empty = Array new: 0. |
array at: 1 put: 'apple'.
array at: 2 put: 'banana'.
assert: (array at: 1) equals: 'apple'.
assert: (array at: 2) equals: 'banana'.
should: [array at: 0] signal: Error.
should: [array at: 3] signal: Error.
should: [array at: -3] signal: Error.
should: [array at: nil] signal: Error.
should: [empty at: 1] signal: Error.
)
public testArrayAtPut = (
| array = Array new: 2. empty = Array new: 0. |
assert: (array at: 1 put: 'apple') equals: 'apple'.
assert: (array at: 2 put: 'banana') equals: 'banana'.
should: [array at: 0 put: 'orange'] signal: Error.
should: [array at: 3 put: 'orange'] signal: Error.
should: [array at: -3 put: 'orange'] signal: Error.
should: [array at: nil put: 'orange'] signal: Error.
should: [empty at: 1 put: 'orange'] signal: Error.
)
public testArrayConcatenation = (
|
a = Array new: 1.
b = Array new: 3.
c
|
a at: 1 put: 'apple'.
b at: 1 put: 'banana'.
b at: 2 put: 'orange'.
b at: 3 put: 'strawberry'.
c:: a, b.
assert: c isKindOfArray.
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'.
)
public testArrayEqualityIsIdentity = (
|
empty1 = Array new: 0.
empty2 = Array new: 0.
simple1 = Array new: 1.
simple2 = Array new: 1.
cycle1 = Array new: 1.
cycle2 = Array new: 1.
|
simple1 at: 1 put: 42.
simple2 at: 1 put: 42.
cycle1 at: 1 put: cycle1.
cycle2 at: 1 put: 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 testArrayFirstLast = (
| array = Array new: 2. empty = Array new: 0. |
array at: 1 put: 'apple'.
array at: 2 put: 'banana'.
assert: (array first) equals: 'apple'.
assert: (array last) equals: 'banana'.
should: [empty first] signal: Error.
should: [empty last] signal: Error.
)
public testArrayFloatIndex = (
| array = Array new: 1. |
should: [array at: 1 asFloat] signal: Error.
should: [array at: 1 asFloat put: 'apple'] signal: Error.
)
public testArrayIndexOf = (
| array = Array new: 6. empty = Array new: 0. |
array at: 1 put: 42.
array at: 2 put: nil.
array at: 3 put: 'apple'.
array at: 4 put: 'banana'.
array at: 5 put: 'apple'.
array at: 6 put: 'banana'.
assert: (array indexOf: 42) equals: 1.
assert: (array indexOf: nil) equals: 2.
assert: (array indexOf: 'apple') equals: 3.
assert: (array indexOf: 'banana') equals: 4.
assert: (array indexOf: 'noSuchElement') equals: 0.
assert: (empty indexOf: 'noSuchElement') equals: 0.
)
public testArrayIsEmpty = (
assert: (Array new: 0) isEmpty.
deny: (Array new: 1) isEmpty.
)
public testArrayNew = (
assert: (Array new: 0) size equals: 0.
assert: (Array new: 500) size equals: 500.
should: [Array new: -1] signal: Error.
should: [Array new: '10'] signal: Error.
should: [Array new: nil] signal: Error.
should: [Array new] signal: MessageNotUnderstood.
)
public testArrayReplace = (
| array replacement result |
array:: Array new: 5.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
replacement:: Array new: 3.
replacement at: 1 put: 110.
replacement at: 2 put: 120.
replacement at: 3 put: 130.
(* Empty. *)
result:: array replaceFrom: 3 to: 2 with: replacement startingAt: 2.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 30.
assert: (array at: 4) equals: 40.
assert: (array at: 5) equals: 50.
(* One. *)
result:: array replaceFrom: 3 to: 3 with: replacement startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 110.
assert: (array at: 4) equals: 40.
assert: (array at: 5) equals: 50.
(* Two. *)
result:: array replaceFrom: 3 to: 4 with: replacement startingAt: 2.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 120.
assert: (array at: 4) equals: 130.
assert: (array at: 5) equals: 50.
(* Three. *)
result:: array replaceFrom: 1 to: 3 with: replacement startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 110.
assert: (array at: 2) equals: 120.
assert: (array at: 3) equals: 130.
assert: (array at: 4) equals: 130.
assert: (array at: 5) equals: 50.
(* After end source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 6] signal: Error.
(* Crossing end source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 3] signal: Error.
(* Before beginning source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: -3] signal: Error.
(* Crossing begining source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 0] signal: Error.
(* After end destination *)
should: [array replaceFrom: 6 to: 6 with: replacement startingAt: 1] signal: Error.
(* Crossing end destination *)
should: [array replaceFrom: 5 to: 6 with: replacement startingAt: 1] signal: Error.
(* Before beginning destination *)
should: [array replaceFrom: 0 to: 0 with: replacement startingAt: 1] signal: Error.
(* Crossing begining destination *)
should: [array replaceFrom: 0 to: 2 with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: nil to: 3 with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: nil with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: 3 with: nil startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: 3 with: replacement startingAt: nil] signal: Error.
)
public testArrayReplaceSlideDown = (
| array result |
array:: Array new: 7.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
array at: 6 put: 60.
array at: 7 put: 70.
result:: array replaceFrom: 3 to: 7 with: array startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 10.
assert: (array at: 4) equals: 20.
assert: (array at: 5) equals: 30.
assert: (array at: 6) equals: 40.
assert: (array at: 7) equals: 50.
)
public testArrayReplaceSlideUp = (
| array result |
array:: Array new: 7.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
array at: 6 put: 60.
array at: 7 put: 70.
result:: array replaceFrom: 1 to: 5 with: array startingAt: 3.
assert: result equals: array.
assert: (array at: 1) equals: 30.
assert: (array at: 2) equals: 40.
assert: (array at: 3) equals: 50.
assert: (array at: 4) equals: 60.
assert: (array at: 5) equals: 70.
assert: (array at: 6) equals: 60.
assert: (array at: 7) equals: 70.
)
public testArraySort = (
| array |
array:: {12. 15. 3. 5. 10. 6. 8. 1. 13. 11. 16. 4. 14. 9. 2. 7}.
assert: array size equals: 16.
array sort: [:a :b | a < b].
1 to: 16 do: [:index | assert: (array at: index) equals: index].
array:: {12. 8. 9. 7. 2. 10. 4. 6. 5. 3. 13. 15. 14. 1. 11}.
assert: array size equals: 15.
array sort: [:a :b | a < b].
1 to: 15 do: [:index | assert: (array at: index) equals: index].
array:: {10. 12. 2. 11. 14. 4. 13. 3. 9. 8. 7. 5. 6. 1}.
assert: array size equals: 14.
array sort: [:a :b | a < b].
1 to: 14 do: [:index | assert: (array at: index) equals: index].
array:: {12. 6. 10. 5. 7. 9. 2. 4. 1. 11. 3. 8. 13}.
assert: array size equals: 13.
array sort: [:a :b | a < b].
1 to: 13 do: [:index | assert: (array at: index) equals: index].
)
public testArrayWithAll = (
| array bytearray list result |
array:: Array new: 2.
array at: 1 put: 'A'.
array at: 2 put: 'B'.
result:: Array 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:: Array withAll: bytearray.
assert: result size equals: 2.
assert: (result at: 1) equals: 67.
assert: (result at: 2) equals: 68.
result:: Array 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:: Array withAll: list.
assert: result size equals: 2.
assert: (result at: 1) equals: 'G'.
assert: (result at: 2) equals: 'H'.
should: [Array withAll: -1] signal: Error.
should: [Array withAll: 16r110000] signal: Error.
should: [Array withAll: nil] signal: Error.
should: [Array withAll: 122 asFloat] signal: Error.
)
public testIsKindOfArray = (
assert: (Array new: 0) isKindOfArray.
deny: (ByteArray new: 0) isKindOfArray.
deny: 0 isKindOfArray.
deny: 1.5 isKindOfArray.
deny: 'Array new' isKindOfArray.
deny: nil isKindOfArray.
)
public testArrayCopyFromTo = (
| array copy1 copy2 copy3|
array:: Array new: 7.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
array at: 6 put: 60.
array at: 7 put: 70.
copy1:: array copyFrom: 2 to: 5.
assert: copy1 size equals: 4.
assert: (copy1 at: 1) equals: 20.
assert: (copy1 at: 2) equals: 30.
assert: (copy1 at: 3) equals: 40.
assert: (copy1 at: 4) equals: 50.
copy2:: array copyFrom: 7 to: 6.
assert: [copy2 isEmpty].
copy3:: array copyFrom: 7 to: 7.
assert: copy3 size equals: 1.
assert: (copy3 at: 1) equals: 70.
should: [array copyFrom: 0 to: 5] signal: Error.
should: [array copyFrom: 2 to: 8] signal: Error.
should: [array copyFrom: 0 to: 8] signal: Error.
should: [array copyFrom: 8 to: 5] signal: Error.
should: [array copyFrom: 7 to: 0] signal: Error.
)
) : (
TEST_CONTEXT = ()
)
public class BehaviorTests = TestContext () (
class Module superclass: s = (
|
Superclass = s.
|
) (
public class Subclass = Superclass (
) (
) : (
)
) : (
)
public testBehaviorBasicNew = (
should: [Object basicNew] signal: MessageNotUnderstood.
(* :todo: basicNew should also fail from a class-side method. *)
)
public testBehaviorInternalsAreHidden = (
(* Squeak, JS *)
should: [Object superclass] signal: MessageNotUnderstood.
should: [Object methodDictionary] signal: MessageNotUnderstood.
should: [Object mixin] signal: MessageNotUnderstood.
should: [Object enclosingObject] signal: MessageNotUnderstood.
should: [Object format] signal: MessageNotUnderstood.
(* PrimordialSoup *)
should: [Object _superclass] signal: MessageNotUnderstood.
should: [Object _methods] signal: MessageNotUnderstood.
should: [Object _enclosingObject] signal: MessageNotUnderstood.
should: [Object _mixin] signal: MessageNotUnderstood.
should: [Object _classIndex] signal: MessageNotUnderstood.
should: [Object _format] signal: MessageNotUnderstood.
)
public testBehaviorName = (
(* :todo: Which to we want? *)
(* should: [Object name] signal: MessageNotUnderstood.
assert: Object name equals: 'Object'.
assert: Object name equals: 'KernelForImplementation`Object'. *)
)
public testClassCannotSubclassArray = (
| module |
module:: Module superclass: Array.
should: [module Subclass] signal: Exception.
)
public testClassCannotSubclassByteArray = (
| module |
module:: Module superclass: ByteArray.
should: [module Subclass] signal: Exception.
)
public testClassCannotSubclassMetaclass = (
| module |
module:: Module superclass: self class class.
should: [module Subclass] signal: Exception.
)
public testClassCannotSubclassNil = (
| module |
module:: Module superclass: nil.
should: [module Subclass] signal: Exception.
)
public testClassCannotSubclassString = (
| module |
module:: Module superclass: String.
should: [module Subclass] signal: Exception.
)
public testIsKindOfBehavior = (
assert: Object isKindOfBehavior.
deny: nil isKindOfBehavior.
deny: false isKindOfBehavior.
deny: true isKindOfBehavior.
deny: 0 isKindOfBehavior.
)
) : (
TEST_CONTEXT = ()
public class = (
^super class
)
)
public class BooleanTests = TestContext () (
public testBooleanAsString = (
assert: true asString equals: 'true'.
assert: false asString equals: 'false'.
)
public testBooleanEvaluatingConjuction = (
assert: true & true.
deny: true & false.
deny: false & true.
deny: false & false.
)
public testBooleanEvaluatingDisjuction = (
assert: true | true.
assert: true | false.
assert: false | true.
deny: false | false.
)
public testBooleanNonEvaluatingConjuction = (
assert: (true and: [true]).
deny: (true and: [false]).
deny: (false and: [doesNotExecute]).
)
public testBooleanNonEvaluatingDisjuction = (
assert: (true or: [doesNotExecute]).
assert: (false or: [true]).
deny: (false or: [false]).
)
public testBooleanNot = (
deny: true not.
assert: false not.
)
public testBooleanPrintString = (
assert: true printString equals: 'true'.
assert: false printString equals: 'false'.
)
) : (
TEST_CONTEXT = ()
)
public class ByteArrayTests = TestContext () (
b: string = (
^ByteArray withAll: string
)
public testByteArrayAt = (
| array = ByteArray new: 2. empty = ByteArray new: 0. |
array at: 1 put: 3.
array at: 2 put: 4.
assert: (array at: 1) equals: 3.
assert: (array at: 2) equals: 4.
should: [array at: 0] signal: Error.
should: [array at: 3] signal: Error.
should: [array at: -3] signal: Error.
should: [array at: nil] signal: Error.
should: [empty at: 1] signal: Error.
)
public testByteArrayAtPut = (
| array = ByteArray new: 2. empty = ByteArray new: 0. |
assert: (array at: 1 put: 3) equals: 3.
assert: (array at: 2 put: 4) equals: 4.
should: [array at: 0 put: 5] signal: Error.
should: [array at: 3 put: 6] signal: Error.
should: [array at: -3 put: 7] signal: Error.
should: [array at: nil put: 8] signal: Error.
assert: (array at: 1 put: 0) equals: 0.
assert: (array at: 2 put: 255) equals: 255.
should: [array at: 1 put: -1] signal: Error.
should: [array at: 1 put: 256] signal: Error.
should: [empty at: 1 put: 9] signal: Error.
)
public testByteArrayCopyByteArrayFromTo = (
| array = ByteArray new: 4. empty = ByteArray new: 0. copy |
array at: 1 put: 16rA.
array at: 2 put: 16rB.
array at: 3 put: 16rC.
array at: 4 put: 16rD.
copy:: array copyByteArrayFrom: 1 to: 4.
assert: copy size equals: 4.
assert: (copy at: 1) equals: 16rA.
assert: (copy at: 4) equals: 16rD.
copy:: array copyByteArrayFrom: 2 to: 3.
assert: copy size equals: 2.
assert: (copy at: 1) equals: 16rB.
assert: (copy at: 2) equals: 16rC.
1 to: 4 do:
[:start |
copy:: array copyByteArrayFrom: start to: start - 1.
assert: copy size equals: 0].
copy:: empty copyByteArrayFrom: 1 to: 0.
assert: copy size equals: 0.
should: [empty copyByteArrayFrom: 0 to: 0] signal: Error.
should: [empty copyByteArrayFrom: 2 to: 0] signal: Error.
should: [empty copyByteArrayFrom: 1 to: 1] signal: Error.
should: [array copyByteArrayFrom: 4 to: 5] signal: Error.
should: [array copyByteArrayFrom: nil to: 2] signal: Error.
should: [array copyByteArrayFrom: 2 to: nil] signal: Error.
should: [array copyByteArrayFrom: 2 to: 50] signal: Error.
should: [array copyByteArrayFrom: 0 to: 2] signal: Error.
should: [array copyByteArrayFrom: 2 asFloat to: 3] signal: Error.
should: [array copyByteArrayFrom: 2 to: 3 asFloat] signal: Error.
)
public testByteArrayCopyFromTo = (
| array = ByteArray new: 4. empty = ByteArray new: 0. copy |
array at: 1 put: 16rA.
array at: 2 put: 16rB.
array at: 3 put: 16rC.
array at: 4 put: 16rD.
copy:: array copyFrom: 1 to: 4.
assert: copy size equals: 4.
assert: (copy at: 1) equals: 16rA.
assert: (copy at: 4) equals: 16rD.
copy:: array copyFrom: 2 to: 3.
assert: copy size equals: 2.
assert: (copy at: 1) equals: 16rB.
assert: (copy at: 2) equals: 16rC.
1 to: 4 do:
[:start |
copy:: array copyFrom: start to: start - 1.
assert: copy size equals: 0].
copy:: empty copyFrom: 1 to: 0.
assert: copy size equals: 0.
should: [empty copyFrom: 0 to: 0] signal: Error.
should: [empty copyFrom: 2 to: 0] signal: Error.
should: [empty copyFrom: 1 to: 1] signal: Error.
should: [array copyFrom: 4 to: 5] signal: Error.
should: [array copyFrom: nil to: 2] signal: Error.
should: [array copyFrom: 2 to: nil] signal: Error.
should: [array copyFrom: 2 to: 50] signal: Error.
should: [array copyFrom: 0 to: 2] signal: Error.
should: [array copyFrom: 2 asFloat to: 3] signal: Error.
should: [array copyFrom: 2 to: 3 asFloat] signal: Error.
)
public testByteArrayCopyStringFromTo = (
| array = ByteArray new: 4. empty = ByteArray new: 0. copy |
array at: 1 put: 65.
array at: 2 put: 66.
array at: 3 put: 67.
array at: 4 put: 68.
copy:: array copyStringFrom: 1 to: 4.
assert: copy equals: 'ABCD'.
copy:: array copyStringFrom: 2 to: 3.
assert: copy equals: 'BC'.
1 to: 4 do:
[:start |
copy:: array copyStringFrom: start to: start - 1.
assert: copy equals: ''].
copy:: empty copyStringFrom: 1 to: 0.
assert: copy equals: ''.
should: [empty copyStringFrom: 0 to: 0] signal: Error.
should: [empty copyStringFrom: 2 to: 0] signal: Error.
should: [empty copyStringFrom: 1 to: 1] signal: Error.
should: [array copyStringFrom: 4 to: 5] signal: Error.
should: [array copyStringFrom: nil to: 2] signal: Error.
should: [array copyStringFrom: 2 to: nil] signal: Error.
should: [array copyStringFrom: 2 to: 50] signal: Error.
should: [array copyStringFrom: 0 to: 2] signal: Error.
should: [array copyStringFrom: 2 asFloat to: 3] signal: Error.
should: [array copyStringFrom: 2 to: 3 asFloat] signal: Error.
)
public testByteArrayEndsWith = (
| foo zero empty |
foo:: ByteArray new: 3.
foo at: 1 put: 102.
foo at: 2 put: 111.
foo at: 3 put: 111.
zero:: ByteArray new: 1.
zero at: 1 put: 48.
empty:: ByteArray new: 0.
assert: (foo endsWith: 'o').
assert: (foo endsWith: 'foo').
assert: (foo endsWith: foo).
assert: (foo endsWith: '').
assert: (foo endsWith: empty).
assert: (empty endsWith: '').
assert: (foo endsWith: empty).
deny: (foo endsWith: 'toolongfoo').
deny: (foo endsWith: 'boo').
deny: (foo endsWith: 'bar').
deny: (empty endsWith: 'foo').
deny: (empty endsWith: foo).
should: [zero endsWith: 0] signal: Error.
should: [foo endsWith: true] signal: Error.
should: [foo endsWith: nil] signal: Error.
)
public testByteArrayFloatIndex = (
| array = ByteArray new: 1. |
should: [array at: 1 asFloat] signal: Error.
should: [array at: 1 asFloat put: 0] signal: Error.
)
public testByteArrayIndexOf = (
assert: ((b: 'fofofobar') indexOf: (b: 'fofo')) equals: 1.
assert: ((b: 'fofofobar') indexOf: (b: 'bar')) equals: 7.
assert: ((b: 'fofofobar') indexOf: (b: 'barr')) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'fob')) equals: 5.
assert: ((b: 'fofofobar') indexOf: (b: 'baz')) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'f')) equals: 1.
assert: ((b: 'fofofobar') indexOf: (b: 'r')) equals: 9.
assert: ((b: 'fofofobar') indexOf: (b: '')) equals: 1.
assert: ((b: 'tiny') indexOf: (b: 'toolong')) equals: 0.
assert: ((b: '') indexOf: (b: '')) equals: 1.
assert: ((b: '') indexOf: (b: 'baz')) equals: 0.
should: [(b: 'fofofobar') indexOf: 0] signal: Error.
should: [(b: '') indexOf: Object new] signal: Error.
)
public testByteArrayIndexOfStartingAt = (
assert: ((b: 'fofofobar') indexOf: (b: 'fofo') startingAt: 1) equals: 1.
assert: ((b: 'fofofobar') indexOf: (b: 'fofo') startingAt: 2) equals: 3.
assert: ((b: 'fofofobar') indexOf: (b: 'fofo') startingAt: 3) equals: 3.
assert: ((b: 'fofofobar') indexOf: (b: 'fofo') startingAt: 4) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'bar') startingAt: 1) equals: 7.
assert: ((b: 'fofofobar') indexOf: (b: 'bar') startingAt: 7) equals: 7.
assert: ((b: 'fofofobar') indexOf: (b: 'bar') startingAt: 8) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'barr') startingAt: 6) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'fob') startingAt: 5) equals: 5.
assert: ((b: 'fofofobar') indexOf: (b: 'fob') startingAt: 6) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'baz') startingAt: 1) equals: 0.
assert: ((b: 'fofofobar') indexOf: (b: 'f') startingAt: 1) equals: 1.
assert: ((b: 'fofofobar') indexOf: (b: 'f') startingAt: 2) equals: 3.
assert: ((b: 'fofofobar') indexOf: (b: 'r') startingAt: 1) equals: 9.
assert: ((b: 'fofofobar') indexOf: (b: 'r') startingAt: 9) equals: 9.
assert: ((b: 'fofofobar') indexOf: (b: '') startingAt: 1) equals: 1.
assert: ((b: 'fofofobar') indexOf: (b: '') startingAt: 5) equals: 5.
assert: ((b: 'fofofobar') indexOf: (b: '') startingAt: 9) equals: 9.
assert: ((b: 'tiny') indexOf: (b: 'toolong') startingAt: 1) equals: 0.
assert: ((b: 'tiny') indexOf: (b: 'toolong') startingAt: 3) equals: 0.
assert: ((b: '') indexOf: (b: '') startingAt: 1) equals: 1.
assert: ((b: '') indexOf: (b: 'baz') startingAt: 1) equals: 0.
should: [(b: 'fofofobar') indexOf: 0 startingAt: 1] signal: Error.
should: [(b: 'fofobobar') indexOf: Object new startingAt: 1] signal: Error.
should: [(b: 'fofofobar') indexOf: (b: 'foo') startingAt: 0] signal: Error.
should: [(b: 'fofofobar') indexOf: (b: 'foo') startingAt: 11] signal: Error.
should: [(b: '') indexOf: (b: '') startingAt: 0] signal: Error.
should: [(b: '') indexOf: (b: '') startingAt: 2] signal: Error.
)
public testByteArrayIsEmpty = (
assert: (ByteArray new: 0) isEmpty.
deny: (ByteArray new: 1) isEmpty.
)
public testByteArrayLargeAllocation = (
| size = 8 * 1024 * 1024. |
assert: (ByteArray new: size) size equals: size.
assert: (ByteArray new: size) size equals: size.
assert: (ByteArray new: size) size equals: size.
)
public testByteArrayLastIndexOf = (
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fofo')) equals: 3.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'bar')) equals: 7.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'barr')) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fob')) equals: 5.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'baz')) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'f')) equals: 5.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'r')) equals: 9.
assert: ((b: 'fofofobar') lastIndexOf: (b: '')) equals: 10.
assert: ((b: 'tiny') lastIndexOf: (b: 'toolong')) equals: 0.
assert: ((b: '') lastIndexOf: (b: '')) equals: 1.
assert: ((b: '') lastIndexOf: (b: 'baz')) equals: 0.
should: [(b: 'fofofobar') lastIndexOf: 0] signal: Error.
should: [(b: '') lastIndexOf: Object new] signal: Error.
)
public testByteArrayLastIndexOfStartingAt = (
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fofo') startingAt: 10) equals: 3.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fofo') startingAt: 4) equals: 3.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fofo') startingAt: 3) equals: 3.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fofo') startingAt: 1) equals: 1.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'bar') startingAt: 10) equals: 7.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'bar') startingAt: 7) equals: 7.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'bar') startingAt: 6) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'barr') startingAt: 10) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fob') startingAt: 10) equals: 5.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'fob') startingAt: 4) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'baz') startingAt: 10) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'f') startingAt: 10) equals: 5.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'f') startingAt: 4) equals: 3.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'f') startingAt: 2) equals: 1.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'r') startingAt: 10) equals: 9.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'r') startingAt: 9) equals: 9.
assert: ((b: 'fofofobar') lastIndexOf: (b: 'r') startingAt: 8) equals: 0.
assert: ((b: 'fofofobar') lastIndexOf: (b: '') startingAt: 10) equals: 10.
assert: ((b: 'fofofobar') lastIndexOf: (b: '') startingAt: 5) equals: 5.
assert: ((b: 'fofofobar') lastIndexOf: (b: '') startingAt: 1) equals: 1.
assert: ((b: 'tiny') lastIndexOf: (b: 'toolong') startingAt: 4) equals: 0.
assert: ((b: 'tiny') lastIndexOf: (b: 'toolong') startingAt: 2) equals: 0.
assert: ((b: '') lastIndexOf: (b: '') startingAt: 1) equals: 1.
assert: ((b: '') lastIndexOf: (b: 'baz') startingAt: 1) equals: 0.
should: [(b: 'fofofobar') lastIndexOf: 0 startingAt: 1] signal: Error.
should: [(b: '') lastIndexOf: Object new startingAt: 1] signal: Error.
should: [(b: 'fofofobar') lastIndexOf: (b: 'foo') startingAt: 0] signal: Error.
should: [(b: 'fofofobar') lastIndexOf: (b: 'foo') startingAt: 11] signal: Error.
should: [(b: '') lastIndexOf: (b: '') startingAt: 0] signal: Error.
should: [(b: '') lastIndexOf: (b: '') startingAt: 2] signal: Error.
)
public testByteArrayNew = (
assert: (ByteArray new: 0) size equals: 0.
assert: (ByteArray new: 500) size equals: 500.
should: [ByteArray new: -1] signal: Error.
should: [ByteArray new: '10'] signal: Error.
should: [ByteArray new: nil] signal: Error.
should: [ByteArray new] signal: MessageNotUnderstood.
)
public testByteArrayReplace = (
| array replacement result |
array:: ByteArray new: 5.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
replacement:: ByteArray new: 3.
replacement at: 1 put: 110.
replacement at: 2 put: 120.
replacement at: 3 put: 130.
(* Empty. *)
result:: array replaceFrom: 3 to: 2 with: replacement startingAt: 2.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 30.
assert: (array at: 4) equals: 40.
assert: (array at: 5) equals: 50.
(* One. *)
result:: array replaceFrom: 3 to: 3 with: replacement startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 110.
assert: (array at: 4) equals: 40.
assert: (array at: 5) equals: 50.
(* Two. *)
result:: array replaceFrom: 3 to: 4 with: replacement startingAt: 2.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 120.
assert: (array at: 4) equals: 130.
assert: (array at: 5) equals: 50.
(* Three. *)
result:: array replaceFrom: 1 to: 3 with: replacement startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 110.
assert: (array at: 2) equals: 120.
assert: (array at: 3) equals: 130.
assert: (array at: 4) equals: 130.
assert: (array at: 5) equals: 50.
(* After end source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 6] signal: Error.
(* Crossing end source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 3] signal: Error.
(* Before beginning source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: -3] signal: Error.
(* Crossing begining source *)
should: [array replaceFrom: 1 to: 3 with: replacement startingAt: 0] signal: Error.
(* After end destination *)
should: [array replaceFrom: 6 to: 6 with: replacement startingAt: 1] signal: Error.
(* Crossing end destination *)
should: [array replaceFrom: 5 to: 6 with: replacement startingAt: 1] signal: Error.
(* Before beginning destination *)
should: [array replaceFrom: 0 to: 0 with: replacement startingAt: 1] signal: Error.
(* Crossing begining destination *)
should: [array replaceFrom: 0 to: 2 with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: nil to: 3 with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: nil with: replacement startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: 3 with: nil startingAt: 1] signal: Error.
should: [array replaceFrom: 3 to: 3 with: replacement startingAt: nil] signal: Error.
)
public testByteArrayReplaceSlideDown = (
| array result |
array:: ByteArray new: 7.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
array at: 6 put: 60.
array at: 7 put: 70.
result:: array replaceFrom: 3 to: 7 with: array startingAt: 1.
assert: result equals: array.
assert: (array at: 1) equals: 10.
assert: (array at: 2) equals: 20.
assert: (array at: 3) equals: 10.
assert: (array at: 4) equals: 20.
assert: (array at: 5) equals: 30.
assert: (array at: 6) equals: 40.
assert: (array at: 7) equals: 50.
)
public testByteArrayReplaceSlideUp = (
| array result |
array:: ByteArray new: 7.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
array at: 4 put: 40.
array at: 5 put: 50.
array at: 6 put: 60.
array at: 7 put: 70.
result:: array replaceFrom: 1 to: 5 with: array startingAt: 3.
assert: result equals: array.
assert: (array at: 1) equals: 30.
assert: (array at: 2) equals: 40.
assert: (array at: 3) equals: 50.
assert: (array at: 4) equals: 60.
assert: (array at: 5) equals: 70.
assert: (array at: 6) equals: 60.
assert: (array at: 7) equals: 70.
)
public testByteArrayStartsWith = (
| foo zero empty |
foo:: ByteArray new: 3.
foo at: 1 put: 102.
foo at: 2 put: 111.
foo at: 3 put: 111.
zero:: ByteArray new: 1.
zero at: 1 put: 48.
empty:: ByteArray new: 0.
assert: (foo startsWith: 'f').
assert: (foo startsWith: 'foo').
assert: (foo startsWith: foo).
assert: (foo startsWith: '').
assert: (foo startsWith: empty).
assert: (empty startsWith: '').
assert: (empty startsWith: empty).
deny: (foo startsWith: 'toolong').
deny: (foo startsWith: 'fob').
deny: (foo startsWith: 'bar').
deny: (empty startsWith: 'foo').
deny: (empty startsWith: foo).
should: [zero startsWith: 0] signal: Error.
should: [foo startsWith: true] signal: Error.
should: [foo startsWith: nil] signal: Error.
)
public testByteArrayWithAll = (
| array bytearray list result |
array:: Array new: 0.
result:: ByteArray withAll: array.
assert: result size equals: 0.
array:: Array new: 3.
array at: 1 put: 10.
array at: 2 put: 20.
array at: 3 put: 30.
result:: ByteArray withAll: array.
assert: result size equals: 3.
assert: (result at: 1) equals: 10.
assert: (result at: 2) equals: 20.
assert: (result at: 3) equals: 30.
bytearray:: ByteArray new: 0.
result:: ByteArray withAll: bytearray.
assert: result size equals: 0.
bytearray:: ByteArray new: 3.
bytearray at: 1 put: 110.
bytearray at: 2 put: 120.
bytearray at: 3 put: 130.
result:: ByteArray withAll: bytearray.
assert: result size equals: 3.
assert: (result at: 1) equals: 110.
assert: (result at: 2) equals: 120.
assert: (result at: 3) equals: 130.
result:: ByteArray withAll: ''.
assert: result size equals: 0.
result:: ByteArray withAll: 'soup'.
assert: result size equals: 4.
assert: (result at: 1) equals: 115.
assert: (result at: 2) equals: 111.
assert: (result at: 3) equals: 117.
assert: (result at: 4) equals: 112.
list:: List new.
list add: 102.
list add: 111.
list add: 120.
result:: ByteArray withAll: list.
assert: result size equals: 3.
assert: (result at: 1) equals: 102.
assert: (result at: 2) equals: 111.
assert: (result at: 3) equals: 120.
should: [ByteArray withAll: -1] signal: Error.
should: [ByteArray withAll: 16r110000] signal: Error.
should: [ByteArray withAll: nil] signal: Error.
should: [ByteArray withAll: 122 asFloat] signal: Error.
should: [ByteArray withAll: {nil}] signal: Error.
should: [ByteArray withAll: {-1}] signal: Error.
should: [ByteArray withAll: {16r256}] signal: Error.
should: [ByteArray withAll: {nil}] signal: Error.
should: [ByteArray withAll: {122 asFloat}] signal: Error.
should: [ByteArray withAll: {'z'}] signal: Error.
list:: List new.
list add: 256.
should: [String withAll: list] signal: Error.
)
public testIsKindOfByteArray = (
assert: (ByteArray new: 0) isKindOfByteArray.
deny: (Array new: 0) isKindOfByteArray.
deny: 0 isKindOfByteArray.
deny: 1.5 isKindOfByteArray.
deny: 'Array new' isKindOfByteArray.
deny: nil isKindOfByteArray.
)
) : (
TEST_CONTEXT = ()
)
public class ClosureTests = TestContext () (
cannotReturn = (
^[^42]
)
ensure1 = (
[^'try-block'] ensure: [^'ensure-block'].
^'afterward'
)
ensure2 = (
^['try-block'] ensure: ['ensure-block'].
)
ensure3 = (
[^'try-block'] ensure: ['ensure-block'].
^'afterward'
)
public testCannotReturn = (
should: [cannotReturn value] signal: Error.
)
public testCull = (
assert: ([42] cull: 7) equals: 42.
assert: ([42] cull: 7 cull: 9) equals: 42.
assert: ([42] cull: 7 cull: 9 cull: 11) equals: 42.