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