Debian Codegraph @

test/io2.scm

raw
1 ;; test for advanced read/write features
2 ;;
3 ;; These test is splitted from io.scm, since they depend on other modules
4 ;; has to be done after their tests.
5
6 (use gauche.test)
7 (use scheme.list)
8 (use file.util)
9 (use util.isomorph)
10
11 (test-start "advanced read/write features")
12
13 ;;===============================================================
14 ;; Hash-bang handling (#!)
15 ;;
16
17 (test-section "hash-bang")
18
19 (test* "script hash-bang" 3
20 (read-from-string "#!/usr/bin/gosh -i\n3"))
21 (test* "script hash-bang" 5
22 (read-from-string "#! /usr/bin/gosh -i\n5"))
23 (test* "script hash-bang" (eof-object)
24 (read-from-string "#! /usr/bin/gosh -i"))
25 (test* "script hash-bang error1" (test-error)
26 (read-from-string "#!"))
27
28 (test* "#!fold-case" '(hello world)
29 (read-from-string "#!fold-case (Hello World)"))
30 (test* "#!fold-case" '(Hello world)
31 (read-from-string "(Hello #!fold-case World)"))
32 (test* "#!no-fold-case" '(hello World)
33 (read-from-string "(#!fold-case Hello #!no-fold-case World)"))
34
35 (define-reader-directive 'usr/bin/gosh (lambda _ (values)))
36
37 (test* "customized hash-bang" -i
38 (read-from-string "#!usr/bin/gosh -i\n8"))
39
40 (define-reader-directive 'true! (lambda _ #t))
41 (define-reader-directive 'false! (lambda _ #f))
42
43 (test* "customized hash-bang" '(#t #f)
44 (read-from-string "#!/usr/bin/gosh -i\n(#!true! #!false!)"))
45
46 (test* "customized hash-bang error1" (test-error)
47 (read-from-string "#!/usr/bin/gosh -i\n#! true!"))
48 (test* "customized hash-bang error2" (test-error)
49 (read-from-string "#!/usr/bin/gosh -i\n#!#:true!"))
50
51 ;;===============================================================
52 ;; SRFI-10 Reader constructor (#,)
53 ;;
54
55 (test-section "SRFI-10 reader constructor")
56
57 (test "read ctor 1a" '(1 2 #f "4 5")
58 (lambda ()
59 (define-reader-ctor 'list list)
60 (with-input-from-string "#,(list 1 2 #f \"4 5\")" read)))
61 (test "read ctor 1b" 3
62 (lambda ()
63 (define-reader-ctor '+ +)
64 (with-input-from-string "#,(+ 1 2)" read)))
65 (define-reader-ctor 'my-vector
66 (lambda x (apply vector (cons 'my-vector x))))
67 (test* "read ctor 2a" '#(my-vector (my-vector 1 2))
68 (with-input-from-string "#,(my-vector (my-vector 1 2))" read))
69 (test* "read ctor 2b" '#(my-vector #(my-vector 1 2))
70 (with-input-from-string "#,(my-vector #,(my-vector 1 2))" read))
71
72 ;;===============================================================
73 ;; Shared structures (#n= and #n#)
74 ;;
75
76 ;;---------------------------------------------------------------
77 (test-section "write/ss basic")
78
79 (test* "pair" "(#0=(a b) #0#)"
80 (let1 x '(a b)
81 (write-to-string (list x x) write/ss)))
82 (test* "pair" "(#0=(a b) . #0#)"
83 (let1 x (list 'a 'b)
84 (write-to-string (cons x x) write/ss)))
85 (test* "pair" "(#0=(a b) #1=(a b) #0# . #1#)"
86 (let ((x (list 'a 'b))
87 (y (list 'a 'b)))
88 (write-to-string (list* x y x y) write/ss)))
89 (test* "pair (circular)" "#0=(a . #0#)"
90 (let1 x (list 'a 'b)
91 (set-cdr! x x)
92 (write-to-string x write/ss)))
93 (test* "pair (circular)" "#0=(#0# b)"
94 (let1 x (list 'a 'b)
95 (set-car! x x)
96 (write-to-string x write/ss)))
97 (test* "pair (circular)" "#0=(#0# . #0#)"
98 (let1 x (list 'a 'b)
99 (set-car! x x)
100 (set-cdr! x x)
101 (write-to-string x write/ss)))
102 (test* "pair (circular)" "#0=(a (b . #0#))"
103 (let1 x (list 'a (list 'b 'c))
104 (set-cdr! (cadr x) x)
105 (write-to-string x write/ss)))
106 (test* "pair (circular)" "#0=(a #1=(b . #0#) . #1#)"
107 (let1 x (list 'a (list 'b 'c))
108 (set-cdr! (cadr x) x)
109 (set-cdr! (cdr x) (cadr x))
110 (write-to-string x write/ss)))
111
112 (test* "vector" "(#0=#(a b) . #0#)"
113 (let1 x (vector 'a 'b)
114 (write-to-string (cons x x) write/ss)))
115 (test* "vector" "(#() . #())"
116 (let1 x (vector)
117 (write-to-string (cons x x) write/ss)))
118 (test* "vector" "#(#0=(a b) #0# #0#)"
119 (let1 x '(a b)
120 (write-to-string (vector x x x) write/ss)))
121 (test* "vector (circular)" "#0=#(#0#)"
122 (let1 x (vector 0)
123 (vector-set! x 0 x)
124 (write-to-string x write/ss)))
125
126 (test* "string" "(#0=\"ab\" . #0#)"
127 (let1 x "ab"
128 (write-to-string (cons x x) write/ss)))
129 (test* "string" "(\"\" . \"\")"
130 (let1 x ""
131 (write-to-string (cons x x) write/ss)))
132
133 (test* "mixed" "#0=(a #1=#(#2=\"xyz\" #0# #1# #2#) . #0#)"
134 (let* ([a (list 'a)]
135 [b "xyz"]
136 [c (vector b a 'x b)])
137 (set-cdr! a (cons c a))
138 (vector-set! c 2 c)
139 (write-to-string a write/ss)))
140 (test* "deeply nested" "#0=(((#1=(#0# #0# z) #1# y) . #2=(#1# #0# . x)) #2#)"
141 (let* ([a (list 'a 'a)]
142 [b (list a a 'z)]
143 [c (list b b 'y)]
144 [d (list* c b a 'x)])
145 (set-car! a d)
146 (set-car! (cdr a) (cdr d))
147 (write-to-string a write/ss)))
148
149 (test* "more than 10 substructures"
150 "(#0=(a) #1=(b) #2=(c) #3=(d) #4=(e) #5=(f) #6=(g) #7=(h) #8=(i) #9=(j) #10=(k) #10# #9# #8# #7# #6# #5# #4# #3# #2# #1# #0#)"
151 (let ((a '(a)) (b '(b)) (c '(c)) (d '(d)) (e '(e))
152 (f '(f)) (g '(g)) (h '(h)) (i '(i)) (j '(j)) (k '(k)))
153 (write-to-string
154 (list a b c d e f g h i j k
155 k j i h g f e d c b a)
156 write/ss)))
157
158 (test* "circular list involving abbrev syntax" "#0=((quote . #0#))"
159 (write-to-string (cdr #0='#0#) write/ss))
160
161
162 (let* ([ht1 (make-hash-table 'eq?)]
163 [ht2 (make-hash-table 'eq?)]
164 [circ (list ht1 ht2)])
165 (hash-table-put! ht1 'circ circ)
166 (hash-table-put! ht1 'ht1 ht1)
167 (hash-table-put! ht2 'circ (cdr circ))
168 (hash-table-put! ht2 'ht1 ht1)
169 (test* "circular structure involving hashtable"
170 "#0=(#1=#<hash-table eq[2] @> . #2=(#<hash-table eq[2] @>))"
171 (regexp-replace-all #/@(0x)?[0-9a-f]+/
172 (write-to-string circ write/ss)
173 "@")))
174
175 (define-class <foo> ()
176 ((a :init-keyword :a)
177 (b :init-keyword :b)))
178 (define-method write-object ((self <foo>) port)
179 (format port "#,(foo ~s ~s)" (ref self 'a) (ref self 'b)))
180
181 (test* "user defined" "#,(foo #0=(a b) #0#)"
182 (let* ((x '(a b))
183 (foo (make <foo> :a x :b x)))
184 (write-to-string foo write/ss)))
185 (test* "user defined" "#0=#,(foo #0# #0#)"
186 (let ((foo (make <foo> :a #f :b #f)))
187 (set! (ref foo 'a) foo)
188 (set! (ref foo 'b) foo)
189 (write-to-string foo write/ss)))
190 (test* "user defined" "#0=#,(foo foo #,(foo bar #0#))"
191 (let* ((foo (make <foo> :a 'foo :b #f))
192 (bar (make <foo> :a 'bar :b foo)))
193 (set! (ref foo 'b) bar)
194 (write-to-string foo write/ss)))
195 (test* "user defined" "(#0=#,(foo foo #1=#,(foo bar #0#)) #1#)"
196 (let* ((foo (make <foo> :a 'foo :b #f))
197 (bar (make <foo> :a 'bar :b foo)))
198 (set! (ref foo 'b) bar)
199 (write-to-string (list foo bar) write/ss)))
200 (test* "user defined" "#0=(#1=#,(foo #2=#,(foo bar #1#) #0#) #2#)"
201 (let* ((foo (make <foo> :a 'foo :b #f))
202 (bar (make <foo> :a 'bar :b foo))
203 (baz (list foo bar)))
204 (set! (ref foo 'a) bar)
205 (set! (ref foo 'b) baz)
206 (write-to-string baz write/ss)))
207
208 ;; write/ss with user-defined write-object method.
209 ;; test by UEYAMA Rui
210 (define-class <bar> ()
211 ((a :init-keyword :a)
212 (b :init-keyword :b)))
213 (define-method write-object ((self <bar>) port)
214 (display "#,(bar " port)
215 (write/ss (ref self 'a) port)
216 (display " " port)
217 (write/ss (ref self 'b) port)
218 (display ")" port))
219 (test* "user defined" "#,(bar #0=(a b) #0#)"
220 (let* ((x '(a b))
221 (bar (make <bar> :a x :b x)))
222 (write-to-string bar write/ss)))
223
224 (define-class <baz> ()
225 ((a :init-keyword :a)
226 (b :init-keyword :b)))
227 (define-method write-object ((self <baz>) port)
228 (display "#," port)
229 (write `(baz :a ,(ref self 'a) :b ,(ref self 'b)) port))
230 (test* "user defined mixed"
231 "#,(baz :a (#0=#,(foo foo foob) #1=#,(foo fee feeb)) :b (#,(bar #0# #1#) #,(bar #1# #0#)))"
232 (let* ((f0 (make <foo> :a 'foo :b 'foob))
233 (f1 (make <foo> :a 'fee :b 'feeb))
234 (b1 (make <bar> :a f0 :b f1))
235 (b2 (make <bar> :a f1 :b f0))
236 (c (make <baz> :a (list f0 f1) :b (list b1 b2))))
237 (write-to-string c write/ss)))
238
239 ;; This test doesn't involve shared structure. It is to test
240 ;; we can handle deep list without busting C stack.
241 (test* "deep list doesn't bust C stack" 2000002
242 (let loop ([cnt 0] [ls '()])
243 (if (< cnt 1000000)
244 (loop (+ cnt 1) (list ls))
245 (string-length (write-to-string ls)))))
246
247 ;;---------------------------------------------------------------
248 (test-section "format/ss")
249
250 (test* "format/ss" "The answer is #0=(\"a\" . #0#)"
251 (let ((a (list "a")))
252 (set-cdr! a a)
253 (format/ss "The answer is ~s" a)))
254
255 (test* "format/ss" "The answer is #0=(a . #0#)"
256 (let ((a (list "a")))
257 (set-cdr! a a)
258 (format/ss "The answer is ~a" a)))
259
260 (test* "format/ss" "The answer is #0=(a . #0#) #0#"
261 (let ((a (list 'a)))
262 (set-cdr! a a)
263 (format/ss "The answer is ~s ~s" a a)))
264
265 ;;---------------------------------------------------------------
266 (test-section "obscure format features")
267
268 (test* "format plural" "0 books, 1 book, 2 books, 1.0 books"
269 (format "~a book~p, ~a book~p, ~a book~p, ~a book~p"
270 0 0 1 1 2 2 1.0 1.0))
271 (test* "format plural" "0 pens, 1 pen, 2 pens, 1.0 pens"
272 (format "~a pen~:p, ~a pen~:p, ~a pen~:p, ~a pen~:p"
273 0 1 2 1.0))
274 (test* "format plural" "0 ponies, 1 pony, 2 ponies, 1.0 ponies"
275 (format "~a pon~:@p, ~a pon~:@p, ~a pon~:@p, ~a pon~:@p"
276 0 1 2 1.0))
277
278 (test* "format roman numerals"
279 '("I" "II" "III" "IV" "V" "VI" "VII" "VIII" "IX" "X")
280 (map (^i (format "~@R" i)) (iota 10 1)))
281 (test* "format roman numerals"
282 '("i" "ii" "iii" "iv" "v" "vi" "vii" "viii" "ix" "x")
283 (map (^i (format "~@r" i)) (iota 10 1)))
284 (test* "format roman numerals"
285 '("I" "II" "III" "IIII" "V" "VI" "VII" "VIII" "VIIII" "X")
286 (map (^i (format "~:@R" i)) (iota 10 1)))
287 (test* "format roman numerals"
288 '("X" "XX" "XXX" "XL" "L" "LX" "LXX" "LXXX" "XC" "C")
289 (map (^i (format "~@R" i)) (iota 10 10 10)))
290 (test* "format roman numerals"
291 '("C" "CC" "CCC" "CD" "D" "DC" "DCC" "DCCC" "CM" "M")
292 (map (^i (format "~@R" i)) (iota 10 100 100)))
293
294 (let1 data '((39 "XXXIX")
295 (246 "CCXLVI")
296 (789 "DCCLXXXIX")
297 (2421 "MMCDXXI")
298 (160 "CLX")
299 (207 "CCVII")
300 (1009 "MIX")
301 (1066 "MLXVI")
302 (1776 "MDCCLXXVI")
303 (1918 "MCMXVIII")
304 (2014 "MMXIV"))
305 (dolist [d data] (test* "format roman numeral" (cadr d)
306 (format "~@R" (car d)))))
307
308 (test* "format conditionals (case)"
309 '("It is a cat."
310 "It is a Siamese cat."
311 "It is a Manx cat."
312 "It is a Persian cat."
313 "It is a cat.")
314 (map (cut format "It is a ~[Siamese ~;Manx ~;Persian ~]cat." <>)
315 '(-1 0 1 2 3)))
316 (test* "format conditionals (case, default)"
317 '("It is a Calico cat."
318 "It is a Siamese cat."
319 "It is a Manx cat."
320 "It is a Persian cat."
321 "It is a Calico cat.")
322 (map (cut format "It is a ~[Siamese ~;Manx ~;Persian ~:;Calico ~]cat." <>)
323 '(-1 0 1 2 3)))
324 (test* "format conditionals (case, error)"
325 (test-error <error> #/Argument for ~\[ must/)
326 (format "It is a ~[Siamese ~;Manx ~;Persian ~]cat." 'a))
327
328 (test* "format conditionals (alterative)"
329 '("Green Lamp" "Blue \"Lamp\"")
330 (map (cut format "~:[Green ~a~;Blue ~s~]" <> "Lamp") '(#f #t)))
331
332 (test* "format conditionals (maybe)"
333 '("Don't worry." "It's OK. Don't worry.")
334 (map (cut format "~@[It's ~a. ~]~a." <> "Don't worry") '(#f "OK")))
335
336 ;; Ensure `format` does _not_ cache in this case.
337 (test* "formatter cache"
338 '("a" "~s")
339 (map (cut format <> "~s" 'a) '(#f "~0@*~a")))
340
341 ;;---------------------------------------------------------------
342 (test-section "read/ss basic")
343
344 ;; NB: in gauche, read/ss is just an alias of read.
345 (test* "scalar (harmless)" 0
346 (read-from-string "#0=0"))
347 (test* "scalar (harmless)" 1
348 (read-from-string "#1=1"))
349 (test* "scalar (harmless)" 2
350 (read-from-string "#0=#1=2"))
351 (test* "scalar (harmless)" #f
352 (read-from-string "#1=#10=#100=#f"))
353 (test* "scalar (harmless)" "aaa"
354 (read-from-string "#1=#0=\"aaa\""))
355
356 (test* "bad syntax" (test-error)
357 (read-from-string "#1"))
358 (test* "bad syntax" (test-error)
359 (read-from-string "#3#"))
360 (test* "bad syntax" (test-error)
361 (read-from-string "#99999999999999999999999999999999999=3"))
362 (test* "bad syntax" (test-error)
363 (read-from-string "#99999999999999999999999999999999999#"))
364
365 (test* "pair 1" (circular-list 1 2)
366 (read-from-string "#0=(1 2 . #0#)")
367 isomorphic?)
368 (test* "pair 2" (let1 r (list #f) (set! (car r) r) r)
369 (read-from-string "#0=(#0#)")
370 isomorphic?)
371 (test* "pair 3" (let1 r '(a b) (list r r r))
372 (read-from-string "(#0=#1=(a b) #0# #1#)")
373 isomorphic?)
374
375 (test* "vector" (let* ((r (vector 'a 'b))
376 (s (vector 'c 'd))
377 (t (vector r s r s 'e)))
378 (vector-set! r 1 s)
379 (vector-set! s 1 r)
380 (vector-set! t 4 t)
381 t)
382 (read-from-string "#0=#(#1=#(a #2=#(c #1#)) #2# #1# #2# #0#)")
383 isomorphic?)
384
385 (test* "string" (let* ((r (string #\a #\a))
386 (s (string #\a #\a)))
387 (list r s r s))
388 (read-from-string "(#0=\"aa\" #1=\"aa\" #0# #1#)")
389 isomorphic?)
390
391 (test-section "whitespaces")
392
393 (test* "skipws" 'a
394 (read-from-string
395 "\u00a0\u1680\u2000\u200a\u2028\u2029\u202f\u205f\u3000a"))
396
397 ;;===============================================================
398 ;; Interference between SRFI-10 and shared structure
399 ;;
400
401 (test-section "combine SRFI-10 and SRFI-38")
402
403 ;; NB: this is an experimental feature. Do not count on this API!
404 (define-reader-ctor 'foo
405 (lambda x `(quote ,x))
406 (lambda (obj)
407 (pair-for-each (lambda (p)
408 (when (read-reference? (car p))
409 (set-car! p (read-reference-value (car p)))))
410 (cadr obj))))
411
412 (test* "user-defined" '#0='(a #0#)
413 (read-from-string "#0=#,(foo a #0#)")
414 isomorphic?)
415
416
417 ;;===============================================================
418 ;; Read lexical mode
419 ;;
420
421 (test-section "read lexical mode")
422
423 (define (test-reader-lexical-mode mode input expect)
424 (test* (format "reader lexical mode ~s: ~s" mode input) expect
425 (let1 old-mode #f
426 (dynamic-wind
427 (^[] (set! old-mode (reader-lexical-mode mode)))
428 (^[] (guard (e [else 'error])
429 (let* ([warn-port (open-output-string)]
430 [r (with-error-to-port warn-port
431 (cut read-from-string input))]
432 [w (rxmatch-case (get-output-string warn-port)
433 [#/^WARNING/ () 'warn]
434 [else #f])])
435 (if w (list r w) r))))
436 (^[] (reader-lexical-mode old-mode))))))
437
438 ;; data ::= ((input expect ...) ...)
439 (define (test-reader-lexical-modes data)
440 (dolist [d data]
441 (for-each (^[m e] (test-reader-lexical-mode m (car d) e))
442 '(legacy permissive warn-legacy strict-r7)
443 (cdr d))))
444
445 (test-reader-lexical-modes
446 '(("\"a\\x30zz\"" "a0zz" "a0zz" ("a0zz" warn) error)))
447 (test-reader-lexical-modes
448 '(("\"a\\x30;zz\"" "a0;zz" "a0zz" "a0zz" "a0zz")))
449 (test-reader-lexical-modes
450 '(("\"a\\x0030zz\"" "a\030zz" "a\030zz" ("a\030zz" warn) error)))
451 (test-reader-lexical-modes
452 '(("\"a\\x0030;zz\"" "a\030;zz" "a0zz" "a0zz" "a0zz")))
453
454 ;; Load and reader-lexical-mode
455 (sys-unlink "test.o")
456 (sys-unlink "test1.o")
457
458 (with-output-to-file "test.o"
459 (lambda ()
460 (write '(reader-lexical-mode 'legacy))))
461
462 (test* "load restores read lexical mode" #t
463 (let1 x (reader-lexical-mode)
464 (load "./test.o")
465 (eq? (reader-lexical-mode) x)))
466
467 (define (test-reader-lexical-mode-directive directive literal)
468 (with-output-to-file "test.o"
469 (lambda ()
470 (display directive)
471 (display "\n")
472 (display "(with-output-to-file \"test1.o\" (lambda () (display ")
473 (display literal)
474 (display ")))")))
475 (load "./test.o")
476 (with-input-from-file "test1.o" (cut read-line)))
477
478 (test* "#!gauche-legacy directive" "0;z"
479 (test-reader-lexical-mode-directive "#!gauche-legacy" "\"\\x30;z\""))
480 (test* "#!r7rs directive" "0z"
481 (test-reader-lexical-mode-directive "#!r7rs" "\"\\x30;z\""))
482 (test* "#!r7rs directive" (test-error <read-error>)
483 (test-reader-lexical-mode-directive "#!r7rs" "\"\\x30\""))
484 (test* "#!r7rs directive effect remains local to the port"
485 '("0z" "0z")
486 (let* ([a (read-from-string "#!r7rs \"\\x30;z\"")]
487 [b (read-from-string "\"\\x30z\"")])
488 (list a b)))
489
490 (sys-unlink "test.o")
491 (sys-unlink "test1.o")
492
493 ;;===============================================================
494 ;; Write parameters
495 ;;
496
497 (use gauche.uvector)
498
499 (let* ([data (iota 5)]
500 [data2 (make-list 5 data)])
501 (define (write-to-string/ctx obj . args)
502 ($ write-to-string obj
503 (^x (write x (apply make-write-controls args)))))
504 (define elli (with-module gauche.internal (string-ellipsis)))
505
506 (test* "print-length"
507 `("(0 1 2 3 4)"
508 ("(0 1 2 3 4)" "#(0 1 2 3 4)" "#u8(0 1 2 3 4)")
509 (,#"(0 1 2 3 ~elli)" ,#"#(0 1 2 3 ~elli)" ,#"#u8(0 1 2 3 ~elli)")
510 (,#"(0 1 2 ~elli)" ,#"#(0 1 2 ~elli)" ,#"#u8(0 1 2 ~elli)")
511 ("(0 1 2 3 4)" "#(0 1 2 3 4)" "#u8(0 1 2 3 4)")
512 (,#"(0 1 ~elli)" ,#"#(0 1 ~elli)" ,#"#u8(0 1 ~elli)")
513 (,#"(0 ~elli)" ,#"#(0 ~elli)" ,#"#u8(0 ~elli)")
514 (,#"(~elli)" ,#"#(~elli)" ,#"#u8(~elli)"))
515 (let ([z (map (^n (list (write-to-string/ctx data :length n)
516 (write-to-string/ctx (list->vector data)
517 :length n)
518 (write-to-string/ctx (list->u8vector data)
519 :length n)))
520 '(5 4 3 #f 2 1 0))])
521 ;; make sure print-length doesn't affect global op
522 (cons (write-to-string data)
523 z)))
524
525 (test* "print-length for zero-length aggregate"
526 '("()" "#()" "#u8()")
527 (map (^x (write-to-string/ctx x :length 0))
528 '(() #() #u8())))
529
530 (test* "print-length (nested)"
531 `((,#"(~elli)"
532 ,#"#(~elli)")
533 (,#"((0 ~elli) ~elli)"
534 ,#"#(#(0 ~elli) ~elli)")
535 (,#"((0 1 ~elli) (0 1 ~elli) ~elli)"
536 ,#"#(#(0 1 ~elli) #(0 1 ~elli) ~elli)")
537 (,#"((0 1 2 ~elli) (0 1 2 ~elli) (0 1 2 ~elli) ~elli)"
538 ,#"#(#(0 1 2 ~elli) #(0 1 2 ~elli) #(0 1 2 ~elli) ~elli)"))
539 (map (^n (list (write-to-string/ctx data2 :length n)
540 (write-to-string/ctx
541 (list->vector (map list->vector data2))
542 :length n)))
543 (iota 4))))
544
545 ;; example from CLHS
546 (let* ([data '(1 (2 (3 (4 (5 (6))))))])
547 (test* "print-level"
548 '("#"
549 "(1 #)"
550 "(1 (2 #))"
551 "(1 (2 (3 #)))"
552 "(1 (2 (3 (4 #))))"
553 "(1 (2 (3 (4 (5 #)))))"
554 "(1 (2 (3 (4 (5 (6))))))"
555 "(1 (2 (3 (4 (5 (6))))))")
556 (map (^n (write-to-string data (^x (write x (current-output-port)
557 (make-write-controls
558 :level n)))))
559 (iota 8))))
560
561 (let* ([data '(a (b (c (d (e) (f) g) h) i) #(j (k #(l #(m) (n) o) p) q) r)])
562 (test* "print-level"
563 '("(a (b (c (d (e) (f) g) h) i) #(j (k #(l #(m) (n) o) p) q) r)"
564 "(a (b (c (d (e) (f) g) h) i) #(j (k #(l #(m) (n) o) p) q) r)"
565 "(a (b (c (d # # g) h) i) #(j (k #(l # # o) p) q) r)"
566 "(a (b (c # h) i) #(j (k # p) q) r)"
567 "(a (b # i) #(j # q) r)"
568 "(a # # r)"
569 "#")
570 (map (^n (write-to-string data (^x (write x (make-write-controls
571 :level n)
572 (current-output-port)))))
573 '(6 5 4 3 2 1 0))))
574
575 ;; another example from CLHS
576 (let* ([level-length '((0 1) (1 1) (1 2) (1 3) (1 4)
577 (2 1) (2 2) (2 3) (3 2) (3 3) (3 4))]
578 [data '(if (member x y) (+ (car x) 3) '(foo . #(a b c d "Baz")))])
579 (define elli (with-module gauche.internal (string-ellipsis)))
580 (test* "print-level & print-length"
581 `(,#"0 1 -- #"
582 ,#"1 1 -- (if ~elli)"
583 ,#"1 2 -- (if # ~elli)"
584 ,#"1 3 -- (if # # ~elli)"
585 ,#"1 4 -- (if # # #)"
586 ,#"2 1 -- (if ~elli)"
587 ,#"2 2 -- (if (member x ~elli) ~elli)"
588 ,#"2 3 -- (if (member x y) (+ # 3) ~elli)"
589 ,#"3 2 -- (if (member x ~elli) ~elli)"
590 ,#"3 3 -- (if (member x y) (+ (car x) 3) ~elli)"
591 ,#"3 4 -- (if (member x y) (+ (car x) 3) '(foo . #(a b c d ~elli)))")
592 (map (^z (let1 c (make-write-controls
593 :level (car z) :length (cadr z))
594 (format c "~d ~d -- ~s" (car z) (cadr z) data)))
595 level-length)))
596
597 ;; print-level and aggregate other than plain vector
598 (let ([data '(a #u8(1 2 3) (b #u16(1 2 3) #(c #u32(1 2 3))))])
599 (test* "print-level with uvector"
600 '("(a #u8(1 2 3) (b #u16(1 2 3) #(c #u32(1 2 3))))"
601 "(a #u8(1 2 3) (b #u16(1 2 3) #(c #)))"
602 "(a #u8(1 2 3) (b # #))"
603 "(a # #)"
604 "#")
605 (map (^n (write-to-string data
606 (^x (write x (current-output-port)
607 (make-write-controls
608 :level n)))))
609 '(4 3 2 1 0))))
610
611 ;; print-level and user-defined write method
612 (define-class <foo> ()
613 ((content :init-keyword :content)))
614 (define-method write-object ((obj <foo>) port)
615 (format port "#<foo ~s>" (~ obj'content)))
616 (define-class <bar> ()
617 ((content :init-keyword :content)))
618 (define-method write-object ((obj <bar>) port)
619 (display "#<bar " port)
620 (write (~ obj'content) port)
621 (display ">" port))
622
623 (let ([data (list '(1 (2 (3 4)))
624 (list 1
625 (make <bar> :content
626 (list 2 '(3 4))))
627 (list 1
628 (make <foo> :content
629 (list 2 '(3 4))))
630 (make <foo> :content (list '(2 (3 (4 5)))
631 (make <bar> :content
632 '(2 (3 (4 5)))))))])
633 (test* "print-level via write-object"
634 '("((1 (2 (3 4))) (1 #<bar (2 (3 4))>) (1 #<foo (2 (3 4))>) #<foo ((2 (3 (4 5))) #<bar (2 (3 (4 5)))>)>)"
635 "((1 (2 (3 4))) (1 #<bar (2 (3 4))>) (1 #<foo (2 (3 4))>) #<foo ((2 (3 #)) #<bar (2 (3 #))>)>)"
636 "((1 (2 #)) (1 #<bar (2 #)>) (1 #<foo (2 #)>) #<foo ((2 #) #<bar (2 #)>)>)"
637 "((1 #) (1 #<bar #>) (1 #<foo #>) #<foo (# #<bar #>)>)"
638 "(# # # #<foo #>)"
639 "#")
640 (map (^n (write-to-string data
641 (^x (write x (make-write-controls
642 :level n)))))
643 '(5 4 3 2 1 0))))
644
645 (define-class <baz> ()
646 ((content :init-keyword :content)))
647 (define *baz-log* #f)
648 (define-method write-object ((obj <baz>) port)
649 (display "#<baz " port)
650 ;; don't do this in real code; this is only for testing
651 (let1 wc (make-write-controls :base 16)
652 (write (~ obj'content) wc port)
653 (unless ((with-module gauche.internal %port-walking?) port)
654 (write (~ obj'content) wc *baz-log*)))
655 (display ">" port))
656
657 (let ([data (list 10 20 (make <baz> :content '(30 40 50)))])
658 (test* "recursive write, control overrides"
659 '("(101 202 #<baz (1010 1111 1212)>)"
660 "(1e 28 32)")
661 (let ([p1 (open-output-string)]
662 [p2 (open-output-string)])
663 (set! *baz-log* p2)
664 (write data p1 (make-write-controls :base 3))
665 (list (get-output-string p1)
666 (get-output-string p2)))))
667
668 ;;===============================================================
669 ;; peek-byte - read-char
670 ;;
671
672 (use gauche.charconv)
673
674 (let ()
675 ;; This test checks peek-byte won't disturb subsequent read-char
676 ;; even the input contains illegal byte sequence.
677 ;; related: https://github.com/shirok/Gauche/pull/177
678 (define (read1 p) (read-char p))
679 (define (read2 p) (let1 c (read-char p) `(,c ,(read-char p))))
680 (define (peek1 p) (peek-char p) (read1 p))
681 (define (peek2 p) (peek-char p) (read2 p))
682 (define (fetch s reader ces)
683 (call-with-input-string (ces-convert s #f ces) reader))
684
685 (define (t ces)
686 (test* #"peek-char - read-char (~ces)"
687 (fetch "\u3042" read1 ces) (fetch "\u3042" peek1 ces))
688 (test* #"peek-char - read-char^2 (~ces) case 1"
689 (fetch "\u3042" read2 ces) (fetch "\u3042" peek2 ces))
690 (test* #"peek-char - read-char^2 (~ces) case 2"
691 (fetch "\u3042\u3044" read2 ces)
692 (fetch "\u3042\u3044" peek2 ces)))
693
694 (for-each t '(utf8 sjis eucjp))
695 )
696
697 ;;===============================================================
698 ;; Pretty printer
699 ;;
700
701 (test-section "pretty printer")
702 (use gauche.pputil)
703 (test-module 'gauche.pputil)
704
705 (let ([data1 '(Lorem ipsum dolor sit amet consectetur adipisicing elit
706 sed do eiusmod tempor incididunt ut labore et dolore)]
707 [data2 '(Lorem (ipsum #(dolor (sit (amet . consectetur)))))]
708 )
709 (define (t name expect data . args)
710 (test* #"~|name| ~|args|" expect
711 (with-output-to-string (^[] (apply pprint data args)))))
712 (define elli (with-module gauche.internal (string-ellipsis)))
713 (let-syntax
714 ([t* (syntax-rules ()
715 [(_ (data . args) expect)
716 (t 'data expect data . args)])])
717
718 (t* (data1)
719 "(Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor\
720 \n incididunt ut labore et dolore)\n")
721 (t* (data1 :width #f)
722 "(Lorem ipsum dolor sit amet consectetur adipisicing elit sed do eiusmod tempor incididunt ut labore et dolore)\n")
723 (t* (data1 :width 40)
724 "(Lorem ipsum dolor sit amet consectetur\
725 \n adipisicing elit sed do eiusmod tempor\
726 \n incididunt ut labore et dolore)\n")
727 (t* (data1 :width 39)
728 "(Lorem ipsum dolor sit amet consectetur\
729 \n adipisicing elit sed do eiusmod tempor\
730 \n incididunt ut labore et dolore)\n")
731 (t* (data1 :width 38)
732 "(Lorem ipsum dolor sit amet\
733 \n consectetur adipisicing elit sed do\
734 \n eiusmod tempor incididunt ut labore\
735 \n et dolore)\n")
736 (t* (data1 :length 5)
737 #"(Lorem ipsum dolor sit amet ~elli)\n")
738 (t* (data1 :length 1)
739 #"(Lorem ~elli)\n")
740 (t* (data1 :length 0)
741 #"(~elli)\n")
742 (t* (data1 :level 1 :length 5)
743 #"(Lorem ipsum dolor sit amet ~elli)\n")
744 (t* (data1 :level 0 :length 5)
745 "#\n")
746 (t* ('a :level 0)
747 "a\n")
748
749 (t* (data2 :level 0)
750 "#\n")
751 (t* (data2 :level 1)
752 "(Lorem #)\n")
753 (t* (data2 :level 2)
754 "(Lorem (ipsum #))\n")
755 (t* (data2 :level 3)
756 "(Lorem (ipsum #(dolor #)))\n")
757 (t* (data2 :level 4)
758 "(Lorem (ipsum #(dolor (sit #))))\n")
759 (t* (data2 :level 5)
760 "(Lorem (ipsum #(dolor (sit (amet . consectetur)))))\n")
761 (t* (data2 :level 4 :width 30)
762 "(Lorem\
763 \n (ipsum #(dolor (sit #))))\n")
764 ))
765
766 (test* "no newline" "(a\n a)"
767 (call-with-output-string
768 (cut pprint '(a a) :width 3 :newline #f :port <>)))
769
770 (test* "label (issue #484)" "#0=(a #1=(b #0# #1#))\n"
771 (call-with-output-string
772 (cut pprint '#0=(a #1=(b #0# #1#)) :port <>)))
773 (test* "label (issue #713)" "#0=(1 . #1=((2 . #1#) 3 . #0#))\n"
774 (call-with-output-string
775 (cut pprint '#0=(1 . #1=((2 . #1#) 3 . #0#)) :port <>)))
776
777 (test* "shorthand notation" "('a `b ,c ,@d `(e ,f 'g ,@h))\n"
778 (call-with-output-string
779 (cut pprint '('a `b ,c ,@d `(e ,f 'g ,@h)) :port <>)))
780 (test* "shorthand notation" "'#0=(a . '#0#)\n"
781 (call-with-output-string
782 (cut pprint ''#0=(a . '#0#) :port <>)))
783 (test* "shorthand notation" "'#0=(a ,@#1='(#0# . ,#1#))\n"
784 (call-with-output-string
785 (cut pprint ''#0=(a ,@#1='(#0# . ,#1#)) :port <>)))
786 (test* "shorthand notation" "#0=(a (quote . #0#))\n"
787 (call-with-output-string
788 (cut pprint '#0=(a (quote . #0#)) :port <>)))
789 (test* "shorthand notation" "#0=(a quote . #0#)\n"
790 (call-with-output-string
791 (cut pprint '#0=(a quote . #0#) :port <>)))
792 (test* "shorthand notation" "'(,@(the quick brown fox\
793 \n jumps over the\
794 \n ,@lazy dog))\n"
795 (call-with-output-string
796 (cut pprint ''(,@(the quick brown fox jumps over the ,@lazy dog))
797 :port <> :width 25)))
798 (test* "shorthand notation" (test-none-of)
799 (call-with-output-string
800 (cut pprint `(,((with-module gauche.internal make-identifier)
801 'quote (current-module) '())
802 foo)
803 :port <>)))
804
805 ;;===============================================================
806 ;; utf-8 with BOM
807 ;;
808
809 (test* "utf-8 with BOM" "foo!"
810 (begin
811 (load "data/utf-8-bom.scm" :paths `(,(build-path 'cld)))
812 (foo)))
813
814 (test-end)