-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
IntegerTest.class.st
1588 lines (1387 loc) · 82.1 KB
/
IntegerTest.class.st
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
"
SUnit tests for integers
"
Class {
#name : #IntegerTest,
#superclass : #TestCase,
#category : #'Kernel-Tests-Numbers'
}
{ #category : #private }
IntegerTest >> assert: a classAndValueEquals: b [
self assert: a class = b class.
self assert: a = b
]
{ #category : #'tests - basic' }
IntegerTest >> testAsLargerPowerOfTwo [
"Invalid input testing"
"LargeNegativeIntegers"
self should: [(2 raisedTo: 80) negated asLargerPowerOfTwo] raise: DomainError.
"Negative SmallIntegers"
self should: [-1 asLargerPowerOfTwo] raise: DomainError.
"0"
self should: [0 asLargerPowerOfTwo] raise: DomainError.
"Valid inputs"
"Small integers"
self assert: 1 asLargerPowerOfTwo equals: 1.
self assert: 2 asLargerPowerOfTwo equals: 2.
self assert: 3 asLargerPowerOfTwo equals: 4.
self assert: 4 asLargerPowerOfTwo equals: 4.
self assert: 5 asLargerPowerOfTwo equals: 8.
"Large integers"
self assert: ((2 raisedTo: 80) +1) asLargerPowerOfTwo equals: (2 raisedTo: 80 +1).
self assert: (2 raisedTo: 80) asLargerPowerOfTwo equals: (2 raisedTo: 80).
self assert: ((2 raisedTo: 80) - 1) asLargerPowerOfTwo equals: (2 raisedTo: 80)
]
{ #category : #'tests - basic' }
IntegerTest >> testAsSmallerPowerOfTwo [
"Invalid input testing"
"LargeNegativeIntegers"
self should: [(2 raisedTo: 80) negated asSmallerPowerOfTwo] raise: DomainError.
"Negative SmallIntegers"
self should: [-1 asSmallerPowerOfTwo] raise: DomainError.
"0"
self should: [0 asSmallerPowerOfTwo] raise: DomainError.
"Valid inputs"
"Small integers"
self assert: 1 asSmallerPowerOfTwo equals: 1.
self assert: 2 asSmallerPowerOfTwo equals: 2.
self assert: 3 asSmallerPowerOfTwo equals: 2.
self assert: 4 asSmallerPowerOfTwo equals: 4.
self assert: 5 asSmallerPowerOfTwo equals: 4.
"Large integers"
self assert: ((2 raisedTo: 80) +1) asSmallerPowerOfTwo equals: (2 raisedTo: 80).
self assert: (2 raisedTo: 80) asSmallerPowerOfTwo equals: (2 raisedTo: 80).
self assert: ((2 raisedTo: 80) - 1) asSmallerPowerOfTwo equals: (2 raisedTo: 80 - 1)
]
{ #category : #'tests - printing' }
IntegerTest >> testAsStringWithCommas [
self assert: 123456789 asStringWithCommas equals: '123,456,789'.
self assert: -123456789 asStringWithCommas equals: '-123,456,789'
]
{ #category : #'tests - printing' }
IntegerTest >> testBadBase [
"This used to get into an endless loop.
See Pharo #114"
self should: [2 printStringBase: 1] raise: Error.
]
{ #category : #'tests - benchmarks' }
IntegerTest >> testBenchFib [
self assert: (0 benchFib = 1).
self assert: (1 benchFib = 1).
self assert: (2 benchFib = 3).
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testBigReceiverInexactNthRoot [
"
IntegerTest new testBigReceiverInexactNthRoot
"
"Inexact 3rd root (not a whole cube number), so a Float must be answered.
However, receiver is too big for Float arithmethic."
| bigNum result |
bigNum := (100 factorial raisedTo: 3) + 1. "Add 1 so it is not a whole cube"
self assert: bigNum asFloat isInfinite. "Otherwise, we chose a bad sample"
result := bigNum nthRoot: 3.
self assert: result isFloat.
self deny: result isInfinite.
self assert: result = 100 factorial asFloat. "No other float is closer. See following line"
self assert: 100 factorial asFloat = (100 factorial+1) asFloat
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testBigReceiverInexactSqrt [
"
IntegerTest new testBigReceiverInexactSqrt
"
"Inexact 3rd root (not a whole cube number), so a Float must be answered.
However, receiver is too big for Float arithmethic."
| bigNum result |
bigNum := 100 factorial squared + 1. "Add 1 so it is not a whole square"
self assert: bigNum asFloat isInfinite. "Otherwise, we chose a bad sample"
result := bigNum sqrt.
self assert: result isFloat.
self deny: result isInfinite.
self assert: result = 100 factorial asFloat. "No other float is closer. See following lines"
self assert: (result successor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.
self assert: (result predecessor asFraction squared - bigNum) abs >= (result asFraction squared - bigNum) abs.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitAnd [
self assert: (2r1100 bitAnd: 2r1010) equals: 2r1000.
self assert: (2r1100 & 2r1010) equals: 2r1000.
self assert: (-2 bitAnd: 16rFFFFFFFF) equals: 16rFFFFFFFE.
self assert: (-2 & 16rFFFFFFFF) equals: 16rFFFFFFFE.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitAt [
| trials bitSequence2 |
self assert: (2r10 bitAt: 1) equals: 0.
self assert: (2r10 bitAt: 2) equals: 1.
self
assert: ((1 to: 100) allSatisfy: [:i | (0 bitAt: i) = 0])
description: 'all bits of zero are set to zero'.
self
assert: ((1 to: 100) allSatisfy: [:i | (-1 bitAt: i) = 1])
description: 'In two complements, all bits of -1 are set to 1'.
trials := #(
'2r10010011'
'2r11100100'
'2r10000000'
'2r0000101011011001'
'2r1000101011011001'
'2r0101010101011000'
'2r0010011110110010'
'2r0010011000000000'
'2r00100111101100101000101011011001'
'2r01110010011110110010100110101101'
'2r10101011101011001010000010110110'
'2r10101000000000000000000000000000'
'2r0010101110101001110010100000101101100010011110110010100010101100'
'2r1010101110101100101000001011011000100111101100101000101011011001'
'2r1010101110101000000000000000000000000000000000000000000000000000').
trials do: [:bitSequence | | aNumber |
aNumber := Number readFrom: bitSequence.
bitSequence2 := (bitSequence size - 2 to: 1 by: -1) inject: '2r' into: [:string :i | string copyWith: (Character digitValue: (aNumber bitAt: i))].
self assert: bitSequence2 = bitSequence].
trials do: [:bitSequence | | bitInvert |
bitInvert := -1 - (Number readFrom: bitSequence).
bitSequence2 := (bitSequence size - 2 to: 1 by: -1) inject: '2r' into: [:string :i | string copyWith: (Character digitValue: 1 - (bitInvert bitAt: i))].
self assert: bitSequence2 = bitSequence description: '-1-x is similar to a bitInvert operation in two complement']
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitClear [
self assert: (2r1111 bitClear: 2r1000) equals: 2r0111.
self assert: (2r1111 bitClear: 2r0100) equals: 2r1011.
self assert: (2r1111 bitClear: 2r0010) equals: 2r1101.
self assert: (2r1111 bitClear: 2r0001) equals: 2r1110.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitLogic [
"This little suite of tests is designed to verify correct operation of most of Pharo's bit manipulation code, including two's complement representation of negative values. It was written in a hurry and is probably lacking several important checks."
| n |
"Shift 1 bit left then right and test for 1"
1 to: 100 do: [:i | self assert: ((1 bitShift: i) bitShift: i negated) equals: 1].
"Shift -1 left then right and test for 1"
1 to: 100 do: [:i | self assert: ((-1 bitShift: i) bitShift: i negated) equals: -1].
"And a single bit with -1 and test for same value"
1 to: 100 do: [:i | self assert: ((1 bitShift: i) bitAnd: -1) equals: (1 bitShift: i)].
"Verify that (n bitAnd: n negated) = n for single bits"
1 to: 100 do: [:i | n := 1 bitShift: i. self assert: (n bitAnd: n negated) equals: n].
"Verify that n negated = (n complemented + 1) for single bits"
1 to: 100 do: [:i |
n := 1 bitShift: i.
self assert: n negated equals: ((n bitXor: -1) + 1)].
"Verify that (n + n complemented) = -1 for single bits"
1 to: 100 do: [:i |
n := 1 bitShift: i.
self assert: (n + (n bitXor: -1)) equals: -1].
"Verify that n negated = (n complemented +1) for single bits"
1 to: 100 do: [:i |
n := 1 bitShift: i.
self assert: n negated equals: ((n bitXor: -1) + 1)]
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitMask [
self assert: (2r11 allMask: 2r11).
self deny: (2r10 allMask: 2r11).
self deny: (2r01 allMask: 2r11).
self assert: (2r10 anyMask: 2r11).
self assert: (2r01 anyMask: 2r11).
self deny: (2r00 anyMask: 2r11).
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitOr [
self assert: (2r0101 | 2r1010) equals: 2r1111.
self assert: (2r0101 bitOr: 2r1010) equals: 2r1111.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitShift [
self assert: 2r11 << 2 equals: 2r1100.
self assert: (2r11 bitShift: 2) equals: 2r1100.
self assert: 2r1011 >> 2 equals: 2r10.
self assert: (2r1011 bitShift: -2) equals: 2r10.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitString [
Smalltalk vm wordSize = 4
ifTrue: [
self assert: 2 bitString equals: '0000000000000000000000000000010'.
self assert: -1 bitString equals: '1111111111111111111111111111111'.
self assert: -2 bitString equals: '1111111111111111111111111111110'.
self assert: 2 bitStringLength equals: 31 ].
Smalltalk vm wordSize = 8
ifTrue: [
self assert: 2 bitString equals: '0000000000000000000000000000000000000000000000000000000000010'.
self assert: -1 bitString equals: '1111111111111111111111111111111111111111111111111111111111111'.
self assert: -2 bitString equals: '1111111111111111111111111111111111111111111111111111111111110'.
self assert: 2 bitStringLength equals: 61 ].
"32 minus 1 for immediate encoding = 31 = 30 for number + 1 for sign"
"64 minus 3 for immediate encoding = 61 = 60 for number + 1 for sign"
self assert: 2 bitStringLength equals: (SmallInteger maxVal highBit + 1).
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testBitXor [
self assert: (2r1100 bitXor: 2r1010) equals: 2r0110.
]
{ #category : #'tests - instance creation' }
IntegerTest >> testCreationFromBytes1 [
"It is illegal for a LargeInteger to be less than SmallInteger maxVal. Here we test that Integer>>byte!byte2:byte3:byte4:
reconstructs SmallInteger maxVal as an instance of SmallInteger."
| maxSmallInt hexString byte1 byte2 byte3 byte4 builtInteger |
Smalltalk vm wordSize = 4 ifFalse: [ ^ self skip ].
maxSmallInt := SmallInteger maxVal.
hexString := maxSmallInt printStringHex.
self assert: hexString size equals: 8.
byte4 := Number readFrom: (hexString copyFrom: 1 to: 2) base: 16.
byte3 := Number readFrom: (hexString copyFrom: 3 to: 4) base: 16.
byte2 := Number readFrom: (hexString copyFrom: 5 to: 6) base: 16.
byte1 := Number readFrom: (hexString copyFrom: 7 to: 8) base: 16.
builtInteger := Integer byte1: byte1 byte2: byte2 byte3: byte3 byte4: byte4.
self assert: builtInteger equals: maxSmallInt.
self assert: builtInteger class equals: SmallInteger
]
{ #category : #'tests - instance creation' }
IntegerTest >> testCreationFromBytes2 [
"It is illegal for a LargeInteger to be less than SmallInteger maxVal. Here we test that Integer>>byte!byte2:byte3:byte4:
reconstructs (SmallInteger maxVal + 1) as an instance of LargePositiveInteger."
| maxSmallInt hexString byte1 byte2 byte3 byte4 builtInteger |
Smalltalk vm wordSize = 4 ifFalse: [ ^ self skip ].
maxSmallInt := SmallInteger maxVal.
hexString := (maxSmallInt + 1) printStringHex.
self assert: hexString size equals: 8.
byte4 := Number readFrom: (hexString copyFrom: 1 to: 2) base: 16.
byte3 := Number readFrom: (hexString copyFrom: 3 to: 4) base: 16.
byte2 := Number readFrom: (hexString copyFrom: 5 to: 6) base: 16.
byte1 := Number readFrom: (hexString copyFrom: 7 to: 8) base: 16.
builtInteger := Integer byte1: byte1 byte2: byte2 byte3: byte3 byte4: byte4.
self assert: builtInteger equals: maxSmallInt + 1.
self deny: builtInteger class = SmallInteger
]
{ #category : #'tests - instance creation' }
IntegerTest >> testCreationFromBytes3 [
"It is illegal for a LargeInteger to be less than SmallInteger maxVal. Here we test that Integer>>byte!byte2:byte3:byte4:
reconstructs (SmallInteger maxVal - 1) as an instance of SmallInteger."
| maxSmallInt hexString byte1 byte2 byte3 byte4 builtInteger |
Smalltalk vm wordSize = 4 ifFalse: [ ^ self skip ].
maxSmallInt := SmallInteger maxVal.
hexString := (maxSmallInt - 1) printStringHex.
self assert: hexString size equals: 8.
byte4 := Number readFrom: (hexString copyFrom: 1 to: 2) base: 16.
byte3 := Number readFrom: (hexString copyFrom: 3 to: 4) base: 16.
byte2 := Number readFrom: (hexString copyFrom: 5 to: 6) base: 16.
byte1 := Number readFrom: (hexString copyFrom: 7 to: 8) base: 16.
builtInteger := Integer byte1: byte1 byte2: byte2 byte3: byte3 byte4: byte4.
self assert: builtInteger equals: (maxSmallInt - 1).
self assert: builtInteger class equals: SmallInteger
]
{ #category : #'tests - arithmetic' }
IntegerTest >> testCrossSumBase [
self assert: ((-20 to: 20) collect: [:each | each crossSumBase: 10]) asArray
equals: #(2 10 9 8 7 6 5 4 3 2 1 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 2).
self assert: ((-20 to: 20) collect: [:each | each crossSumBase: 2]) asArray
equals: #(2 3 2 2 1 4 3 3 2 3 2 2 1 3 2 2 1 2 1 1 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2).
self should: [ 10 crossSumBase: 1 ] raise: AssertionFailure
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testDegreeCos [
45 degreeCos. "Following tests use approximate equality, because cosine are generally evaluated using inexact Floating point arithmetic"
self assert: (45 degreeCos squared - (1 / 2)) abs <= Float epsilon.
self assert: (60 degreeCos - (1 / 2)) abs <= Float epsilon.
self assert: (120 degreeCos + (1 / 2)) abs <= Float epsilon.
-360 to: 360 do: [ :i | self assert: (i degreeCos closeTo: i degreesToRadians cos) ]. "Following tests use strict equality which is a requested property of degreeCos"
-10 to: 10 do: [ :k |
self assert: (k * 360 + 90) degreeCos equals: 0.
self assert: (k * 360 - 90) degreeCos equals: 0.
self assert: (k * 360 + 180) degreeCos + 1 equals: 0.
self assert: (k * 360) degreeCos - 1 equals: 0 ]
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testDegreeSin [
45 degreeSin. "Following tests use approximate equality, because sine are generally evaluated using inexact Floating point arithmetic"
self assert: (45 degreeSin squared - (1 / 2)) abs <= Float epsilon.
self assert: (30 degreeSin - (1 / 2)) abs <= Float epsilon.
self assert: (-30 degreeSin + (1 / 2)) abs <= Float epsilon.
-360 to: 360 do: [ :i | self assert: (i degreeSin closeTo: i degreesToRadians sin) ]. "Following tests use strict equality which is a requested property of degreeSin"
-10 to: 10 do: [ :k |
self assert: (k * 360 + 90) degreeSin - 1 equals: 0.
self assert: (k * 360 - 90) degreeSin + 1 equals: 0.
self assert: (k * 360 + 180) degreeSin equals: 0.
self assert: (k * 360) degreeSin equals: 0 ]
]
{ #category : #'tests - instance creation' }
IntegerTest >> testDifferentBases [
"| value |
2 to: 36 do: [:each|
value := 0.
1 to: each-1 do: [:n| value := value + (n * (each raisedToInteger: n))].
value := value negated.
Transcript tab; show: 'self assert: (', value printString, ' printStringBase: ', each printString, ') = ''', (value printStringBase: each), '''.'; cr.
Transcript tab; show: 'self assert: (', value printString, ' radix: ', each printString, ') = ''', (value radix: each), '''.'; cr.
Transcript tab; show: 'self assert: ', value printString, ' printStringHex = ''', (value printStringBase: 16), '''.'; cr.
Transcript tab; show: 'self assert: (', value printString, ' storeStringBase: ', each printString, ') = ''', (value storeStringBase: each), '''.'; cr.
Transcript tab; show: 'self assert: ', value printString, ' storeStringHex = ''', (value storeStringBase: 16), '''.'; cr.
].
"
self assert: 2r10 equals: 2.
self assert: 3r210 equals: 21.
self assert: 4r3210 equals: 228.
self assert: 5r43210 equals: 2930.
self assert: 6r543210 equals: 44790.
self assert: 7r6543210 equals: 800667.
self assert: 8r76543210 equals: 16434824.
self assert: 9r876543210 equals: 381367044.
self assert: 10r9876543210 equals: 9876543210.
self assert: 11rA9876543210 equals: 282458553905.
self assert: 12rBA9876543210 equals: 8842413667692.
self assert: 13rCBA9876543210 equals: 300771807240918.
self assert: 14rDCBA9876543210 equals: 11046255305880158.
self assert: 15rEDCBA9876543210 equals: 435659737878916215.
self assert: 16rFEDCBA9876543210 equals: 18364758544493064720.
self assert: 17rGFEDCBA9876543210 equals: 824008854613343261192.
self assert: 18rHGFEDCBA9876543210 equals: 39210261334551566857170.
self assert: 19rIHGFEDCBA9876543210 equals: 1972313422155189164466189.
self assert: 20rJIHGFEDCBA9876543210 equals: 104567135734072022160664820.
self assert: 21rKJIHGFEDCBA9876543210 equals: 5827980550840017565077671610.
self assert: 22rLKJIHGFEDCBA9876543210 equals: 340653664490377789692799452102.
self assert: 23rMLKJIHGFEDCBA9876543210 equals: 20837326537038308910317109288851.
self assert: 24rNMLKJIHGFEDCBA9876543210 equals: 1331214537196502869015340298036888.
self assert: 25rONMLKJIHGFEDCBA9876543210 equals: 88663644327703473714387251271141900.
self assert: 26rPONMLKJIHGFEDCBA9876543210 equals: 6146269788878825859099399609538763450.
self assert: 27rQPONMLKJIHGFEDCBA9876543210 equals: 442770531899482980347734468443677777577.
self assert: 28rRQPONMLKJIHGFEDCBA9876543210 equals: 33100056003358651440264672384704297711484.
self assert: 29rSRQPONMLKJIHGFEDCBA9876543210 equals: 2564411043271974895869785066497940850811934.
self assert: 30rTSRQPONMLKJIHGFEDCBA9876543210 equals: 205646315052919334126040428061831153388822830.
self assert: 31rUTSRQPONMLKJIHGFEDCBA9876543210 equals: 17050208381689099029767742314582582184093573615.
self assert: 32rVUTSRQPONMLKJIHGFEDCBA9876543210 equals: 1459980823972598128486511383358617792788444579872.
self assert: 33rWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: 128983956064237823710866404905431464703849549412368.
self assert: 34rXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: 11745843093701610854378775891116314824081102660800418.
self assert: 35rYXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: 1101553773143634726491620528194292510495517905608180485.
self assert: 36rZYXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: 106300512100105327644605138221229898724869759421181854980.
self assert: -2r10 equals: -2.
self assert: -3r210 equals: -21.
self assert: -4r3210 equals: -228.
self assert: -5r43210 equals: -2930.
self assert: -6r543210 equals: -44790.
self assert: -7r6543210 equals: -800667.
self assert: -8r76543210 equals: -16434824.
self assert: -9r876543210 equals: -381367044.
self assert: -10r9876543210 equals: -9876543210.
self assert: -11rA9876543210 equals: -282458553905.
self assert: -12rBA9876543210 equals: -8842413667692.
self assert: -13rCBA9876543210 equals: -300771807240918.
self assert: -14rDCBA9876543210 equals: -11046255305880158.
self assert: -15rEDCBA9876543210 equals: -435659737878916215.
self assert: -16rFEDCBA9876543210 equals: -18364758544493064720.
self assert: -17rGFEDCBA9876543210 equals: -824008854613343261192.
self assert: -18rHGFEDCBA9876543210 equals: -39210261334551566857170.
self assert: -19rIHGFEDCBA9876543210 equals: -1972313422155189164466189.
self assert: -20rJIHGFEDCBA9876543210 equals: -104567135734072022160664820.
self assert: -21rKJIHGFEDCBA9876543210 equals: -5827980550840017565077671610.
self assert: -22rLKJIHGFEDCBA9876543210 equals: -340653664490377789692799452102.
self assert: -23rMLKJIHGFEDCBA9876543210 equals: -20837326537038308910317109288851.
self assert: -24rNMLKJIHGFEDCBA9876543210 equals: -1331214537196502869015340298036888.
self assert: -25rONMLKJIHGFEDCBA9876543210 equals: -88663644327703473714387251271141900.
self assert: -26rPONMLKJIHGFEDCBA9876543210 equals: -6146269788878825859099399609538763450.
self assert: -27rQPONMLKJIHGFEDCBA9876543210 equals: -442770531899482980347734468443677777577.
self assert: -28rRQPONMLKJIHGFEDCBA9876543210 equals: -33100056003358651440264672384704297711484.
self assert: -29rSRQPONMLKJIHGFEDCBA9876543210 equals: -2564411043271974895869785066497940850811934.
self assert: -30rTSRQPONMLKJIHGFEDCBA9876543210 equals: -205646315052919334126040428061831153388822830.
self assert: -31rUTSRQPONMLKJIHGFEDCBA9876543210 equals: -17050208381689099029767742314582582184093573615.
self assert: -32rVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -1459980823972598128486511383358617792788444579872.
self assert: -33rWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -128983956064237823710866404905431464703849549412368.
self assert: -34rXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -11745843093701610854378775891116314824081102660800418.
self assert: -35rYXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -1101553773143634726491620528194292510495517905608180485.
self assert: -36rZYXWVUTSRQPONMLKJIHGFEDCBA9876543210 equals: -106300512100105327644605138221229898724869759421181854980.
]
{ #category : #'tests - other' }
IntegerTest >> testDigitSum [
self assert: 1234 digitSum equals: 10
]
{ #category : #'tests - other' }
IntegerTest >> testDigitSumOnNegative [
self assert: -1234 digitSum equals: 10
]
{ #category : #'tests - basic' }
IntegerTest >> testDigitsAccess [
self assert: (42 digitAt: 2 base: 10) equals: 4.
self assert: (42 digitAt: 1 base: 10) equals: 2.
self assert: (1 digitAt: 2 base: 10) equals: 0.
self assert: (16rFF digitAt: 1 base: 16) equals: 15.
self assert: (42 decimalDigitAt: 2) equals: 4.
self assert: (42 decimalDigitAt: 1) equals: 2.
self assert: (42 decimalDigitLength) equals: 2.
self assert: (0 decimalDigitLength) equals: 1.
self assert: (100000000000 decimalDigitLength) equals: 12. "large integer"
"last digit of the integer base 256"
self assert: (255 lastDigit) equals: 255.
self assert: (256 lastDigit) equals: 1.
self assert: ((256*256-1) lastDigit) equals: 255.
self assert: ((256*256) lastDigit) equals: 1.
]
{ #category : #'tests - basic' }
IntegerTest >> testEven [
self deny: (1073741825 even).
self assert: (1073741824 even).
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testExactRaisedTo [
"
IntegerTest new testExactRaisedTo
"
self assert: (4 raisedTo: 1/2) classAndValueEquals: 2.
self assert: (9 raisedTo: 1/2) classAndValueEquals: 3.
self assert: (9 raisedTo: -1/2) classAndValueEquals: 1/3.
self assert: (-1 raisedTo: 1/3) classAndValueEquals: -1.
#( 1 5 29 135 1234 567890 123123123 456456456456 98765432109876543210987654321 987123987123987123987123987123987123987123987123) do: [ :i |
self assert: (i squared raisedTo: 1/2) classAndValueEquals: i.
self assert: (i negated squared raisedTo: 1/2) classAndValueEquals: i ].
self assert: (8 raisedTo: 1/3) classAndValueEquals: 2.
self assert: (27 raisedTo: 1/3) classAndValueEquals: 3.
#( 1 5 29 135 1234 567890 123123123 456456456456 98765432109876543210987654321 987123987123987123987123987123987123987123987123) do: [ :i |
self assert: ((i raisedTo: 3) raisedTo: 1/3) classAndValueEquals: i.
self assert: ((i negated raisedTo: 3) raisedTo: 1/3) classAndValueEquals: i negated ].
self assert: (4 raisedTo: 3/2) classAndValueEquals: 8.
self assert: (8 raisedTo: 2/3) classAndValueEquals: 4.
self assert: (8 raisedTo: -2/3) classAndValueEquals: 1/4.
#( 1 5 29 135 1234 567890 123123123 456456456456 98765432109876543210987654321 987123987123987123987123987123987123987123987123) do: [ :i |
self assert: ((i raisedTo: 3) raisedTo: 2/3) classAndValueEquals: i*i.
self assert: ((i raisedTo: 2) raisedTo: 3/2) classAndValueEquals: i*i*i.
self assert: ((i negated raisedTo: 3) raisedTo: 2/3) classAndValueEquals: i*i.
self assert: ((i negated raisedTo: 2) raisedTo: 3/2) classAndValueEquals: i*i*i ].
self assert: (32 raisedTo: 3/5) classAndValueEquals: 8.
self assert: (8 raisedTo: 5/3) classAndValueEquals: 32.
#( 1 5 29 135 1234 567890 123123123 456456456456 98765432109876543210987654321 987123987123987123987123987123987123987123987123) do: [ :i |
self assert: ((i raisedTo: 5) raisedTo: 3/5) classAndValueEquals: i*i*i.
self assert: ((i raisedTo: 3) raisedTo: 5/3) classAndValueEquals: i*i*i*i*i.
self assert: ((i negated raisedTo: 5) raisedTo: 3/5) classAndValueEquals: (i*i*i) negated.
self assert: ((i negated raisedTo: 3) raisedTo: 5/3) classAndValueEquals: (i*i*i*i*i) negated.
self assert: ((i raisedTo: -5) raisedTo: 3/5) classAndValueEquals: 1/(i*i*i).
self assert: ((i raisedTo: -3) raisedTo: 5/3) classAndValueEquals: 1/(i*i*i*i*i).
self assert: ((i negated raisedTo: -5) raisedTo: 3/5) classAndValueEquals: -1/(i*i*i).
self assert: ((i negated raisedTo: -3) raisedTo: 5/3) classAndValueEquals: -1/(i*i*i*i*i).
self assert: ((i raisedTo: 5) raisedTo: -3/5) classAndValueEquals: 1/(i*i*i).
self assert: ((i raisedTo: 3) raisedTo: -5/3) classAndValueEquals: 1/(i*i*i*i*i).
self assert: ((i negated raisedTo: 5) raisedTo: -3/5) classAndValueEquals: -1/(i*i*i).
self assert: ((i negated raisedTo: 3) raisedTo: -5/3) classAndValueEquals: -1/(i*i*i*i*i).
"No exact result => Float result"
self assert: ((i raisedTo: 3) +1 raisedTo: 5/3) isFloat.
self assert: ((i negated raisedTo: 3) -1 raisedTo: 5/3) isFloat ].
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testExactSqrt [
"
IntegerTest new testExactSqrt
"
self assert: 4 sqrt classAndValueEquals: 2.
self assert: 9 sqrt classAndValueEquals: 3.
self assert: Float maxExactInteger squared sqrt classAndValueEquals: Float maxExactInteger.
self assert: (Float maxExactInteger+1) squared sqrt classAndValueEquals: Float maxExactInteger+1.
#( 1 5 29 135 1234 567890 123123123 456456456456 98765432109876543210987654321 987123987123987123987123987123987123987123987123) do: [ :i |
self assert: i squared sqrt classAndValueEquals: i ]
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testFloorLog [
self assert: (100 floorLog: 10) = 2.
self assert: (((2 raisedTo: Float emax + 3) floorLog: 10) = (2 log*(Float emax + 3)) floor) description: 'Integer>>floorLog: should not overflow'
]
{ #category : #'tests - printing' }
IntegerTest >> testHex [
self assert: 0 hex = '16r0'.
self assert: 12 hex = '16rC'.
self assert: 1234 hex = '16r4D2'.
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testHighBit [
| suite |
self assert: (2r1110 highBit) equals: 4.
self assert: (2r0110 highBit) equals: 3.
self assert: (2r0000 highBit) equals: 0.
suite := (0 to: 1024) asArray , #(16rFDFD 16rFFFF 16r1000 16r1000000 16r1000001 16r70000000 16r7AFAFAFA ) , {SmallInteger maxVal . SmallInteger maxVal+1}.
suite := suite , (suite collect: [:e | e raisedTo: 20]).
suite do: [:anInteger |
| highBit shifted |
highBit := 0.
shifted := 1.
[shifted > anInteger] whileFalse: [highBit := highBit+1. shifted := shifted bitShift: 1].
self assert: anInteger highBit = highBit].
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testHighBitOfMagnitude [
| suite |
suite := (0 to: 1024) asArray , #(16rFDFD 16rFFFF 16r1000 16r1000000 16r1000001 16r70000000 16r7AFAFAFA ) , {SmallInteger maxVal . SmallInteger maxVal+1}.
suite := suite , (suite collect: [:e | e raisedTo: 20]).
suite do: [:anInteger |
| highBit shifted |
highBit := 0.
shifted := 1.
[shifted > anInteger] whileFalse: [highBit := highBit+1. shifted := shifted bitShift: 1].
self assert: anInteger highBitOfMagnitude = highBit.
self assert: anInteger negated highBitOfMagnitude = highBit].
]
{ #category : #'tests - printing' }
IntegerTest >> testIntegerHex [
| result |
result := 15 asInteger hex.
self assert: result = '16rF'.
result := 0 asInteger hex.
self assert: result = '16r0'.
result := 255 asInteger hex.
self assert: result = '16rFF'.
result := 90 asInteger hex.
self assert: result = '16r5A'
]
{ #category : #'tests - printing' }
IntegerTest >> testIntegerPadding [
self assert: (1 printStringBase: 10 length: 0 padded: false) equals: '1'.
self assert: (1 printStringBase: 10 length: 1 padded: false) equals: '1'.
self assert: (1 printStringBase: 10 length: 2 padded: false) equals: ' 1'.
self assert: (1024 printStringBase: 10 length: 19 padded: false) equals: ' 1024'.
self assert: (1024 printStringBase: 10 length: -1 padded: false) equals: '1024'.
self assert: (1024 printStringBase: 10 length: 5 padded: false) equals: ' 1024'.
self assert: (-1024 printStringBase: 10 length: 5 padded: false) equals: '-1024'.
self assert: (-1024 printStringBase: 10 length: 19 padded: false) equals: ' -1024'.
self assert: (1 printStringBase: 10 length: 0 padded: true) equals: '1'.
self assert: (1 printStringBase: 10 length: 1 padded: true) equals: '1'.
self assert: (1 printStringBase: 10 length: 2 padded: true) equals: '01'.
self assert: (1024 printStringBase: 10 length: 19 padded: true) equals: '0000000000000001024'.
self assert: (1024 printStringBase: 10 length: -1 padded: true) equals: '1024'.
self assert: (1024 printStringBase: 10 length: 5 padded: true) equals: '01024'.
self assert: (-1024 printStringBase: 10 length: 5 padded: true) equals: '-1024'.
self assert: (-1024 printStringBase: 10 length: 19 padded: true) equals: '-000000000000001024'.
self assert: (1 printStringBase: 16 length: 0 padded: false) equals: '1'.
self assert: (1 printStringBase: 16 length: 1 padded: false) equals: '1'.
self assert: (1 printStringBase: 16 length: 2 padded: false) equals: ' 1'.
self assert: (2047 printStringBase: 16 length: 19 padded: false) equals: ' 7FF'.
self assert: (2047 printStringBase: 16 length: -1 padded: false) equals: '7FF'.
self assert: (2047 printStringBase: 16 length: 4 padded: false) equals: ' 7FF'.
self assert: (-2047 printStringBase: 16 length: 4 padded: false) equals: '-7FF'.
self assert: (-2047 printStringBase: 16 length: 19 padded: false) equals: ' -7FF'.
self assert: (1 printStringBase: 16 length: 0 padded: true) equals: '1'.
self assert: (1 printStringBase: 16 length: 1 padded: true) equals: '1'.
self assert: (1 printStringBase: 16 length: 2 padded: true) equals: '01'.
self assert: (2047 printStringBase: 16 length: 19 padded: true) equals: '00000000000000007FF'.
self assert: (2047 printStringBase: 16 length: -1 padded: true) equals: '7FF'.
self assert: (2047 printStringBase: 16 length: 4 padded: true) equals: '07FF'.
self assert: (-2047 printStringBase: 16 length: 4 padded: true) equals: '-7FF'.
self assert: (-2047 printStringBase: 16 length: 19 padded: true) equals: '-0000000000000007FF'.
self assert: (1 storeStringBase: 10 length: 0 padded: false) equals: '1'.
self assert: (1 storeStringBase: 10 length: 1 padded: false) equals: '1'.
self assert: (1 storeStringBase: 10 length: 2 padded: false) equals: ' 1'.
self assert: (1024 storeStringBase: 10 length: 19 padded: false) equals: ' 1024'.
self assert: (1024 storeStringBase: 10 length: -1 padded: false) equals: '1024'.
self assert: (1024 storeStringBase: 10 length: 5 padded: false) equals:' 1024'.
self assert: (-1024 storeStringBase: 10 length: 5 padded: false) equals: '-1024'.
self assert: (-1024 storeStringBase: 10 length: 19 padded: false) equals: ' -1024'.
self assert: (1 storeStringBase: 10 length: 0 padded: true) equals: '1'.
self assert: (1 storeStringBase: 10 length: 1 padded: true) equals: '1'.
self assert: (1 storeStringBase: 10 length: 2 padded: true) equals: '01'.
self assert: (1024 storeStringBase: 10 length: 19 padded: true) equals: '0000000000000001024'.
self assert: (1024 storeStringBase: 10 length: -1 padded: true) equals: '1024'.
self assert: (1024 storeStringBase: 10 length: 5 padded: true) equals: '01024'.
self assert: (-1024 storeStringBase: 10 length: 5 padded: true) equals: '-1024'.
self assert: (-1024 storeStringBase: 10 length: 19 padded: true) equals: '-000000000000001024'.
self assert: (1 storeStringBase: 16 length: 0 padded: false) equals: '16r1'.
self assert: (1 storeStringBase: 16 length: 4 padded: false) equals: '16r1'.
self assert: (1 storeStringBase: 16 length: 5 padded: false) equals: ' 16r1'.
self assert: (2047 storeStringBase: 16 length: 19 padded: false) equals: ' 16r7FF'.
self assert: (2047 storeStringBase: 16 length: -1 padded: false) equals: '16r7FF'.
self assert: (2047 storeStringBase: 16 length: 7 padded: false) equals: ' 16r7FF'.
self assert: (-2047 storeStringBase: 16 length: 7 padded: false) equals: '-16r7FF'.
self assert: (-2047 storeStringBase: 16 length: 19 padded: false) equals: ' -16r7FF'.
self assert: (1 storeStringBase: 16 length: 0 padded: true) equals: '16r1'.
self assert: (1 storeStringBase: 16 length: 4 padded: true) equals: '16r1'.
self assert: (1 storeStringBase: 16 length: 5 padded: true) equals: '16r01'.
self assert: (2047 storeStringBase: 16 length: 19 padded: true) equals: '16r00000000000007FF'.
self assert: (2047 storeStringBase: 16 length: -1 padded: true) equals: '16r7FF'.
self assert: (2047 storeStringBase: 16 length: 7 padded: true) equals: '16r07FF'.
self assert: (-2047 storeStringBase: 16 length: 7 padded: true) equals: '-16r7FF'.
self assert: (-2047 storeStringBase: 16 length: 19 padded: true) equals: '-16r0000000000007FF'
]
{ #category : #'tests - instance creation' }
IntegerTest >> testIntegerReadsNotOkFromStream [
self should: [Integer readFrom: 'a23' readStream] raise: Error.
self should: [Integer readFrom: '-a23' readStream] raise: Error.
self should: [Integer readFrom: 'a3' readStream base: 8] raise: Error.
self should: [Integer readFrom: '-a3' readStream base: 8] raise: Error.
]
{ #category : #'tests - instance creation' }
IntegerTest >> testIntegerReadsNotOkFromString [
self should: [Integer readFrom: 'aaa'] raise: Error.
self should: [Integer readFrom: '-aaa'] raise: Error.
self should: [Integer readFrom: 'a3' base: 8] raise: Error.
]
{ #category : #'tests - instance creation' }
IntegerTest >> testIntegerReadsOkFromStream [
self assert: (Integer readFrom: '123' readStream) = 123.
self assert: (Integer readFrom: '-123' readStream) = -123.
self assert: (Integer readFrom: 'a3' readStream base: 16) = 163.
self assert: (Integer readFrom: '-a3' readStream base: 16) = -163.
self assert: (Integer readFrom: '3a' readStream base: 10) = 3.
]
{ #category : #'tests - instance creation' }
IntegerTest >> testIntegerReadsOkFromString [
self assert: (Integer readFrom: '123') = 123.
self assert: (Integer readFrom: '-123') = -123.
self assert: (Integer readFrom: 'a3' base: 16) = 163.
self assert: (Integer readFrom: '-a3' base: 16) = -163.
]
{ #category : #'tests - basic' }
IntegerTest >> testIsInteger [
self assert: (0 isInteger).
]
{ #category : #'tests - basic' }
IntegerTest >> testIsPowerOfTwo [
"LargeNegativeIntegers"
self deny: (2 raisedTo: 80) negated isPowerOfTwo.
"Negative SmallIntegers"
self deny: (-1 isPowerOfTwo).
" 0, incase implementation has forgotten edge case"
self deny: (0 isPowerOfTwo).
"Positive SmallIntegers"
self assert: (1 isPowerOfTwo).
self assert: (2 isPowerOfTwo).
self deny: (3 isPowerOfTwo).
self assert: (4 isPowerOfTwo).
self deny: (5 isPowerOfTwo).
" LargePositiveIntegers"
self deny: ((2 raisedTo: 80) - 1) isPowerOfTwo.
self assert: (2 raisedTo: 80) isPowerOfTwo.
self deny: ((2 raisedTo: 80) + 1) isPowerOfTwo
]
{ #category : #'tests - basic' }
IntegerTest >> testIsPowerOfTwoM6873 [
"This is a non regression test for http://bugs.squeak.org/view.php?id=6873"
self deny: ((1 to: 80) anySatisfy: [:n | (2 raisedTo: n) negated isPowerOfTwo])
description: 'A negative integer cannot be a power of two'.
]
{ #category : #'tests - basic' }
IntegerTest >> testIsPrime [
"The following tests should return 'true'"
self assert: 17 isPrime.
self assert: 78901 isPrime.
self assert: 104729 isPrime.
self assert: 15485863 isPrime.
self assert: 2038074743 isPrime.
self assert: 29996224275833 isPrime.
"The following tests should return 'false' (first 5 are Carmichael integers)"
self deny: 561 isPrime.
self deny: 2821 isPrime.
self deny: 6601 isPrime.
self deny: 10585 isPrime.
self deny: 15841 isPrime.
self deny: 256 isPrime.
self deny: 29996224275831 isPrime.
]
{ #category : #'tests - basic' }
IntegerTest >> testIsPrime2 [
"Not primes:"
#(-100 -5 -3 -2 -1 0 1) do: [ :each |
self deny: each isPrime ].
"The following tests should return 'true'"
#(17 78901 104729 15485863 2038074743) do: [ :each |
self assert: each isPrime ].
"The following tests should return 'false' (first 5 are Carmichael integers)"
#(561 2821 6601 10585 15841 256 29996224275831) do: [ :each |
self deny: each isPrime ].
]
{ #category : #'tests - basic' }
IntegerTest >> testIsProbablyPrime [
"Not primes:"
#(-100 -5 -3 -2 -1 0 1) do: [ :each |
self deny: each isProbablyPrime ].
"The following tests should return 'true'"
#(17 78901 104729 15485863 2038074743 29996224275833) do: [ :each |
self assert: each isProbablyPrime ].
"The following tests should return 'false' (first 5 are Carmichael integers)"
#(561 2821 6601 10585 15841 256 29996224275831) do: [ :each |
self deny: each isProbablyPrime ].
]
{ #category : #'tests - basic' }
IntegerTest >> testLargePrimesUpTo [
| nn |
nn := (2 raisedTo: 17) - 1.
self deny: (Integer primesUpTo: nn) last = nn.
self assert: (Integer primesUpTo: nn + 1) last = nn.
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testLn [
self assert: (100 ln closeTo: 10 ln*2).
self assert: ((2 raisedTo: Float emax + 3) ln closeTo: 2 ln*(Float emax + 3)) description: 'Integer>>ln should not overflow'
]
{ #category : #'tests - mathematical functions' }
IntegerTest >> testLog [
self assert: (100 log closeTo: 2).
self assert: ((2 raisedTo: Float emax + 3) log closeTo: 2 log*(Float emax + 3)) description: 'Integer>>log should not overflow'
]
{ #category : #'tests - bitLogic' }
IntegerTest >> testLowBit [
| suite |
"Simple examples"
self assert: (2r1011 lowBit) equals: 1.
self assert: (2r1010 lowBit) equals: 2.
self assert: (2r000000 lowBit) equals: 0.
suite := (0 to: 1024) asArray , #(16rFDFD 16rFFFF 16r1000 16r1000000 16r1000001 16r70000000 16r7AFAFAFA ) , {SmallInteger maxVal . SmallInteger maxVal+1}.
suite := suite , (suite collect: [:e | e raisedTo: 20]).
suite do: [:anInteger |
| lowBit |
lowBit := (anInteger respondsTo: #bitAt:)
ifTrue: [(1 to: anInteger highBit) detect: [:bitIndex | (anInteger bitAt: bitIndex) ~= 0] ifNone: [0]]
ifFalse: [(1 to: anInteger highBit) detect: [:bitIndex | (anInteger bitAnd: (1 bitShift: bitIndex-1)) ~= 0] ifNone: [0]].
self assert: anInteger lowBit = lowBit.
self assert: anInteger negated lowBit = lowBit].
]
{ #category : #'tests - printing' }
IntegerTest >> testNegativeIntegerPrinting [
self assert: (-2 printStringBase: 2) equals: '-10'.
self assert: (-2 radix: 2) equals: '-10'.
self assert: -2 printStringHex equals: '-2'.
self assert: (-2 storeStringBase: 2) equals: '-2r10'.
self assert: -2 storeStringHex equals: '-16r2'.
self assert: (-21 printStringBase: 3) equals: '-210'.
self assert: (-21 radix: 3) equals: '-210'.
self assert: -21 printStringHex equals: '-15'.
self assert: (-21 storeStringBase: 3) equals: '-3r210'.
self assert: -21 storeStringHex equals: '-16r15'.
self assert: (-228 printStringBase: 4) equals: '-3210'.
self assert: (-228 radix: 4) equals: '-3210'.
self assert: -228 printStringHex equals: '-E4'.
self assert: (-228 storeStringBase: 4) equals: '-4r3210'.
self assert: -228 storeStringHex equals: '-16rE4'.
self assert: (-2930 printStringBase: 5) equals: '-43210'.
self assert: (-2930 radix: 5) equals: '-43210'.
self assert: -2930 printStringHex equals: '-B72'.
self assert: (-2930 storeStringBase: 5) equals: '-5r43210'.
self assert: -2930 storeStringHex equals: '-16rB72'.
self assert: (-44790 printStringBase: 6) equals: '-543210'.
self assert: (-44790 radix: 6) equals: '-543210'.
self assert: -44790 printStringHex equals: '-AEF6'.
self assert: (-44790 storeStringBase: 6) equals: '-6r543210'.
self assert: -44790 storeStringHex equals: '-16rAEF6'.
self assert: (-800667 printStringBase: 7) equals: '-6543210'.
self assert: (-800667 radix: 7) equals: '-6543210'.
self assert: -800667 printStringHex equals: '-C379B'.
self assert: (-800667 storeStringBase: 7) equals: '-7r6543210'.
self assert: -800667 storeStringHex equals: '-16rC379B'.
self assert: (-16434824 printStringBase: 8) equals: '-76543210'.
self assert: (-16434824 radix: 8) equals: '-76543210'.
self assert: -16434824 printStringHex equals: '-FAC688'.
self assert: (-16434824 storeStringBase: 8) equals: '-8r76543210'.
self assert: -16434824 storeStringHex equals: '-16rFAC688'.
self assert: (-381367044 printStringBase: 9) equals: '-876543210'.
self assert: (-381367044 radix: 9) equals: '-876543210'.
self assert: -381367044 printStringHex equals: '-16BB3304'.
self assert: (-381367044 storeStringBase: 9) equals: '-9r876543210'.
self assert: -381367044 storeStringHex equals: '-16r16BB3304'.
self assert: (-9876543210 printStringBase: 10) equals: '-9876543210'.
self assert: (-9876543210 radix: 10) equals: '-9876543210'.
self assert: -9876543210 printStringHex equals: '-24CB016EA'.
self assert: (-9876543210 storeStringBase: 10) equals: '-9876543210'.
self assert: -9876543210 storeStringHex equals: '-16r24CB016EA'.
self assert: (-282458553905 printStringBase: 11) equals: '-A9876543210'.
self assert: (-282458553905 radix: 11) equals: '-A9876543210'.
self assert: -282458553905 printStringHex equals: '-41C3D77E31'.
self assert: (-282458553905 storeStringBase: 11) equals: '-11rA9876543210'.
self assert: -282458553905 storeStringHex equals: '-16r41C3D77E31'.
self assert: (-8842413667692 printStringBase: 12) equals: '-BA9876543210'.
self assert: (-8842413667692 radix: 12) equals: '-BA9876543210'.
self assert: -8842413667692 printStringHex equals: '-80AC8ECF56C'.
self assert: (-8842413667692 storeStringBase: 12) equals: '-12rBA9876543210'.
self assert: -8842413667692 storeStringHex equals: '-16r80AC8ECF56C'.
self assert: (-300771807240918 printStringBase: 13) equals: '-CBA9876543210'.
self assert: (-300771807240918 radix: 13) equals: '-CBA9876543210'.
self assert: -300771807240918 printStringHex equals: '-1118CE4BAA2D6'.
self assert: (-300771807240918 storeStringBase: 13) equals: '-13rCBA9876543210'.
self assert: -300771807240918 storeStringHex equals: '-16r1118CE4BAA2D6'.
self assert: (-11046255305880158 printStringBase: 14) equals: '-DCBA9876543210'.
self assert: (-11046255305880158 radix: 14) equals: '-DCBA9876543210'.
self assert: -11046255305880158 printStringHex equals: '-273E82BB9AF25E'.
self assert: (-11046255305880158 storeStringBase: 14) equals: '-14rDCBA9876543210'.
self assert: -11046255305880158 storeStringHex equals: '-16r273E82BB9AF25E'.
self assert: (-435659737878916215 printStringBase: 15) equals: '-EDCBA9876543210'.
self assert: (-435659737878916215 radix: 15) equals: '-EDCBA9876543210'.
self assert: -435659737878916215 printStringHex equals: '-60BC6392F366C77'.
self assert: (-435659737878916215 storeStringBase: 15) equals: '-15rEDCBA9876543210'.
self assert: -435659737878916215 storeStringHex equals: '-16r60BC6392F366C77'.
self assert: (-18364758544493064720 printStringBase: 16) equals: '-FEDCBA9876543210'.
self assert: (-18364758544493064720 radix: 16) equals: '-FEDCBA9876543210'.
self assert: -18364758544493064720 printStringHex equals: '-FEDCBA9876543210'.
self assert: (-18364758544493064720 storeStringBase: 16) equals: '-16rFEDCBA9876543210'.
self assert: -18364758544493064720 storeStringHex equals: '-16rFEDCBA9876543210'.
self assert: (-824008854613343261192 printStringBase: 17) equals: '-GFEDCBA9876543210'.
self assert: (-824008854613343261192 radix: 17) equals: '-GFEDCBA9876543210'.
self assert: -824008854613343261192 printStringHex equals: '-2CAB6B877C1CD2D208'.
self assert: (-824008854613343261192 storeStringBase: 17) equals: '-17rGFEDCBA9876543210'.
self assert: -824008854613343261192 storeStringHex equals: '-16r2CAB6B877C1CD2D208'.
self assert: (-39210261334551566857170 printStringBase: 18) equals: '-HGFEDCBA9876543210'.
self assert: (-39210261334551566857170 radix: 18) equals: '-HGFEDCBA9876543210'.
self assert: -39210261334551566857170 printStringHex equals: '-84D97AFCAE81415B3D2'.
self assert: (-39210261334551566857170 storeStringBase: 18) equals: '-18rHGFEDCBA9876543210'.
self assert: -39210261334551566857170 storeStringHex equals: '-16r84D97AFCAE81415B3D2'.
self assert: (-1972313422155189164466189 printStringBase: 19) equals: '-IHGFEDCBA9876543210'.
self assert: (-1972313422155189164466189 radix: 19) equals: '-IHGFEDCBA9876543210'.
self assert: -1972313422155189164466189 printStringHex equals: '-1A1A75329C5C6FC00600D'.
self assert: (-1972313422155189164466189 storeStringBase: 19) equals: '-19rIHGFEDCBA9876543210'.
self assert: -1972313422155189164466189 storeStringHex equals: '-16r1A1A75329C5C6FC00600D'.
self assert: (-104567135734072022160664820 printStringBase: 20) equals: '-JIHGFEDCBA9876543210'.
self assert: (-104567135734072022160664820 radix: 20) equals: '-JIHGFEDCBA9876543210'.
self assert: -104567135734072022160664820 printStringHex equals: '-567EF3C9636D242A8C68F4'.
self assert: (-104567135734072022160664820 storeStringBase: 20) equals: '-20rJIHGFEDCBA9876543210'.
self assert: -104567135734072022160664820 storeStringHex equals: '-16r567EF3C9636D242A8C68F4'.
self assert: (-5827980550840017565077671610 printStringBase: 21) equals: '-KJIHGFEDCBA9876543210'.
self assert: (-5827980550840017565077671610 radix: 21) equals: '-KJIHGFEDCBA9876543210'.
self assert: -5827980550840017565077671610 printStringHex equals: '-12D4CAE2B8A09BCFDBE30EBA'.
self assert: (-5827980550840017565077671610 storeStringBase: 21) equals: '-21rKJIHGFEDCBA9876543210'.
self assert: -5827980550840017565077671610 storeStringHex equals: '-16r12D4CAE2B8A09BCFDBE30EBA'.
self assert: (-340653664490377789692799452102 printStringBase: 22) equals: '-LKJIHGFEDCBA9876543210'.
self assert: (-340653664490377789692799452102 radix: 22) equals: '-LKJIHGFEDCBA9876543210'.
self assert: -340653664490377789692799452102 printStringHex equals: '-44CB61B5B47E1A5D8F88583C6'.
self assert: (-340653664490377789692799452102 storeStringBase: 22) equals: '-22rLKJIHGFEDCBA9876543210'.
self assert: -340653664490377789692799452102 storeStringHex equals: '-16r44CB61B5B47E1A5D8F88583C6'.
self assert: (-20837326537038308910317109288851 printStringBase: 23) equals: '-MLKJIHGFEDCBA9876543210'.
self assert: (-20837326537038308910317109288851 radix: 23) equals: '-MLKJIHGFEDCBA9876543210'.
self assert: -20837326537038308910317109288851 printStringHex equals: '-1070108876456E0EF115B389F93'.
self assert: (-20837326537038308910317109288851 storeStringBase: 23) equals: '-23rMLKJIHGFEDCBA9876543210'.
self assert: -20837326537038308910317109288851 storeStringHex equals: '-16r1070108876456E0EF115B389F93'.
self assert: (-1331214537196502869015340298036888 printStringBase: 24) equals: '-NMLKJIHGFEDCBA9876543210'.
self assert: (-1331214537196502869015340298036888 radix: 24) equals: '-NMLKJIHGFEDCBA9876543210'.
self assert: -1331214537196502869015340298036888 printStringHex equals: '-41A24A285154B026B6ED206C6698'.
self assert: (-1331214537196502869015340298036888 storeStringBase: 24) equals: '-24rNMLKJIHGFEDCBA9876543210'.
self assert: -1331214537196502869015340298036888 storeStringHex equals: '-16r41A24A285154B026B6ED206C6698'.
self assert: (-88663644327703473714387251271141900 printStringBase: 25) equals: '-ONMLKJIHGFEDCBA9876543210'.
self assert: (-88663644327703473714387251271141900 radix: 25) equals: '-ONMLKJIHGFEDCBA9876543210'.
self assert: -88663644327703473714387251271141900 printStringHex equals: '-111374860A2C6CEBE5999630398A0C'.
self assert: (-88663644327703473714387251271141900 storeStringBase: 25) equals: '-25rONMLKJIHGFEDCBA9876543210'.
self assert: -88663644327703473714387251271141900 storeStringHex equals: '-16r111374860A2C6CEBE5999630398A0C'.
self assert: (-6146269788878825859099399609538763450 printStringBase: 26) equals: '-PONMLKJIHGFEDCBA9876543210'.
self assert: (-6146269788878825859099399609538763450 radix: 26) equals: '-PONMLKJIHGFEDCBA9876543210'.
self assert: -6146269788878825859099399609538763450 printStringHex equals: '-49FBA7F30B0F48BD14E6A99BD8ADABA'.
self assert: (-6146269788878825859099399609538763450 storeStringBase: 26) equals: '-26rPONMLKJIHGFEDCBA9876543210'.
self assert: -6146269788878825859099399609538763450 storeStringHex equals: '-16r49FBA7F30B0F48BD14E6A99BD8ADABA'.