1 USING: kernel math math.functions namespaces prettyprint
2 math.private continuations tools.test sequences random ;
3 IN: math.integers.tests
5 [ "-8" ] [ -8 unparse ] unit-test
7 [ t ] [ 0 fixnum? ] unit-test
8 [ t ] [ 31415 number? ] unit-test
9 [ t ] [ 31415 >bignum number? ] unit-test
10 [ t ] [ 2345621 fixnum? ] unit-test
12 [ t ] [ 2345621 dup >bignum >fixnum = ] unit-test
14 [ t ] [ 0 >fixnum 0 >bignum = ] unit-test
15 [ f ] [ 0 >fixnum 1 >bignum = ] unit-test
16 [ f ] [ 1 >bignum 0 >bignum = ] unit-test
17 [ t ] [ 0 >bignum 0 >fixnum = ] unit-test
19 [ t ] [ 0 >bignum bignum? ] unit-test
20 [ f ] [ 0 >fixnum bignum? ] unit-test
21 [ f ] [ 0 >fixnum bignum? ] unit-test
22 [ t ] [ 0 >fixnum fixnum? ] unit-test
24 [ -1 ] [ 1 neg ] unit-test
25 [ -1 ] [ 1 >bignum neg ] unit-test
26 [ 268435456 ] [ -268435456 >fixnum -1 * ] unit-test
27 [ 268435456 ] [ -268435456 >fixnum neg ] unit-test
29 [ 9 3 ] [ 93 10 /mod ] unit-test
30 [ 9 3 ] [ 93 >bignum 10 /mod ] unit-test
32 [ 5 ] [ 2 >bignum 3 >bignum + ] unit-test
34 [ -10000000001981284352 ] [
36 HEX: -100000000 bitand
39 [ 9999999997686317056 ] [
41 HEX: -100000000 bitand
54 [ -1 ] [ -1 >bignum >fixnum ] unit-test
57 [ 134217728 dup + dup + dup + dup + dup + dup + unparse ]
60 [ 7 ] [ 255 log2 ] unit-test
61 [ 8 ] [ 256 log2 ] unit-test
62 [ 8 ] [ 257 log2 ] unit-test
63 [ 0 ] [ 1 log2 ] unit-test
65 [ 7 ] [ 255 >bignum log2 ] unit-test
66 [ 8 ] [ 256 >bignum log2 ] unit-test
67 [ 8 ] [ 257 >bignum log2 ] unit-test
68 [ 0 ] [ 1 >bignum log2 ] unit-test
70 [ t ] [ BIN: 1101 0 bit? ] unit-test
71 [ f ] [ BIN: 1101 1 bit? ] unit-test
72 [ t ] [ BIN: 1101 2 bit? ] unit-test
73 [ t ] [ BIN: 1101 3 bit? ] unit-test
74 [ f ] [ BIN: 1101 4 bit? ] unit-test
76 [ t ] [ BIN: 1101 >bignum 0 bit? ] unit-test
77 [ f ] [ BIN: 1101 >bignum 1 bit? ] unit-test
78 [ t ] [ BIN: 1101 >bignum 2 bit? ] unit-test
79 [ t ] [ BIN: 1101 >bignum 3 bit? ] unit-test
80 [ f ] [ BIN: 1101 >bignum 4 bit? ] unit-test
82 [ t ] [ BIN: -1101 0 bit? ] unit-test
83 [ t ] [ BIN: -1101 1 bit? ] unit-test
84 [ f ] [ BIN: -1101 2 bit? ] unit-test
85 [ f ] [ BIN: -1101 3 bit? ] unit-test
86 [ t ] [ BIN: -1101 4 bit? ] unit-test
88 [ t ] [ BIN: -1101 >bignum 0 bit? ] unit-test
89 [ t ] [ BIN: -1101 >bignum 1 bit? ] unit-test
90 [ f ] [ BIN: -1101 >bignum 2 bit? ] unit-test
91 [ f ] [ BIN: -1101 >bignum 3 bit? ] unit-test
92 [ t ] [ BIN: -1101 >bignum 4 bit? ] unit-test
94 [ 2 ] [ 0 next-power-of-2 ] unit-test
95 [ 2 ] [ 1 next-power-of-2 ] unit-test
96 [ 2 ] [ 2 next-power-of-2 ] unit-test
97 [ 4 ] [ 3 next-power-of-2 ] unit-test
98 [ 16 ] [ 13 next-power-of-2 ] unit-test
99 [ 16 ] [ 16 next-power-of-2 ] unit-test
101 [ 268435456 ] [ -268435456 >fixnum -1 /i ] unit-test
102 [ 268435456 0 ] [ -268435456 >fixnum -1 /mod ] unit-test
103 [ 0 ] [ -1 -268435456 >fixnum /i ] unit-test
104 [ 4420880996869850977 ] [ 13262642990609552931 3 /i ] unit-test
105 [ 0 -1 ] [ -1 -268435456 >fixnum /mod ] unit-test
106 [ 0 -1 ] [ -1 -268435456 >bignum /mod ] unit-test
107 [ 14355 ] [ 1591517158873146351817850880000000 32769 mod ] unit-test
108 [ 8 530505719624382123 ] [ 13262642990609552931 1591517158873146351 /mod ] unit-test
109 [ 8 ] [ 13262642990609552931 1591517158873146351 /i ] unit-test
110 [ 530505719624382123 ] [ 13262642990609552931 1591517158873146351 mod ] unit-test
112 [ -351382792 ] [ -43922849 3 shift ] unit-test
114 [ t ] [ 0 zero? ] unit-test
115 [ f ] [ 30 zero? ] unit-test
116 [ t ] [ 0 >bignum zero? ] unit-test
118 [ 4294967280 ] [ 268435455 >fixnum 16 fixnum* ] unit-test
120 [ 23603949310011464311086123800853779733506160743636399259558684142844552151041 ]
122 1957739506503920732625800353008742584087090810400921800808997218266517557963281171906190947801528098188887586755474449585677502695226712388326288208691204
123 79562815144503850065234921197651376510595262628033069372760833939060637564931
127 ! We don't care if this fails or returns 0 (its CPU-specific)
128 ! as long as it doesn't crash
129 [ ] [ [ 0 0 /i drop ] ignore-errors ] unit-test
130 [ ] [ [ 100000000000000000 0 /i drop ] ignore-errors ] unit-test
132 [ -2 ] [ 1 bitnot ] unit-test
133 [ -2 ] [ 1 >bignum bitnot ] unit-test
134 [ -2 ] [ 1 >bignum bitnot ] unit-test
135 [ 0 ] [ 123 dup bitnot bitand ] unit-test
136 [ 0 ] [ 123 >bignum dup bitnot bitand ] unit-test
137 [ 0 ] [ 123 dup bitnot >bignum bitand ] unit-test
138 [ 0 ] [ 123 dup bitnot bitand >bignum ] unit-test
139 [ -1 ] [ 123 dup bitnot bitor ] unit-test
140 [ -1 ] [ 123 >bignum dup bitnot bitor ] unit-test
141 [ -1 ] [ 123 dup bitnot >bignum bitor ] unit-test
142 [ -1 ] [ 123 dup bitnot bitor >bignum ] unit-test
143 [ -1 ] [ 123 dup bitnot bitxor ] unit-test
144 [ -1 ] [ 123 >bignum dup bitnot bitxor ] unit-test
145 [ -1 ] [ 123 dup bitnot >bignum bitxor ] unit-test
146 [ -1 ] [ 123 dup bitnot bitxor >bignum ] unit-test
147 [ 4 ] [ 4 7 bitand ] unit-test
149 [ 256 ] [ 65536 -8 shift ] unit-test
150 [ 256 ] [ 65536 >bignum -8 shift ] unit-test
151 [ 256 ] [ 65536 -8 >bignum shift ] unit-test
152 [ 256 ] [ 65536 >bignum -8 >bignum shift ] unit-test
153 [ 4294967296 ] [ 1 16 shift 16 shift ] unit-test
154 [ 4294967296 ] [ 1 32 shift ] unit-test
155 [ 1267650600228229401496703205376 ] [ 1 100 shift ] unit-test
157 [ t ] [ 1 27 shift fixnum? ] unit-test
161 [ 27 28 29 30 31 32 59 60 61 62 63 64 ]
163 1 over shift swap 1 >bignum swap shift = and
169 [ 27 28 29 30 31 32 59 60 61 62 63 64 ]
171 -1 over shift swap -1 >bignum swap shift = and
175 [ 12 ] [ 11 4 align ] unit-test
176 [ 12 ] [ 12 4 align ] unit-test
177 [ 10 ] [ 10 2 align ] unit-test
178 [ 14 ] [ 13 2 align ] unit-test
179 [ 11 ] [ 11 1 align ] unit-test
182 B{ HEX: 11 HEX: 22 HEX: 33 } byte-array>bignum
185 [ HEX: 7a2c793b2ff08554 ] [
186 B{ HEX: 54 HEX: 85 HEX: f0 HEX: 2f HEX: 3b HEX: 79 HEX: 2c HEX: 7a } byte-array>bignum
189 [ HEX: 988a259c3433f237 ] [
190 B{ HEX: 37 HEX: f2 HEX: 33 HEX: 34 HEX: 9c HEX: 25 HEX: 8a HEX: 98 } byte-array>bignum
193 [ t ] [ 256 power-of-2? ] unit-test
194 [ f ] [ 123 power-of-2? ] unit-test
196 [ f ] [ -128 power-of-2? ] unit-test
197 [ f ] [ 0 power-of-2? ] unit-test
198 [ t ] [ 1 power-of-2? ] unit-test
200 : ratio>float ( a b -- f ) [ >bignum ] bi@ /f ;
202 [ 5. ] [ 5 1 ratio>float ] unit-test
203 [ 4. ] [ 4 1 ratio>float ] unit-test
204 [ 2. ] [ 2 1 ratio>float ] unit-test
205 [ .5 ] [ 1 2 ratio>float ] unit-test
206 [ .75 ] [ 3 4 ratio>float ] unit-test
207 [ 1. ] [ 2000 2^ 2000 2^ 1+ ratio>float ] unit-test
208 [ -1. ] [ 2000 2^ neg 2000 2^ 1+ ratio>float ] unit-test
209 [ 0.4 ] [ 6 15 ratio>float ] unit-test
211 [ HEX: 3fe553522d230931 ]
212 [ 61967020039 92984792073 ratio>float double>bits ] unit-test
214 : random-integer ( -- n )
216 1 random zero? [ neg ] when
217 1 random zero? [ >bignum ] when ;
224 [ >float / ] [ /f ] 2bi 0.1 ~