1(import (chicken bitwise)) (import srfi-151) (import test)
2(current-test-verbosity #f)
3(test-group "bitwise"
4 (test-group "bitwise/basic"
5 (test "test-1" -1 (bitwise-not 0))
6 (test "test-122" 0 (bitwise-not -1))
7 (test "test-248" -11 (bitwise-not 10))
8 (test "test-249" 36 (bitwise-not -37))
9 (test "test-2" 0 (bitwise-and #b0 #b1))
10 (test "test-10" 1680869008 (bitwise-and -193073517 1689392892))
11 (test "test-20" 3769478 (bitwise-and 1694076839 -4290775858))
12 (test "test-115" 6 (bitwise-and 14 6))
13 (test "test-251" 10 (bitwise-and 11 26))
14 (test "test-254" 4 (bitwise-and 37 12))
15 (test "test-288" 1 (bitwise-and #b1 #b1))
16 (test "test-289" 0 (bitwise-and #b1 #b10))
17 (test "test-290" #b10 (bitwise-and #b11 #b10))
18 (test "test-291" #b101 (bitwise-and #b101 #b111))
19 (test "test-292" #b111 (bitwise-and -1 #b111))
20 (test "test-293" #b110 (bitwise-and -2 #b111))
21 (test "test-294" 3769478 (bitwise-and -4290775858 1694076839))
22 (test "test-11" -4294967295 (bitwise-ior 1 (- -1 #xffffffff)))
23 (test "test-12" -18446744073709551615 (bitwise-ior 1 (- -1 #xffffffffffffffff)))
24 (test "test-117" 14 (bitwise-ior 10 12))
25 (test "test-250" 11 (bitwise-ior 3 10))
26 (test "test-13" -4294967126 (bitwise-xor #b10101010 (- -1 #xffffffff)))
27 (test "test-15" -18446744073709551446 (bitwise-xor #b10101010 (- -1 #xffffffffffffffff)))
28 (test "test-16" -2600468497 (bitwise-ior 1694076839 -4290775858))
29 (test "test-17" -184549633 (bitwise-ior -193073517 1689392892))
30 (test "test-18" -2604237975 (bitwise-xor 1694076839 -4290775858))
31 (test "test-19" -1865418641 (bitwise-xor -193073517 1689392892))
32 (test "test-119" 6 (bitwise-xor 10 12))
33 (test "test-252" 9 (bitwise-xor 3 10))
34 (test "test-14" (bitwise-not -4294967126) (bitwise-eqv #b10101010 (- -1 #xffffffff)))
35 (test "test-253" -42 (bitwise-eqv 37 12))
36 (test "test-27" -1 (bitwise-nand 0 0))
37 (test "test-28" -1 (bitwise-nand 0 -1))
38 (test "test-29" -124 (bitwise-nand -1 123))
39 (test "test-326" -11 (bitwise-nand 11 26))
40 (test "test-327" -28 (bitwise-nor 11 26))
41 (test "test-317" 0 (bitwise-nor -1 123))
42 (test "test-328" 16 (bitwise-andc1 11 26))
43 (test "test-329" 1 (bitwise-andc2 11 26))
44 (test "test-330" -2 (bitwise-orc1 11 26))
45 (test "test-30" -1 (bitwise-nor 0 0))
46 (test "test-31" 0 (bitwise-nor 0 -1))
47 (test "test-22" 0 (bitwise-andc1 0 0))
48 (test "test-23" -1 (bitwise-andc1 0 -1))
49 (test "test-24" 123 (bitwise-andc1 0 123))
50 (test "test-25" 0 (bitwise-andc2 0 0))
51 (test "test-26" -1 (bitwise-andc2 -1 0))
52 (test "test-318" -1 (bitwise-orc1 0 0))
53 (test "test-319" -1 (bitwise-orc1 0 -1))
54 (test "test-320" 0 (bitwise-orc1 -1 0))
55 (test "test-321" -124 (bitwise-orc1 123 0))
56 (test "test-322" -1 (bitwise-orc2 0 0))
57 (test "test-323" -1 (bitwise-orc2 -1 0))
58 (test "test-324" 0 (bitwise-orc2 0 -1))
59 (test "test-325" -124 (bitwise-orc2 0 123))
60 )
61 (test-group "bitwise/integer"
62 (test "test-78" #x1000000000000000100000000000000000000000000000000
63 (arithmetic-shift #x100000000000000010000000000000000 64))
64 (test "test-79" #x8e73b0f7da0e6452c810f32b809079e5
65 (arithmetic-shift #x8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b -64))
66 (test "test-196" 2 (arithmetic-shift 1 1))
67 (test "test-197" 0 (arithmetic-shift 1 -1))
68 (test "test-331" 1 (arithmetic-shift 1 0))
69 (test "test-333" 4 (arithmetic-shift 1 2))
70 (test "test-334" 8 (arithmetic-shift 1 3))
71 (test "test-335" 16 (arithmetic-shift 1 4))
72 (test "test-336" (expt 2 31) (arithmetic-shift 1 31))
73 (test "test-337" (expt 2 32) (arithmetic-shift 1 32))
74 (test "test-338" (expt 2 33) (arithmetic-shift 1 33))
75 (test "test-339" (expt 2 63) (arithmetic-shift 1 63))
76 (test "test-340" (expt 2 64) (arithmetic-shift 1 64))
77 (test "test-341" (expt 2 65) (arithmetic-shift 1 65))
78 (test "test-342" (expt 2 127) (arithmetic-shift 1 127))
79 (test "test-343" (expt 2 128) (arithmetic-shift 1 128))
80 (test "test-344" (expt 2 129) (arithmetic-shift 1 129))
81 (test "test-345" 3028397001194014464 (arithmetic-shift 11829675785914119 8))
82 (test "test-346" -1 (arithmetic-shift -1 0))
83 (test "test-347" -2 (arithmetic-shift -1 1))
84 (test "test-348" -4 (arithmetic-shift -1 2))
85 (test "test-349" -8 (arithmetic-shift -1 3))
86 (test "test-350" -16 (arithmetic-shift -1 4))
87 (test "test-351" (- (expt 2 31)) (arithmetic-shift -1 31))
88 (test "test-352" (- (expt 2 32)) (arithmetic-shift -1 32))
89 (test "test-353" (- (expt 2 33)) (arithmetic-shift -1 33))
90 (test "test-354" (- (expt 2 63)) (arithmetic-shift -1 63))
91 (test "test-355" (- (expt 2 64)) (arithmetic-shift -1 64))
92 (test "test-356" (- (expt 2 65)) (arithmetic-shift -1 65))
93 (test "test-357" (- (expt 2 127)) (arithmetic-shift -1 127))
94 (test "test-358" (- (expt 2 128)) (arithmetic-shift -1 128))
95 (test "test-359" (- (expt 2 129)) (arithmetic-shift -1 129))
96 (test "test-360" 0 (arithmetic-shift 1 -63))
97 (test "test-361" 0 (arithmetic-shift 1 -64))
98 (test "test-362" 0 (arithmetic-shift 1 -65))
99 (test "test-255" 32 (arithmetic-shift 8 2))
100 (test "test-256" 4 (arithmetic-shift 4 0))
101 (test "test-257" 4 (arithmetic-shift 8 -1))
102 (test "test-258" -79 (arithmetic-shift -100000000000000000000000000000000 -100))
103 (test "test-135" 2 (bit-count 12))
104 (test "test-263" 0 (integer-length 0))
105 (test "test-264" 1 (integer-length 1))
106 (test "test-265" 0 (integer-length -1))
107 (test "test-266" 3 (integer-length 7))
108 (test "test-267" 3 (integer-length -7))
109 (test "test-268" 4 (integer-length 8))
110 (test "test-269" 3 (integer-length -8))
111 (test "test-125" 9 (bitwise-if 3 1 8))
112 (test "test-126" 0 (bitwise-if 3 8 1))
113 (test "test-373" 3 (bitwise-if 1 1 2))
114 (test "test-378" #b00110011 (bitwise-if #b00111100 #b11110000 #b00001111))
115 )
116 (test-group "bitwise/single"
117 (test "test-160" #t (bit-set? 0 1))
118 (test "test-161" #f (bit-set? 1 1))
119 (test "test-162" #f (bit-set? 1 8))
120 (test "test-163" #t (bit-set? 10000 -1))
121 (test "test-167" #t (bit-set? 1000 -1))
122 (test "test-541" #t (bit-set? 64 #x10000000000000000))
123 (test "test-542" #f (bit-set? 64 1))
124 (test "test-272" #t (bit-set? 3 10))
125 (test "test-273" #t (bit-set? 2 6))
126 (test "test-274" #f (bit-set? 0 6))
127 (test "test-168" 0 (copy-bit 0 0 #f))
128 (test "test-169" 0 (copy-bit 30 0 #f))
129 (test "test-170" 0 (copy-bit 31 0 #f))
130 (test "test-171" 0 (copy-bit 62 0 #f))
131 (test "test-172" 0 (copy-bit 63 0 #f))
132 (test "test-173" 0 (copy-bit 128 0 #f))
133 (test "test-174" -1 (copy-bit 0 -1 #t))
134 (test "test-175" -1 (copy-bit 30 -1 #t))
135 (test "test-176" -1 (copy-bit 31 -1 #t))
136 (test "test-177" -1 (copy-bit 62 -1 #t))
137 (test "test-178" -1 (copy-bit 63 -1 #t))
138 (test "test-179" -1 (copy-bit 128 -1 #t))
139 (test "test-180" 1 (copy-bit 0 0 #t))
140 (test "test-181" #x106 (copy-bit 8 6 #t))
141 (test "test-182" 6 (copy-bit 8 6 #f))
142 (test "test-183" -2 (copy-bit 0 -1 #f))
143 (test "test-184" 0 (copy-bit 128 #x100000000000000000000000000000000 #f))
144 (test "test-185" #x100000000000000000000000000000000
145 (copy-bit 128 #x100000000000000000000000000000000 #t))
146 (test "test-186" #x100000000000000000000000000000000
147 (copy-bit 64 #x100000000000000000000000000000000 #f))
148 (test "test-187" #x-100000000000000000000000000000000
149 (copy-bit 64 #x-100000000000000000000000000000000 #f))
150 (test "test-188" #x-100000000000000000000000000000000
151 (copy-bit 256 #x-100000000000000000000000000000000 #t))
152 (test "test-276" #b100 (copy-bit 2 0 #t))
153 (test "test-277" #b1011 (copy-bit 2 #b1111 #f))
154 (test "test-379" #b1 (copy-bit 0 0 #t))
155 (test "test-100" #b1011 (bit-swap 1 2 #b1101))
156 (test "test-101" #b1011 (bit-swap 2 1 #b1101))
157 (test "test-382" #b1110 (bit-swap 0 1 #b1101))
158 (test "test-102" #b10000000101 (bit-swap 3 10 #b1101))
159 (test "test-278" 1 (bit-swap 0 2 4))
160 (test "test-129" #t (any-bit-set? 3 6))
161 (test "test-130" #f (any-bit-set? 3 12))
162 (test "test-133" #t (every-bit-set? 4 6))
163 (test "test-134" #f (every-bit-set? 7 6))
164 (test "test-141" -1 (first-set-bit 0))
165 (test "test-142" 0 (first-set-bit 1))
166 (test "test-143" 0 (first-set-bit 3))
167 (test "test-144" 2 (first-set-bit 4))
168 (test "test-145" 1 (first-set-bit 6))
169 (test "test-146" 0 (first-set-bit -1))
170 (test "test-147" 1 (first-set-bit -2))
171 (test "test-148" 0 (first-set-bit -3))
172 (test "test-149" 2 (first-set-bit -4))
173 (test "test-150" 128 (first-set-bit #x100000000000000000000000000000000))
174 (test "test-280" 1 (first-set-bit 2))
175 (test "test-282" 3 (first-set-bit 40))
176 (test "test-283" 2 (first-set-bit -28))
177 (test "test-284" 99 (first-set-bit (expt 2 99)))
178 (test "test-285" 99 (first-set-bit (expt -2 99)))
179 )
180 (test-group "bitwise/field"
181 (test "test-189" 0 (bit-field 6 0 1))
182 (test "test-190" 3 (bit-field 6 1 3))
183 (test "test-191" 1 (bit-field 6 2 999))
184 (test "test-192" 1 (bit-field #x100000000000000000000000000000000 128 129))
185 (test "test-363" #b1010 (bit-field #b1101101010 0 4))
186 (test "test-364" #b101101 (bit-field #b1101101010 3 9))
187 (test "test-365" #b10110 (bit-field #b1101101010 4 9))
188 (test "test-366" #b110110 (bit-field #b1101101010 4 10))
189 (test "test-367" #t (bit-field-any? #b101101 0 2))
190 (test "test-368" #t (bit-field-any? #b101101 2 4))
191 (test "test-369" #f (bit-field-any? #b101101 1 2))
192 (test "test-370" #f (bit-field-every? #b101101 0 2))
193 (test "test-371" #t (bit-field-every? #b101101 2 4))
194 (test "test-372" #t (bit-field-every? #b101101 0 1))
195 (test "test-374" #b100000 (bit-field-clear #b101010 1 4))
196 (test "test-375" #b101110 (bit-field-set #b101010 1 4))
197 (test "test-193" #b111 (bit-field-replace #b110 1 0 1))
198 (test "test-194" #b110 (bit-field-replace #b110 1 1 2))
199 (test "test-195" #b010 (bit-field-replace #b110 1 1 3))
200 (test "test-376" #b100100 (bit-field-replace #b101010 #b010 1 4))
201 (test "test-377" #b1001 (bit-field-replace-same #b1111 #b0000 1 3))
202 (test "test-200" #b110 (bit-field-rotate #b110 1 1 2))
203 (test "test-201" #b1010 (bit-field-rotate #b110 1 2 4))
204 (test "test-202" #b1011 (bit-field-rotate #b0111 -1 1 4))
205 (test "test-203" #b0 (bit-field-rotate #b0 128 0 256))
206 (test "test-204" #b1 (bit-field-rotate #b1 128 1 256))
207 (test "test-205" #x100000000000000000000000000000000
208 (bit-field-rotate #x100000000000000000000000000000000 128 0 64))
209 (test "test-206" #x100000000000000000000000000000008
210 (bit-field-rotate #x100000000000000000000000000000001 3 0 64))
211 (test "test-207" #x100000000000000002000000000000000
212 (bit-field-rotate #x100000000000000000000000000000001 -3 0 64))
213 (test "test-208" #b110 (bit-field-rotate #b110 0 0 10))
214 (test "test-209" #b110 (bit-field-rotate #b110 0 0 256))
215 (test "test-475" 1 (bit-field-rotate #x100000000000000000000000000000000 1 0 129))
216 (test "test-211" 6 (bit-field-reverse 6 1 3))
217 (test "test-212" 12 (bit-field-reverse 6 1 4))
218 (test "test-213" #x80000000 (bit-field-reverse 1 0 32))
219 (test "test-214" #x40000000 (bit-field-reverse 1 0 31))
220 (test "test-215" #x20000000 (bit-field-reverse 1 0 30))
221 (test "test-216" (bitwise-ior (arithmetic-shift -1 32) #xFBFFFFFF)
222 (bit-field-reverse -2 0 27))
223 (test "test-217" (bitwise-ior (arithmetic-shift -1 32) #xF7FFFFFF)
224 (bit-field-reverse -2 0 28))
225 (test "test-218" (bitwise-ior (arithmetic-shift -1 32) #xEFFFFFFF)
226 (bit-field-reverse -2 0 29))
227 (test "test-219" (bitwise-ior (arithmetic-shift -1 32) #xDFFFFFFF)
228 (bit-field-reverse -2 0 30))
229 (test "test-220" (bitwise-ior (arithmetic-shift -1 32) #xBFFFFFFF)
230 (bit-field-reverse -2 0 31))
231 (test "test-221" (bitwise-ior (arithmetic-shift -1 32) #x7FFFFFFF)
232 (bit-field-reverse -2 0 32))
233 (test "test-222" 5 (bit-field-reverse #x140000000000000000000000000000000 0 129))
234 )
235 (test-group "bitwise/conversion"
236 (test "test-103" '(#t #f #t #f #t #t #t) (bits->list #b1110101))
237 (test "test-104" '(#f #t #f #t) (bits->list #b111010 4))
238 (test "test-106" #b1110101 (list->bits '(#t #f #t #f #t #t #t)))
239 (test "test-107" #b111010100 (list->bits '(#f #f #t #f #t #f #t #t #t)))
240 (test "test-223" '(#t #t) (bits->list 3))
241 (test "test-224" '(#f #t #t #f) (bits->list 6 4))
242 (test "test-225" '(#f #t) (bits->list 6 2))
243 (test "test-226" '(#t #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
244 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
245 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
246 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
247 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
248 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
249 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
250 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f)
251 (bits->list 1 128))
252 (test "test-228" '(#f
253 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
254 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
255 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
256 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
257 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
258 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
259 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
260 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #t)
261 (bits->list #x100000000000000000000000000000000))
262 (test "test-229" 6 (list->bits '(#f #t #t)))
263 (test "test-230" 12 (list->bits '(#f #f #t #t)))
264 (test "test-231" 6 (list->bits '(#f #t #t #f)))
265 (test "test-232" 2 (list->bits '(#f #t)))
266 (test "test-233" 1 (list->bits
267 '(#t #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
268 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
269 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
270 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
271 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
272 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
273 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
274 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f)))
275 (test "test-234" #x100000000000000000000000000000000
276 (list->bits
277 '(#f
278 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
279 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
280 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
281 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
282 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
283 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
284 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f
285 #f #f #f #f #f #f #f #f #f #f #f #f #f #f #f #t)))
286 (test "test-235" #x03FFFFFF (list->bits '(#t #t
287 #t #t #t #t #t #t #t #t
288 #t #t #t #t #t #t #t #t
289 #t #t #t #t #t #t #t #t)))
290 (test "test-236" #x07FFFFFF (list->bits '(#t #t #t
291 #t #t #t #t #t #t #t #t
292 #t #t #t #t #t #t #t #t
293 #t #t #t #t #t #t #t #t)))
294 (test "test-237" #x0FFFFFFF (list->bits '(#t #t #t #t
295 #t #t #t #t #t #t #t #t
296 #t #t #t #t #t #t #t #t
297 #t #t #t #t #t #t #t #t)))
298 (test "test-238" #x1FFFFFFF (list->bits '(#t #t #t #t #t
299 #t #t #t #t #t #t #t #t
300 #t #t #t #t #t #t #t #t
301 #t #t #t #t #t #t #t #t)))
302 (test "test-239" #x3FFFFFFF (list->bits '(#t #t #t #t #t #t
303 #t #t #t #t #t #t #t #t
304 #t #t #t #t #t #t #t #t
305 #t #t #t #t #t #t #t #t)))
306 (test "test-240" #x7FFFFFFF (list->bits '(#t #t #t #t #t #t #t
307 #t #t #t #t #t #t #t #t
308 #t #t #t #t #t #t #t #t
309 #t #t #t #t #t #t #t #t)))
310 (test "test-241" #xFFFFFFFF (list->bits '(#t #t #t #t #t #t #t #t
311 #t #t #t #t #t #t #t #t
312 #t #t #t #t #t #t #t #t
313 #t #t #t #t #t #t #t #t)))
314 (test "test-242" #x1FFFFFFFF (list->bits '(#t
315 #t #t #t #t #t #t #t #t
316 #t #t #t #t #t #t #t #t
317 #t #t #t #t #t #t #t #t
318 #t #t #t #t #t #t #t #t)))
319 (test "test-490" 1 (list->bits '(#t #f)))
320 (test "test-108" #b1110101 (vector->bits '#(#t #f #t #f #t #t #t)))
321 (test "test-109" #b00011010100 (vector->bits '#(#f #f #t #f #t #f #t #t)))
322 (test "test-105" '#(#t #t #t #f #t #f #t #f #f) (bits->vector #b1010111 9))
323 (test "test-105" '#(#t #t #t #f #t #f #t #f #f) (bits->vector #b1010111 9))
324 (test "test-110" #b1110101 (bits #t #f #t #f #t #t #t))
325 (test "test-243" 0 (bits))
326 (test "test-111" #b111010100 (bits #f #f #t #f #t #f #t #t #t))
327 )
328 (test-group "bitwise/fold"
329 (test "test-112" '(#t #f #t #f #t #t #t) (bitwise-fold cons '() #b1010111))
330 (test "test-113" 5
331 (let ((count 0))
332 (bitwise-for-each (lambda (b) (if b (set! count (+ count 1))))
333 #b1010111)
334 count))
335 (test "test-114" #b101010101
336 (bitwise-unfold (lambda (i) (= i 10)) even? (lambda (i) (+ i 1)) 0))
337 (let ((g (make-bitwise-generator #b110)))
338 (test "test-244a" #f (g))
339 (test "test-244b" #t (g))
340 (test "test-244c" #t (g))
341 (test "test-244d" #f (g)))
342 )
343)
344(test-exit)