rename (rnrs bytevector) to (rnrs bytevectors)
[bpt/guile.git] / test-suite / tests / bytevectors.test
1 ;;;; bytevectors.test --- R6RS bytevectors. -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 ;;;; Ludovic Courtès
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-bytevector)
21 :use-module (test-suite lib)
22 :use-module (system base compile)
23 :use-module (rnrs bytevectors))
24
25 ;;; Some of the tests in here are examples taken from the R6RS Standard
26 ;;; Libraries document.
27
28 (define-syntax c&e
29 (syntax-rules (pass-if pass-if-exception)
30 ((_ (pass-if test-name exp))
31 (begin (pass-if (string-append test-name " (eval)")
32 (primitive-eval 'exp))
33 (pass-if (string-append test-name " (compile)")
34 (compile 'exp #:to 'value #:env (current-module)))))
35 ((_ (pass-if-exception test-name exc exp))
36 (begin (pass-if-exception (string-append test-name " (eval)")
37 exc (primitive-eval 'exp))
38 (pass-if-exception (string-append test-name " (compile)")
39 exc (compile 'exp #:to 'value
40 #:env (current-module)))))))
41
42 (define-syntax with-test-prefix/c&e
43 (syntax-rules ()
44 ((_ section-name exp ...)
45 (with-test-prefix section-name (c&e exp) ...))))
46
47
48 \f
49 (with-test-prefix/c&e "2.2 General Operations"
50
51 (pass-if "native-endianness"
52 (not (not (memq (native-endianness) '(big little)))))
53
54 (pass-if "make-bytevector"
55 (and (bytevector? (make-bytevector 20))
56 (bytevector? (make-bytevector 20 3))))
57
58 (pass-if "bytevector-length"
59 (= (bytevector-length (make-bytevector 20)) 20))
60
61 (pass-if "bytevector=?"
62 (and (bytevector=? (make-bytevector 20 7)
63 (make-bytevector 20 7))
64 (not (bytevector=? (make-bytevector 20 7)
65 (make-bytevector 20 0))))))
66
67 \f
68 (with-test-prefix/c&e "2.3 Operations on Bytes and Octets"
69
70 (pass-if "bytevector-{u8,s8}-ref"
71 (equal? '(-127 129 -1 255)
72 (let ((b1 (make-bytevector 16 -127))
73 (b2 (make-bytevector 16 255)))
74 (list (bytevector-s8-ref b1 0)
75 (bytevector-u8-ref b1 0)
76 (bytevector-s8-ref b2 0)
77 (bytevector-u8-ref b2 0)))))
78
79 (pass-if "bytevector-{u8,s8}-set!"
80 (equal? '(-126 130 -10 246)
81 (let ((b (make-bytevector 16 -127)))
82
83 (bytevector-s8-set! b 0 -126)
84 (bytevector-u8-set! b 1 246)
85
86 (list (bytevector-s8-ref b 0)
87 (bytevector-u8-ref b 0)
88 (bytevector-s8-ref b 1)
89 (bytevector-u8-ref b 1)))))
90
91 (pass-if "bytevector->u8-list"
92 (let ((lst '(1 2 3 128 150 255)))
93 (equal? lst
94 (bytevector->u8-list
95 (let ((b (make-bytevector 6)))
96 (for-each (lambda (i v)
97 (bytevector-u8-set! b i v))
98 (iota 6)
99 lst)
100 b)))))
101
102 (pass-if "u8-list->bytevector"
103 (let ((lst '(1 2 3 128 150 255)))
104 (equal? lst
105 (bytevector->u8-list (u8-list->bytevector lst)))))
106
107 (pass-if "bytevector-uint-{ref,set!} [small]"
108 (let ((b (make-bytevector 15)))
109 (bytevector-uint-set! b 0 #x1234
110 (endianness little) 2)
111 (equal? (bytevector-uint-ref b 0 (endianness big) 2)
112 #x3412)))
113
114 (pass-if "bytevector-uint-set! [large]"
115 (let ((b (make-bytevector 16)))
116 (bytevector-uint-set! b 0 (- (expt 2 128) 3)
117 (endianness little) 16)
118 (equal? (bytevector->u8-list b)
119 '(253 255 255 255 255 255 255 255
120 255 255 255 255 255 255 255 255))))
121
122 (pass-if "bytevector-uint-{ref,set!} [large]"
123 (let ((b (make-bytevector 120)))
124 (bytevector-uint-set! b 0 (- (expt 2 128) 3)
125 (endianness little) 16)
126 (equal? (bytevector-uint-ref b 0 (endianness little) 16)
127 #xfffffffffffffffffffffffffffffffd)))
128
129 (pass-if "bytevector-sint-ref [small]"
130 (let ((b (u8-list->bytevector '(#xff #xf0 #xff))))
131 (equal? (bytevector-sint-ref b 0 (endianness big) 2)
132 (bytevector-sint-ref b 1 (endianness little) 2)
133 -16)))
134
135 (pass-if "bytevector-sint-ref [large]"
136 (let ((b (make-bytevector 50)))
137 (bytevector-uint-set! b 0 (- (expt 2 128) 3)
138 (endianness little) 16)
139 (equal? (bytevector-sint-ref b 0 (endianness little) 16)
140 -3)))
141
142 (pass-if "bytevector-sint-set! [small]"
143 (let ((b (make-bytevector 3)))
144 (bytevector-sint-set! b 0 -16 (endianness big) 2)
145 (bytevector-sint-set! b 1 -16 (endianness little) 2)
146 (equal? (bytevector->u8-list b)
147 '(#xff #xf0 #xff))))
148
149 (pass-if "equal?"
150 (let ((bv1 (u8-list->bytevector (iota 123)))
151 (bv2 (u8-list->bytevector (iota 123))))
152 (equal? bv1 bv2))))
153
154 \f
155 (with-test-prefix/c&e "2.4 Operations on Integers of Arbitrary Size"
156
157 (pass-if "bytevector->sint-list"
158 (let ((b (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
159 (equal? (bytevector->sint-list b (endianness little) 2)
160 '(513 -253 513 513))))
161
162 (pass-if "bytevector->uint-list"
163 (let ((b (u8-list->bytevector '(2 1 255 3 2 1 2 1))))
164 (equal? (bytevector->uint-list b (endianness big) 2)
165 '(513 65283 513 513))))
166
167 (pass-if "bytevector->uint-list [empty]"
168 (let ((b (make-bytevector 0)))
169 (null? (bytevector->uint-list b (endianness big) 2))))
170
171 (pass-if-exception "bytevector->sint-list [out-of-range]"
172 exception:out-of-range
173 (bytevector->sint-list (make-bytevector 6) (endianness little) 8))
174
175 (pass-if "bytevector->sint-list [off-by-one]"
176 (equal? (bytevector->sint-list (make-bytevector 31 #xff)
177 (endianness little) 8)
178 '(-1 -1 -1)))
179
180 (pass-if "{sint,uint}-list->bytevector"
181 (let ((b1 (sint-list->bytevector '(513 -253 513 513)
182 (endianness little) 2))
183 (b2 (uint-list->bytevector '(513 65283 513 513)
184 (endianness little) 2))
185 (b3 (u8-list->bytevector '(1 2 3 255 1 2 1 2))))
186 (and (bytevector=? b1 b2)
187 (bytevector=? b2 b3))))
188
189 (pass-if "sint-list->bytevector [limits]"
190 (bytevector=? (sint-list->bytevector '(-32768 32767)
191 (endianness big) 2)
192 (let ((bv (make-bytevector 4)))
193 (bytevector-u8-set! bv 0 #x80)
194 (bytevector-u8-set! bv 1 #x00)
195 (bytevector-u8-set! bv 2 #x7f)
196 (bytevector-u8-set! bv 3 #xff)
197 bv)))
198
199 (pass-if-exception "sint-list->bytevector [out-of-range]"
200 exception:out-of-range
201 (sint-list->bytevector (list 0 0 (expt 2 16)) (endianness big)
202 2))
203
204 (pass-if-exception "uint-list->bytevector [out-of-range]"
205 exception:out-of-range
206 (uint-list->bytevector '(0 -1) (endianness big) 2)))
207
208 \f
209 (with-test-prefix/c&e "2.5 Operations on 16-Bit Integers"
210
211 (pass-if "bytevector-u16-ref"
212 (let ((b (u8-list->bytevector
213 '(255 255 255 255 255 255 255 255
214 255 255 255 255 255 255 255 253))))
215 (and (equal? (bytevector-u16-ref b 14 (endianness little))
216 #xfdff)
217 (equal? (bytevector-u16-ref b 14 (endianness big))
218 #xfffd))))
219
220 (pass-if "bytevector-s16-ref"
221 (let ((b (u8-list->bytevector
222 '(255 255 255 255 255 255 255 255
223 255 255 255 255 255 255 255 253))))
224 (and (equal? (bytevector-s16-ref b 14 (endianness little))
225 -513)
226 (equal? (bytevector-s16-ref b 14 (endianness big))
227 -3))))
228
229 (pass-if "bytevector-s16-ref [unaligned]"
230 (let ((b (u8-list->bytevector '(#xff #xf0 #xff))))
231 (equal? (bytevector-s16-ref b 1 (endianness little))
232 -16)))
233
234 (pass-if "bytevector-{u16,s16}-ref"
235 (let ((b (make-bytevector 2)))
236 (bytevector-u16-set! b 0 44444 (endianness little))
237 (and (equal? (bytevector-u16-ref b 0 (endianness little))
238 44444)
239 (equal? (bytevector-s16-ref b 0 (endianness little))
240 (- 44444 65536)))))
241
242 (pass-if "bytevector-native-{u16,s16}-{ref,set!}"
243 (let ((b (make-bytevector 2)))
244 (bytevector-u16-native-set! b 0 44444)
245 (and (equal? (bytevector-u16-native-ref b 0)
246 44444)
247 (equal? (bytevector-s16-native-ref b 0)
248 (- 44444 65536)))))
249
250 (pass-if "bytevector-s16-{ref,set!} [unaligned]"
251 (let ((b (make-bytevector 3)))
252 (bytevector-s16-set! b 1 -77 (endianness little))
253 (equal? (bytevector-s16-ref b 1 (endianness little))
254 -77))))
255
256 \f
257 (with-test-prefix/c&e "2.6 Operations on 32-bit Integers"
258
259 (pass-if "bytevector-u32-ref"
260 (let ((b (u8-list->bytevector
261 '(255 255 255 255 255 255 255 255
262 255 255 255 255 255 255 255 253))))
263 (and (equal? (bytevector-u32-ref b 12 (endianness little))
264 #xfdffffff)
265 (equal? (bytevector-u32-ref b 12 (endianness big))
266 #xfffffffd))))
267
268 (pass-if "bytevector-s32-ref"
269 (let ((b (u8-list->bytevector
270 '(255 255 255 255 255 255 255 255
271 255 255 255 255 255 255 255 253))))
272 (and (equal? (bytevector-s32-ref b 12 (endianness little))
273 -33554433)
274 (equal? (bytevector-s32-ref b 12 (endianness big))
275 -3))))
276
277 (pass-if "bytevector-{u32,s32}-ref"
278 (let ((b (make-bytevector 4)))
279 (bytevector-u32-set! b 0 2222222222 (endianness little))
280 (and (equal? (bytevector-u32-ref b 0 (endianness little))
281 2222222222)
282 (equal? (bytevector-s32-ref b 0 (endianness little))
283 (- 2222222222 (expt 2 32))))))
284
285 (pass-if "bytevector-{u32,s32}-native-{ref,set!}"
286 (let ((b (make-bytevector 4)))
287 (bytevector-u32-native-set! b 0 2222222222)
288 (and (equal? (bytevector-u32-native-ref b 0)
289 2222222222)
290 (equal? (bytevector-s32-native-ref b 0)
291 (- 2222222222 (expt 2 32)))))))
292
293 \f
294 (with-test-prefix/c&e "2.7 Operations on 64-bit Integers"
295
296 (pass-if "bytevector-u64-ref"
297 (let ((b (u8-list->bytevector
298 '(255 255 255 255 255 255 255 255
299 255 255 255 255 255 255 255 253))))
300 (and (equal? (bytevector-u64-ref b 8 (endianness little))
301 #xfdffffffffffffff)
302 (equal? (bytevector-u64-ref b 8 (endianness big))
303 #xfffffffffffffffd))))
304
305 (pass-if "bytevector-s64-ref"
306 (let ((b (u8-list->bytevector
307 '(255 255 255 255 255 255 255 255
308 255 255 255 255 255 255 255 253))))
309 (and (equal? (bytevector-s64-ref b 8 (endianness little))
310 -144115188075855873)
311 (equal? (bytevector-s64-ref b 8 (endianness big))
312 -3))))
313
314 (pass-if "bytevector-{u64,s64}-ref"
315 (let ((b (make-bytevector 8))
316 (big 9333333333333333333))
317 (bytevector-u64-set! b 0 big (endianness little))
318 (and (equal? (bytevector-u64-ref b 0 (endianness little))
319 big)
320 (equal? (bytevector-s64-ref b 0 (endianness little))
321 (- big (expt 2 64))))))
322
323 (pass-if "bytevector-{u64,s64}-native-{ref,set!}"
324 (let ((b (make-bytevector 8))
325 (big 9333333333333333333))
326 (bytevector-u64-native-set! b 0 big)
327 (and (equal? (bytevector-u64-native-ref b 0)
328 big)
329 (equal? (bytevector-s64-native-ref b 0)
330 (- big (expt 2 64))))))
331
332 (pass-if "ref/set! with zero"
333 (let ((b (make-bytevector 8)))
334 (bytevector-s64-set! b 0 -1 (endianness big))
335 (bytevector-u64-set! b 0 0 (endianness big))
336 (= 0 (bytevector-u64-ref b 0 (endianness big))))))
337
338 \f
339 (with-test-prefix/c&e "2.8 Operations on IEEE-754 Representations"
340
341 (pass-if "bytevector-ieee-single-native-{ref,set!}"
342 (let ((b (make-bytevector 4))
343 (number 3.00))
344 (bytevector-ieee-single-native-set! b 0 number)
345 (equal? (bytevector-ieee-single-native-ref b 0)
346 number)))
347
348 (pass-if "bytevector-ieee-single-{ref,set!}"
349 (let ((b (make-bytevector 8))
350 (number 3.14))
351 (bytevector-ieee-single-set! b 0 number (endianness little))
352 (bytevector-ieee-single-set! b 4 number (endianness big))
353 (equal? (bytevector-ieee-single-ref b 0 (endianness little))
354 (bytevector-ieee-single-ref b 4 (endianness big)))))
355
356 (pass-if "bytevector-ieee-single-{ref,set!} [unaligned]"
357 (let ((b (make-bytevector 9))
358 (number 3.14))
359 (bytevector-ieee-single-set! b 1 number (endianness little))
360 (bytevector-ieee-single-set! b 5 number (endianness big))
361 (equal? (bytevector-ieee-single-ref b 1 (endianness little))
362 (bytevector-ieee-single-ref b 5 (endianness big)))))
363
364 (pass-if "bytevector-ieee-double-native-{ref,set!}"
365 (let ((b (make-bytevector 8))
366 (number 3.14))
367 (bytevector-ieee-double-native-set! b 0 number)
368 (equal? (bytevector-ieee-double-native-ref b 0)
369 number)))
370
371 (pass-if "bytevector-ieee-double-{ref,set!}"
372 (let ((b (make-bytevector 16))
373 (number 3.14))
374 (bytevector-ieee-double-set! b 0 number (endianness little))
375 (bytevector-ieee-double-set! b 8 number (endianness big))
376 (equal? (bytevector-ieee-double-ref b 0 (endianness little))
377 (bytevector-ieee-double-ref b 8 (endianness big))))))
378
379 \f
380
381 ;; Default to the C locale for the following tests.
382 (setlocale LC_ALL "C")
383
384
385 (with-test-prefix "2.9 Operations on Strings"
386
387 (pass-if "string->utf8"
388 (let* ((str "hello, world")
389 (utf8 (string->utf8 str)))
390 (and (bytevector? utf8)
391 (= (bytevector-length utf8)
392 (string-length str))
393 (equal? (string->list str)
394 (map integer->char (bytevector->u8-list utf8))))))
395
396 (pass-if "string->utf8 [latin-1]"
397 (let* ((str "hé, ça va bien ?")
398 (utf8 (string->utf8 str)))
399 (and (bytevector? utf8)
400 (= (bytevector-length utf8)
401 (+ 2 (string-length str))))))
402
403 (pass-if "string->utf16"
404 (let* ((str "hello, world")
405 (utf16 (string->utf16 str)))
406 (and (bytevector? utf16)
407 (= (bytevector-length utf16)
408 (* 2 (string-length str)))
409 (equal? (string->list str)
410 (map integer->char
411 (bytevector->uint-list utf16
412 (endianness big) 2))))))
413
414 (pass-if "string->utf16 [little]"
415 (let* ((str "hello, world")
416 (utf16 (string->utf16 str (endianness little))))
417 (and (bytevector? utf16)
418 (= (bytevector-length utf16)
419 (* 2 (string-length str)))
420 (equal? (string->list str)
421 (map integer->char
422 (bytevector->uint-list utf16
423 (endianness little) 2))))))
424
425
426 (pass-if "string->utf32"
427 (let* ((str "hello, world")
428 (utf32 (string->utf32 str)))
429 (and (bytevector? utf32)
430 (= (bytevector-length utf32)
431 (* 4 (string-length str)))
432 (equal? (string->list str)
433 (map integer->char
434 (bytevector->uint-list utf32
435 (endianness big) 4))))))
436
437 (pass-if "string->utf32 [Greek]"
438 (let* ((str "Ἄνεμοι")
439 (utf32 (string->utf32 str)))
440 (and (bytevector? utf32)
441 (equal? (bytevector->uint-list utf32 (endianness big) 4)
442 '(#x1f0c #x3bd #x3b5 #x3bc #x3bf #x3b9)))))
443
444 (pass-if "string->utf32 [little]"
445 (let* ((str "hello, world")
446 (utf32 (string->utf32 str (endianness little))))
447 (and (bytevector? utf32)
448 (= (bytevector-length utf32)
449 (* 4 (string-length str)))
450 (equal? (string->list str)
451 (map integer->char
452 (bytevector->uint-list utf32
453 (endianness little) 4))))))
454
455 (pass-if "utf8->string"
456 (let* ((utf8 (u8-list->bytevector (map char->integer
457 (string->list "hello, world"))))
458 (str (utf8->string utf8)))
459 (and (string? str)
460 (= (string-length str)
461 (bytevector-length utf8))
462 (equal? (string->list str)
463 (map integer->char (bytevector->u8-list utf8))))))
464
465 (pass-if "utf8->string [latin-1]"
466 (let* ((utf8 (string->utf8 "hé, ça va bien ?"))
467 (str (utf8->string utf8)))
468 (and (string? str)
469 (= (string-length str)
470 (- (bytevector-length utf8) 2)))))
471
472 (pass-if "utf16->string"
473 (let* ((utf16 (uint-list->bytevector (map char->integer
474 (string->list "hello, world"))
475 (endianness big) 2))
476 (str (utf16->string utf16)))
477 (and (string? str)
478 (= (* 2 (string-length str))
479 (bytevector-length utf16))
480 (equal? (string->list str)
481 (map integer->char
482 (bytevector->uint-list utf16 (endianness big)
483 2))))))
484
485 (pass-if "utf16->string [little]"
486 (let* ((utf16 (uint-list->bytevector (map char->integer
487 (string->list "hello, world"))
488 (endianness little) 2))
489 (str (utf16->string utf16 (endianness little))))
490 (and (string? str)
491 (= (* 2 (string-length str))
492 (bytevector-length utf16))
493 (equal? (string->list str)
494 (map integer->char
495 (bytevector->uint-list utf16 (endianness little)
496 2))))))
497 (pass-if "utf32->string"
498 (let* ((utf32 (uint-list->bytevector (map char->integer
499 (string->list "hello, world"))
500 (endianness big) 4))
501 (str (utf32->string utf32)))
502 (and (string? str)
503 (= (* 4 (string-length str))
504 (bytevector-length utf32))
505 (equal? (string->list str)
506 (map integer->char
507 (bytevector->uint-list utf32 (endianness big)
508 4))))))
509
510 (pass-if "utf32->string [little]"
511 (let* ((utf32 (uint-list->bytevector (map char->integer
512 (string->list "hello, world"))
513 (endianness little) 4))
514 (str (utf32->string utf32 (endianness little))))
515 (and (string? str)
516 (= (* 4 (string-length str))
517 (bytevector-length utf32))
518 (equal? (string->list str)
519 (map integer->char
520 (bytevector->uint-list utf32 (endianness little)
521 4)))))))
522
523
524 \f
525 (with-test-prefix "Datum Syntax"
526
527 (pass-if "empty"
528 (equal? (with-input-from-string "#vu8()" read)
529 (make-bytevector 0)))
530
531 (pass-if "simple"
532 (equal? (with-input-from-string "#vu8(1 2 3 4 5)" read)
533 (u8-list->bytevector '(1 2 3 4 5))))
534
535 (pass-if ">127"
536 (equal? (with-input-from-string "#vu8(0 255 127 128)" read)
537 (u8-list->bytevector '(0 255 127 128))))
538
539 (pass-if "self-evaluating?"
540 (self-evaluating? (make-bytevector 1)))
541
542 (pass-if "self-evaluating"
543 (equal? (eval (with-input-from-string "#vu8(1 2 3 4 5)" read)
544 (current-module))
545 (u8-list->bytevector '(1 2 3 4 5))))
546
547 (pass-if "quoted"
548 (equal? (eval (with-input-from-string "'#vu8(1 2 3 4 5)" read)
549 (current-module))
550 (u8-list->bytevector '(1 2 3 4 5))))
551
552 (pass-if "literal simple"
553 (equal? #vu8(1 2 3 4 5)
554 (u8-list->bytevector '(1 2 3 4 5))))
555
556 (pass-if "literal >127"
557 (equal? #vu8(0 255 127 128)
558 (u8-list->bytevector '(0 255 127 128))))
559
560 (pass-if "literal quoted"
561 (equal? '#vu8(1 2 3 4 5)
562 (u8-list->bytevector '(1 2 3 4 5))))
563
564 (pass-if-exception "incorrect prefix"
565 exception:read-error
566 (with-input-from-string "#vi8(1 2 3)" read))
567
568 (pass-if-exception "extraneous space"
569 exception:read-error
570 (with-input-from-string "#vu8 (1 2 3)" read))
571
572 (pass-if-exception "negative integers"
573 exception:wrong-type-arg
574 (with-input-from-string "#vu8(-1 -2 -3)" read))
575
576 (pass-if-exception "out-of-range integers"
577 exception:wrong-type-arg
578 (with-input-from-string "#vu8(0 256)" read)))
579
580 \f
581 (with-test-prefix "Generalized Vectors"
582
583 (pass-if "generalized-vector?"
584 (generalized-vector? #vu8(1 2 3)))
585
586 (pass-if "generalized-vector-length"
587 (equal? (iota 16)
588 (map generalized-vector-length
589 (map make-bytevector (iota 16)))))
590
591 (pass-if "generalized-vector-ref"
592 (let ((bv #vu8(255 127)))
593 (and (= 255 (generalized-vector-ref bv 0))
594 (= 127 (generalized-vector-ref bv 1)))))
595
596 (pass-if-exception "generalized-vector-ref [index out-of-range]"
597 exception:out-of-range
598 (let ((bv #vu8(1 2)))
599 (generalized-vector-ref bv 2)))
600
601 (pass-if "generalized-vector-set!"
602 (let ((bv (make-bytevector 2)))
603 (generalized-vector-set! bv 0 255)
604 (generalized-vector-set! bv 1 77)
605 (equal? '(255 77)
606 (bytevector->u8-list bv))))
607
608 (pass-if-exception "generalized-vector-set! [index out-of-range]"
609 exception:out-of-range
610 (let ((bv (make-bytevector 2)))
611 (generalized-vector-set! bv 2 0)))
612
613 (pass-if-exception "generalized-vector-set! [value out-of-range]"
614 exception:out-of-range
615 (let ((bv (make-bytevector 2)))
616 (generalized-vector-set! bv 0 256)))
617
618 (pass-if "array-type"
619 (eq? 'vu8 (array-type #vu8())))
620
621 (pass-if "array-contents"
622 (let ((bv (u8-list->bytevector (iota 10))))
623 (eq? bv (array-contents bv))))
624
625 (pass-if "array-ref"
626 (let ((bv (u8-list->bytevector (iota 10))))
627 (equal? (iota 10)
628 (map (lambda (i) (array-ref bv i))
629 (iota 10)))))
630
631 (pass-if "array-set!"
632 (let ((bv (make-bytevector 10)))
633 (for-each (lambda (i)
634 (array-set! bv i i))
635 (iota 10))
636 (equal? (iota 10)
637 (bytevector->u8-list bv))))
638
639 (pass-if "make-typed-array"
640 (let ((bv (make-typed-array 'vu8 77 33)))
641 (equal? bv (u8-list->bytevector (make-list 33 77)))))
642
643 (pass-if-exception "make-typed-array [out-of-range]"
644 exception:out-of-range
645 (make-typed-array 'vu8 256 77)))
646
647 \f
648 (with-test-prefix "uniform-array->bytevector"
649
650 (pass-if "bytevector"
651 (let ((bv #vu8(0 1 128 255)))
652 (equal? bv (uniform-array->bytevector bv))))
653
654 (pass-if "empty bitvector"
655 (let ((bv (uniform-array->bytevector (make-bitvector 0))))
656 (equal? bv #vu8())))
657
658 (pass-if "bitvector < 8"
659 (let ((bv (uniform-array->bytevector (make-bitvector 4 #t))))
660 (= (bytevector-length bv) 1)))
661
662 (pass-if "bitvector == 8"
663 (let ((bv (uniform-array->bytevector (make-bitvector 8 #t))))
664 (= (bytevector-length bv) 1)))
665
666 (pass-if "bitvector > 8"
667 (let ((bv (uniform-array->bytevector (make-bitvector 9 #t))))
668 (= (bytevector-length bv) 2))))