| 1 |
|
| 2 |
(library (tests io) |
| 3 |
(export run-tests) |
| 4 |
(import (ikarus)(tests framework)) |
| 5 |
|
| 6 |
|
| 7 |
(define-syntax test |
| 8 |
(syntax-rules () |
| 9 |
[(_ name body) |
| 10 |
(begin |
| 11 |
(printf "running ~s ... " 'name) |
| 12 |
body |
| 13 |
(printf "ok\n"))])) |
| 14 |
|
| 15 |
(define (make-n-byte-custom-binary-input-port n) |
| 16 |
(assert (<= 0 n 256)) |
| 17 |
(make-custom-binary-input-port "test0" |
| 18 |
(let ([c 0]) |
| 19 |
(lambda (bv i count) |
| 20 |
(if (< c n) |
| 21 |
(begin |
| 22 |
(bytevector-u8-set! bv i c) |
| 23 |
(set! c (+ c 1)) |
| 24 |
1) |
| 25 |
0))) |
| 26 |
#f #f #f)) |
| 27 |
|
| 28 |
(define (make-n-byte-bytevector-binary-input-port n) |
| 29 |
(assert (<= 0 n 256)) |
| 30 |
(let ([bv (make-bytevector n)]) |
| 31 |
(let f ([i 0]) |
| 32 |
(unless (= i n) |
| 33 |
(bytevector-u8-set! bv i i) |
| 34 |
(f (+ i 1)))) |
| 35 |
(open-bytevector-input-port bv))) |
| 36 |
|
| 37 |
(define (make-ascii-range-bytevector) |
| 38 |
(let ([bv (make-bytevector 128)]) |
| 39 |
(let f ([i 0]) |
| 40 |
(unless (= i 128) |
| 41 |
(bytevector-u8-set! bv i i) |
| 42 |
(f (+ i 1)))) |
| 43 |
bv)) |
| 44 |
|
| 45 |
(define (make-ascii-range-bytevector+utf8-bom) |
| 46 |
(let ([bv (make-bytevector (+ 128 3))]) |
| 47 |
(bytevector-u8-set! bv 0 #xEF) |
| 48 |
(bytevector-u8-set! bv 1 #xBB) |
| 49 |
(bytevector-u8-set! bv 2 #xBF) |
| 50 |
(let f ([i 0]) |
| 51 |
(unless (= i 128) |
| 52 |
(bytevector-u8-set! bv (+ i 3) i) |
| 53 |
(f (+ i 1)))) |
| 54 |
bv)) |
| 55 |
|
| 56 |
(define (test-get-u8-1 p n) |
| 57 |
(let f ([i 0]) |
| 58 |
(let ([x (get-u8 p)]) |
| 59 |
(cond |
| 60 |
[(eof-object? x) |
| 61 |
(unless (= i n) |
| 62 |
(error 'test0 "premature termination" i))] |
| 63 |
[(= x i) (f (+ i 1))] |
| 64 |
[else |
| 65 |
(error 'test0 "incorrect value returned" x)])))) |
| 66 |
|
| 67 |
(define (test-get-char-1 p n) |
| 68 |
(let f ([i 0]) |
| 69 |
(let ([x (get-char p)]) |
| 70 |
(cond |
| 71 |
[(eof-object? x) |
| 72 |
(unless (= i n) |
| 73 |
(error 'test0 "premature termination" i))] |
| 74 |
[(= (char->integer x) i) (f (+ i 1))] |
| 75 |
[else |
| 76 |
(error 'test0 "incorrect value returned" x)])))) |
| 77 |
|
| 78 |
|
| 79 |
(define (test-peek-u8-1 p n) |
| 80 |
(let f ([i 0]) |
| 81 |
(let* ([px (lookahead-u8 p)] |
| 82 |
[x (get-u8 p)]) |
| 83 |
(cond |
| 84 |
[(not (eqv? px x)) (error #f "peek invalid" px x)] |
| 85 |
[(eof-object? x) |
| 86 |
(unless (= i n) |
| 87 |
(error #f "premature termination" i))] |
| 88 |
[(= x i) (f (+ i 1))] |
| 89 |
[else |
| 90 |
(error #f "incorrect value returned" x i)])))) |
| 91 |
|
| 92 |
(define (test-peek-char-1 p n) |
| 93 |
(let f ([i 0]) |
| 94 |
(let* ([px (lookahead-char p)] |
| 95 |
[x (get-char p)]) |
| 96 |
(cond |
| 97 |
[(not (eqv? px x)) (error #f "peek invalid" px x)] |
| 98 |
[(eof-object? x) |
| 99 |
(unless (= i n) |
| 100 |
(error #f "premature termination" i))] |
| 101 |
[(= (char->integer x) i) (f (+ i 1))] |
| 102 |
[else |
| 103 |
(error #f "incorrect value returned" x i)])))) |
| 104 |
|
| 105 |
(define (test-binary-port-eof?-1 p n) |
| 106 |
(let f ([i 0]) |
| 107 |
(cond |
| 108 |
[(port-eof? p) |
| 109 |
(unless (= i n) |
| 110 |
(error #f "premature termination" i)) |
| 111 |
(assert (eof-object? (lookahead-u8 p))) |
| 112 |
(assert (eof-object? (get-u8 p)))] |
| 113 |
[(= (get-u8 p) i) (f (+ i 1))] |
| 114 |
[else |
| 115 |
(error #f "incorrect value returned" i)]))) |
| 116 |
|
| 117 |
(define (test-textual-port-eof?-1 p n) |
| 118 |
(let f ([i 0]) |
| 119 |
(cond |
| 120 |
[(port-eof? p) |
| 121 |
(unless (= i n) |
| 122 |
(error #f "premature termination" i)) |
| 123 |
(assert (eof-object? (lookahead-char p))) |
| 124 |
(assert (eof-object? (get-char p)))] |
| 125 |
[(= (char->integer (get-char p)) i) (f (+ i 1))] |
| 126 |
[else |
| 127 |
(error #f "incorrect value returned" i)]))) |
| 128 |
|
| 129 |
(define (test-custom-binary-input-ports) |
| 130 |
(test "reading 256 bytes in ascending order" |
| 131 |
(test-get-u8-1 (make-n-byte-custom-binary-input-port 256) 256)) |
| 132 |
|
| 133 |
(test "reading 256 bytes in ascending order 2 at a time" |
| 134 |
(test-get-u8-1 |
| 135 |
(make-custom-binary-input-port "test0" |
| 136 |
(let ([c 0]) |
| 137 |
(lambda (bv i count) |
| 138 |
(if (< c 256) |
| 139 |
(begin |
| 140 |
(assert (>= count 2)) |
| 141 |
(bytevector-u8-set! bv i c) |
| 142 |
(bytevector-u8-set! bv (+ i 1) (+ c 1)) |
| 143 |
(set! c (+ c 2)) |
| 144 |
2) |
| 145 |
0))) |
| 146 |
#f #f #f) |
| 147 |
256)) |
| 148 |
|
| 149 |
(test "peeking 256 bytes in ascending order" |
| 150 |
(test-peek-u8-1 (make-n-byte-custom-binary-input-port 256) 256)) |
| 151 |
|
| 152 |
(test "custom-binary-port port-eof?" |
| 153 |
(test-binary-port-eof?-1 (make-n-byte-custom-binary-input-port 256) 256)) |
| 154 |
|
| 155 |
;;; |
| 156 |
(test "reading 256 bytes from bytevector-input-port" |
| 157 |
(test-get-u8-1 (make-n-byte-bytevector-binary-input-port 256) 256)) |
| 158 |
|
| 159 |
(test "peeking 256 bytes from bytevector-input-port" |
| 160 |
(test-peek-u8-1 (make-n-byte-bytevector-binary-input-port 256) 256)) |
| 161 |
|
| 162 |
(test "bytevector-binary-port port-eof?" |
| 163 |
(test-binary-port-eof?-1 (make-n-byte-bytevector-binary-input-port 256) 256)) |
| 164 |
|
| 165 |
;;; |
| 166 |
|
| 167 |
(test "reading 256 latin1 chars from bytevector-input-port" |
| 168 |
(test-get-char-1 |
| 169 |
(transcoded-port (make-n-byte-bytevector-binary-input-port 256) |
| 170 |
(make-transcoder (latin-1-codec) 'none 'raise)) |
| 171 |
256)) |
| 172 |
|
| 173 |
(test "peeking 256 bytes from latin1 transcoded port" |
| 174 |
(test-peek-char-1 |
| 175 |
(transcoded-port (make-n-byte-bytevector-binary-input-port 256) |
| 176 |
(make-transcoder (latin-1-codec) 'none 'raise)) |
| 177 |
256)) |
| 178 |
|
| 179 |
(test "latin1 transcoded port port-eof?" |
| 180 |
(test-textual-port-eof?-1 |
| 181 |
(transcoded-port (make-n-byte-bytevector-binary-input-port 256) |
| 182 |
(make-transcoder (latin-1-codec) 'none 'raise)) |
| 183 |
256)) |
| 184 |
|
| 185 |
;;; |
| 186 |
|
| 187 |
(test "reading 128 utf8 chars from bytevector-input-port" |
| 188 |
(test-get-char-1 |
| 189 |
(open-bytevector-input-port (make-ascii-range-bytevector) |
| 190 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 191 |
128)) |
| 192 |
|
| 193 |
(test "peeking 128 chars from utf8 port" |
| 194 |
(test-peek-char-1 |
| 195 |
(open-bytevector-input-port (make-ascii-range-bytevector) |
| 196 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 197 |
128)) |
| 198 |
|
| 199 |
(test "utf8 transcoded port port-eof?" |
| 200 |
(test-textual-port-eof?-1 |
| 201 |
(open-bytevector-input-port (make-ascii-range-bytevector) |
| 202 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 203 |
128))) |
| 204 |
|
| 205 |
(define (make-utf8-bytevector-range1) |
| 206 |
(u8-list->bytevector |
| 207 |
(let f ([i 0] [j #x7F]) |
| 208 |
(cond |
| 209 |
[(> i j) '()] |
| 210 |
[else |
| 211 |
(cons* i (f (+ i 1) j))])))) |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
(define (make-utf8-bytevector-range2) |
| 216 |
(u8-list->bytevector |
| 217 |
(let f ([i #x80] [j #x7FF]) |
| 218 |
(cond |
| 219 |
[(> i j) '()] |
| 220 |
[else |
| 221 |
(cons* (fxior #b11000000 (fxsra i 6)) |
| 222 |
(fxior #b10000000 (fxand i #b111111)) |
| 223 |
(f (+ i 1) j))])))) |
| 224 |
|
| 225 |
(define (make-utf8-bytevector-range3) |
| 226 |
(u8-list->bytevector |
| 227 |
(let f ([i #x800] [j #xFFFF]) |
| 228 |
(cond |
| 229 |
[(> i j) '()] |
| 230 |
[(fx= i #xD800) (f #xE000 j)] |
| 231 |
[else |
| 232 |
(cons* (fxior #b11100000 (fxsra i 12)) |
| 233 |
(fxior #b10000000 (fxand (fxsra i 6) #b111111)) |
| 234 |
(fxior #b10000000 (fxand i #b111111)) |
| 235 |
(f (+ i 1) j))])))) |
| 236 |
|
| 237 |
(define (make-utf8-bytevector-range4) |
| 238 |
(u8-list->bytevector |
| 239 |
(let f ([i #x10000] [j #x10FFFF]) |
| 240 |
(cond |
| 241 |
[(> i j) '()] |
| 242 |
[else |
| 243 |
(cons* (fxior #b11110000 (fxsra i 18)) |
| 244 |
(fxior #b10000000 (fxand (fxsra i 12) #b111111)) |
| 245 |
(fxior #b10000000 (fxand (fxsra i 6) #b111111)) |
| 246 |
(fxior #b10000000 (fxand i #b111111)) |
| 247 |
(f (+ i 1) j))])))) |
| 248 |
|
| 249 |
(define (make-utf8-string-range1) |
| 250 |
(list->string |
| 251 |
(let f ([i 0] [j #x7F]) |
| 252 |
(cond |
| 253 |
[(> i j) '()] |
| 254 |
[else |
| 255 |
(cons (integer->char i) |
| 256 |
(f (+ i 1) j))])))) |
| 257 |
|
| 258 |
(define (make-utf8-string-range2) |
| 259 |
(list->string |
| 260 |
(let f ([i #x80] [j #x7FF]) |
| 261 |
(cond |
| 262 |
[(> i j) '()] |
| 263 |
[else |
| 264 |
(cons (integer->char i) |
| 265 |
(f (+ i 1) j))])))) |
| 266 |
|
| 267 |
(define (make-utf8-string-range3) |
| 268 |
(list->string |
| 269 |
(let f ([i #x800] [j #xFFFF]) |
| 270 |
(cond |
| 271 |
[(> i j) '()] |
| 272 |
[(fx= i #xD800) (f #xE000 j)] |
| 273 |
[else |
| 274 |
(cons (integer->char i) |
| 275 |
(f (+ i 1) j))])))) |
| 276 |
|
| 277 |
(define (make-utf8-string-range4) |
| 278 |
(list->string |
| 279 |
(let f ([i #x10000] [j #x10FFFF]) |
| 280 |
(cond |
| 281 |
[(> i j) '()] |
| 282 |
[else |
| 283 |
(cons (integer->char i) |
| 284 |
(f (+ i 1) j))])))) |
| 285 |
|
| 286 |
(define (test-port-string-output p str) |
| 287 |
(let f ([i 0]) |
| 288 |
(let ([x (get-char p)]) |
| 289 |
(cond |
| 290 |
[(eof-object? x) |
| 291 |
(unless (= i (string-length str)) |
| 292 |
(error #f "premature eof"))] |
| 293 |
[(= i (string-length str)) |
| 294 |
(error #f "too many chars")] |
| 295 |
[(char=? x (string-ref str i)) |
| 296 |
(f (+ i 1))] |
| 297 |
[else |
| 298 |
(error #f |
| 299 |
(format |
| 300 |
"mismatch at index ~a, got char ~a (code #x~x), \ |
| 301 |
expected char ~a (code #x~x)" |
| 302 |
i |
| 303 |
x |
| 304 |
(char->integer x) |
| 305 |
(string-ref str i) |
| 306 |
(char->integer (string-ref str i))))])))) |
| 307 |
|
| 308 |
|
| 309 |
(define (test-port-string-peeking-output p str) |
| 310 |
(let f ([i 0]) |
| 311 |
(let ([x (lookahead-char p)]) |
| 312 |
(cond |
| 313 |
[(eof-object? x) |
| 314 |
(unless (= i (string-length str)) |
| 315 |
(error #f "premature eof"))] |
| 316 |
[(= i (string-length str)) |
| 317 |
(error #f "too many chars")] |
| 318 |
[(not (char=? x (get-char p))) |
| 319 |
(error #f "peek not same as get")] |
| 320 |
[(char=? x (string-ref str i)) |
| 321 |
(f (+ i 1))] |
| 322 |
[else |
| 323 |
(error #f "mismatch" x (string-ref str i) i)])))) |
| 324 |
|
| 325 |
|
| 326 |
(define (invalid-code? n) (not (valid-code? n))) |
| 327 |
(define (valid-code? n) |
| 328 |
(cond |
| 329 |
[(< n 0) #f] |
| 330 |
[(<= n #xD7FF) #t] |
| 331 |
[(< n #xE000) #f] |
| 332 |
[(<= n #x10FFFF) #t] |
| 333 |
[else (error 'valid-code? "out of range" n)])) |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
(define (make-u16le-bv min max) |
| 338 |
(u8-list->bytevector |
| 339 |
(let f ([i min]) |
| 340 |
(cond |
| 341 |
[(> i max) '()] |
| 342 |
[(invalid-code? i) (f (+ i 1))] |
| 343 |
[(< i #x10000) |
| 344 |
(cons* |
| 345 |
(fxand i #xFF) |
| 346 |
(fxsra i 8) |
| 347 |
(f (+ i 1)))] |
| 348 |
[else |
| 349 |
(let ([ii (fx- i #x10000)]) |
| 350 |
(let ([w1 (fxior #xD800 (fxand #x3FF (fxsra ii 10)))] |
| 351 |
[w2 (fxior #xDC00 (fxand #x3FF ii))]) |
| 352 |
(cons* |
| 353 |
(fxand w1 #xFF) |
| 354 |
(fxsra w1 8) |
| 355 |
(fxand w2 #xFF) |
| 356 |
(fxsra w2 8) |
| 357 |
(f (+ i 1)))))])))) |
| 358 |
|
| 359 |
(define (make-string-slice min max) |
| 360 |
(list->string |
| 361 |
(let f ([i min]) |
| 362 |
(cond |
| 363 |
[(> i max) '()] |
| 364 |
[(invalid-code? i) (f (+ i 1))] |
| 365 |
[else (cons (integer->char i) (f (+ i 1)))])))) |
| 366 |
|
| 367 |
|
| 368 |
(define (make-u16le-range1) |
| 369 |
(make-u16le-bv 0 #x7FFF)) |
| 370 |
(define (make-u16le-range2) |
| 371 |
(make-u16le-bv #x8000 #x10FFFF)) |
| 372 |
(define (make-utf16-string-range1) |
| 373 |
(make-string-slice 0 #x7FFF)) |
| 374 |
(define (make-utf16-string-range2) |
| 375 |
(make-string-slice #x8000 #x10FFFF)) |
| 376 |
|
| 377 |
(define (run-exhaustive-tests) |
| 378 |
|
| 379 |
(test "utf8 range 1" |
| 380 |
(test-port-string-output |
| 381 |
(open-bytevector-input-port (make-utf8-bytevector-range1) |
| 382 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 383 |
(make-utf8-string-range1))) |
| 384 |
|
| 385 |
(test "utf8 range 2" |
| 386 |
(test-port-string-output |
| 387 |
(open-bytevector-input-port (make-utf8-bytevector-range2) |
| 388 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 389 |
(make-utf8-string-range2))) |
| 390 |
|
| 391 |
(test "utf8 range 3" |
| 392 |
(test-port-string-output |
| 393 |
(open-bytevector-input-port (make-utf8-bytevector-range3) |
| 394 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 395 |
(make-utf8-string-range3))) |
| 396 |
|
| 397 |
(test "utf8 range 4" |
| 398 |
(test-port-string-output |
| 399 |
(open-bytevector-input-port (make-utf8-bytevector-range4) |
| 400 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 401 |
(make-utf8-string-range4))) |
| 402 |
|
| 403 |
|
| 404 |
(test "utf16 range 1" |
| 405 |
(test-port-string-output |
| 406 |
(open-bytevector-input-port (make-u16le-range1) |
| 407 |
(make-transcoder (utf-16-codec) 'none 'raise)) |
| 408 |
(make-utf16-string-range1))) |
| 409 |
|
| 410 |
|
| 411 |
(test "utf16 range 2" |
| 412 |
(test-port-string-output |
| 413 |
(open-bytevector-input-port (make-u16le-range2) |
| 414 |
(make-transcoder (utf-16-codec) 'none 'raise)) |
| 415 |
(make-utf16-string-range2))) |
| 416 |
|
| 417 |
|
| 418 |
(test "utf8 peek range 1" |
| 419 |
(test-port-string-peeking-output |
| 420 |
(open-bytevector-input-port (make-utf8-bytevector-range1) |
| 421 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 422 |
(make-utf8-string-range1))) |
| 423 |
|
| 424 |
(test "utf8 peek range 2" |
| 425 |
(test-port-string-peeking-output |
| 426 |
(open-bytevector-input-port (make-utf8-bytevector-range2) |
| 427 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 428 |
(make-utf8-string-range2))) |
| 429 |
|
| 430 |
(test "utf8 peek range 3" |
| 431 |
(test-port-string-peeking-output |
| 432 |
(open-bytevector-input-port (make-utf8-bytevector-range3) |
| 433 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 434 |
(make-utf8-string-range3))) |
| 435 |
|
| 436 |
(test "utf8 peek range 4" |
| 437 |
(test-port-string-peeking-output |
| 438 |
(open-bytevector-input-port (make-utf8-bytevector-range4) |
| 439 |
(make-transcoder (utf-8-codec) 'none 'raise)) |
| 440 |
(make-utf8-string-range4))) |
| 441 |
|
| 442 |
(test "utf16 peek range 1" |
| 443 |
(test-port-string-peeking-output |
| 444 |
(open-bytevector-input-port (make-u16le-range1) |
| 445 |
(make-transcoder (utf-16-codec) 'none 'raise)) |
| 446 |
(make-utf16-string-range1))) |
| 447 |
|
| 448 |
(test "utf16 peek range 2" |
| 449 |
(test-port-string-peeking-output |
| 450 |
(open-bytevector-input-port (make-u16le-range2) |
| 451 |
(make-transcoder (utf-16-codec) 'none 'raise)) |
| 452 |
(make-utf16-string-range2))) |
| 453 |
|
| 454 |
(test "utf8 range 1 string" |
| 455 |
(test-port-string-output |
| 456 |
(open-string-input-port (make-utf8-string-range1)) |
| 457 |
(make-utf8-string-range1))) |
| 458 |
|
| 459 |
(test "utf8 range 2 string" |
| 460 |
(test-port-string-output |
| 461 |
(open-string-input-port (make-utf8-string-range2)) |
| 462 |
(make-utf8-string-range2))) |
| 463 |
|
| 464 |
(test "utf8 range 3 string" |
| 465 |
(test-port-string-output |
| 466 |
(open-string-input-port (make-utf8-string-range3)) |
| 467 |
(make-utf8-string-range3))) |
| 468 |
|
| 469 |
(test "utf8 range 4 string" |
| 470 |
(test-port-string-output |
| 471 |
(open-string-input-port (make-utf8-string-range4)) |
| 472 |
(make-utf8-string-range4))) |
| 473 |
|
| 474 |
(test "utf8 peek range 2 string" |
| 475 |
(test-port-string-peeking-output |
| 476 |
(open-string-input-port (make-utf8-string-range2)) |
| 477 |
(make-utf8-string-range2))) |
| 478 |
|
| 479 |
(test "utf8 peek range 3 string" |
| 480 |
(test-port-string-peeking-output |
| 481 |
(open-string-input-port (make-utf8-string-range3)) |
| 482 |
(make-utf8-string-range3))) |
| 483 |
|
| 484 |
(test "utf8 peek range 4 string" |
| 485 |
(test-port-string-peeking-output |
| 486 |
(open-string-input-port (make-utf8-string-range4)) |
| 487 |
(make-utf8-string-range4)))) |
| 488 |
|
| 489 |
|
| 490 |
(define (run-interactive-tests) |
| 491 |
(display "now write something on the keyboard ...\n") |
| 492 |
(printf "you typed ~s\n" |
| 493 |
(list->string |
| 494 |
(let ([p (standard-input-port)]) |
| 495 |
(let f () |
| 496 |
(let ([x (get-u8 p)]) |
| 497 |
(if (eof-object? x) |
| 498 |
'() |
| 499 |
(cons (integer->char x) (f)))))))) |
| 500 |
|
| 501 |
(display "let's do it again ...\n") |
| 502 |
(printf "you typed ~s\n" |
| 503 |
(list->string |
| 504 |
(let ([p (transcoded-port (standard-input-port) |
| 505 |
(make-transcoder (utf-8-codec)))]) |
| 506 |
(let f () |
| 507 |
(let ([x (get-char p)]) |
| 508 |
(if (eof-object? x) |
| 509 |
'() |
| 510 |
(cons x (f))))))))) |
| 511 |
|
| 512 |
(define (file-size-char-by-char filename) |
| 513 |
(with-input-from-file filename |
| 514 |
(lambda () |
| 515 |
(let f ([i 0]) |
| 516 |
(let ([x (get-char (current-input-port))]) |
| 517 |
(if (eof-object? x) |
| 518 |
i |
| 519 |
(f (+ i 1)))))))) |
| 520 |
|
| 521 |
|
| 522 |
(define (file->bytevector filename) |
| 523 |
(let ([p (open-file-input-port filename (file-options) 'block #f)]) |
| 524 |
(u8-list->bytevector |
| 525 |
(let f () |
| 526 |
(let ([x (get-u8 p)]) |
| 527 |
(if (eof-object? x) |
| 528 |
(begin (close-input-port p) '()) |
| 529 |
(cons x (f)))))))) |
| 530 |
|
| 531 |
(define (bytevector->binary-port bv p) |
| 532 |
(let f ([i 0]) |
| 533 |
(unless (fx= i (bytevector-length bv)) |
| 534 |
(put-u8 p (bytevector-u8-ref bv i)) |
| 535 |
(f (fx+ i 1))))) |
| 536 |
|
| 537 |
(define (bytevector->textual-port bv p) |
| 538 |
(let f ([i 0]) |
| 539 |
(unless (fx= i (bytevector-length bv)) |
| 540 |
(put-char p (integer->char (bytevector-u8-ref bv i))) |
| 541 |
(f (fx+ i 1))))) |
| 542 |
|
| 543 |
(define (test-input-files) |
| 544 |
(assert (= (file-size-char-by-char (src-file "tests/SRFI-1.ss")) 56573)) |
| 545 |
(assert (= (file-size (src-file "tests/SRFI-1.ss")) 56573)) |
| 546 |
(let ([bv (file->bytevector (src-file "tests/SRFI-1.ss"))]) |
| 547 |
(let-values ([(p extract) (open-bytevector-output-port #f)]) |
| 548 |
(bytevector->binary-port bv p) |
| 549 |
(let ([bv2 (extract)]) |
| 550 |
(assert (bytevector=? bv bv2)) |
| 551 |
(assert (bytevector=? #vu8() (extract)))))) |
| 552 |
|
| 553 |
(let ([bv (file->bytevector (src-file "tests/SRFI-1.ss"))]) |
| 554 |
(let-values ([(p extract) (open-bytevector-output-port |
| 555 |
(native-transcoder))]) |
| 556 |
(bytevector->textual-port bv p) |
| 557 |
(let ([bv2 (extract)]) |
| 558 |
(assert (bytevector=? bv bv2)) |
| 559 |
(assert (bytevector=? #vu8() (extract)))))) |
| 560 |
|
| 561 |
(let ([bv (file->bytevector (src-file "tests/SRFI-1.ss"))]) |
| 562 |
(let-values ([(p extract) (open-bytevector-output-port |
| 563 |
(make-transcoder (latin-1-codec)))]) |
| 564 |
(bytevector->textual-port bv p) |
| 565 |
(let ([bv2 (extract)]) |
| 566 |
(assert (bytevector=? bv bv2)) |
| 567 |
(assert (bytevector=? #vu8() (extract)))))) |
| 568 |
|
| 569 |
(let ([bv (file->bytevector (src-file "tests/SRFI-1.ss"))]) |
| 570 |
(let-values ([(p extract) (open-string-output-port)]) |
| 571 |
(bytevector->textual-port bv p) |
| 572 |
(let ([str (extract)]) |
| 573 |
(assert (bytevector=? bv (string->utf8 str))) |
| 574 |
(assert (string=? "" (extract)))))) |
| 575 |
|
| 576 |
(let ([p (standard-output-port)]) |
| 577 |
(bytevector->binary-port |
| 578 |
(string->utf8 "HELLO THERE\n") |
| 579 |
p) |
| 580 |
(flush-output-port p)) |
| 581 |
|
| 582 |
(let ([p (current-output-port)]) |
| 583 |
(bytevector->textual-port |
| 584 |
(string->utf8 "HELLO THERE\n") |
| 585 |
p) |
| 586 |
(flush-output-port p)) |
| 587 |
|
| 588 |
(let ([p (current-output-port)]) |
| 589 |
(put-string p "HELLO THERE\n") |
| 590 |
(flush-output-port p))) |
| 591 |
|
| 592 |
(define (test-custom-binary-output-ports) |
| 593 |
(define ls '()) |
| 594 |
(let ([p (make-custom-binary-output-port "foo" |
| 595 |
(lambda (bv i c) |
| 596 |
(let f ([i i] [c c]) |
| 597 |
(unless (fx= c 0) |
| 598 |
(set! ls (cons (bytevector-u8-ref bv i) ls)) |
| 599 |
(f (fx+ i 1) (fx- c 1)))) |
| 600 |
c) |
| 601 |
#f |
| 602 |
#f |
| 603 |
#f)]) |
| 604 |
(let f ([i 0]) |
| 605 |
(unless (fx= i 10000) |
| 606 |
(put-u8 p (mod i 37)) |
| 607 |
(f (+ i 1)))) |
| 608 |
(flush-output-port p) |
| 609 |
(let f ([i 0] [ls (reverse ls)]) |
| 610 |
(unless (null? ls) |
| 611 |
(assert (fx= (mod i 37) (car ls))) |
| 612 |
(f (fx+ i 1) (cdr ls)))))) |
| 613 |
|
| 614 |
|
| 615 |
(define (test-partial-reads) |
| 616 |
(define (make-test-string) |
| 617 |
(list->string |
| 618 |
(let f ([i 0]) |
| 619 |
(cond |
| 620 |
[(fx=? i #x110000) '()] |
| 621 |
[(fx=? i #xD800) (f #xE000)] |
| 622 |
[else (cons (integer->char i) (f (+ i 1)))])))) |
| 623 |
(define (make-slow-input-port bv transcoder) |
| 624 |
(let ([n 0]) |
| 625 |
(transcoded-port |
| 626 |
(make-custom-binary-input-port "foo" |
| 627 |
(lambda (buffer i cnt) |
| 628 |
(cond |
| 629 |
[(fx=? n (bytevector-length bv)) 0] |
| 630 |
[else |
| 631 |
(let ([u8 (bytevector-u8-ref bv n)]) |
| 632 |
; (printf "got [~s] #b~b\n" n u8) |
| 633 |
(bytevector-u8-set! buffer i u8)) |
| 634 |
(set! n (+ n 1)) |
| 635 |
1])) |
| 636 |
#f #f #f) |
| 637 |
transcoder))) |
| 638 |
(define (test name codec s->bv bv->s) |
| 639 |
(printf "testing partial reads for ~s codec ... " name) |
| 640 |
(let ([s (make-test-string)]) |
| 641 |
(assert (string=? s (bv->s (s->bv s)))) |
| 642 |
(let ([r (call-with-port |
| 643 |
(make-slow-input-port (s->bv s) |
| 644 |
(make-transcoder codec |
| 645 |
(eol-style none) (error-handling-mode raise))) |
| 646 |
get-string-all)]) |
| 647 |
(unless (string=? r s) |
| 648 |
(if (= (string-length r) (string-length s)) |
| 649 |
(error #f "test failed") |
| 650 |
(error #f "length mismatch" |
| 651 |
(string-length s) (string-length r)))))) |
| 652 |
(printf "ok\n")) |
| 653 |
(test 'utf8 (utf-8-codec) |
| 654 |
string->utf8 |
| 655 |
utf8->string) |
| 656 |
(test 'utf16 (utf-16-codec) |
| 657 |
(lambda (x) (string->utf16 x 'little)) |
| 658 |
(lambda (x) (utf16->string x 'little)))) |
| 659 |
|
| 660 |
(define-tests test-input-ports |
| 661 |
[eof-object? |
| 662 |
(get-line (open-string-input-port ""))] |
| 663 |
[(lambda (x) (equal? x "abcd")) |
| 664 |
(get-line (open-string-input-port "abcd"))] |
| 665 |
[(lambda (x) (equal? x "")) |
| 666 |
(get-line (open-string-input-port "\nabcd"))] |
| 667 |
[(lambda (x) (equal? x "abcd")) |
| 668 |
(get-line (open-string-input-port "abcd\nefg"))]) |
| 669 |
|
| 670 |
(define (test-has-port-position) |
| 671 |
(define-syntax check |
| 672 |
(syntax-rules () |
| 673 |
[(_ e) |
| 674 |
(begin ;;; evaluating e twice |
| 675 |
(assert (not (port-has-port-position? e))) |
| 676 |
(assert |
| 677 |
(guard (con |
| 678 |
[(assertion-violation? con) #t] |
| 679 |
[else #f]) |
| 680 |
(begin (port-position e) #f))))])) |
| 681 |
(check (make-custom-binary-input-port "foo" (lambda a 0) #f #f #f)) |
| 682 |
(check (make-custom-binary-output-port "foo" (lambda a 0) #f #f #f)) |
| 683 |
(check (make-custom-textual-input-port "foo" (lambda a 0) #f #f #f)) |
| 684 |
(check (make-custom-textual-output-port "foo" (lambda a 0) #f #f #f))) |
| 685 |
|
| 686 |
|
| 687 |
(define (test-put-bytevector) |
| 688 |
(call-with-values open-bytevector-output-port |
| 689 |
(lambda (p e) |
| 690 |
(do ((i 0 (+ i 1))) ((= i 86)) |
| 691 |
(put-bytevector p '#vu8(0)) |
| 692 |
(put-u8 p 0)) |
| 693 |
(assert (equal? (e) (make-bytevector (* 86 2) 0))))) |
| 694 |
|
| 695 |
(call-with-values open-bytevector-output-port |
| 696 |
(lambda (p e) |
| 697 |
(do ((i 0 (+ i 1))) ((= i 86)) |
| 698 |
(put-u8 p 0) |
| 699 |
(put-u8 p 0)) |
| 700 |
(assert (equal? (e) (make-bytevector (* 86 2) 0))))) |
| 701 |
|
| 702 |
(call-with-values open-bytevector-output-port |
| 703 |
(lambda (p e) |
| 704 |
(do ((i 0 (+ i 1))) ((= i 86)) |
| 705 |
(put-bytevector p '#vu8(0)) |
| 706 |
(put-bytevector p '#vu8(0))) |
| 707 |
(assert (equal? (e) (make-bytevector (* 86 2) 0)))))) |
| 708 |
|
| 709 |
(define (test-get-bytevector-n) |
| 710 |
(let ((p (open-bytevector-input-port '#vu8(1 2 3 4 5 6 7 8 9))) |
| 711 |
(buf (make-bytevector 10 #xff))) |
| 712 |
(let ([buf1 |
| 713 |
(begin |
| 714 |
(printf "going to read 5 bytes: ~s\n" (get-bytevector-n! p buf 0 5)) |
| 715 |
(printf "result: ~s\n" buf) |
| 716 |
(bytevector-copy buf))]) |
| 717 |
(printf "going to read 1 byte: ~s\n" (get-bytevector-n! p buf 5 1)) |
| 718 |
(printf "result: ~s\n" buf) |
| 719 |
(unless (bytevector=? buf '#vu8(1 2 3 4 5 6 #xff #xff #xff #xff)) |
| 720 |
(error 'test "the data is not correct" buf1 buf))))) |
| 721 |
|
| 722 |
(define (run-tests) |
| 723 |
(test-get-bytevector-n) |
| 724 |
(test-custom-binary-input-ports) |
| 725 |
(test-custom-binary-output-ports) |
| 726 |
(run-exhaustive-tests) |
| 727 |
(test-input-files) |
| 728 |
(test-partial-reads) |
| 729 |
(test-input-ports) |
| 730 |
(test-has-port-position) |
| 731 |
(test-put-bytevector)) |
| 732 |
|
| 733 |
) |
| 734 |
|
| 735 |
|
| 736 |
|