Debian Codegraph @

test/include/srfi-159-tests.scm

raw
1 ;(define-library (chibi show-test)
2 ; (export run-tests)
3 ; (import (scheme base) (scheme char) (scheme read) (scheme file)
4 ; (only (srfi 1) circular-list)
5 ; (chibi test)
6 ; (chibi show) (chibi show base) (chibi show color)
7 ; (chibi show column) (chibi show pretty)
8 ; (chibi show unicode))
9 ; (begin
10 (define-syntax test-pretty
11 (syntax-rules ()
12 ((test-pretty str)
13 (let ((sexp (read (open-input-string str))))
14 (test str (show #f (pretty sexp)))))))
15 ; (define (run-tests)
16 ; (test-begin "show")
17
18 ;; basic data types
19
20 (test "hi" (show #f "hi"))
21 (test "\"hi\"" (show #f (written "hi")))
22 (test "\"hi \\\"bob\\\"\"" (show #f (written "hi \"bob\"")))
23 (test "\"hello\\nworld\"" (show #f (written "hello\nworld")))
24 (test "#(1 2 3)" (show #f (written '#(1 2 3))))
25 (test "(1 2 3)" (show #f (written '(1 2 3))))
26 (test "(1 2 . 3)" (show #f (written '(1 2 . 3))))
27 #|
28 (test "ABC" (show #f (upcased "abc")))
29 (test "abc" (show #f (downcased "ABC")))
30 |#
31
32 (test "a b" (show #f "a" (space-to 5) "b"))
33 (test "ab" (show #f "a" (space-to 0) "b"))
34
35 (test "abc def" (show #f "abc" (tab-to) "def"))
36 (test "abc def" (show #f "abc" (tab-to 5) "def"))
37 (test "abcdef" (show #f "abc" (tab-to 3) "def"))
38 (test "abc\ndef\n" (show #f "abc" nl "def" nl))
39 (test "abc\ndef\n" (show #f "abc" fl "def" nl fl))
40 (test "abc\ndef\n" (show #f "abc" fl "def" fl fl))
41
42 (test "ab" (show #f "a" nothing "b"))
43
44 ;; numbers
45
46 (test "-1" (show #f -1))
47 (test "0" (show #f 0))
48 (test "1" (show #f 1))
49 (test "10" (show #f 10))
50 (test "100" (show #f 100))
51 (test "-1" (show #f (numeric -1)))
52 (test "0" (show #f (numeric 0)))
53 (test "1" (show #f (numeric 1)))
54 (test "10" (show #f (numeric 10)))
55 (test "100" (show #f (numeric 100)))
56 (test "57005" (show #f #xDEAD))
57 (test "#xdead" (show #f (with ((radix 16)) #xDEAD)))
58 (test "#xdead1234" (show #f (with ((radix 16)) #xDEAD) 1234))
59 (test "de.ad"
60 (show #f (with ((radix 16) (precision 2)) (numeric (/ #xDEAD #x100)))))
61 (test "d.ead"
62 (show #f (with ((radix 16) (precision 3)) (numeric (/ #xDEAD #x1000)))))
63 (test "0.dead"
64 (show #f (with ((radix 16) (precision 4)) (numeric (/ #xDEAD #x10000)))))
65 (test "1g"
66 (show #f (with ((radix 17)) (numeric 33))))
67
68 (test "3.14159" (show #f 3.14159))
69 (test "3.14" (show #f (with ((precision 2)) 3.14159)))
70 (test "3.14" (show #f (with ((precision 2)) 3.14)))
71 (test "3.00" (show #f (with ((precision 2)) 3.)))
72 (test "1.10" (show #f (with ((precision 2)) 1.099)))
73 (test "0.00" (show #f (with ((precision 2)) 1e-17)))
74 (test "0.0000000010" (show #f (with ((precision 10)) 1e-9)))
75 (test "0.0000000000" (show #f (with ((precision 10)) 1e-17)))
76 (test "0.000004" (show #f (with ((precision 6)) 0.000004)))
77 (test "0.0000040" (show #f (with ((precision 7)) 0.000004)))
78 (test "0.00000400" (show #f (with ((precision 8)) 0.000004)))
79 (test "1.00" (show #f (with ((precision 2)) .997554209949891)))
80 (test "1.00" (show #f (with ((precision 2)) .99755420)))
81 (test "1.00" (show #f (with ((precision 2)) .99755)))
82 (test "1.00" (show #f (with ((precision 2)) .997)))
83 (test "0.99" (show #f (with ((precision 2)) .99)))
84 (test "-15" (show #f (with ((precision 0)) -14.99995999999362)))
85
86 (test " 3.14159" (show #f (with ((decimal-align 5)) (numeric 3.14159))))
87 (test " 31.4159" (show #f (with ((decimal-align 5)) (numeric 31.4159))))
88 (test " 314.159" (show #f (with ((decimal-align 5)) (numeric 314.159))))
89 (test "3141.59" (show #f (with ((decimal-align 5)) (numeric 3141.59))))
90 (test "31415.9" (show #f (with ((decimal-align 5)) (numeric 31415.9))))
91 (test " -3.14159" (show #f (with ((decimal-align 5)) (numeric -3.14159))))
92 (test " -31.4159" (show #f (with ((decimal-align 5)) (numeric -31.4159))))
93 (test "-314.159" (show #f (with ((decimal-align 5)) (numeric -314.159))))
94 (test "-3141.59" (show #f (with ((decimal-align 5)) (numeric -3141.59))))
95 (test "-31415.9" (show #f (with ((decimal-align 5)) (numeric -31415.9))))
96
97 (test "+inf.0" (show #f +inf.0))
98 (test "-inf.0" (show #f -inf.0))
99 (test "+nan.0" (show #f +nan.0))
100 (test "+inf.0" (show #f (numeric +inf.0)))
101 (test "-inf.0" (show #f (numeric -inf.0)))
102 (test "+nan.0" (show #f (numeric +nan.0)))
103
104 (cond
105 ((exact? (/ 1 3)) ;; exact rationals
106 (test "333.333333333333333333333333333333"
107 (show #f (with ((precision 30)) (numeric 1000/3))))
108 (test "33.333333333333333333333333333333"
109 (show #f (with ((precision 30)) (numeric 100/3))))
110 (test "3.333333333333333333333333333333"
111 (show #f (with ((precision 30)) (numeric 10/3))))
112 (test "0.333333333333333333333333333333"
113 (show #f (with ((precision 30)) (numeric 1/3))))
114 (test "0.033333333333333333333333333333"
115 (show #f (with ((precision 30)) (numeric 1/30))))
116 (test "0.003333333333333333333333333333"
117 (show #f (with ((precision 30)) (numeric 1/300))))
118 (test "0.000333333333333333333333333333"
119 (show #f (with ((precision 30)) (numeric 1/3000))))
120 (test "0.666666666666666666666666666667"
121 (show #f (with ((precision 30)) (numeric 2/3))))
122 (test "0.090909090909090909090909090909"
123 (show #f (with ((precision 30)) (numeric 1/11))))
124 (test "1.428571428571428571428571428571"
125 (show #f (with ((precision 30)) (numeric 10/7))))
126 (test "0.123456789012345678901234567890"
127 (show #f (with ((precision 30))
128 (numeric (/ 123456789012345678901234567890
129 1000000000000000000000000000000)))))
130 (test " 333.333333333333333333333333333333"
131 (show #f (with ((precision 30) (decimal-align 5)) (numeric 1000/3))))
132 (test " 33.333333333333333333333333333333"
133 (show #f (with ((precision 30) (decimal-align 5)) (numeric 100/3))))
134 (test " 3.333333333333333333333333333333"
135 (show #f (with ((precision 30) (decimal-align 5)) (numeric 10/3))))
136 (test " 0.333333333333333333333333333333"
137 (show #f (with ((precision 30) (decimal-align 5)) (numeric 1/3))))
138 ))
139
140 (test "11.75" (show #f (with ((precision 2)) (/ 47 4))))
141 (test "-11.75" (show #f (with ((precision 2)) (/ -47 4))))
142
143 (test "(#x11 #x22 #x33)" (show #f (with ((radix 16)) '(#x11 #x22 #x33))))
144
145 (test "299792458" (show #f (with ((comma-rule 3)) 299792458)))
146 (test "299,792,458" (show #f (with ((comma-rule 3)) (numeric 299792458))))
147 (test "-29,97,92,458"
148 (show #f (with ((comma-rule '(3 2))) (numeric -299792458))))
149 (test "299.792.458"
150 (show #f (with ((comma-rule 3) (comma-sep #\.)) (numeric 299792458))))
151 (test "299.792.458,0"
152 (show #f (with ((comma-rule 3) (decimal-sep #\,)) (numeric 299792458.0))))
153
154 (test "100,000" (show #f (with ((comma-rule 3)) (numeric 100000))))
155 (test "100,000.0"
156 (show #f (with ((comma-rule 3) (precision 1)) (numeric 100000))))
157 (test "100,000.00"
158 (show #f (with ((comma-rule 3) (precision 2)) (numeric 100000))))
159
160 ;; radix argument:
161 (test "0" (show #f (numeric 0 2)))
162 (test "0" (show #f (numeric 0 10)))
163 (test "0" (show #f (numeric 0 36)))
164
165 (test "0.0" (show #f (numeric 0.0 2)))
166 (test "0.0" (show #f (numeric 0.0 10)))
167 (test "0.0" (show #f (numeric 0.0 36)))
168
169 (test "1" (show #f (numeric 1 2)))
170 (test "1" (show #f (numeric 1 10)))
171 (test "1" (show #f (numeric 1 36)))
172
173 (test "1.0" (show #f (numeric 1.0 2)))
174 (test "1.0" (show #f (numeric 1.0 10)))
175 (test "1.0" (show #f (numeric 1.0 36)))
176
177 (test "0" (show #f (numeric 0.0 10 0)))
178 (test "0" (show #f (numeric 0.0 9 0)))
179 (test "3/4" (show #f (numeric #e.75)))
180
181 (test "0.0000000000000001" (show #f (numeric 1e-25 36)))
182 (test "100000000000000000000000000000000000000000000000000000000000000000000000000000000.0"
183 (show #f (numeric (expt 2.0 80) 2)))
184
185 ;; numeric, radix=2
186 (test "10" (show #f (numeric 2 2)))
187 (test "10.0" (show #f (numeric 2.0 2)))
188 (test "11/10" (show #f (numeric 3/2 2)))
189 (test "1001" (show #f (numeric 9 2)))
190 (test "1001.0" (show #f (numeric 9.0 2)))
191 (test "1001.01" (show #f (numeric 9.25 2)))
192
193 ;; numeric, radix=3
194 (test "11" (show #f (numeric 4 3)))
195 (test "10.0" (show #f (numeric 3.0 3)))
196 (test "11/10" (show #f (numeric 4/3 3)))
197 (test "1001" (show #f (numeric 28 3)))
198 (test "1001.0" (show #f (numeric 28.0 3)))
199 (test "1001.01" (show #f (numeric #i253/9 3 2)))
200
201 ;; radix 36
202 (test "zzz" (show #f (numeric (- (* 36 36 36) 1) 36)))
203
204 ;; Precision:
205 (test "1.1250" (show #f (numeric 9/8 10 4)))
206 (test "1.125" (show #f (numeric 9/8 10 3)))
207 (test "1.12" (show #f (numeric 9/8 10 2)))
208 (test "1.1" (show #f (numeric 9/8 10 1)))
209 (test "1" (show #f (numeric 9/8 10 0)))
210
211 (test "1.1250" (show #f (numeric #i9/8 10 4)))
212 (test "1.125" (show #f (numeric #i9/8 10 3)))
213 (test "1.12" (show #f (numeric #i9/8 10 2)))
214 (test "1.1" (show #f (numeric #i9/8 10 1)))
215 (test "1" (show #f (numeric #i9/8 10 0)))
216
217 ;; precision-show, base-4
218 (test "1.1230" (show #f (numeric 91/64 4 4)))
219 (test "1.123" (show #f (numeric 91/64 4 3)))
220 (test "1.13" (show #f (numeric 91/64 4 2)))
221 (test "1.2" (show #f (numeric 91/64 4 1)))
222 (test "1" (show #f (numeric 91/64 4 0)))
223
224 (test "1.1230" (show #f (numeric #i91/64 4 4)))
225 (test "1.123" (show #f (numeric #i91/64 4 3)))
226 (test "1.13" (show #f (numeric #i91/64 4 2)))
227 (test "1.2" (show #f (numeric #i91/64 4 1)))
228 (test "1" (show #f (numeric #i91/64 4 0)))
229
230 ;; sign
231 (test "+1" (show #f (numeric 1 10 #f #t)))
232 (test "+1" (show #f (with ((sign-rule #t)) (numeric 1))))
233 (test "(1)" (show #f (with ((sign-rule '("(" . ")"))) (numeric -1))))
234 (test "-1" (show #f (with ((sign-rule '("-" . ""))) (numeric -1))))
235 (test "−1" (show #f (with ((sign-rule '("−" . ""))) (numeric -1))))
236 (test "-0.0" (show #f (with ((sign-rule #t)) (numeric -0.0))))
237 (test "+0.0" (show #f (with ((sign-rule #t)) (numeric +0.0))))
238
239 ;; comma
240 (test "1,234,567" (show #f (numeric 1234567 10 #f #f 3)))
241 (test "567" (show #f (numeric 567 10 #f #f 3)))
242 (test "1,23,45,67" (show #f (numeric 1234567 10 #f #f 2)))
243 (test "12,34,567" (show #f (numeric 1234567 10 #f #f '(3 2))))
244
245 ;; comma-sep
246 (test "1|234|567" (show #f (numeric 1234567 10 #f #f 3 #\|)))
247 (test "1&234&567" (show #f (with ((comma-sep #\&)) (numeric 1234567 10 #f #f 3))))
248 (test "1*234*567" (show #f (with ((comma-sep #\&)) (numeric 1234567 10 #f #f 3 #\*))))
249 (test "567" (show #f (numeric 567 10 #f #f 3 #\|)))
250 (test "1,23,45,67" (show #f (numeric 1234567 10 #f #f 2)))
251
252 ;; decimal
253 (test "1_5" (show #f (with ((decimal-sep #\_)) (numeric 1.5))))
254 (test "1,5" (show #f (with ((comma-sep #\.)) (numeric 1.5))))
255 (test "1,5" (show #f (numeric 1.5 10 #f #f #f #\.)))
256 (test "1%5" (show #f (numeric 1.5 10 #f #f #f #\. #\%)))
257
258 ;(test "1+2i" (show #f (string->number "1+2i")))
259 (test "1.0+2.0i" (show #f (string->number "1+2i")))
260 (test "1.00+2.00i"
261 (show #f (with ((precision 2)) (string->number "1+2i"))))
262 (test "3.14+2.00i"
263 (show #f (with ((precision 2)) (string->number "3.14159+2i"))))
264
265 (test "608" (show #f (numeric/si 608)))
266 (test "608 B" (show #f (numeric/si 608 1000 " ") "B"))
267 (test "3.9Ki" (show #f (numeric/si 3986)))
268 (test "4kB" (show #f (numeric/si 3986 1000) "B"))
269 (test "1.2Mm" (show #f (numeric/si 1.23e6 1000) "m"))
270 (test "123km" (show #f (numeric/si 1.23e5 1000) "m"))
271 (test "12.3km" (show #f (numeric/si 1.23e4 1000) "m"))
272 (test "1.2km" (show #f (numeric/si 1.23e3 1000) "m"))
273 (test "123m" (show #f (numeric/si 1.23e2 1000) "m"))
274 (test "12.3m" (show #f (numeric/si 1.23e1 1000) "m"))
275 (test "1.2m" (show #f (numeric/si 1.23 1000) "m"))
276 (test "1.2 m" (show #f (numeric/si 1.23 1000 " ") "m"))
277 (test "123mm" (show #f (numeric/si 0.123 1000) "m"))
278 (test "12.3mm" (show #f (numeric/si 1.23e-2 1000) "m")) ;?
279 (test "1.2mm" (show #f (numeric/si 1.23e-3 1000) "m"))
280 (test "123µm" (show #f (numeric/si 1.23e-4 1000) "m")) ;?
281 (test "12.3µm" (show #f (numeric/si 1.23e-5 1000) "m")) ;?
282 (test "1.2µm" (show #f (numeric/si 1.23e-6 1000) "m"))
283 (test "1.2 µm" (show #f (numeric/si 1.23e-6 1000 " ") "m"))
284
285 (test "1,234,567" (show #f (numeric/comma 1234567)))
286
287 (test "1.23" (show #f (numeric/fitted 4 1.2345 10 2)))
288 (test "1.00" (show #f (numeric/fitted 4 1 10 2)))
289 (test "#.##" (show #f (numeric/fitted 4 12.345 10 2)))
290 (test "#" (show #f (numeric/fitted 1 12.345 10 0)))
291
292 ;; padding/trimming
293
294 (test "abc " (show #f (padded/right 5 "abc")))
295 (test " abc" (show #f (padded 5 "abc")))
296 (test "abcdefghi" (show #f (padded 5 "abcdefghi")))
297 (test " abc " (show #f (padded/both 5 "abc")))
298 (test " abc " (show #f (padded/both 6 "abc")))
299 (test "abcde" (show #f (padded/right 5 "abcde")))
300 (test "abcdef" (show #f (padded/right 5 "abcdef")))
301
302 (test "abc" (show #f (trimmed/right 3 "abcde")))
303 (test "abc" (show #f (trimmed/right 3 "abcd")))
304 (test "abc" (show #f (trimmed/right 3 "abc")))
305 (test "ab" (show #f (trimmed/right 3 "ab")))
306 (test "a" (show #f (trimmed/right 3 "a")))
307 (test "cde" (show #f (trimmed 3 "abcde")))
308 (test "bcd" (show #f (trimmed/both 3 "abcde")))
309 (test "bcdef" (show #f (trimmed/both 5 "abcdefgh")))
310 (test "abc" (show #f (trimmed/lazy 3 "abcde")))
311 (test "abc" (show #f (trimmed/lazy 3 "abc\nde")))
312
313 (test "prefix: abc" (show #f "prefix: " (trimmed/right 3 "abcde")))
314 (test "prefix: cde" (show #f "prefix: " (trimmed 3 "abcde")))
315 (test "prefix: bcd" (show #f "prefix: " (trimmed/both 3 "abcde")))
316 (test "prefix: abc" (show #f "prefix: " (trimmed/lazy 3 "abcde")))
317 (test "prefix: abc" (show #f "prefix: " (trimmed/lazy 3 "abc\nde")))
318
319 (test "abc :suffix" (show #f (trimmed/right 3 "abcde") " :suffix"))
320 (test "cde :suffix" (show #f (trimmed 3 "abcde") " :suffix"))
321 (test "bcd :suffix" (show #f (trimmed/both 3 "abcde") " :suffix"))
322 (test "abc :suffix" (show #f (trimmed/lazy 3 "abcde") " :suffix"))
323 (test "abc :suffix" (show #f (trimmed/lazy 3 "abc\nde") " :suffix"))
324
325 (test "abc" (show #f (trimmed/lazy 10 (trimmed/lazy 3 "abcdefghijklmnopqrstuvwxyz"))))
326 (test "abc" (show #f (trimmed/lazy 3 (trimmed/lazy 10 "abcdefghijklmnopqrstuvwxyz"))))
327
328 (test "abcde"
329 (show #f (with ((ellipsis "...")) (trimmed/right 5 "abcde"))))
330 (test "ab..."
331 (show #f (with ((ellipsis "...")) (trimmed/right 5 "abcdef"))))
332 (test "abc..."
333 (show #f (with ((ellipsis "...")) (trimmed/right 6 "abcdefg"))))
334 (test "abcde"
335 (show #f (with ((ellipsis "...")) (trimmed 5 "abcde"))))
336 (test "...ef"
337 (show #f (with ((ellipsis "...")) (trimmed 5 "abcdef"))))
338 (test "...efg"
339 (show #f (with ((ellipsis "...")) (trimmed 6 "abcdefg"))))
340 (test "abcdefg"
341 (show #f (with ((ellipsis "...")) (trimmed/both 7 "abcdefg"))))
342 (test "...d..."
343 (show #f (with ((ellipsis "...")) (trimmed/both 7 "abcdefgh"))))
344 (test "...e..."
345 (show #f (with ((ellipsis "...")) (trimmed/both 7 "abcdefghi"))))
346
347 (test "abc " (show #f (fitted/right 5 "abc")))
348 (test " abc" (show #f (fitted 5 "abc")))
349 (test " abc " (show #f (fitted/both 5 "abc")))
350 (test "abcde" (show #f (fitted/right 5 "abcde")))
351 (test "abcde" (show #f (fitted 5 "abcde")))
352 (test "abcde" (show #f (fitted/both 5 "abcde")))
353 (test "abcde" (show #f (fitted/right 5 "abcdefgh")))
354 (test "defgh" (show #f (fitted 5 "abcdefgh")))
355 (test "bcdef" (show #f (fitted/both 5 "abcdefgh")))
356
357 (test "prefix: abc :suffix"
358 (show #f "prefix: " (fitted/right 5 "abc") " :suffix"))
359 (test "prefix: abc :suffix"
360 (show #f "prefix: " (fitted 5 "abc") " :suffix"))
361 (test "prefix: abc :suffix"
362 (show #f "prefix: " (fitted/both 5 "abc") " :suffix"))
363 (test "prefix: abcde :suffix"
364 (show #f "prefix: " (fitted/right 5 "abcde") " :suffix"))
365 (test "prefix: abcde :suffix"
366 (show #f "prefix: " (fitted 5 "abcde") " :suffix"))
367 (test "prefix: abcde :suffix"
368 (show #f "prefix: " (fitted/both 5 "abcde") " :suffix"))
369 (test "prefix: abcde :suffix"
370 (show #f "prefix: " (fitted/right 5 "abcdefgh") " :suffix"))
371 (test "prefix: defgh :suffix"
372 (show #f "prefix: " (fitted 5 "abcdefgh") " :suffix"))
373 (test "prefix: bcdef :suffix"
374 (show #f "prefix: " (fitted/both 5 "abcdefgh") " :suffix"))
375
376 ;; joining
377
378 (test "1 2 3" (show #f (joined each '(1 2 3) " ")))
379
380 (test ":abc:123"
381 (show #f (joined/prefix
382 (lambda (x) (trimmed/right 3 x))
383 '("abcdef" "123456")
384 ":")))
385
386 (test "abc\n123\n"
387 (show #f (joined/suffix
388 (lambda (x) (trimmed/right 3 x))
389 '("abcdef" "123456")
390 nl)))
391
392 (test "lions, tigers, and bears"
393 (show #f (joined/last
394 each
395 (lambda (x) (each "and " x))
396 '(lions tigers bears)
397 ", ")))
398
399 (test "lions, tigers, or bears"
400 (show #f (joined/dot
401 each
402 (lambda (x) (each "or " x))
403 '(lions tigers . bears)
404 ", ")))
405
406 ;; escaping
407
408 (test "hi, bob!" (show #f (escaped "hi, bob!")))
409 (test "hi, \\\"bob!\\\"" (show #f (escaped "hi, \"bob!\"")))
410 (test "hi, \\'bob\\'" (show #f (escaped "hi, 'bob'" #\')))
411 (test "hi, ''bob''" (show #f (escaped "hi, 'bob'" #\' #\')))
412 (test "hi, ''bob''" (show #f (escaped "hi, 'bob'" #\' #f)))
413 (test "line1\\nline2\\nkapow\\a\\n"
414 (show #f (escaped "line1\nline2\nkapow\a\n"
415 #\" #\\
416 (lambda (c) (case c ((#\newline) #\n) ((#\alarm) #\a) (else #f))))))
417
418 (test "bob" (show #f (maybe-escaped "bob" char-whitespace?)))
419 (test "\"hi, bob!\""
420 (show #f (maybe-escaped "hi, bob!" char-whitespace?)))
421 (test "\"foo\\\"bar\\\"baz\"" (show #f (maybe-escaped "foo\"bar\"baz" char-whitespace?)))
422 (test "'hi, ''bob'''" (show #f (maybe-escaped "hi, 'bob'" (lambda (c) #f) #\' #f)))
423 (test "\\" (show #f (maybe-escaped "\\" (lambda (c) #f) #\' #f)))
424 (test "''''" (show #f (maybe-escaped "'" (lambda (c) #f) #\' #f)))
425
426 ;; shared structures
427
428 (test "#0=(1 . #0#)"
429 (show #f (written (let ((ones (list 1))) (set-cdr! ones ones) ones))))
430 (test "(0 . #0=(1 . #0#))"
431 (show #f (written (let ((ones (list 1)))
432 (set-cdr! ones ones)
433 (cons 0 ones)))))
434 (test "(sym . #0=(sym . #0#))"
435 (show #f (written (let ((syms (list 'sym)))
436 (set-cdr! syms syms)
437 (cons 'sym syms)))))
438 (test "(#0=(1 . #0#) #1=(2 . #1#))"
439 (show #f (written (let ((ones (list 1))
440 (twos (list 2)))
441 (set-cdr! ones ones)
442 (set-cdr! twos twos)
443 (list ones twos)))))
444 (test "(#0=(1 . #0#) #0#)"
445 (show #f (written (let ((ones (list 1)))
446 (set-cdr! ones ones)
447 (list ones ones)))))
448 (test "((1) (1))"
449 (show #f (written (let ((ones (list 1)))
450 (list ones ones)))))
451
452 #|
453 (test "(#0=(1) #0#)"
454 (show #f (written-shared (let ((ones (list 1)))
455 (list ones ones)))))
456 |#
457
458 ;; cycles without shared detection
459
460 (test "(1 1 1 1 1"
461 (show #f (trimmed/lazy
462 10
463 (written-simply
464 (let ((ones (list 1))) (set-cdr! ones ones) ones)))))
465
466 (test "(1 1 1 1 1 "
467 (show #f (trimmed/lazy
468 11
469 (written-simply
470 (let ((ones (list 1))) (set-cdr! ones ones) ones)))))
471
472 ;; pretty printing
473
474 (test-pretty "(foo bar)\n")
475
476 (test-pretty
477 "((self . aquanet-paper-1991)
478 (type . paper)
479 (title . \"Aquanet: a hypertext tool to hold your\"))
480 ")
481
482 (test-pretty
483 "(abracadabra xylophone
484 bananarama
485 yellowstonepark
486 cryptoanalysis
487 zebramania
488 delightful
489 wubbleflubbery)\n")
490
491 (test-pretty
492 "#(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
493 26 27 28 29 30 31 32 33 34 35 36 37)\n")
494
495 (test-pretty
496 "(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
497 26 27 28 29 30 31 32 33 34 35 36 37)\n")
498
499 (test-pretty
500 "(#(0 1) #(2 3) #(4 5) #(6 7) #(8 9) #(10 11) #(12 13) #(14 15)
501 #(16 17) #(18 19))\n")
502
503 (test-pretty
504 "(define (fold kons knil ls)
505 (define (loop ls acc)
506 (if (null? ls) acc (loop (cdr ls) (kons (car ls) acc))))
507 (loop ls knil))\n")
508
509 (test-pretty
510 "(do ((vec (make-vector 5)) (i 0 (+ i 1))) ((= i 5) vec) (vector-set! vec i i))\n")
511
512 (test-pretty
513 "(do ((vec (make-vector 5)) (i 0 (+ i 1))) ((= i 5) vec)
514 (vector-set! vec i 'supercalifrajalisticexpialidocious))\n")
515
516 (test-pretty
517 "(do ((my-vector (make-vector 5)) (index 0 (+ index 1)))
518 ((= index 5) my-vector)
519 (vector-set! my-vector index index))\n")
520
521 (test-pretty
522 "(define (fold kons knil ls)
523 (let loop ((ls ls) (acc knil))
524 (if (null? ls) acc (loop (cdr ls) (kons (car ls) acc)))))\n")
525
526 (test-pretty
527 "(define (file->sexp-list pathname)
528 (call-with-input-file pathname
529 (lambda (port)
530 (let loop ((res '()))
531 (let ((line (read port)))
532 (if (eof-object? line) (reverse res) (loop (cons line res))))))))\n")
533
534 (test-pretty
535 "(design
536 (module (name \"\\\\testshiftregister\") (attributes (attribute (name \"\\\\src\"))))
537 (wire (name \"\\\\shreg\") (attributes (attribute (name \"\\\\src\")))))\n")
538
539 (test-pretty
540 "(design
541 (module (name \"\\\\testshiftregister\")
542 (attributes
543 (attribute (name \"\\\\src\") (value \"testshiftregister.v:10\"))))
544 (wire (name \"\\\\shreg\")
545 (attributes
546 (attribute (name \"\\\\src\") (value \"testshiftregister.v:15\")))))\n")
547
548 (test "(let ((ones '#0=(1 . #0#))) ones)\n"
549 (show #f (pretty (let ((ones (list 1)))
550 (set-cdr! ones ones)
551 `(let ((ones ',ones)) ones)))))
552
553 '(test
554 "(let ((zeros '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
555 (ones '#0=(1 . #0#)))
556 (append zeros ones))\n"
557 (show #f (pretty
558 (let ((ones (list 1)))
559 (set-cdr! ones ones)
560 `(let ((zeros '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0))
561 (ones ',ones))
562 (append zeros ones))))))
563
564 ;; pretty-simply
565 (let* ((d (let ((d (list 'a 'b #f)))
566 (list-set! d 2 d)
567 (list d)))
568 (ca (circular-list 'a)))
569 (test "((a b (a b (a b" (show #f (trimmed/lazy 15 (pretty-simply '((a b (a b (a b (a b)))))))))
570 (test "((a b\n (a b\n" (show #f (trimmed/lazy 15 (pretty-simply d))))
571 (test "'(a a\n a\n " (show #f (trimmed/lazy 15 (pretty-simply `(quote ,ca)))))
572 (test "(foo\n (a a\n " (show #f (trimmed/lazy 15 (pretty-simply `(foo ,ca)))))
573 (test "(with-x \n (a a" (show #f (trimmed/lazy 15 (pretty-simply `(with-x ,ca)))))
574 )
575
576 ;; columns
577
578 (test "abc\ndef\n"
579 (show #f (show-columns (list displayed "abc\ndef\n"))))
580 (test "abc123\ndef456\n"
581 (show #f (show-columns (list displayed "abc\ndef\n")
582 (list displayed "123\n456\n"))))
583 (test "abc123\ndef456\n"
584 (show #f (show-columns (list displayed "abc\ndef\n")
585 (list displayed "123\n456"))))
586 (test "abc123\ndef456\n"
587 (show #f (show-columns (list displayed "abc\ndef")
588 (list displayed "123\n456\n"))))
589 (test "abc123\ndef456\nghi789\n"
590 (show #f (show-columns (list displayed "abc\ndef\nghi\n")
591 (list displayed "123\n456\n789\n"))))
592 (test "abc123wuv\ndef456xyz\n"
593 (show #f (show-columns (list displayed "abc\ndef\n")
594 (list displayed "123\n456\n")
595 (list displayed "wuv\nxyz\n"))))
596 (test "abc 123\ndef 456\n"
597 (show #f (show-columns (list (lambda (x) (padded/right 5 x))
598 "abc\ndef\n")
599 (list displayed "123\n456\n"))))
600 #|
601 (test "ABC 123\nDEF 456\n"
602 (show #f (show-columns (list (lambda (x) (upcased (padded/right 5 x)))
603 "abc\ndef\n")
604 (list displayed "123\n456\n"))))
605 (test "ABC 123\nDEF 456\n"
606 (show #f (show-columns (list (lambda (x) (padded/right 5 (upcased x)))
607 "abc\ndef\n")
608 (list displayed "123\n456\n"))))
609 |#
610
611 (test "hello\nworld\n"
612 (show #f (with ((width 8)) (wrapped "hello world"))))
613 (test "\n" (show #f (wrapped " ")))
614
615 (test
616 "The quick
617 brown fox
618 jumped
619 over the
620 lazy dog
621 "
622 (show #f
623 (with ((width 10))
624 (justified "The quick brown fox jumped over the lazy dog"))))
625
626 (test
627 "The fundamental list iterator.
628 Applies KONS to each element of
629 LS and the result of the previous
630 application, beginning with KNIL.
631 With KONS as CONS and KNIL as '(),
632 equivalent to REVERSE.
633 "
634 (show #f
635 (with ((width 36))
636 (wrapped "The fundamental list iterator. Applies KONS to each element of LS and the result of the previous application, beginning with KNIL. With KONS as CONS and KNIL as '(), equivalent to REVERSE."))))
637
638 #|
639 (test
640 "(define (fold kons knil ls)
641 (let lp ((ls ls) (acc knil))
642 (if (null? ls)
643 acc
644 (lp (cdr ls)
645 (kons (car ls) acc)))))
646 "
647 (show #f
648 (with ((width 36))
649 (pretty '(define (fold kons knil ls)
650 (let lp ((ls ls) (acc knil))
651 (if (null? ls)
652 acc
653 (lp (cdr ls)
654 (kons (car ls) acc)))))))))
655
656 (test
657 "(define (fold kons knil ls) ; The fundamental list iterator.
658 (let lp ((ls ls) (acc knil)) ; Applies KONS to each element of
659 (if (null? ls) ; LS and the result of the previous
660 acc ; application, beginning with KNIL.
661 (lp (cdr ls) ; With KONS as CONS and KNIL as '(),
662 (kons (car ls) acc))))) ; equivalent to REVERSE.
663 "
664 (show #f
665 (show-columns
666 (list
667 (lambda (x) (padded/right 36 x))
668 (with ((width 36))
669 (pretty '(define (fold kons knil ls)
670 (let lp ((ls ls) (acc knil))
671 (if (null? ls)
672 acc
673 (lp (cdr ls)
674 (kons (car ls) acc))))))))
675 (list
676 (lambda (x) (each " ; " x))
677 (with ((width 36))
678 (wrapped "The fundamental list iterator. Applies KONS to each element of LS and the result of the previous application, beginning with KNIL. With KONS as CONS and KNIL as '(), equivalent to REVERSE."))))))
679
680 (test
681 "(define (fold kons knil ls) ; The fundamental list iterator.
682 (let lp ((ls ls) (acc knil)) ; Applies KONS to each element of
683 (if (null? ls) ; LS and the result of the previous
684 acc ; application, beginning with KNIL.
685 (lp (cdr ls) ; With KONS as CONS and KNIL as '(),
686 (kons (car ls) acc))))) ; equivalent to REVERSE.
687 "
688 (show #f (with ((width 76))
689 (columnar
690 (pretty '(define (fold kons knil ls)
691 (let lp ((ls ls) (acc knil))
692 (if (null? ls)
693 acc
694 (lp (cdr ls)
695 (kons (car ls) acc))))))
696 " ; "
697 (wrapped "The fundamental list iterator. Applies KONS to each element of LS and the result of the previous application, beginning with KNIL. With KONS as CONS and KNIL as '(), equivalent to REVERSE.")))))
698 |#
699
700 (test
701 "- Item 1: The text here is
702 indented according
703 to the space \"Item
704 1\" takes, and one
705 does not known what
706 goes here.
707 "
708 (show #f (columnar 9 (each "- Item 1:") " " (with ((width 20)) (wrapped "The text here is indented according to the space \"Item 1\" takes, and one does not known what goes here.")))))
709
710 (test
711 "- Item 1: The text here is
712 indented according
713 to the space \"Item
714 1\" takes, and one
715 does not known what
716 goes here.
717 "
718 (show #f (columnar 9 (each "- Item 1:\n") " " (with ((width 20)) (wrapped "The text here is indented according to the space \"Item 1\" takes, and one does not known what goes here.")))))
719
720 (test
721 "- Item 1: The-text-here-is----------------------------------------------------
722 --------- indented-according--------------------------------------------------
723 --------- to-the-space-\"Item--------------------------------------------------
724 --------- 1\"-takes,-and-one---------------------------------------------------
725 --------- does-not-known-what-------------------------------------------------
726 --------- goes-here.----------------------------------------------------------
727 "
728 (show #f (with ((pad-char #\-)) (columnar 9 (each "- Item 1:\n") " " (with ((width 20)) (wrapped "The text here is indented according to the space \"Item 1\" takes, and one does not known what goes here."))))))
729
730 (test
731 "a | 123
732 bc | 45
733 def | 6
734 "
735 (show #f (with ((width 20))
736 (tabular (each "a\nbc\ndef\n") " | "
737 (each "123\n45\n6\n")))))
738
739 ;; color
740 (test "\x1B;[31mred\x1B;[0m" (show #f (as-red "red")))
741 (test "\x1B;[31mred\x1B;[34mblue\x1B;[31mred\x1B;[0m"
742 (show #f (as-red "red" (as-blue "blue") "red")))
743 (test "\x1b;[31m1234567\x1b;[0m col: 7"
744 (show #f (as-unicode (as-red "1234567") (fn ((col)) (each " col: " col)))))
745
746 ;; unicode
747 (test "〜日本語〜"
748 (show #f (with ((pad-char #\〜)) (padded/both 5 "日本語"))))
749 (test "日本語"
750 (show #f (as-unicode (with ((pad-char #\〜)) (padded/both 5 "日本語")))))
751 (test "日本語 col: 6"
752 (show #f (as-unicode "日本語" (fn ((col)) (each " col: " col)))))
753
754 ;; from-file
755 ;; for reference, filesystem-test relies on creating files under /tmp
756 (let* ((tmp-file "chibi-show-test-0123456789")
757 (content-string "first line\nsecond line\nthird line"))
758 (with-output-to-file tmp-file (lambda () (write-string content-string)))
759 (test (string-append content-string "\n")
760 (show #f (from-file tmp-file)))
761 (test
762 " 1 first line\n 2 second line\n 3 third line\n"
763 (show #f (columnar 4 'right 'infinite (line-numbers) " " (from-file tmp-file))))
764 (delete-file tmp-file))
765
766 ; (test-end))))