Debian Codegraph @

scheme/tests/bytevectors.ss

raw
1
2 (library (tests bytevectors)
3 (export run-tests)
4 (import (ikarus) (tests framework))
5
6 (define (not-bytevector? x)
7 (not (bytevector? x)))
8
9 (define-tests test-bytevectors
10 [bytevector? (make-bytevector 1)]
11 [bytevector? (make-bytevector 1 17)]
12 [bytevector? (make-bytevector 10 -17)]
13 [not-bytevector? 'foo]
14 [not-bytevector? "hey"]
15 [not-bytevector? (current-output-port)]
16 [not-bytevector? (current-input-port)]
17 [not-bytevector? '#(2837 2398 239)]
18 [zero? (bytevector-length (make-bytevector 0))]
19 [(lambda (x) (= x 100)) (bytevector-length (make-bytevector 100 -30))]
20 [(lambda (x) (equal? x '(-127 129 -1 255)))
21 (let ([b1 (make-bytevector 16 -127)]
22 [b2 (make-bytevector 16 255)])
23 (list
24 (bytevector-s8-ref b1 0)
25 (bytevector-u8-ref b1 0)
26 (bytevector-s8-ref b2 0)
27 (bytevector-u8-ref b2 0)))]
28 [(lambda (x) (equal? x '(-126 130 -10 246)))
29 (let ([b (make-bytevector 16 -127)])
30 (bytevector-s8-set! b 0 -126)
31 (bytevector-u8-set! b 1 246)
32 (list
33 (bytevector-s8-ref b 0)
34 (bytevector-u8-ref b 0)
35 (bytevector-s8-ref b 1)
36 (bytevector-u8-ref b 1)))]
37 [(lambda (x) (equal? x '(1 2 3 1 2 3 4 8)))
38 (let ([b (u8-list->bytevector '(1 2 3 4 5 6 7 8))])
39 (bytevector-copy! b 0 b 3 4)
40 (bytevector->u8-list b))]
41 [(lambda (x) (= x 17))
42 (bytevector-uint-ref
43 (u8-list->bytevector '(17))
44 0 'little 1)]
45 [(lambda (x) (= x 17))
46 (bytevector-uint-ref
47 (u8-list->bytevector '(17))
48 0 'big 1)]
49 [(lambda (x) (= x (+ 17 (* 54 256))))
50 (bytevector-uint-ref
51 (u8-list->bytevector '(17 54))
52 0 'little 2)]
53 [(lambda (x) (= x (+ 17 (* 54 256))))
54 (bytevector-uint-ref
55 (u8-list->bytevector (reverse '(17 54)))
56 0 'big 2)]
57 [(lambda (x) (= x (+ 17 (* 54 256) (* 98 256 256))))
58 (bytevector-uint-ref
59 (u8-list->bytevector '(17 54 98))
60 0 'little 3)]
61 [(lambda (x) (= x (+ 17 (* 54 256) (* 98 256 256))))
62 (bytevector-uint-ref
63 (u8-list->bytevector (reverse '(17 54 98)))
64 0 'big 3)]
65 [(lambda (x) (= x (+ 17 (* 54 256) (* 98 256 256) (* 120 256 256 256))))
66 (bytevector-uint-ref
67 (u8-list->bytevector '(17 54 98 120))
68 0 'little 4)]
69 [(lambda (x) (= x #x123897348738947983174893204982390489))
70 (bytevector-uint-ref
71 (u8-list->bytevector
72 '(#x89 #x04 #x39 #x82 #x49 #x20 #x93 #x48 #x17
73 #x83 #x79 #x94 #x38 #x87 #x34 #x97 #x38 #x12))
74 0 'little 18)]
75 [(lambda (x) (= x #x123897348738947983174893204982390489))
76 (bytevector-uint-ref
77 (u8-list->bytevector
78 (reverse
79 '(#x89 #x04 #x39 #x82 #x49 #x20 #x93 #x48 #x17
80 #x83 #x79 #x94 #x38 #x87 #x34 #x97 #x38 #x12)))
81 0 'big 18)]
82 [(lambda (x) (equal? x '(513 65283 513 513)))
83 (let ([b (u8-list->bytevector '(1 2 3 255 1 2 1 2))])
84 (bytevector->uint-list b 'little 2))]
85 [(lambda (x) (equal? x '(513 -253 513 513)))
86 (let ([b (u8-list->bytevector '(1 2 3 255 1 2 1 2))])
87 (bytevector->sint-list b 'little 2))]
88 [(lambda (x) (equal? x '(#xfffffffffffffffffffffffffffffffd
89 -3
90 (253 255 255 255 255 255 255 255
91 255 255 255 255 255 255 255 255))))
92 (let ([b (make-bytevector 16 -127)])
93 (bytevector-uint-set! b 0 (- (expt 2 128) 3) 'little 16)
94 (list
95 (bytevector-uint-ref b 0 'little 16)
96 (bytevector-sint-ref b 0 'little 16)
97 (bytevector->u8-list b)))]
98 [(lambda (x) (equal? x '(#xfffffffffffffffffffffffffffffffd
99 -3
100 (255 255 255 255 255 255 255 255
101 255 255 255 255 255 255 255 253))))
102 (let ([b (make-bytevector 16 -127)])
103 (bytevector-uint-set! b 0 (- (expt 2 128) 3) 'big 16)
104 (list
105 (bytevector-uint-ref b 0 'big 16)
106 (bytevector-sint-ref b 0 'big 16)
107 (bytevector->u8-list b)))]
108 [(lambda (x) (equal? x '(1 2 3 4)))
109 (bytevector->u8-list '#vu8(1 2 3 4))]
110 [(lambda (x) (= x #xFFFFFFFF))
111 (let ([b (make-bytevector 4 0)])
112 (bytevector-sint-set! b 0 -1 'little 4)
113 (bytevector-uint-ref b 0 'little 4))]
114 [(lambda (x) (= x #xFFFFFF00))
115 (let ([b (make-bytevector 4 0)])
116 (bytevector-sint-set! b 0 -256 'little 4)
117 (bytevector-uint-ref b 0 'little 4))]
118 [(lambda (x) (= x #xFFFF0000))
119 (let ([b (make-bytevector 4 0)])
120 (bytevector-sint-set! b 0 (- (expt 256 2)) 'little 4)
121 (bytevector-uint-ref b 0 'little 4))]
122 [(lambda (x) (= x #xFFFFFFFFFFFF0000))
123 (let ([b (make-bytevector 8 0)])
124 (bytevector-sint-set! b 0 (- (expt 256 2)) 'little 8)
125 (bytevector-uint-ref b 0 'little 8))]
126 [(lambda (x) (= x #xFFFFFFFF00000000))
127 (let ([b (make-bytevector 8 0)])
128 (bytevector-sint-set! b 0 (- (expt 256 4)) 'little 8)
129 (bytevector-uint-ref b 0 'little 8))]
130 [(lambda (x) (= x #xFF00000000000000))
131 (let ([b (make-bytevector 8 0)])
132 (bytevector-sint-set! b 0 (- (expt 256 7)) 'little 8)
133 (bytevector-uint-ref b 0 'little 8))]
134 [(lambda (x) (= x (- 1 (expt 2 63))))
135 (let ([b (make-bytevector 8 0)])
136 (bytevector-sint-set! b 0 (- 1 (expt 2 63)) 'little 8)
137 (bytevector-sint-ref b 0 'little 8))]
138 [(lambda (x) (= x #x7FFFFFFF))
139 (let ([b (make-bytevector 4 38)])
140 (bytevector-sint-set! b 0 (sub1 (expt 2 31)) 'little 4)
141 (bytevector-sint-ref b 0 'little 4))]
142 [(lambda (x) (= x #x-80000000))
143 (let ([b (make-bytevector 4 38)])
144 (bytevector-sint-set! b 0 (- (expt 2 31)) 'little 4)
145 (bytevector-sint-ref b 0 'little 4))]
146 [(lambda (x) (= x #x-100000000))
147 (let ([b (make-bytevector 5 38)])
148 (bytevector-sint-set! b 0 (- (expt 2 32)) 'little 5)
149 (bytevector-sint-ref b 0 'little 5))]
150 [(lambda (x) (= x #xFFFFFFFF))
151 (let ([b (make-bytevector 4 0)])
152 (bytevector-sint-set! b 0 -1 'big 4)
153 (bytevector-uint-ref b 0 'big 4))]
154 [(lambda (x) (= x #xFFFFFF00))
155 (let ([b (make-bytevector 4 0)])
156 (bytevector-sint-set! b 0 -256 'big 4)
157 (bytevector-uint-ref b 0 'big 4))]
158 [(lambda (x) (= x #xFFFF0000))
159 (let ([b (make-bytevector 4 0)])
160 (bytevector-sint-set! b 0 (- (expt 256 2)) 'big 4)
161 (bytevector-uint-ref b 0 'big 4))]
162 [(lambda (x) (= x #xFFFFFFFFFFFF0000))
163 (let ([b (make-bytevector 8 0)])
164 (bytevector-sint-set! b 0 (- (expt 256 2)) 'big 8)
165 (bytevector-uint-ref b 0 'big 8))]
166 [(lambda (x) (= x #xFFFFFFFF00000000))
167 (let ([b (make-bytevector 8 0)])
168 (bytevector-sint-set! b 0 (- (expt 256 4)) 'big 8)
169 (bytevector-uint-ref b 0 'big 8))]
170 [(lambda (x) (= x #xFF00000000000000))
171 (let ([b (make-bytevector 8 0)])
172 (bytevector-sint-set! b 0 (- (expt 256 7)) 'big 8)
173 (bytevector-uint-ref b 0 'big 8))]
174 [(lambda (x) (= x (- 1 (expt 2 63))))
175 (let ([b (make-bytevector 8 0)])
176 (bytevector-sint-set! b 0 (- 1 (expt 2 63)) 'big 8)
177 (bytevector-sint-ref b 0 'big 8))]
178 [(lambda (x) (= x #x7FFFFFFF))
179 (let ([b (make-bytevector 4 38)])
180 (bytevector-sint-set! b 0 (sub1 (expt 2 31)) 'big 4)
181 (bytevector-sint-ref b 0 'big 4))]
182 [(lambda (x) (= x #x-80000000))
183 (let ([b (make-bytevector 4 38)])
184 (bytevector-sint-set! b 0 (- (expt 2 31)) 'big 4)
185 (bytevector-sint-ref b 0 'big 4))]
186 [(lambda (x) (= x #x-100000000))
187 (let ([b (make-bytevector 5 38)])
188 (bytevector-sint-set! b 0 (- (expt 2 32)) 'big 5)
189 (bytevector-sint-ref b 0 'big 5))]
190
191 [(lambda (x) (= x 65023))
192 (bytevector-u16-ref '#vu8(255 253) 0 'little)]
193 [(lambda (x) (= x 65533))
194 (bytevector-u16-ref '#vu8(255 253) 0 'big)]
195 [(lambda (x) (= x -513))
196 (bytevector-s16-ref '#vu8(255 253) 0 'little)]
197 [(lambda (x) (= x -3))
198 (bytevector-s16-ref '#vu8(255 253) 0 'big)]
199
200 [(lambda (x) (= x 12345))
201 (let ([v (make-bytevector 2)])
202 (bytevector-u16-native-set! v 0 12345)
203 (bytevector-u16-native-ref v 0))]
204 [(lambda (x) (= x 12345))
205 (let ([v (make-bytevector 2)])
206 (bytevector-u16-set! v 0 12345 'little)
207 (bytevector-u16-ref v 0 'little))]
208 [(lambda (x) (= x 12345))
209 (let ([v (make-bytevector 2)])
210 (bytevector-u16-set! v 0 12345 'big)
211 (bytevector-u16-ref v 0 'big))]
212
213 [(lambda (x) (= x 12345))
214 (let ([v (make-bytevector 2)])
215 (bytevector-s16-native-set! v 0 12345)
216 (bytevector-s16-native-ref v 0))]
217 [(lambda (x) (= x 12345))
218 (let ([v (make-bytevector 2)])
219 (bytevector-s16-set! v 0 12345 'little)
220 (bytevector-s16-ref v 0 'little))]
221 [(lambda (x) (= x 12345))
222 (let ([v (make-bytevector 2)])
223 (bytevector-s16-set! v 0 12345 'big)
224 (bytevector-s16-ref v 0 'big))]
225
226 [(lambda (x) (= x -12345))
227 (let ([v (make-bytevector 2)])
228 (bytevector-s16-native-set! v 0 -12345)
229 (bytevector-s16-native-ref v 0))]
230 [(lambda (x) (= x -12345))
231 (let ([v (make-bytevector 2)])
232 (bytevector-s16-set! v 0 -12345 'little)
233 (bytevector-s16-ref v 0 'little))]
234 [(lambda (x) (= x -12345))
235 (let ([v (make-bytevector 2)])
236 (bytevector-s16-set! v 0 -12345 'big)
237 (bytevector-s16-ref v 0 'big))]
238
239 [(lambda (x) (= x 4261412863))
240 (let ([v (u8-list->bytevector '(255 255 255 253))])
241 (bytevector-u32-ref v 0 'little))]
242 [(lambda (x) (= x 4294967293))
243 (let ([v (u8-list->bytevector '(255 255 255 253))])
244 (bytevector-u32-ref v 0 'big))]
245
246 [(lambda (x) (= x -33554433))
247 (let ([v (u8-list->bytevector '(255 255 255 253))])
248 (bytevector-s32-ref v 0 'little))]
249 [(lambda (x) (= x -3))
250 (let ([v (u8-list->bytevector '(255 255 255 253))])
251 (bytevector-s32-ref v 0 'big))]
252
253 [(lambda (x) (= x 12345))
254 (let ([v (make-bytevector 4)])
255 (bytevector-u32-set! v 0 12345 'little)
256 (bytevector-u32-ref v 0 'little))]
257 [(lambda (x) (= x 12345))
258 (let ([v (make-bytevector 4)])
259 (bytevector-u32-set! v 0 12345 'big)
260 (bytevector-u32-ref v 0 'big))]
261
262 [(lambda (x) (= x 12345))
263 (let ([v (make-bytevector 4)])
264 (bytevector-s32-set! v 0 12345 'little)
265 (bytevector-s32-ref v 0 'little))]
266 [(lambda (x) (= x 12345))
267 (let ([v (make-bytevector 4)])
268 (bytevector-s32-set! v 0 12345 'big)
269 (bytevector-s32-ref v 0 'big))]
270
271 [(lambda (x) (= x -12345))
272 (let ([v (make-bytevector 4)])
273 (bytevector-s32-set! v 0 -12345 'little)
274 (bytevector-s32-ref v 0 'little))]
275
276 [(lambda (x) (= x -12345))
277 (let ([v (make-bytevector 4)])
278 (bytevector-s32-set! v 0 -12345 'big)
279 (bytevector-s32-ref v 0 'big))]
280
281 [(lambda (x) (= x 17.0))
282 (let ([v (make-bytevector 8)])
283 (bytevector-ieee-double-native-set! v 0 17.0)
284 (bytevector-ieee-double-native-ref v 0))]
285
286 [(lambda (x) (= x 17.0))
287 (let ([v (make-bytevector 8)])
288 (bytevector-ieee-double-set! v 0 17.0 'little)
289 (bytevector-ieee-double-ref v 0 'little))]
290
291 [(lambda (x) (= x 17.0))
292 (let ([v (make-bytevector 8)])
293 (bytevector-ieee-double-set! v 0 17.0 'big)
294 (bytevector-ieee-double-ref v 0 'big))]
295
296 [(lambda (x) (= x 17.0))
297 (let ([v1 (make-bytevector 8)])
298 (bytevector-ieee-double-set! v1 0 17.0 'little)
299 (let ([v2 (u8-list->bytevector
300 (reverse (bytevector->u8-list v1)))])
301 (bytevector-ieee-double-ref v2 0 'big)))]
302
303 [(lambda (x) (= x 17.0))
304 (let ([v1 (make-bytevector 8)])
305 (bytevector-ieee-double-set! v1 0 17.0 'big)
306 (let ([v2 (u8-list->bytevector
307 (reverse (bytevector->u8-list v1)))])
308 (bytevector-ieee-double-ref v2 0 'little)))]
309
310 [(lambda (x) (= x 17.0))
311 (let ([v (make-bytevector 4)])
312 (bytevector-ieee-single-native-set! v 0 17.0)
313 (bytevector-ieee-single-native-ref v 0))]
314
315 [(lambda (x) (= x 17.0))
316 (let ([v (make-bytevector 4)])
317 (bytevector-ieee-single-set! v 0 17.0 'little)
318 (bytevector-ieee-single-ref v 0 'little))]
319
320 [(lambda (x) (= x 17.0))
321 (let ([v (make-bytevector 4)])
322 (bytevector-ieee-single-set! v 0 17.0 'big)
323 (bytevector-ieee-single-ref v 0 'big))]
324
325 [(lambda (x) (= x 17.0))
326 (let ([v1 (make-bytevector 4)])
327 (bytevector-ieee-single-set! v1 0 17.0 'little)
328 (let ([v2 (u8-list->bytevector
329 (reverse (bytevector->u8-list v1)))])
330 (bytevector-ieee-single-ref v2 0 'big)))]
331
332 [(lambda (x) (= x 17.0))
333 (let ([v1 (make-bytevector 4)])
334 (bytevector-ieee-single-set! v1 0 17.0 'big)
335 (let ([v2 (u8-list->bytevector
336 (reverse (bytevector->u8-list v1)))])
337 (bytevector-ieee-single-ref v2 0 'little)))]
338
339 [(lambda (x) (= x 18302628885633695743))
340 (let ([bv (u8-list->bytevector
341 '(255 255 255 255 255 255 255 255
342 255 255 255 255 255 255 255 253))])
343 (bytevector-u64-ref bv 8 (endianness little)))]
344
345 [(lambda (x) (= x -144115188075855873))
346 (let ([bv (u8-list->bytevector
347 '(255 255 255 255 255 255 255 255
348 255 255 255 255 255 255 255 253))])
349 (bytevector-s64-ref bv 8 (endianness little)))]
350
351 [(lambda (x) (= x 18446744073709551613))
352 (let ([bv (u8-list->bytevector
353 '(255 255 255 255 255 255 255 255
354 255 255 255 255 255 255 255 253))])
355 (bytevector-u64-ref bv 8 (endianness big)))]
356
357 [(lambda (x) (= x -3))
358 (let ([bv (u8-list->bytevector
359 '(255 255 255 255 255 255 255 255
360 255 255 255 255 255 255 255 253))])
361 (bytevector-s64-ref bv 8 (endianness big)))]
362
363 [(lambda (x) (= x 18302628885633695743))
364 (let ([bv (u8-list->bytevector
365 '(255 255 255 255 255 255 255 255
366 255 255 255 255 255 255 255 253))])
367 (bytevector-u64-native-ref bv 8))]
368
369 [(lambda (x) (= x -144115188075855873))
370 (let ([bv (u8-list->bytevector
371 '(255 255 255 255 255 255 255 255
372 255 255 255 255 255 255 255 253))])
373 (bytevector-s64-native-ref bv 8))]
374
375
376 [(lambda (x) (= x 73))
377 (let ([sz (- (* 10 4096) 8)])
378 (import (ikarus system $bytevectors))
379 (let ([bv (make-bytevector sz)])
380 (bytevector-u8-set! bv (- sz 1) 73)
381 (collect)
382 ($bytevector-u8-ref bv (- sz 1))))]
383 )
384
385 (define (run-tests)
386 (test-bytevectors))
387 )
388
389
390
391
392