Debian Codegraph @

%3a166/test.scm

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