| 1 |
;;;; srfi-207.test --- SRFI 207 test suite -*- scheme -*- |
| 2 |
|
| 3 |
;;; Copyright (C) 2020 Wolfgang Corcoran-Mathe |
| 4 |
;;; Copyright (C) 2025 Free Software Foundation, Inc. |
| 5 |
;;; |
| 6 |
;;; Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
;;; copy of this software and associated documentation files (the |
| 8 |
;;; "Software"), to deal in the Software without restriction, including |
| 9 |
;;; without limitation the rights to use, copy, modify, merge, publish, |
| 10 |
;;; distribute, sublicense, and/or sell copies of the Software, and to |
| 11 |
;;; permit persons to whom the Software is furnished to do so, subject to |
| 12 |
;;; the following conditions: |
| 13 |
;;; |
| 14 |
;;; The above copyright notice and this permission notice shall be included |
| 15 |
;;; in all copies or substantial portions of the Software. |
| 16 |
;;; |
| 17 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 |
;;; OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 |
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 |
;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 |
;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 |
;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 |
;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
(define-module (srfi-207-test) |
| 26 |
#:use-module ((rnrs bytevectors) #:select (string->utf8 u8-list->bytevector)) |
| 27 |
#:use-module ((srfi srfi-1) #:select (every list-tabulate)) |
| 28 |
#:use-module ((srfi srfi-207) |
| 29 |
#:select (base64->bytevector |
| 30 |
bytestring |
| 31 |
bytestring->list |
| 32 |
bytestring-break |
| 33 |
bytestring-error? |
| 34 |
bytestring-index |
| 35 |
bytestring-index-right |
| 36 |
bytestring-join |
| 37 |
bytestring-pad |
| 38 |
bytestring-pad-right |
| 39 |
bytestring-replace |
| 40 |
bytestring-span |
| 41 |
bytestring-split |
| 42 |
bytestring-trim |
| 43 |
bytestring-trim-both |
| 44 |
bytestring-trim-right |
| 45 |
bytestring<=? |
| 46 |
bytestring<? |
| 47 |
bytestring>=? |
| 48 |
bytestring>? |
| 49 |
bytevector->base64 |
| 50 |
bytevector->hex-string |
| 51 |
hex-string->bytevector |
| 52 |
make-bytestring |
| 53 |
make-bytestring! |
| 54 |
make-bytestring-generator |
| 55 |
read-textual-bytestring |
| 56 |
write-binary-bytestring |
| 57 |
write-textual-bytestring)) |
| 58 |
#:use-module ((srfi srfi-34) #:select (guard)) |
| 59 |
#:use-module ((test-suite lib) |
| 60 |
#:select (exception:read-error |
| 61 |
pass-if-equal |
| 62 |
pass-if-exception |
| 63 |
with-test-prefix)) |
| 64 |
#:use-module ((scheme base) |
| 65 |
#:select (bytevector |
| 66 |
bytevector-length |
| 67 |
bytevector-u8-ref |
| 68 |
get-output-bytevector |
| 69 |
make-bytevector |
| 70 |
open-output-bytevector |
| 71 |
utf8->string))) |
| 72 |
|
| 73 |
;; No srfi-158 |
| 74 |
(define (generator->list gen) |
| 75 |
(let rec ((x (gen))) |
| 76 |
(if (eof-object? x) |
| 77 |
'() |
| 78 |
(cons x (rec (gen)))))) |
| 79 |
|
| 80 |
(define-syntax check |
| 81 |
(syntax-rules (=>) |
| 82 |
((check expr => expected) |
| 83 |
(pass-if-equal expected expr)))) |
| 84 |
|
| 85 |
;;;; Utility |
| 86 |
|
| 87 |
(define (print-header message) #t) |
| 88 |
|
| 89 |
(define-syntax constantly |
| 90 |
(syntax-rules () |
| 91 |
((_ obj) (lambda _ obj)))) |
| 92 |
|
| 93 |
(define always (constantly #t)) |
| 94 |
(define never (constantly #f)) |
| 95 |
|
| 96 |
;; Returns a list of the values produced by expr. |
| 97 |
(define-syntax values~>list |
| 98 |
(syntax-rules () |
| 99 |
((_ expr) |
| 100 |
(call-with-values (lambda () expr) list)))) |
| 101 |
|
| 102 |
;; If expr causes an exception to be raised, return 'bytestring-error |
| 103 |
;; if the raised object satisfies bytestring-error?, and #f otherwise. |
| 104 |
(define-syntax catch-bytestring-error |
| 105 |
(syntax-rules () |
| 106 |
((_ expr) |
| 107 |
(guard (condition ((bytestring-error? condition) 'bytestring-error) |
| 108 |
(else |
| 109 |
(format (current-error-port) "exception: ~s\n" condition) |
| 110 |
#f)) |
| 111 |
expr)))) |
| 112 |
|
| 113 |
;; Testing shorthand for write-binary-bytestring. |
| 114 |
(define (%bytestring/IO . args) |
| 115 |
(call-with-port (open-output-bytevector) |
| 116 |
(lambda (port) |
| 117 |
(apply write-binary-bytestring port args) |
| 118 |
(get-output-bytevector port)))) |
| 119 |
|
| 120 |
;; Testing shorthands for SNB I/O. Coverage library fans, eat your |
| 121 |
;; hearts out. |
| 122 |
(define (parse-SNB/prefix s) |
| 123 |
(call-with-port (open-input-string s) |
| 124 |
(lambda (p) |
| 125 |
(read-textual-bytestring #t p)))) |
| 126 |
|
| 127 |
(define (parse-SNB s) |
| 128 |
(call-with-port (open-input-string s) |
| 129 |
(lambda (p) |
| 130 |
(read-textual-bytestring #f p)))) |
| 131 |
|
| 132 |
(define (%bytestring->SNB bstring) |
| 133 |
(call-with-port (open-output-string) |
| 134 |
(lambda (port) |
| 135 |
(write-textual-bytestring bstring port) |
| 136 |
(get-output-string port)))) |
| 137 |
|
| 138 |
|
| 139 |
(define test-bstring (bytestring "lorem")) |
| 140 |
|
| 141 |
(define homer |
| 142 |
(bytestring |
| 143 |
(string-append "The Man, O Muse, informe, who many a way / " |
| 144 |
"Wound in his wisedome to his wished stay;"))) |
| 145 |
|
| 146 |
(define homer64 |
| 147 |
(string-append |
| 148 |
"VGhlIE1hbiwgTyBNdXNlLCBpbmZvcm1lLCB3aG8gbWFueSBhIHdheSAvIFdvd" |
| 149 |
"W5kIGluIGhpcyB3aXNlZG9tZSB0byBoaXMgd2lzaGVkIHN0YXk7")) |
| 150 |
|
| 151 |
(define homer64-w |
| 152 |
(string-append |
| 153 |
"VGhlIE1hb iwgTyBNdXNlL CBpbmZvcm1lL\nCB3aG8gbWF\tueSBhIH\rdheSAvIFdvd" |
| 154 |
"W5kIGluI GhpcyB 3aXNlZ\t\t\nG9tZSB0b yBoaXMgd\t2lzaGVkIHN0YXk7")) |
| 155 |
|
| 156 |
;;;; Constructors |
| 157 |
|
| 158 |
(define (check-constructor) |
| 159 |
(print-header "Running constructor tests...") |
| 160 |
(check (bytestring "lo" #\r #x65 #u8(#x6d)) => test-bstring) |
| 161 |
(check (bytestring) => (bytevector)) |
| 162 |
|
| 163 |
(check (catch-bytestring-error (bytestring #x100)) => 'bytestring-error) |
| 164 |
(check (catch-bytestring-error (bytestring "λ")) => 'bytestring-error)) |
| 165 |
|
| 166 |
(define (check-conversion) |
| 167 |
(print-header "Running conversion tests...") |
| 168 |
|
| 169 |
(check (bytevector->hex-string test-bstring) => "6c6f72656d") |
| 170 |
(check (hex-string->bytevector "6c6f72656d") => test-bstring) |
| 171 |
(check (catch-bytestring-error |
| 172 |
(hex-string->bytevector "c6f72656d")) |
| 173 |
=> 'bytestring-error) |
| 174 |
(check (catch-bytestring-error |
| 175 |
(hex-string->bytevector "6czf72656d")) |
| 176 |
=> 'bytestring-error) |
| 177 |
(check (equal? (hex-string->bytevector (bytevector->hex-string homer)) |
| 178 |
homer) |
| 179 |
=> #t) |
| 180 |
|
| 181 |
(check (hex-string->bytevector (bytevector->hex-string #u8())) => #u8()) |
| 182 |
|
| 183 |
(check (bytevector->base64 test-bstring) => "bG9yZW0=") |
| 184 |
(check (bytevector->base64 #u8(#xff #xef #xff)) => "/+//") |
| 185 |
(check (bytevector->base64 #u8(#xff #xef #xff) "*@") => "@*@@") |
| 186 |
(check (bytevector->base64 homer) => homer64) |
| 187 |
(check (bytevector->base64 #u8(1)) => "AQ==") |
| 188 |
(check (bytevector->base64 #u8()) => "") |
| 189 |
(check (base64->bytevector "bG9yZW0=") => test-bstring) |
| 190 |
(check (base64->bytevector "/+//") => #u8(#xff #xef #xff)) |
| 191 |
(check (base64->bytevector "@*@@" "*@") => #u8(#xff #xef #xff)) |
| 192 |
(check (base64->bytevector homer64) => homer) |
| 193 |
(check (base64->bytevector homer64-w) => homer) |
| 194 |
(check (base64->bytevector "AQ==") => #u8(1)) |
| 195 |
(check (base64->bytevector "") => #u8()) |
| 196 |
(check (base64->bytevector "\n\n\n==\t\r\n") => #u8()) |
| 197 |
(check (catch-bytestring-error |
| 198 |
(base64->bytevector "bG9@frob")) => 'bytestring-error) |
| 199 |
|
| 200 |
(check (bytestring->list #u8()) => '()) |
| 201 |
(check (bytestring->list (bytestring 70 82 0 66)) => '(#\F #\R 0 #\B)) |
| 202 |
(check (bytestring->list (bytestring "\a\t\t\n" 200)) => '(7 9 9 10 200)) |
| 203 |
(check (make-bytestring (bytestring->list test-bstring)) => test-bstring) |
| 204 |
(check (make-bytestring (bytestring->list test-bstring 2)) |
| 205 |
=> (bytestring "rem")) |
| 206 |
(check (make-bytestring (bytestring->list test-bstring 1 3)) |
| 207 |
=> (bytestring "or")) |
| 208 |
|
| 209 |
(let ((bvec (make-bytevector 5))) |
| 210 |
(check (begin |
| 211 |
(make-bytestring! bvec 0 '(#x6c #x6f #x72 #x65 #x6d)) |
| 212 |
bvec) |
| 213 |
=> test-bstring)) |
| 214 |
(let ((bvec (make-bytevector 9 #x20))) |
| 215 |
(check (begin (make-bytestring! bvec 2 '("lo" #\r #x65 #u8(#x6d))) |
| 216 |
bvec) |
| 217 |
=> (bytestring " lorem "))) |
| 218 |
(check (catch-bytestring-error (make-bytestring '("λ"))) |
| 219 |
=> 'bytestring-error) |
| 220 |
(check (catch-bytestring-error (make-bytestring '(#x100))) |
| 221 |
=> 'bytestring-error) |
| 222 |
|
| 223 |
(let ((s (list-tabulate (bytevector-length test-bstring) |
| 224 |
(lambda (i) |
| 225 |
(bytevector-u8-ref test-bstring i))))) |
| 226 |
(check (let ((g (make-bytestring-generator "lo" #\r #x65 #u8(#x6d)))) |
| 227 |
(generator->list g)) |
| 228 |
=> s)) |
| 229 |
(check (catch-bytestring-error (make-bytestring-generator "λ" #\m #\u)) |
| 230 |
=> 'bytestring-error) |
| 231 |
(check (catch-bytestring-error (make-bytestring-generator 89 90 300)) |
| 232 |
=> 'bytestring-error)) |
| 233 |
|
| 234 |
(define (check-selection) |
| 235 |
(print-header "Running selection tests...") |
| 236 |
|
| 237 |
(check (bytestring-pad test-bstring (bytevector-length test-bstring) #x7a) |
| 238 |
=> test-bstring) |
| 239 |
(check (utf8->string (bytestring-pad test-bstring 8 #x7a)) |
| 240 |
=> "zzzlorem") |
| 241 |
(check (equal? (bytestring-pad test-bstring 8 #\z) |
| 242 |
(bytestring-pad test-bstring 8 (char->integer #\z))) |
| 243 |
=> #t) |
| 244 |
(check (bytestring-pad-right test-bstring |
| 245 |
(bytevector-length test-bstring) |
| 246 |
#x7a) |
| 247 |
=> test-bstring) |
| 248 |
(check (utf8->string (bytestring-pad-right test-bstring 8 #x7a)) |
| 249 |
=> "loremzzz") |
| 250 |
(check (equal? (bytestring-pad-right test-bstring 8 #\z) |
| 251 |
(bytestring-pad-right test-bstring 8 (char->integer #\z))) |
| 252 |
=> #t) |
| 253 |
|
| 254 |
(check (bytestring-trim test-bstring always) => #u8()) |
| 255 |
(check (bytestring-trim test-bstring never) => test-bstring) |
| 256 |
(check (bytestring-trim test-bstring (lambda (u8) (< u8 #x70))) |
| 257 |
=> #u8(#x72 #x65 #x6d)) |
| 258 |
(check (bytestring-trim-right test-bstring always) => #u8()) |
| 259 |
(check (bytestring-trim-right test-bstring never) => test-bstring) |
| 260 |
(check (bytestring-trim-right test-bstring (lambda (u8) (< u8 #x70))) |
| 261 |
=> #u8(#x6c #x6f #x72)) |
| 262 |
(check (bytestring-trim-both test-bstring always) => #u8()) |
| 263 |
(check (bytestring-trim-both test-bstring never) => test-bstring) |
| 264 |
(check (bytestring-trim-both test-bstring (lambda (u8) (< u8 #x70))) |
| 265 |
=> #u8(#x72))) |
| 266 |
|
| 267 |
(define (check-replacement) |
| 268 |
(print-header "Running bytestring-replace tests...") |
| 269 |
|
| 270 |
(check (bytestring-replace test-bstring (bytestring "mists") 1 5 1 5) |
| 271 |
=> (bytestring "lists")) |
| 272 |
(check (bytestring-replace test-bstring (bytestring "faded") 2 5 1 5) |
| 273 |
=> (bytestring "loaded")) |
| 274 |
(check (bytestring-replace (make-bytevector 5) |
| 275 |
test-bstring |
| 276 |
0 |
| 277 |
(bytevector-length test-bstring)) |
| 278 |
=> test-bstring) |
| 279 |
|
| 280 |
(let ((bv1 (bytestring "food")) (bv2 (bytestring "od fo"))) |
| 281 |
(check (bytestring-replace bv1 bv2 2 2 0 5) => (bytestring "food food"))) |
| 282 |
(let ((bv1 (bytestring "food food"))) |
| 283 |
(check (bytestring-replace bv1 (bytevector) 2 7 0 0) |
| 284 |
=> (bytestring "food")))) |
| 285 |
|
| 286 |
(define (check-comparison) |
| 287 |
(define short-bstring (bytestring "lore")) |
| 288 |
(define long-bstring (bytestring "lorem ")) |
| 289 |
(define mixed-case-bstring (bytestring "loreM")) |
| 290 |
(print-header "Runnng comparison tests...") |
| 291 |
|
| 292 |
(check (bytestring<? test-bstring test-bstring) => #f) |
| 293 |
(check (bytestring<? short-bstring test-bstring) => #t) |
| 294 |
(check (bytestring<? mixed-case-bstring test-bstring) => #t) |
| 295 |
(check (bytestring>? test-bstring test-bstring) => #f) |
| 296 |
(check (bytestring>? test-bstring short-bstring) => #t) |
| 297 |
(check (bytestring>? test-bstring mixed-case-bstring) => #t) |
| 298 |
(check (bytestring<=? test-bstring test-bstring) => #t) |
| 299 |
(check (bytestring<=? short-bstring test-bstring) => #t) |
| 300 |
(check (bytestring<=? mixed-case-bstring test-bstring) => #t) |
| 301 |
(check (bytestring<=? test-bstring mixed-case-bstring) => #f) |
| 302 |
(check (bytestring<=? long-bstring test-bstring) => #f) |
| 303 |
(check (bytestring>=? test-bstring test-bstring) => #t) |
| 304 |
(check (bytestring>=? test-bstring short-bstring) => #t) |
| 305 |
(check (bytestring>=? test-bstring mixed-case-bstring) => #t) |
| 306 |
(check (bytestring>=? mixed-case-bstring test-bstring) => #f) |
| 307 |
(check (bytestring>=? short-bstring test-bstring) => #f)) |
| 308 |
|
| 309 |
(define (check-searching) |
| 310 |
(define (eq-r? b) (= b #x72)) |
| 311 |
(define (lt-r? b) (< b #x72)) |
| 312 |
(print-header "Running search tests...") |
| 313 |
|
| 314 |
(check (bytestring-index test-bstring always) => 0) |
| 315 |
(check (bytestring-index test-bstring never) => #f) |
| 316 |
(check (bytestring-index test-bstring always 3) => 3) |
| 317 |
(check (bytestring-index test-bstring eq-r?) => 2) |
| 318 |
|
| 319 |
(check (bytestring-index-right test-bstring always) => 4) |
| 320 |
(check (bytestring-index-right test-bstring never) => #f) |
| 321 |
(check (bytestring-index-right test-bstring always 3) => 4) |
| 322 |
(check (bytestring-index-right test-bstring eq-r?) => 2) |
| 323 |
|
| 324 |
(check (values~>list (bytestring-span test-bstring always)) |
| 325 |
=> (list test-bstring (bytevector))) |
| 326 |
(check (values~>list (bytestring-span test-bstring never)) |
| 327 |
=> (list (bytevector) test-bstring)) |
| 328 |
(check (values~>list (bytestring-span test-bstring lt-r?)) |
| 329 |
=> (list (bytestring "lo") (bytestring "rem"))) |
| 330 |
|
| 331 |
(check (values~>list (bytestring-break test-bstring always)) |
| 332 |
=> (list (bytevector) test-bstring)) |
| 333 |
(check (values~>list (bytestring-break test-bstring never)) |
| 334 |
=> (list test-bstring (bytevector))) |
| 335 |
(check (values~>list (bytestring-break test-bstring eq-r?)) |
| 336 |
=> (list (bytestring "lo") (bytestring "rem")))) |
| 337 |
|
| 338 |
(define (check-join-and-split) |
| 339 |
(define test-segments '(#u8(1) #u8(2) #u8(3))) |
| 340 |
(print-header "Running joining and splitting tests...") |
| 341 |
|
| 342 |
(check (bytestring-join test-segments #u8(0)) => #u8(1 0 2 0 3)) |
| 343 |
(check (bytestring-join test-segments #u8(0) 'prefix) => #u8(0 1 0 2 0 3)) |
| 344 |
(check (bytestring-join test-segments #u8(0) 'suffix) => #u8(1 0 2 0 3 0)) |
| 345 |
(check (bytestring-join '() #u8(0)) => #u8()) |
| 346 |
(check (bytestring-join test-segments #\space) => #u8(1 32 2 32 3)) |
| 347 |
(check (bytestring-join test-segments 0) => #u8(1 0 2 0 3)) |
| 348 |
(check (bytestring-join test-segments "AB") |
| 349 |
=> #u8(1 65 66 2 65 66 3)) |
| 350 |
(check (bytestring-join test-segments #u8(7 8)) => #u8(1 7 8 2 7 8 3)) |
| 351 |
(check (catch-bytestring-error |
| 352 |
(bytestring-join test-segments 300)) => 'bytestring-error) |
| 353 |
(check (catch-bytestring-error |
| 354 |
(bytestring-join test-segments "λ")) => 'bytestring-error) |
| 355 |
(check (catch-bytestring-error |
| 356 |
(bytestring-join '() #u8(0) 'strict-infix)) => 'bytestring-error) |
| 357 |
(check (catch-bytestring-error |
| 358 |
(bytestring-join '() #u8(0) 'foofix)) => 'bytestring-error) |
| 359 |
|
| 360 |
(check (bytestring-split #u8(1 0 2 0 3) 0 'infix) => test-segments) |
| 361 |
(check (bytestring-split #u8(0 1 0 2 0 3) 0 'prefix) => test-segments) |
| 362 |
(check (bytestring-split #u8(1 0 2 0 3 0) 0 'suffix) => test-segments) |
| 363 |
(check (bytestring-split #u8(0 0) 0) => '(#u8() #u8() #u8())) |
| 364 |
(check (bytestring-split #u8() 0) => '()) |
| 365 |
(check (catch-bytestring-error |
| 366 |
(bytestring-split #u8() 0 'foofix)) => 'bytestring-error)) |
| 367 |
|
| 368 |
(define (check-io) |
| 369 |
(print-header "Running I/O tests...") |
| 370 |
|
| 371 |
(check (%bytestring/IO "lo" #\r #x65 #u8(#x6d)) => test-bstring) |
| 372 |
(check (%bytestring/IO) => #u8()) |
| 373 |
(check (catch-bytestring-error (%bytestring/IO #x100)) => 'bytestring-error) |
| 374 |
(check (catch-bytestring-error (%bytestring/IO "λ")) => 'bytestring-error) |
| 375 |
|
| 376 |
;;; read-textual-bytestring |
| 377 |
|
| 378 |
(check (parse-SNB/prefix "#u8\"\"") => #u8()) |
| 379 |
(check (parse-SNB/prefix "#u8\"lorem\"") => test-bstring) |
| 380 |
(check (parse-SNB/prefix "#u8\"\\xde;\\xad;\\xf0;\\x0d;\"") |
| 381 |
=> (bytevector #xde #xad #xf0 #x0d)) |
| 382 |
(check (parse-SNB/prefix "#u8\"\\\"\\\\\\a\\b\\t\\n\\r\\\|\"") |
| 383 |
=> (bytestring #\" #\\ #\alarm #\backspace #\tab #\newline #\return #\|)) |
| 384 |
(check (parse-SNB/prefix "#u8\"lor\\\n\te\\ \r\n\tm\"") |
| 385 |
=> test-bstring) |
| 386 |
(check (parse-SNB "\"lorem\"") => test-bstring) |
| 387 |
|
| 388 |
;; Invalid SNB detection. |
| 389 |
(check (catch-bytestring-error (parse-SNB/prefix "#u\"lorem\"")) |
| 390 |
=> 'bytestring-error) |
| 391 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8lorem\"")) |
| 392 |
=> 'bytestring-error) |
| 393 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"lorem")) |
| 394 |
=> 'bytestring-error) |
| 395 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"lorem")) |
| 396 |
=> 'bytestring-error) |
| 397 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"l\\orem\"")) |
| 398 |
=> 'bytestring-error) |
| 399 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"l\\ orem\"")) |
| 400 |
=> 'bytestring-error) |
| 401 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"l\\x6frem\"")) |
| 402 |
=> 'bytestring-error) |
| 403 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"l\\x6z;rem\"")) |
| 404 |
=> 'bytestring-error) |
| 405 |
(check (catch-bytestring-error (parse-SNB/prefix "#u8\"α equivalence\"")) |
| 406 |
=> 'bytestring-error) |
| 407 |
|
| 408 |
;;; write-textual-bytestring |
| 409 |
|
| 410 |
(check (%bytestring->SNB #u8()) => "#u8\"\"") |
| 411 |
(check (%bytestring->SNB test-bstring) => "#u8\"lorem\"") |
| 412 |
(check (%bytestring->SNB (bytevector #xde #xad #xbe #xef)) |
| 413 |
=> "#u8\"\\xde;\\xad;\\xbe;\\xef;\"") |
| 414 |
(check (%bytestring->SNB |
| 415 |
(bytestring #\" #\\ #\alarm #\backspace #\tab #\newline #\return #\|)) |
| 416 |
=> "#u8\"\\\"\\\\\\a\\b\\t\\n\\r\\\|\"") |
| 417 |
|
| 418 |
(let ((test-bstrings |
| 419 |
'(#u8(124 199 173 212 209 232 249 16 198 32 123 111 130 92 64 155) |
| 420 |
#u8(50 133 193 27 177 105 10 186 61 149 177 105 96 70 223 190) |
| 421 |
#u8(0 117 226 155 110 0 66 216 27 129 187 81 17 210 71 152) |
| 422 |
#u8(123 31 159 25 100 135 246 47 249 137 243 241 45 241 240 221) |
| 423 |
#u8(207 186 70 110 118 231 79 195 153 253 93 101 126 198 70 235) |
| 424 |
#u8(138 176 92 152 208 107 28 236 198 254 111 37 241 116 191 206) |
| 425 |
#u8(221 254 214 90 0 155 132 92 157 246 199 224 224 142 91 114) |
| 426 |
#u8(228 216 233 80 142 15 158 54 5 85 174 101 111 75 126 209) |
| 427 |
#u8(191 16 83 245 45 98 72 212 148 202 135 19 213 150 141 121) |
| 428 |
#u8(41 169 182 96 47 184 16 116 196 251 243 93 81 162 175 140) |
| 429 |
#u8(85 49 218 138 132 11 27 11 182 27 120 71 254 169 132 166) |
| 430 |
#u8(89 216 175 23 97 10 237 112 208 195 112 80 198 154 241 254) |
| 431 |
#u8(187 54 6 57 250 137 129 89 188 19 225 217 168 178 174 129) |
| 432 |
#u8(88 164 89 40 175 194 108 56 12 124 109 96 148 149 119 109) |
| 433 |
#u8(241 66 32 115 203 71 128 154 240 111 194 137 73 44 146 3) |
| 434 |
#u8(177 185 177 233 18 14 178 106 110 109 222 147 111 157 216 208)))) |
| 435 |
(check |
| 436 |
(every (lambda (bvec) |
| 437 |
(equal? bvec (parse-SNB/prefix (%bytestring->SNB bvec)))) |
| 438 |
test-bstrings) |
| 439 |
=> #t))) |
| 440 |
|
| 441 |
;; All the reference implementation tests |
| 442 |
(define (check-all) |
| 443 |
(check-constructor) |
| 444 |
(check-conversion) |
| 445 |
(check-selection) |
| 446 |
(check-replacement) |
| 447 |
(check-comparison) |
| 448 |
(check-searching) |
| 449 |
(check-join-and-split) |
| 450 |
(check-io)) |
| 451 |
|
| 452 |
(check-all) |
| 453 |
|
| 454 |
(let ((all-decoded (u8-list->bytevector (iota 256)))) |
| 455 |
|
| 456 |
(define (hex-esc i) (format #f "\\x~x;" i)) |
| 457 |
(define (hex-escs start end) |
| 458 |
(string-concatenate (map hex-esc (iota (1+ (- end start)) start)))) |
| 459 |
(define (ascii start end) |
| 460 |
(list->string (map integer->char (iota (1+ (- end start)) start)))) |
| 461 |
(define all-encoded |
| 462 |
(string-append "#u8\"" |
| 463 |
(hex-escs 0 6) |
| 464 |
"\\a\\b\\t\\n\\xb;\\xc;\\r" |
| 465 |
(hex-escs 14 31) |
| 466 |
" !" |
| 467 |
"\\\"" |
| 468 |
(ascii 35 91) |
| 469 |
"\\\\" |
| 470 |
(ascii 93 123) |
| 471 |
"\\|" |
| 472 |
(ascii 125 126) |
| 473 |
(hex-escs 127 255) |
| 474 |
"\"")) |
| 475 |
(define (read-with-bytestrings-enabled s) |
| 476 |
(call-with-input-string s |
| 477 |
(λ (port) |
| 478 |
(let ((keep (memq 'bytestrings (read-options)))) |
| 479 |
(dynamic-wind (λ () (read-enable 'bytestrings)) |
| 480 |
(λ () (read port)) |
| 481 |
(λ () (unless keep (read-disable 'bytestrings)))))))) |
| 482 |
(define (captured-bytestring-output f) |
| 483 |
(call-with-output-string |
| 484 |
(λ (port) |
| 485 |
(let ((keep (memq 'bytestrings (print-options)))) |
| 486 |
(dynamic-wind (λ () (print-enable 'bytestrings)) |
| 487 |
(λ () (f port)) |
| 488 |
(λ () (unless keep (print-disable 'bytestrings)))))))) |
| 489 |
|
| 490 |
(pass-if-exception "reading when not enabled" exception:read-error |
| 491 |
(call-with-input-string "#u8\"\\xe2;\\x88;\\x9e; Improbability\"" read)) |
| 492 |
(pass-if-equal "reading when enabled" |
| 493 |
(string->utf8 "∞ Improbability") |
| 494 |
(read-with-bytestrings-enabled "#u8\"\\xe2;\\x88;\\x9e; Improbability\"")) |
| 495 |
(pass-if-equal "reading all encodings" |
| 496 |
all-decoded |
| 497 |
(read-with-bytestrings-enabled all-encoded)) |
| 498 |
|
| 499 |
(pass-if-equal "writing when not enabled" |
| 500 |
"#vu8(226 136 158 32 73 109 112 114 111 98 97 98 105 108 105 116 121)" |
| 501 |
(call-with-output-string |
| 502 |
(λ (port) (write (string->utf8 "∞ Improbability") port)))) |
| 503 |
(pass-if-equal "writing when enabled" |
| 504 |
"#u8\"\\xe2;\\x88;\\x9e; Improbability\"" |
| 505 |
(captured-bytestring-output |
| 506 |
(λ (port) (write (string->utf8 "∞ Improbability") port)))) |
| 507 |
(pass-if-equal "writing all encodings" |
| 508 |
all-encoded |
| 509 |
(captured-bytestring-output |
| 510 |
(λ (port) (write (u8-list->bytevector (iota 256)) port))))) |