Debian Codegraph @

ext/vport/test.scm

raw
1 ;;
2 ;; testing vport
3 ;;
4
5 (use gauche.test)
6
7 (test-start "vport")
8 (use gauche.vport)
9 (use gauche.uvector)
10 (use gauche.sequence)
11 (use gauche.generator)
12 (use scheme.list)
13 (use srfi.13)
14 (use file.util)
15 (test-module 'gauche.vport)
16
17 ;;-----------------------------------------------------------
18 (test-section "virtual-input-port")
19
20 (test* "vanilla" (make-list 8 #t)
21 (let ((p (make <virtual-input-port>)))
22 (list (eof-object? (read-byte p))
23 (eof-object? (read-char p))
24 (eof-object? (read-line p))
25 (eof-object? (read-block 10 p))
26 (byte-ready? p)
27 (char-ready? p)
28 (eof-object? (peek-byte p))
29 (eof-object? (peek-char p)))))
30
31 (test* "getc" '(#\a #\a #\a 97 97 97 #*"aaaaaaaaaa")
32 (let* ((p (make <virtual-input-port> :getc (lambda () #\a)))
33 (c0 (read-char p))
34 (c1 (read-char p))
35 (c2 (read-char p))
36 (b0 (read-byte p))
37 (b1 (read-byte p))
38 (b2 (read-byte p))
39 (s (read-block 10 p)))
40 (list c0 c1 c2 b0 b1 b2 s)))
41
42 (test* "getb" '(#\a #\a #\a 97 97 97 #*"aaaaaaaaaa")
43 (let* ((p (make <virtual-input-port> :getb (lambda () 97)))
44 (c0 (read-char p))
45 (c1 (read-char p))
46 (c2 (read-char p))
47 (b0 (read-byte p))
48 (b1 (read-byte p))
49 (b2 (read-byte p))
50 (s (read-block 10 p)))
51 (list c0 c1 c2 b0 b1 b2 s)))
52
53 (test* "peekc/peekb & getc" '(#\a #\a 98 #\b)
54 (let* ((x '(#\a #\b #\c))
55 (p (make <virtual-input-port>
56 :getc (lambda ()
57 (and (pair? x) (pop! x)))))
58 (c0 (peek-char p))
59 (c1 (read-char p))
60 (c2 (peek-byte p))
61 (c3 (read-char p))
62 )
63 (list c0 c1 c2 c3)))
64
65 (test* "peekc/peekb & getb" '(97 #\a #\b 98)
66 (let* ((x '(97 98 99))
67 (p (make <virtual-input-port>
68 :getb (lambda ()
69 (and (pair? x) (pop! x)))))
70 (c0 (peek-byte p))
71 (c1 (read-char p))
72 (c2 (peek-char p))
73 (c3 (read-byte p)))
74 (list c0 c1 c2 c3)))
75
76 (test* "getc -> read-line" "abcd"
77 (let* ((x '(#\a #\b #\c #\d))
78 (p (make <virtual-input-port>
79 :getc (lambda ()
80 (and (pair? x) (pop! x))))))
81 (read-line p)))
82
83 (test* "getc -> read-line" '("abcd" "efg")
84 (let* ((x '(#\a #\b #\c #\d #\newline #\e #\f #\g #\newline))
85 (p (make <virtual-input-port>
86 :getc (lambda ()
87 (and (pair? x) (pop! x))))))
88 (port->string-list p)))
89
90 (test* "getb -> read-line" '("abcd" "efg")
91 (let* ((x '(97 98 99 100 10 101 102 103 10))
92 (p (make <virtual-input-port>
93 :getb (lambda ()
94 (and (pair? x) (pop! x))))))
95 (port->string-list p)))
96
97 (test* "getc -> read-block" #*"abcd\ne"
98 (let* ((x '(#\a #\b #\c #\d #\newline #\e #\f #\g #\newline))
99 (p (make <virtual-input-port>
100 :getc (lambda ()
101 (and (pair? x) (pop! x))))))
102 (read-block 6 p)))
103
104 (test* "getc -> read-block" #*"abcd\nefg\n"
105 (let* ((x '(#\a #\b #\c #\d #\newline #\e #\f #\g #\newline))
106 (p (make <virtual-input-port>
107 :getc (lambda ()
108 (and (pair? x) (pop! x))))))
109 (read-block 200 p)))
110
111 (test* "getb -> read-block" #*"abcd\ne"
112 (let* ((x '(97 98 99 100 10 101 102 103 10))
113 (p (make <virtual-input-port>
114 :getb (lambda ()
115 (and (pair? x) (pop! x))))))
116 (read-block 6 p)))
117
118 (test* "getb -> read-block" #*"abcd\nefg\n"
119 (let* ((x '(97 98 99 100 10 101 102 103 10))
120 (p (make <virtual-input-port>
121 :getb (lambda ()
122 (and (pair? x) (pop! x))))))
123 (read-block 200 p)))
124
125 (test* "gets -> read-block" #*"this is fr"
126 (let* ((buf "this is from gets")
127 (p (make <virtual-input-port>
128 :getc (lambda () #f)
129 :getb (lambda () #f)
130 :gets (lambda (length)
131 (if (< length (string-size buf))
132 (substring buf 0 length)
133 buf)))))
134 (read-block 10 p)))
135
136 ;; regression tests for peek consuming EOF
137 (test* "peek-char EOF regression" (make-list 3 (eof-object))
138 (let* ((state #f)
139 (p (make <virtual-input-port>
140 :getc (lambda ()
141 (set! state (not state))
142 (if state (eof-object) #\a)))))
143 (list (peek-char p)
144 (peek-byte p)
145 (read-char p))))
146
147 (test* "peek-byte EOF regression" (make-list 3 (eof-object))
148 (let* ((state #f)
149 (p (make <virtual-input-port>
150 :getc (lambda ()
151 (set! state (not state))
152 (if state (eof-object) #\a)))))
153 (list (peek-byte p)
154 (peek-char p)
155 (read-byte p))))
156
157 ;;-----------------------------------------------------------
158 (test-section "virtual-output-port")
159
160 (test* "putc" "abcdef"
161 (call-with-output-string
162 (lambda (o)
163 (let* ((p (make <virtual-output-port>
164 :putc (^c (write-char c o)))))
165 (write-char #\a p)
166 (display "bcd" p)
167 (write-byte 101 p)
168 (write-char #\f p)
169 (get-output-string o)))))
170
171 (test* "putb" "abcdef"
172 (call-with-output-string
173 (lambda (o)
174 (let* ((p (make <virtual-output-port>
175 :putb (^b (write-byte b o)))))
176 (write-char #\a p)
177 (display "bcd" p)
178 (write-byte 101 p)
179 (write-char #\f p)
180 (get-output-string o)))))
181
182 (test* "puts" "bcdxyz"
183 (call-with-output-string
184 (lambda (o)
185 (let* ((p (make <virtual-output-port>
186 :putb (^b #f)
187 :putc (^c #f)
188 :puts (^s (display s o)))))
189 (write-char #\a p)
190 (display "bcd" p)
191 (write-byte 101 p)
192 (write-char #\f p)
193 (display "xyz" p)
194 (get-output-string o)))))
195
196 ;;-----------------------------------------------------------
197 (test-section "virtual-io-port")
198
199 (let ([data (make-vector 100)]
200 [pos 0]
201 [end 0])
202 (define p (make <virtual-io-port>
203 :getc (^[]
204 (if (>= pos end)
205 (eof-object)
206 (begin0 (vector-ref data pos)
207 (inc! pos))))
208 :putc (^c
209 (vector-set! data pos c)
210 (inc! pos)
211 (set! end (max pos end)))
212 :seek (^[off whence]
213 (cond
214 [(= whence SEEK_SET) (set! pos off)]
215 [(= whence SEEK_CUR) (inc! pos off)]
216 [(= whence SEEK_END) (set! pos (+ end off))])
217 pos)))
218 (vector-copy! data 0 '#(#\a #\b #\c #\d #\e))
219 (inc! end 5)
220 (test* "read from io port" "abcde" (port->string p))
221 (test* "port-tell" 5 (port-tell p))
222 (write-char #\X p)
223 (write-char #\Y p)
224 (write-char #\Z p)
225 (port-seek p 0)
226 (test* "write, then read" "abcdeXYZ" (port->string p))
227 (port-seek p -8 SEEK_END)
228 (write-string "PQR" p)
229 (port-seek p 2 SEEK_CUR)
230 (write-string "12345" p)
231 (port-seek p 0)
232 (test* "write, then read 2" "PQRde12345" (port->string p))
233 )
234
235 (let ([data (make-vector 100)]
236 [pos 0]
237 [end 0])
238 (define p (make <virtual-io-port>
239 :getc (^[]
240 (if (>= pos end)
241 (eof-object)
242 (begin0 (vector-ref data pos)
243 (inc! pos))))
244 :putc (^c
245 (vector-set! data pos c)
246 (inc! pos)
247 (set! end (max pos end)))
248 :getpos (^[] pos)
249 :setpos (^p (set! pos p))))
250 (vector-copy! data 0 '#(#\a #\b #\c #\d #\e))
251 (inc! end 5)
252 (test* "read from io port" "abcde" (port->string p))
253 (test* "port-tell" 5 (port-position p))
254 (write-char #\X p)
255 (write-char #\Y p)
256 (write-char #\Z p)
257 (set-port-position! p 0)
258 (test* "write, then read" "abcdeXYZ" (port->string p))
259 (set-port-position! p 5)
260 (write-string "PQR" p)
261 (set-port-position! p 0)
262 (write-string "12345" p)
263 (set-port-position! p 0)
264 (test* "write, then read 2" "12345PQR" (port->string p))
265 )
266
267 ;;-----------------------------------------------------------
268 (test-section "buffered-input-port")
269
270 (let ()
271 (define (test-biport file size)
272 (let* ((ifile (build-path 'cld file))
273 (src (open-input-file ifile))
274 (p (apply make <buffered-input-port>
275 :fill (lambda (buf) (read-block! buf src))
276 :close (lambda () (close-input-port src))
277 (if size
278 (list :buffer-size size)
279 '())))
280 (a (file->string-list ifile))
281 (b (port->string-list p)))
282 (close-input-port p)
283 (list (equal? a b) (port-closed? src))))
284
285 (test* "vport.c" '(#t #t) (test-biport "vport.c" #f))
286 (test* "vport.h" '(#t #t) (test-biport "vport.h" #f))
287
288 (test* "vport.c (bufsize=100)"
289 '(#t #t) (test-biport "vport.c" 100))
290 (test* "vport.h (bufsize=100)"
291 '(#t #t) (test-biport "vport.h" 100))
292 (test* "vport.c (bufsize=65536)"
293 '(#t #t) (test-biport "vport.c" 65536))
294 (test* "vport.h (bufsize=65536)"
295 '(#t #t) (test-biport "vport.h" 65536))
296 (test* "vport.c (bufsize=1)"
297 '(#t #t) (test-biport "vport.c" 1))
298 (test* "vport.c (bufsize=0)"
299 '(#t #t) (test-biport "vport.c" 0))
300 )
301
302 ;; test with no seeker; pointed in WiLiKi:Gauche:Bugs
303 (let ()
304 (define in
305 (let1 index 0
306 (make <buffered-input-port>
307 :fill (lambda (buf)
308 (for-each-with-index
309 (lambda (i _)
310 (u8vector-set! buf i (logand (+ index i) #xFF)))
311 buf)
312 (let ((size (u8vector-length buf)))
313 (inc! index size)
314 size))
315 )))
316 (test* "buffered-input-port w/o seeker"
317 '(0 1 #t 2)
318 (let* ([a (read-byte in)]
319 [b (read-byte in)]
320 [s (guard (e [(<port-error> e) #t])
321 (port-seek in 5 SEEK_SET))] ;this shouldn't move the point
322 [c (read-byte in)])
323 (list a b s c)))
324 )
325
326 ;; buffered input port and buffering mode
327 (let ()
328 (define p (make <buffered-input-port>
329 :fill (^[buf] (u8vector-set! buf 0 #x20) 1)
330 :ready (^[] #f)))
331 (set! (port-buffering p) :none)
332 (test* "buffered-input-port premature return" '#u8(#x20)
333 (read-uvector <u8vector> 20 p))
334 )
335
336 ;;-----------------------------------------------------------
337 (test-section "buffered-output-port")
338
339 (let ()
340 (define (test-boport file size)
341 (let* ((ifile (build-path 'cld file))
342 (src (file->string ifile))
343 (sink (open-output-string))
344 (closed? #f)
345 (p (apply make <buffered-output-port>
346 :flush (lambda (buf force?)
347 (write-block buf sink)
348 (u8vector-length buf))
349 :close (lambda () (set! closed? #t))
350 (if size
351 (list :buffer-size size)
352 '()))))
353 (string-for-each (^c (write-char c p)) src)
354 (close-output-port p)
355 (list (equal? src (get-output-string sink))
356 closed?)))
357
358 (test* "vport.c" '(#t #t) (test-boport "vport.c" #f))
359 (test* "vport.h" '(#t #t) (test-boport "vport.h" #f))
360
361 (test* "vport.c (bufsize=100)"
362 '(#t #t) (test-boport "vport.c" 100))
363 (test* "vport.h (bufsize=100)"
364 '(#t #t) (test-boport "vport.h" 100))
365 (test* "vport.c (bufsize=65536)"
366 '(#t #t) (test-boport "vport.c" 65536))
367 (test* "vport.h (bufsize=65536)"
368 '(#t #t) (test-boport "vport.h" 65536))
369 (test* "vport.c (bufsize=1)"
370 '(#t #t) (test-boport "vport.c" 1))
371 (test* "vport.c (bufsize=0)"
372 '(#t #t) (test-boport "vport.c" 0))
373 )
374
375 ;;-----------------------------------------------------------
376 (test-section "uvector-input-port")
377
378 (let ()
379 (define (tester size)
380 (test* #"size=~size" #t
381 (let1 v (make-u8vector size 0)
382 (dotimes (i size) (u8vector-set! v i (modulo i 256)))
383 (let* ((p (open-input-uvector v))
384 (d (with-output-to-string
385 (lambda ()
386 (let loop ((b (read-byte p)))
387 (unless (eof-object? b)
388 (write-byte b)
389 (loop (read-byte p))))))))
390 (equal? v (string->u8vector d))))))
391 (tester 0)
392 (tester 10)
393 (tester 16385))
394
395 (let* ((size 1024)
396 (v (make-u8vector size 0)))
397 (dotimes (i size) (u8vector-set! v i (modulo i 256)))
398 (let1 p (open-input-uvector v)
399 (test* "port-seek (SEEK_SET)" (+ 128 256)
400 (port-seek p (+ 128 256) SEEK_SET))
401 (test* "read after seek" 128
402 (read-byte p))
403 (test* "port-seek (SEEK_CUR)" (+ 127 256)
404 (port-seek p -2 SEEK_CUR))
405 (test* "read after seek" 127
406 (read-byte p))
407 (test* "port-seek (SEEK_END)" (- size 5)
408 (port-seek p -5 SEEK_END))
409 (test* "read after seek" (- 256 5)
410 (read-byte p))
411 (test* "port-seek (oob)" #t
412 (begin (port-seek p 10 SEEK_END)
413 (eof-object? (read-byte p))))
414 ))
415
416 ;;-----------------------------------------------------------
417 (test-section "uvector-output-port")
418
419 (let ()
420 (define (tester size)
421 (test* #"size=~size" #t
422 (let1 v (make-u8vector size 0)
423 (dotimes (i size) (u8vector-set! v i (modulo i 256)))
424 (let* ((dst (make-u8vector size 0))
425 (p (open-output-uvector dst)))
426 (dotimes (i size)
427 (write-byte (u8vector-ref v i) p))
428 (close-output-port p)
429 (equal? v dst)))))
430 (tester 0)
431 (tester 10)
432 (tester 16385))
433
434 (let* ((v (make-u8vector 16 0)))
435 (let1 p (open-output-uvector v)
436 (test* "port-seek (SEEK_SET)" 3 (port-seek p 3 SEEK_SET))
437 (test* "write after seek" '#u8(0 0 0 #xff 0 0 0 0 0 0 0 0 0 0 0 0 )
438 (begin (write-byte #xff p) (flush p) (u8vector-copy v)))
439 (test* "port-seek (SEEK_CUR)" 8 (port-seek p 4 SEEK_CUR))
440 (test* "write after seek" '#u8(0 0 0 #xff 0 0 0 0 #x77 0 0 0 0 0 0 0 )
441 (begin (write-byte #x77 p) (flush p) (u8vector-copy v)))
442 (test* "port-seek (SEEK_END)" 15 (port-seek p -1 SEEK_END))
443 (test* "write after seek" '#u8(0 0 0 #xff 0 0 0 0 #x77 0 0 0 0 0 0 #x11)
444 (begin (write-byte #x11 p) (flush p) (u8vector-copy v)))
445 ))
446
447 (let ()
448 (define (tester size)
449 ;; We disable port-buffering so that we can test gradually extending
450 ;; the backing storage.
451 (test* #"extendable u8 size=~size"
452 (list (make-u8vector size 255) (make-u8vector size 255))
453 (let1 p (open-output-uvector '#u8() :extendable #t)
454 (set! (port-buffering p) :none)
455 (dotimes [size] (write-byte 255 p))
456 (list (get-output-uvector p)
457 (get-output-uvector p :shared #t))))
458 (test* #"extendable s16 size=~size" (make-s16vector size #x1212)
459 (let1 p (open-output-uvector (make-s16vector 10 -1) :extendable #t)
460 (set! (port-buffering p) :none)
461 (dotimes [size] (write-byte #x12 p) (write-byte #x12 p))
462 (get-output-uvector p))))
463
464 (tester 0)
465 (tester 16)
466 (tester 17)
467 (tester 255)
468 (tester 256)
469 (tester 257))
470
471 (test* "unaligned extendable output" '#s32(#x01010101)
472 (let1 p (open-output-uvector '#s32() :extendable #t)
473 (dotimes [7] (write-byte 1 p))
474 (get-output-uvector p)))
475
476 (test* "seek with extendable output" '#u8(1 0 0 0 3 0 0 2 0 4 0 0 5)
477 (let1 p (open-output-uvector)
478 (set! (port-buffering p) :none)
479 (write-byte 1 p)
480 (port-seek p 7 SEEK_SET)
481 (write-byte 2 p)
482 (port-seek p -4 SEEK_END)
483 (write-byte 3 p)
484 (port-seek p 4 SEEK_CUR)
485 (write-byte 4 p)
486 (port-seek p 2 SEEK_END)
487 (write-byte 5 p)
488 (get-output-uvector p)))
489
490 ;;-----------------------------------------------------------
491 (test-section "input-limited-length-port")
492
493 (let ()
494 (define (tester size limit)
495 (test* #"size=~size limit=~limit" #t
496 (let* ((source (string-tabulate
497 (^i (integer->char (modulo i 128)))
498 size))
499 (expected (if (<= limit size)
500 (string-take source limit)
501 source))
502 (sp (open-input-string source))
503 (p (open-input-limited-length-port sp limit))
504 (result (port->string p)))
505 (equal? expected result))))
506
507 (tester 10 0)
508 (tester 10 1)
509 (tester 10 5)
510 (tester 10 9)
511 (tester 10 10)
512 (tester 10 15)
513
514 (tester 20000 19999)
515 (tester 20000 20000)
516 (tester 20000 20001)
517
518 (tester 0 0)
519 (tester 0 1)
520 (tester 0 10)
521 )
522
523 ;;-----------------------------------------------------------
524 (test-section "input-list-port")
525
526 ;; A common routine for input list port and input generator port
527 ;; make-char-input takes a string and returns a port
528 ;; make-byte-input takes start and end bytes and returns a port
529 (define (test-input-seq-ports name
530 make-char-input
531 make-byte-input
532 get-remaining)
533 (define (char-port) (make-char-input "Aloha, honua."))
534 (define (byte-port) (make-byte-input 10 48))
535
536 (test* #"char-~name, all" "Aloha, honua."
537 (call-with-output-string
538 (cut copy-port (char-port) <>)))
539 (test* #"char-~name, remaining" "honua."
540 (let1 p (char-port)
541 (dotimes [n 7] (read-char p))
542 (list->string (get-remaining p))))
543 (test* #"char-~name, remaining, with push-back" "honua."
544 (let1 p (char-port)
545 (dotimes [n 7] (read-char p))
546 (peek-char p)
547 (list->string (get-remaining p))))
548
549 (test* #"byte-~name, all" "0123456789"
550 (let1 p (byte-port)
551 (with-output-to-string
552 (cut generator-for-each (^b (write-char (integer->char b)))
553 (cut read-byte p)))))
554 (test* #"byte-~name, remaining" '(53 54 55 56 57)
555 (let1 p (byte-port)
556 (dotimes [n 5] (read-byte p))
557 (get-remaining p)))
558 (test* #"byte-~name push-back, remaining" '(53 54 55 56 57)
559 (let1 p (byte-port)
560 (dotimes [n 5] (read-byte p))
561 (peek-byte p)
562 (get-remaining p)))
563 (test* #"byte-~name push-back as char, remaining" '(53 54 55 56 57)
564 (let1 p (byte-port)
565 (dotimes [n 5] (read-byte p))
566 (peek-char p)
567 (get-remaining p)))
568 )
569
570 (test-input-seq-ports "list"
571 (^s (open-input-char-list (string->list s)))
572 (^[s e] (open-input-byte-list (iota s e)))
573 get-remaining-input-list)
574
575 ;;-----------------------------------------------------------
576 (test-section "input-generator-port")
577
578 (test-input-seq-ports "generator"
579 (^s (open-input-char-generator (string->generator s)))
580 (^[s e] (open-input-byte-generator (giota s e)))
581 (^p (generator->list
582 (get-remaining-input-generator p))))
583
584 ;;-----------------------------------------------------------
585 (test-section "output-accumulator-port")
586
587 (let ([data '()]
588 [finished #f])
589 (define (acc x)
590 (if (eof-object? x)
591 (begin (set! finished (reverse-list->string data))
592 (set! data '()))
593 (push! data x)))
594
595 (define accout (open-output-char-accumulator acc))
596
597 (test* "output char accumulator" "abcdef"
598 (begin
599 (write-char #\a accout)
600 (write 'bcd accout)
601 (display "ef" accout)
602 (close-output-port accout)
603 finished)))
604
605 ;;-----------------------------------------------------------
606 (test-section "SRFI-181 and 192")
607
608 (define-module srfi-181-tests
609 (use gauche.test)
610 (use gauche.uvector)
611 (use scheme.list)
612 (use srfi.13)
613 (use srfi.64)
614 (use srfi.181)
615 (use srfi.192)
616 (use scheme.base :only (bytevector
617 bytevector-copy
618 bytevector-u8-ref
619 flush-output-port))
620
621 (test-module 'srfi.181)
622 (test-module 'srfi.192)
623 (test-include-r7 "../../test/include/srfi-181-192-tests.scm")
624 )
625
626 ;; Auxiliary SRFI-181 tests
627
628 ;; i/o-invalid-position-error
629 (define-module srfi-181-tests-2
630 (use gauche.test)
631 (use srfi.181)
632 (use srfi.192)
633 (define p (make-custom-binary-input-port
634 'invalid-position-error-test
635 (^ _ 1)
636 (^[] 0)
637 (^[pos] (raise (make-i/o-invalid-position-error pos)))
638 #f))
639 (test* "invalid-position-error"
640 "Invalid position object: foo"
641 (guard (e [else
642 (and (condition-has-type? e <io-invalid-position-error>)
643 (eq? (condition-ref e 'port) p)
644 (~ e'message))])
645 (set-port-position! p 'foo)))
646 )
647
648 (define-module srfi-181-transcoder
649 (use gauche.test)
650 (use gauche.vport)
651 (use gauche.charconv) ; chaconv is tested before vport
652 (use gauche.uvector) ; uvector is tested before vport
653 (use srfi.181)
654
655 (define (import-test codec external-data internal-string handling)
656 (let1 native-codec (~ (native-transcoder)'codec)
657 (test* `(bytevector->string (,(~ codec'name) -> ,(~ native-codec'name)))
658 internal-string
659 (bytevector->string external-data
660 (make-transcoder codec
661 (native-eol-style)
662 handling)))
663 (test* `(port (,(~ codec'name) -> ,(~ native-codec'name)))
664 internal-string
665 (let1 p
666 (transcoded-port (open-input-bytevector external-data)
667 (make-transcoder codec
668 (native-eol-style)
669 handling))
670 (begin0 (port->string p)
671 (close-port p))))))
672 (define (export-test codec internal-string external-data handling)
673 (let1 native-codec (~ (native-transcoder)'codec)
674 (test* `(string->bytevector (,(~ native-codec'name) -> ,(~ codec'name)))
675 external-data
676 (string->bytevector internal-string
677 (make-transcoder codec
678 (native-eol-style)
679 handling)))
680 (test* `(port (,(~ native-codec'name) -> ,(~ codec'name)))
681 external-data
682 (let* ([buf (open-output-bytevector)]
683 [p (transcoded-port buf
684 (make-transcoder codec
685 (native-eol-style)
686 handling))])
687 (display internal-string p)
688 (flush p)
689 (begin0 (get-output-bytevector buf)
690 (close-port p))))))
691
692 (define (roundtrip-test codec external-data internal-string handling)
693 (import-test codec external-data internal-string handling)
694 (export-test codec internal-string external-data handling))
695
696 (roundtrip-test (make-codec 'latin1)
697 '#u8(#x41 #xc2 #x42)
698 "A\u00c2B" 'replace)
699 (roundtrip-test (make-codec 'utf-16be)
700 '#u8(#x00 #x41 #x00 #xc2 #x00 #x42)
701 "A\u00c2B" 'replace)
702 (roundtrip-test (make-codec 'utf-16le)
703 '#u8(#x41 #x00 #xc2 #x00 #x42 #x00)
704 "A\u00c2B" 'replace)
705 (import-test (make-codec 'utf-16)
706 '#u8(#xfe #xff #x00 #x41 #x00 #xc2 #x00 #x42)
707 "A\u00c2B" 'replace)
708 (import-test (make-codec 'utf-16)
709 '#u8(#xff #xfe #x41 #x00 #xc2 #x00 #x42 #x00)
710 "A\u00c2B" 'replace)
711 (export-test (make-codec 'utf-16)
712 "A\u00c2B"
713 (test-one-of
714 '#u8(#xff #xfe #x41 #x00 #xc2 #x00 #x42 #x00)
715 '#u8(#xfe #xff #x00 #x41 #x00 #xc2 #x00 #x42))
716 'replace)
717
718 (import-test (make-codec 'utf-16be)
719 '#u8(#xdc #x00)
720 (test-error <io-decoding-error>) 'raise)
721 (export-test (make-codec 'ascii)
722 "A\u3000"
723 (test-error <io-encoding-error>) 'raise)
724 )
725
726 ;;-----------------------------------------------------------
727 (test-section "Base64 with uvector")
728
729 (use rfc.base64)
730
731 (test* "base64 encode from uvector"
732 '("" "AA==" "//79/Pv6")
733 (list (base64-encode-bytevector '#u8())
734 (base64-encode-bytevector '#u8(0))
735 (base64-encode-bytevector '#u8(255 254 253 252 251 250))))
736
737 (test* "base64 decode to uvector"
738 '(#u8() #u8(0) #u8(255 254 253 252 251 250))
739 (list (base64-decode-bytevector "")
740 (base64-decode-bytevector "AA==")
741 (base64-decode-bytevector "//79/Pv6")))
742
743 (test* "base64 encode from uvector (digits)"
744 "__79+_z6"
745 (base64-encode-bytevector '#u8(255 254 253 251 252 250)
746 :digits "+_"))
747
748 (test* "base64 decode from uvector (digits)"
749 '#u8(255 254 253 251 252 250)
750 (base64-decode-bytevector "__79+_z6"
751 :digits #(#\+ #\_)))
752
753
754 (test-end)