Debian Codegraph @

test/include/srfi-207-test.scm

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