Debian Codegraph @

test/utf-8.scm

raw
1 ;; -*- coding: utf-8 -*-
2
3 (use gauche.test)
4
5 (test-start "multibyte (utf-8)")
6 (use scheme.list)
7
8 ;;-------------------------------------------------------------------
9 (test-section "string builtins")
10
11 (test "string" "いろhにほへt"
12 (lambda () (string #\い #\ろ #\h #\に #\ほ #\へ #\t)))
13 (test "list->string" "いろhにほへt"
14 (lambda () (list->string '(#\い #\ろ #\h #\に #\ほ #\へ #\t))))
15 (test "make-string" "へへへへへ" (lambda () (make-string 5 #\へ)))
16 (test "make-string" "" (lambda () (make-string 0 #\へ)))
17
18 (test "string->list" '(#\い #\ろ #\h #\に #\ほ #\へ #\t)
19 (lambda () (string->list "いろhにほへt")))
20 (test "string->list" '(#\ろ #\h #\に #\ほ #\へ #\t)
21 (lambda () (string->list "いろhにほへt" 1)))
22 (test "string->list" '(#\ろ #\h #\に)
23 (lambda () (string->list "いろhにほへt" 1 4)))
24
25 (test "string-copy" '("ぁゃνぃ" #f)
26 (lambda () (let* ((x "ぁゃνぃ") (y (string-copy x)))
27 (list y (eq? x y)))))
28 (test "string-copy" "ゃνぃ" (lambda () (string-copy "ぁゃνぃ" 1)))
29 (test "string-copy" "ゃν" (lambda () (string-copy "ぁゃνぃ" 1 3)))
30
31 (test "string-ref" #\ろ (lambda () (string-ref "いろは" 1)))
32 (define x (string-copy "いろはにほ"))
33 (test "string-set!" "いろZにほ" (lambda () (string-set! x 2 #\Z) x))
34
35 (test "string-fill!" "のののののの"
36 (lambda () (string-fill! (string-copy "000000") #\の)))
37 (test "string-fill!" "000ののの"
38 (lambda () (string-fill! (string-copy "000000") #\の 3)))
39 (test "string-fill!" "000のの0"
40 (lambda () (string-fill! (string-copy "000000") #\の 3 5)))
41
42 (test "string-join" "ふぅ ばぁ ばず"
43 (lambda () (string-join '("ふぅ" "ばぁ" "ばず"))))
44 (test "string-join" "ふぅ!ばぁ!ばず"
45 (lambda () (string-join '("ふぅ" "ばぁ" "ばず") "!")))
46 (test "string-join" "ふぅ→←ばぁ→←ばず"
47 (lambda () (string-join '("ふぅ" "ばぁ" "ばず") "→←" 'infix)))
48 (test "string-join" ""
49 (lambda () (string-join '() "→←")))
50 (test "string-join" "ふぅ!ばぁ!ばず!"
51 (lambda () (string-join '("ふぅ" "ばぁ" "ばず") "!" 'suffix)))
52 (test "string-join" "!ふぅ!ばぁ!ばず"
53 (lambda () (string-join '("ふぅ" "ばぁ" "ばず") "!" 'prefix)))
54 (test "string-join" "ふぅ!ばぁ!ばず"
55 (lambda () (string-join '("ふぅ" "ばぁ" "ばず") "!" 'strict-infix)))
56
57 (let ()
58 (define (test-string-scan out s1 s2 . opt)
59 (if (pair? out)
60 (begin
61 (test* "string-scan" (car out) (apply string-scan s1 s2 opt))
62 (test* "string-scan-right" (cadr out)
63 (apply string-scan-right s1 s2 opt)))
64 (apply test-string-scan (list out out) s1 s2 opt)))
65 (define (test-string-scan2 out1 out2 s1 s2 . opt)
66 (if (pair? out1)
67 (begin
68 (test* "string-scan" (list (car out1) (car out2))
69 (receive r (apply string-scan s1 s2 opt) r))
70 (test* "string-scan-right" (list (cadr out1) (cadr out2))
71 (receive r (apply string-scan-right s1 s2 opt) r)))
72 (apply test-string-scan2 (list out1 out1) (list out2 out2) s1 s2 opt)))
73
74 (test-string-scan '(7 20)
75 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい")
76 (test-string-scan '("あえいうえおあ" "あえいうえおあおあいうえおあえいうえおあ")
77 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい" 'before)
78 (test-string-scan '("うえおあえいうえおあおあいうえお" "うえお")
79 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい" 'after)
80 (test-string-scan2 '("あえいうえおあ" "あえいうえおあおあいうえおあえいうえおあ")
81 '("おあいうえおあえいうえおあおあいうえお" "おあいうえお")
82 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい" 'before*)
83 (test-string-scan2 '("あえいうえおあおあい" "あえいうえおあおあいうえおあえいうえおあおあい")
84 '("うえおあえいうえおあおあいうえお" "うえお")
85 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい" 'after*)
86 (test-string-scan2 '("あえいうえおあ" "あえいうえおあおあいうえおあえいうえおあ")
87 '("うえおあえいうえおあおあいうえお" "うえお")
88 "あえいうえおあおあいうえおあえいうえおあおあいうえお" "おあい" 'both)
89 (test-string-scan #f "あえいうえおあおあいうえお" "おい")
90 )
91
92 ;;-------------------------------------------------------------------
93 (test-section "string-cursor")
94
95 (define str "いろはにhoへと")
96 (define ss #f)
97 (define se #f)
98 (test* "string-cursor-start" #t
99 (begin
100 (set! ss (string-cursor-start str))
101 (string-cursor? ss)))
102 (test* "string-cursor-start" #t
103 (begin
104 (set! se (string-cursor-end str))
105 (string-cursor? se)))
106 (test* "string-cursor-next" #\ろ
107 (string-ref str (string-cursor-next str ss)))
108 (test* "string-cursor-forward" #\に
109 (string-ref str (string-cursor-forward str ss 3)))
110 (test* "string-cursor-prev" #\と
111 (string-ref str (string-cursor-prev str se)))
112 (test* "string-cursor-back" #\に
113 (string-ref str (string-cursor-back str se 5)))
114 (test* "string-index->cursor" #\へ
115 (string-ref str (string-index->cursor str 6)))
116 (test* "string-cursor->index" 0
117 (string-cursor->index str ss))
118 (test* "string-cursor->index" 8
119 (string-cursor->index str se))
120 (test* "substring with cursors" "はにho"
121 (substring str
122 (string-cursor-forward str ss 2)
123 (string-cursor-back str se 2)))
124 (test* "substring with cursors" "はにho"
125 (substring str
126 2
127 (string-cursor-back str se 2)))
128 (test* "substring with cursors" "はにh"
129 (substring str
130 (string-cursor-forward str ss 2)
131 5))
132 (test* "string->list" '(#\ろ #\は #\に #\h #\o #\へ)
133 (string->list str
134 (string-cursor-next str ss)
135 (string-cursor-prev str se)))
136 (test* "string->list" '(#\ろ #\は #\に #\h #\o #\へ)
137 (string->list str
138 1
139 (string-cursor-prev str se)))
140 (test* "string->list" '(#\ろ #\は #\に #\h #\o #\へ)
141 (string->list str
142 (string-cursor-next str ss)
143 7))
144 (test* "string-cursor=?" #t
145 (string-cursor=? ss (string-cursor-back str se 8)))
146 (test* "string-cursor=?" #t
147 (string-cursor=? 0 0))
148 (test* "string-cursor<?" #t
149 (string-cursor<? ss se))
150 (test* "string-cursor<=?" #t
151 (string-cursor<=? ss se))
152 (test* "string-cursor<=?" #t
153 (string-cursor<=? ss ss))
154 (test* "string-cursor>?" #t
155 (string-cursor>? se ss))
156 (test* "string-cursor>=?" #t
157 (string-cursor>=? se se))
158 (test* "string-cursor-diff" 8
159 (string-cursor-diff str ss se))
160 (test* "string-cursor-diff" -8
161 (string-cursor-diff str se ss))
162
163 ;;-------------------------------------------------------------------
164 (test-section "string index building")
165
166 (let* ([make-cycle (^[start-char count]
167 ($ circular-list
168 $* map ucs->char (iota count (char->ucs start-char))))]
169 [cycle1 (make-cycle #\a 26)]
170 [cycle2 (make-cycle #\α 25)]
171 [cycle3 (make-cycle #\あ 85)])
172 (define (make-str max-size)
173 (let loop ([size 0] [r '()]
174 [srcs (circular-list cycle1 cycle2 cycle3)]
175 [counts (circular-list 1 2 3)])
176 (let ([next-char (caar srcs)]
177 [next-count (car counts)])
178 (if (> (+ size next-count) max-size)
179 (list->string (reverse r))
180 (loop (+ size next-count)
181 (cons next-char r)
182 (circular-list (cadr srcs) (caddr srcs) (cdar srcs))
183 (cdr counts))))))
184 (define (test-string-index max-size)
185 (let1 s (string-build-index! (make-str max-size))
186 ;;((with-module gauche.internal %string-index-dump) s)
187 (test* #"string indexing (size=~max-size)"
188 #f
189 (any (^p (and (not (eqv? (car p) (string-ref s (cdr p))))
190 (list 'pos (cdr p)
191 'expect (car p)
192 'actual (string-ref s (cdr p)))))
193 (map cons (string->list s) (liota))))))
194 (test-string-index 0)
195 (test-string-index 15)
196 (test-string-index 33)
197 (test-string-index 34)
198 (test-string-index 49)
199 (test-string-index 63)
200 (test-string-index 64)
201 (test-string-index 65)
202 (test-string-index 255)
203 (test-string-index 256)
204 (test-string-index 257)
205 (test-string-index 8191)
206 (test-string-index 8193)
207 (test-string-index 65535)
208 (test-string-index 65537)
209 (test-string-index 131072)
210 (test-string-index 131073)
211 )
212
213 ;;-------------------------------------------------------------------
214 (test-section "incomplete strings")
215
216 (test "string-length" 9 (lambda () (string-length #*"あいう")))
217 (test "string-complete->incomplete" #*"あいう"
218 (lambda () (string-complete->incomplete "あいう")))
219 (test "string-complete->incomplete" #*"あいう"
220 (lambda () (string-complete->incomplete #*"あいう")))
221 (test "string-incomplete->complete" "あいう"
222 (lambda () (string-incomplete->complete #*"あいう")))
223 (test "string-incomplete->complete" "あいう"
224 (lambda () (string-incomplete->complete "あいう")))
225
226 (test "string-incomplete->complete (reject)" #f
227 (lambda () (string-incomplete->complete #*"あい\x80う" #f)))
228 (test "string-incomplete->complete (omit)" "あいう"
229 (lambda () (string-incomplete->complete #*"あい\x80う" :omit)))
230 (test "string-incomplete->complete (omit)" "あいう"
231 (lambda () (string-incomplete->complete #*"\x80あいう" :omit)))
232 (test "string-incomplete->complete (omit)" "あいう"
233 (lambda () (string-incomplete->complete #*"\x80\xe3あいう" :omit)))
234 (test "string-incomplete->complete (omit)" "あいう"
235 (lambda () (string-incomplete->complete #*"あいう\xe3" :omit)))
236 (test "string-incomplete->complete (omit)" "あいう"
237 (lambda () (string-incomplete->complete #*"あいう\xe3\xe3" :omit)))
238 (test "string-incomplete->complete (omit)" "あいう"
239 (lambda () (string-incomplete->complete #*"あいう\xe3\x80" :omit)))
240 (test "string-incomplete->complete (replace)" "あい?う"
241 (lambda () (string-incomplete->complete #*"あい\x80う" :replace)))
242 (test "string-incomplete->complete (replace)" "ふあいう"
243 (lambda () (string-incomplete->complete #*"\x80あいう" :replace #\ふ)))
244 (test "string-incomplete->complete (replace)" "ふふあいう"
245 (lambda () (string-incomplete->complete #*"\x80\xe3あいう" :replace #\ふ)))
246 (test "string-incomplete->complete (replace)" "あいうふ"
247 (lambda () (string-incomplete->complete #*"あいう\xe3" :replace #\ふ)))
248 (test "string-incomplete->complete (replace)" "あいうふふ"
249 (lambda () (string-incomplete->complete #*"あいう\xe3\xe3" :replace #\ふ)))
250 (test "string-incomplete->complete (replace)" "あいうふふ"
251 (lambda () (string-incomplete->complete #*"あいう\xe3\x80" :replace #\ふ)))
252 (test "string-incomplete->complete (escape)" "あ??い?91う?80え"
253 (lambda () (string-incomplete->complete #*"あ?い\x91う\x80え" :escape)))
254 (test "string-incomplete->complete (escape)" "あ__い_91う_80え"
255 (lambda () (string-incomplete->complete #*"あ_い\x91う\x80え" :escape #\_)))
256
257 (test "string=?" #t (lambda () (string=? #*"あいう" #*"あいう")))
258
259 (test "string-byte-ref" #x81 (lambda () (string-byte-ref #*"あいう" 1)))
260
261 (test "string-append" #*"あいうえお"
262 (lambda () (string-append "あいう" #*"えお")))
263 (test "string-append" #*"あいうえお"
264 (lambda () (string-append #*"あいう" "えお")))
265 (test "string-append" #*"あいうえお"
266 (lambda () (string-append #*"あいう" #*"えお")))
267 (test "string-append" 15
268 (lambda () (string-length (string-append "あいう" "えお" #*""))))
269
270 (test "string-incomplete->complete" "あ"
271 (lambda () (string-incomplete->complete
272 (string-append #*"\xe3" #*"\x81" #*"\x82"))))
273
274 ;;-------------------------------------------------------------------
275 (test-section "format")
276
277 (test "format" "あぶら"
278 (lambda () (format #f "~,,,,3a" "あぶらかだぶら")))
279 (test "format" "abら"
280 (lambda () (format #f "~,,,,3a" "abらかだぶら")))
281 (test "format" "あぶらかだぶら"
282 (lambda () (format #f "~,,,,7:a" "あぶらかだぶら")))
283 (test "format" "あぶらか"
284 (lambda () (format #f "~,,,,7:a" "あぶらか")))
285 (test "format" "あぶら ..."
286 (lambda () (format #f "~,,,,7:a" "あぶらかだぶらぶらぶら")))
287
288 ;;-------------------------------------------------------------------
289 (test-section "string-library")
290 (use srfi.13)
291
292 (test "string-every" #t (lambda () (string-every #\あ "")))
293 (test "string-every" #t (lambda () (string-every #\あ "ああああ")))
294 (test "string-every" #f (lambda () (string-every #\あ "あああa")))
295 (test "string-every" #t (lambda () (string-every #[あ-ん] "ああいあ")))
296 (test "string-every" #f (lambda () (string-every #[あ-ん] "ああaあ")))
297 (test "string-every" #t (lambda () (string-every #[あ-ん] "")))
298 (test "string-every" #t (lambda () (string-every (^x (char-ci=? x #\あ)) "ああああ")))
299 (test "string-every" #f (lambda () (string-every (^x (char-ci=? x #\あ)) "あいあい")))
300
301 (test "string-any" #t (lambda () (string-any #\あ "ああああ")))
302 (test "string-any" #f (lambda () (string-any #\あ "いうえお")))
303 (test "string-any" #f (lambda () (string-any #\あ "")))
304 (test "string-any" #t (lambda () (string-any #[あ-ん] "すきーむ")))
305 (test "string-any" #f (lambda () (string-any #[あ-ん] "スキーム")))
306 (test "string-any" #f (lambda () (string-any #[あ-ん] "")))
307 (test "string-any" #t (lambda () (string-any (^x (char-ci=? x #\あ)) "らららあ")))
308 (test "string-any" #f (lambda () (string-any (^x (char-ci=? x #\あ)) "ラララア")))
309 (test "string-tabulate" "アィイゥウ"
310 (lambda ()
311 (string-tabulate (lambda (code)
312 (integer->char (+ code
313 (char->integer #\ア))))
314 5)))
315 (test "reverse-list->string" "んをわ"
316 (lambda () (reverse-list->string '(#\わ #\を #\ん))))
317 (test "string-copy!" "abうえおfg"
318 (lambda () (let ((x (string-copy "abcdefg")))
319 (string-copy! x 2 "あいうえおか" 2 5)
320 x)))
321 (test "string-take" "あいうえ" (lambda () (string-take "あいうえおか" 4)))
322 (test "string-drop" "おか" (lambda () (string-drop "あいうえおか" 4)))
323 (test "string-take-right" "うえおか" (lambda () (string-take-right "あいうえおか" 4)))
324 (test "string-drop-right" "あい" (lambda () (string-drop-right "あいうえおか" 4)))
325 (test "string-pad" "■■パッド" (lambda () (string-pad "パッド" 5 #\■)))
326 (test "string-pad" "パディング" (lambda () (string-pad "パディング" 5 #\■)))
327 (test "string-pad" "ディングス" (lambda () (string-pad "パディングス" 5 #\■)))
328 (test "string-pad-right" "パッド■■" (lambda () (string-pad-right "パッド" 5 #\■)))
329 (test "string-pad" "パディング" (lambda () (string-pad-right "パディングス" 5 #\■)))
330
331 ;;-------------------------------------------------------------------
332 (test-section "char set")
333
334 (use scheme.charset)
335
336 (let ((data '(#[ぁ-ん他-唖ァ-ン]
337 #[aあ唖ア])))
338 (define (t x)
339 (test* "char-set mutable/immutable roundtrip" #t
340 (char-set= (char-set-copy x)
341 (char-set-copy (char-set-freeze! (char-set-copy x))))))
342 (for-each t data))
343
344 (test-char-set-1 "char-set-contains?" #t (cut char-set-contains? <> #\あ)
345 (char-set #\あ #\い #\う #\え #\お))
346 (test-char-set-1 "char-set-contains?" #f (cut char-set-contains? <> #\ぁ)
347 (char-set #\あ #\い #\う #\え #\お))
348 (test-char-set-1 "char-set-contains?" #t (cut char-set-contains? <> #\ほ)
349 (char-set-union
350 (integer-range->char-set (char->integer #\ぁ)
351 (char->integer #\ん))
352 (integer-range->char-set (char->integer #\ァ)
353 (char->integer #\ン))))
354 (test-char-set-1 "char-set-contains?" #t (cut char-set-contains? <> #\ケ)
355 (char-set-union
356 (integer-range->char-set (char->integer #\ぁ)
357 (char->integer #\ん))
358 (integer-range->char-set (char->integer #\ァ)
359 (char->integer #\ン))))
360 (test-char-set-1 "char-set-contains?" #f (cut char-set-contains? <> #\ん)
361 (char-set-union
362 (integer-range->char-set (char->integer #\ぁ)
363 (char->integer #\ん))
364 (integer-range->char-set (char->integer #\ァ)
365 (char->integer #\ン))))
366 (test-char-set-1 "char-set-contains?" #f (cut char-set-contains? <> #\ヴ)
367 (char-set-union
368 (integer-range->char-set (char->integer #\ぁ)
369 (char->integer #\ん))
370 (integer-range->char-set (char->integer #\ァ)
371 (char->integer #\ン))))
372
373 (test-char-set-2 "char-set=" #t char-set=
374 (char-set #\あ #\い #\う #\え #\お)
375 (string->char-set "おうえいあ"))
376 (test-char-set-2 "char-set=" #t char-set=
377 (list->char-set '(#\あ #\い #\う #\ん))
378 (string->char-set "んんいいいああう"))
379 (test-char-set-2 "char-set<=" #t char-set<=
380 (list->char-set '(#\ほ #\げ))
381 char-set:full)
382 (test-char-set-2 "char-set<=" #t char-set<= #[あい] #[あい])
383 (test-char-set-2 "char-set<=" #t char-set<= #[あい] #[あ-い])
384 (test-char-set-2 "char-set<=" #f char-set<= #[あ-い] #[あい])
385 (test-char-set-2 "char-set<=" #t char-set<= #[あ-いう-え] #[あ-え])
386 (test-char-set-2 "char-set<=" #f char-set<= #[あ-え] #[あ-いう-え])
387 (test-char-set-2 "char-set<=" #f char-set<= #[あ-いう-ぉ] #[あ-え])
388 (test-char-set-2 "char-set<=" #f char-set<= #[ぁ-いう-え] #[あ-え])
389 (test-char-set-2 "char-set<=" #t char-set<= #[あ-いか-き] #[あ-うお-け])
390 (test-char-set-2 "char-set<=" #t char-set<= #[あ-いか-き] #[あ-うお-き])
391 (test-char-set-2 "char-set<=" #t char-set<= #[あ-いか-き] #[あ-うか-こ])
392 (test-char-set-2 "char-set<=" #t char-set<= #[か-き] #[あ-うか-こ])
393 (test-char-set-2 "char-set<=" #t char-set<= #[か-きく-け] #[あ-うか-こ])
394 (test-char-set-2 "char-set<=" #f char-set<= #[う-く] #[あ-えか-こ])
395 (test-char-set-2 "->char-set" #t char-set=
396 (->char-set "ぁぃぅぇぉあいうえ")
397 (integer-range->char-set (char->integer #\ぁ)
398 (char->integer #\お)))
399
400 ;;-------------------------------------------------------------------
401 (test-section "ports")
402
403 ;; イ ロ ハ ニ ホ ヘ ト
404 ;; e382a4.e383ad.e3838f.e3838b.e3839b.e38398.e38388
405 (define istr (open-input-string "イロハニホヘト"))
406 (test* "read-char" #\イ (read-char istr))
407 (test* "read-byte" #xe3 (read-byte istr))
408 (test* "read-byte (using scratch)" #xad
409 (begin (read-byte istr) (read-byte istr)))
410 (test* "read-char (using scratch)" #\ハ
411 (begin (peek-byte istr) (read-char istr)))
412 (test* "read-block (using scratch)" #*"ニ"
413 (begin (peek-char istr) (read-block 3 istr)))
414 (test* "read-block (using scratch)" #*"\xe3"
415 (begin (peek-char istr) (read-block 1 istr)))
416 (test* "read-block (using scratch)" #*"\x83\x9bヘト"
417 (begin (read-block 10 istr)))
418
419 ;; start over
420 (set! istr (open-input-string "イロハニホヘト"))
421 (test* "peek-byte" #xe3 (peek-byte istr))
422 (test* "peek-char" #\イ (peek-char istr))
423 (test* "read-byte" #xe3 (read-byte istr))
424 (test* "peek-byte" #x82 (peek-byte istr))
425 (test* "peek-char" #\ロ
426 (begin (read-byte istr) (read-byte istr) (peek-char istr)))
427 (test* "read-byte" #\ロ (begin (peek-byte istr) (read-char istr)))
428 (test* "peek-byte" #x83
429 (begin (peek-char istr) (read-byte istr) (peek-byte istr)))
430 (test* "read-block" #*"\x83\x8fニホヘ\xe3\x83" (read-block 13 istr))
431 (test* "peek-byte" #x88 (peek-byte istr))
432 (test* "peek-byte" #t (begin (read-byte istr) (eof-object? (peek-byte istr))))
433
434 (test* "read-line (LF)" "なむ"
435 (read-line (open-input-string "なむ\n")))
436 (test* "read-line (CR)" "なむ"
437 (read-line (open-input-string "なむ\r")))
438 (test* "read-line (CRLF)" "なむ"
439 (read-line (open-input-string "なむ\r\n")))
440 (test* "read-line (using ungotten)" "なむ"
441 (let1 s (open-input-string "なむ\n")
442 (peek-char s) (read-line s)))
443 (test* "read-line (using ungotten)" "なむ"
444 (let1 s (open-input-string "なむ\n")
445 (peek-byte s) (read-line s)))
446
447 ;;-------------------------------------------------------------------
448 (test-section "buffered ports")
449
450 (define (make-filler)
451 (let* ((str #*"あいうえおかきくけこ") ;incomplete string
452 (len (string-size str))
453 (ind 0))
454 (lambda (siz)
455 (cond ((>= ind len) #f)
456 ((>= (+ ind siz) len)
457 (let ((r (substring str ind len)))
458 (set! ind len)
459 r))
460 (else
461 (let ((r (substring str ind (+ ind siz))))
462 (set! ind (+ ind siz))
463 r))))))
464
465 (define (port->char-list p)
466 (let loop ((c (read-char p)) (r '()))
467 (if (eof-object? c) (reverse r) (loop (read-char p) (cons c r)))))
468
469 (define (port->byte-list p)
470 (let loop ((b (read-byte p)) (r '()))
471 (if (eof-object? b) (reverse r) (loop (read-byte p) (cons b r)))))
472
473 (define (port->chunk-list p siz)
474 (let loop ((b (read-block siz p)) (r '()))
475 (if (eof-object? b) (reverse r) (loop (read-block siz p) (cons b r)))))
476
477 (test "buffered port (getc, bufsiz=256)"
478 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ)
479 (lambda ()
480 (port->char-list (open-input-buffered-port (make-filler) 256))))
481
482 (test "buffered port (getc, bufsiz=7)"
483 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ)
484 (lambda ()
485 (port->char-list (open-input-buffered-port (make-filler) 7))))
486
487 (test "buffered port (getc, bufsiz=3)"
488 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ)
489 (lambda ()
490 (port->char-list (open-input-buffered-port (make-filler) 3))))
491
492 (test "buffered port (getc, bufsiz=2)"
493 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ)
494 (lambda ()
495 (port->char-list (open-input-buffered-port (make-filler) 2))))
496
497 (test "buffered port (getc, bufsiz=1)"
498 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ)
499 (lambda ()
500 (port->char-list (open-input-buffered-port (make-filler) 1))))
501
502 (test "buffered port (getb, bufsiz=256)"
503 '(#xe3 #x81 #x82 #xe3 #x81 #x84 #xe3 #x81 #x86 #xe3 #x81 #x88
504 #xe3 #x81 #x8a #xe3 #x81 #x8b #xe3 #x81 #x8d #xe3 #x81 #x8f
505 #xe3 #x81 #x91 #xe3 #x81 #x93)
506 (lambda ()
507 (port->byte-list (open-input-buffered-port (make-filler) 256))))
508
509 (test "buffered port (getb, bufsiz=20)"
510 '(#xe3 #x81 #x82 #xe3 #x81 #x84 #xe3 #x81 #x86 #xe3 #x81 #x88
511 #xe3 #x81 #x8a #xe3 #x81 #x8b #xe3 #x81 #x8d #xe3 #x81 #x8f
512 #xe3 #x81 #x91 #xe3 #x81 #x93)
513 (lambda ()
514 (port->byte-list (open-input-buffered-port (make-filler) 20))))
515
516 (test "buffered port (getb, bufsiz=19)"
517 '(#xe3 #x81 #x82 #xe3 #x81 #x84 #xe3 #x81 #x86 #xe3 #x81 #x88
518 #xe3 #x81 #x8a #xe3 #x81 #x8b #xe3 #x81 #x8d #xe3 #x81 #x8f
519 #xe3 #x81 #x91 #xe3 #x81 #x93)
520 (lambda ()
521 (port->byte-list (open-input-buffered-port (make-filler) 19))))
522
523 (test "buffered port (getb, bufsiz=2)"
524 '(#xe3 #x81 #x82 #xe3 #x81 #x84 #xe3 #x81 #x86 #xe3 #x81 #x88
525 #xe3 #x81 #x8a #xe3 #x81 #x8b #xe3 #x81 #x8d #xe3 #x81 #x8f
526 #xe3 #x81 #x91 #xe3 #x81 #x93)
527 (lambda ()
528 (port->byte-list (open-input-buffered-port (make-filler) 2))))
529
530 (test "buffered port (getb, bufsiz=1)"
531 '(#xe3 #x81 #x82 #xe3 #x81 #x84 #xe3 #x81 #x86 #xe3 #x81 #x88
532 #xe3 #x81 #x8a #xe3 #x81 #x8b #xe3 #x81 #x8d #xe3 #x81 #x8f
533 #xe3 #x81 #x91 #xe3 #x81 #x93)
534 (lambda ()
535 (port->byte-list (open-input-buffered-port (make-filler) 1))))
536
537 (test "buffered port (getz, siz=20,5)"
538 '(#*"\xe3\x81\x82\xe3\x81" #*"\x84\xe3\x81\x86\xe3"
539 #*"\x81\x88\xe3\x81\x8a" #*"\xe3\x81\x8b\xe3\x81"
540 #*"\x8d\xe3\x81\x8f\xe3" #*"\x81\x91\xe3\x81\x93")
541 (lambda ()
542 (port->chunk-list (open-input-buffered-port (make-filler) 20) 5)))
543
544 (test "buffered port (getz, siz=20,20)"
545 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a\xe3\x81\x8b\xe3\x81"
546 #*"\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93")
547 (lambda ()
548 (port->chunk-list (open-input-buffered-port (make-filler) 20) 20)))
549
550 (test "buffered port (getz, siz=9,20)"
551 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a\xe3\x81\x8b\xe3\x81"
552 #*"\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93")
553 (lambda ()
554 (port->chunk-list (open-input-buffered-port (make-filler) 9) 20)))
555
556 (test "buffered port (getz, siz=9,7)"
557 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3"
558 #*"\x81\x86\xe3\x81\x88\xe3\x81"
559 #*"\x8a\xe3\x81\x8b\xe3\x81\x8d"
560 #*"\xe3\x81\x8f\xe3\x81\x91\xe3"
561 #*"\x81\x93")
562 (lambda ()
563 (port->chunk-list (open-input-buffered-port (make-filler) 9) 7)))
564
565 (test "buffered port (getz, siz=3,50)"
566 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a\xe3\x81\x8b\xe3\x81\x8d\xe3\x81\x8f\xe3\x81\x91\xe3\x81\x93")
567 (lambda ()
568 (port->chunk-list (open-input-buffered-port (make-filler) 3) 50)))
569
570 (test "buffered port (getz, siz=2,7)"
571 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3"
572 #*"\x81\x86\xe3\x81\x88\xe3\x81"
573 #*"\x8a\xe3\x81\x8b\xe3\x81\x8d"
574 #*"\xe3\x81\x8f\xe3\x81\x91\xe3"
575 #*"\x81\x93")
576 (lambda ()
577 (port->chunk-list (open-input-buffered-port (make-filler) 2) 7)))
578
579 (test "buffered port (getz, siz=1,7)"
580 '(#*"\xe3\x81\x82\xe3\x81\x84\xe3"
581 #*"\x81\x86\xe3\x81\x88\xe3\x81"
582 #*"\x8a\xe3\x81\x8b\xe3\x81\x8d"
583 #*"\xe3\x81\x8f\xe3\x81\x91\xe3"
584 #*"\x81\x93")
585 (lambda ()
586 (port->chunk-list (open-input-buffered-port (make-filler) 1) 7)))
587
588 (define *flusher-out* '())
589
590 (define (flusher str)
591 (if str
592 (set! *flusher-out* (cons str *flusher-out*))
593 (set! *flusher-out* (string-concatenate-reverse *flusher-out*))))
594
595 (define (byte-list->port p bytes)
596 (set! *flusher-out* '())
597 (for-each (^b (write-byte b p)) bytes)
598 (close-output-port p)
599 *flusher-out*)
600
601 (define (char-list->port p chars)
602 (set! *flusher-out* '())
603 (for-each (^c (write-char c p)) chars)
604 (close-output-port p)
605 *flusher-out*)
606
607 (define (string-list->port p strs)
608 (set! *flusher-out* '())
609 (for-each (^s (display s p)) strs)
610 (close-output-port p)
611 *flusher-out*)
612
613 (test "buffered port (putb, bufsiz=7)"
614 #*"@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
615 (lambda ()
616 (byte-list->port (open-output-buffered-port flusher 7)
617 (iota 27 #x40))))
618
619 (test "buffered port (putb, bufsiz=30)"
620 #*"@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
621 (lambda ()
622 (byte-list->port (open-output-buffered-port flusher 30)
623 (iota 27 #x40))))
624
625 (test "buffered port (putc, bufsiz=7)"
626 #*"あいうえおかきくけこさしすせそ"
627 (lambda ()
628 (char-list->port (open-output-buffered-port flusher 7)
629 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ
630 #\さ #\し #\す #\せ #\そ))))
631
632 (test "buffered port (putc, bufsiz=30)"
633 #*"あいうえおかきくけこさしすせそ"
634 (lambda ()
635 (char-list->port (open-output-buffered-port flusher 30)
636 '(#\あ #\い #\う #\え #\お #\か #\き #\く #\け #\こ
637 #\さ #\し #\す #\せ #\そ))))
638
639 (test "buffered port (puts, bufsiz=6)"
640 #*"あいうえおかきくけこさしすせそ"
641 (lambda ()
642 (string-list->port (open-output-buffered-port flusher 6)
643 '("あいう" "えおか" "きくけ" "こさし" "すせそ"))))
644
645 (test "buffered port (puts, bufsiz=7)"
646 #*"あいうえおかきくけこさしすせそ"
647 (lambda ()
648 (string-list->port (open-output-buffered-port flusher 7)
649 '("あいう" "えおか" "きくけ" "こさし" "すせそ"))))
650
651 (test "buffered port (puts, bufsiz=7)"
652 #*"あいうえおかきくけこさしすせそ"
653 (lambda ()
654 (string-list->port (open-output-buffered-port flusher 7)
655 '("あいうえお" "かきくけこ" "さしすせ" "そ"))))
656
657 (test "buffered port (puts, bufsiz=3)"
658 #*"あいうえおかきくけこさしすせそ"
659 (lambda ()
660 (string-list->port (open-output-buffered-port flusher 3)
661 '("あいうえお" "かきくけこ" "さしすせ" "そ"))))
662
663 ;;-------------------------------------------------------------------
664 (test-section "regexp")
665
666 (test "regexp" "いaろbはc"
667 (lambda ()
668 (cond ((rxmatch #/([ぁ-ん][a-z])+/ "xyいaろbはcdに")
669 => rxmatch-substring)
670 (else #f))))
671 (test "regexp" "いaろBはC"
672 (lambda ()
673 (cond ((rxmatch #/([ぁ-ん][a-z])+/i "XYいaろBはCdに")
674 => rxmatch-substring)
675 (else #f))))
676
677 (test* "regexp" #f
678 (cond ((rxmatch #/(.*)a/ "あいう")
679 => rxmatch-substring)
680 (else #f)))
681
682 (test* "regexp" "あいa"
683 (cond ((rxmatch #/(.*)a/ "あいaう")
684 => rxmatch-substring)
685 (else #f)))
686
687 (test* "regexp" #f
688 (cond ((rxmatch #/([^a]*)a/ "あいう")
689 => rxmatch-substring)
690 (else #f)))
691
692 (test* "regexp" "あいう"
693 (cond ((rxmatch #/([^a]*)う/ "あいう")
694 => rxmatch-substring)
695 (else #f)))
696
697 (test* "regexp" "あいa"
698 (cond ((rxmatch #/([^a]+)a/ "aあいaう")
699 => rxmatch-substring)
700 (else #f)))
701
702 (test* "regexp" #f
703 (cond ((rxmatch #/([^a]+)う/ "aあいaう")
704 => rxmatch-substring)
705 (else #f)))
706
707 (test* "regexp" "あい"
708 (cond ((rxmatch #/([^a]+)い/ "aあいaう")
709 => rxmatch-substring)
710 (else #f)))
711
712 (test* "regexp" "う"
713 (cond ((rxmatch #/(?<=[あい]+)う/ "あいう")
714 => rxmatch-substring)
715 (else #f)))
716
717 (test* "regexp" #f
718 (cond ((rxmatch #/(?<=[あい]+)う/ "aあいaう")
719 => rxmatch-substring)
720 (else #f)))
721
722 (test* "regexp" "う"
723 (cond ((rxmatch #/(?<=[^あいう]+)う/ "えおう")
724 => rxmatch-substring)
725 (else #f)))
726
727 (test* "regexp" "う"
728 (cond ((rxmatch #/(?<=.あ.)う/ "aあaう")
729 => rxmatch-substring)
730 (else #f)))
731
732 (test* "regexp" #f
733 (cond ((rxmatch #/(?<=.あ.)う/ "あaaう")
734 => rxmatch-substring)
735 (else #f)))
736
737 ;; The following tests are tailored to cover all paths
738 ;; introduced at regexp.c,v 1.63 to minimize counting
739 ;; string length.
740
741 (test* "regexp (full coverage - start)" '("あa" "あaいiうu")
742 (list
743 ;; count start directly
744 (rxmatch-before (rxmatch #/いiう/ "あaいiうuえeおo"))
745 ;; count the rest
746 (rxmatch-before (rxmatch #/えeお/ "あaいiうuえeおo"))))
747
748 (test* "regexp (full coverage - length)" '("いiう" "aいiうuえeお")
749 (list
750 ;; count length directly
751 (rxmatch-substring (rxmatch #/いiう/ "あaいiうuえeおo"))
752 ;; count the rest
753 (rxmatch-substring (rxmatch #/aいiうuえeお/ "あaいiうuえeおo"))))
754
755 (test* "regexp (full coverage - after)" '("おo" "iうuえeおo")
756 (list
757 ;; count after directly
758 (rxmatch-after (rxmatch #/iうuえe/ "あaいiうuえeおo"))
759 ;; count the rest
760 (rxmatch-after (rxmatch #/あaい/ "あaいiうuえeおo"))))
761
762 (test* "regexp (full coverage - memoized length/after)" '("えeお" "o")
763 (let1 m (rxmatch #/えeお/ "あaいiうuえeおo")
764 (rxmatch-start m) ;; memoizes length and after
765 (list (rxmatch-substring m) (rxmatch-after m))))
766
767 (test* "regexp (full coverage - memoized start/after)" '("あ" "おo")
768 (let1 m (rxmatch #/aいiうuえe/ "あaいiうuえeおo")
769 (rxmatch-substring m) ;; memoizes start and after
770 (list (rxmatch-before m) (rxmatch-after m))))
771
772 (test* "regexp (full coverage - memoized start/length)" '("あa" "いi")
773 (let1 m (rxmatch #/いi/ "あaいiうuえeおo")
774 (rxmatch-after m) ;; memoizes start and length
775 (list (rxmatch-before m) (rxmatch-substring m))))
776
777 ;;-------------------------------------------------------------------
778 (test* "regexp/unicode-ci (aa)" "λ"
779 (cond ((rxmatch #/λ/i "λ") => rxmatch-substring)
780 (else #f)))
781 (test* "regexp/unicode-ci (aA)" "Λ"
782 (cond ((rxmatch #/λ/i "Λ") => rxmatch-substring)
783 (else #f)))
784 (test* "regexp/unicode-ci (Aa)" "λ"
785 (cond ((rxmatch #/Λ/i "λ") => rxmatch-substring)
786 (else #f)))
787 (test* "regexp/unicode-ci (AA)" "Λ"
788 (cond ((rxmatch #/Λ/i "Λ") => rxmatch-substring)
789 (else #f)))
790
791 (test* "regexp/unicode-ci (uncase + backref, aa)" "λλ"
792 (cond ((rxmatch #/(λ)(?i:\1)/ "λλ") => rxmatch-substring)
793 (else #f)))
794 (test* "regexp/unicode-ci (uncase + backref, aA)" "λΛ"
795 (cond ((rxmatch #/(λ)(?i:\1)/ "λΛ") => rxmatch-substring)
796 (else #f)))
797 (test* "regexp/unicode-ci (uncase + backref, Aa)" "Λλ"
798 (cond ((rxmatch #/(Λ)(?i:\1)/ "Λλ") => rxmatch-substring)
799 (else #f)))
800 (test* "regexp/unicode-ci (uncase + backref, AA)" "ΛΛ"
801 (cond ((rxmatch #/(Λ)(?i:\1)/ "ΛΛ") => rxmatch-substring)
802 (else #f)))
803
804 (test* "regexp/unicode-ci (charset, aa)" "λ"
805 (cond ((rxmatch #/[λ]/i "λ") => rxmatch-substring)
806 (else #f)))
807 (test* "regexp/unicode-ci (charset, aA)" "Λ"
808 (cond ((rxmatch #/[λ]/i "Λ") => rxmatch-substring)
809 (else #f)))
810 (test* "regexp/unicode-ci (charset, Aa)" "λ"
811 (cond ((rxmatch #/[Λ]/i "λ") => rxmatch-substring)
812 (else #f)))
813 (test* "regexp/unicode-ci (charset, AA)" "Λ"
814 (cond ((rxmatch #/[Λ]/i "Λ") => rxmatch-substring)
815 (else #f)))
816
817 (test* "regexp/unicode-ci" "ΒΓ"
818 (cond ((rxmatch #/βγ/i "ΑΒΓΔ") => rxmatch-substring)
819 (else #f)))
820 (test* "regexp/unicode-ci" "βΓ"
821 (cond ((rxmatch #/Βγ/i "ΑβΓΔ") => rxmatch-substring)
822 (else #f)))
823 (test* "regexp/unicode-ci" "βγ"
824 (cond ((rxmatch #/ΒΓ/i "ΑβγΔ") => rxmatch-substring)
825 (else #f)))
826 (test* "regexp/unicode-ci" #f
827 (cond ((rxmatch #/Βγ/ "ΑβΓΔ") => rxmatch-substring)
828 (else #f)))
829 (test* "regexp/unicode-ci" #f
830 (cond ((rxmatch #/ΒΓ/ "ΑΒγΔ") => rxmatch-substring)
831 (else #f)))
832
833 (test* "regexp/unicode-ci" "ОНА"
834 (cond ((rxmatch #/о[а-я]а/i "ОНА") => rxmatch-substring)
835 (else #f)))
836 (test* "regexp/unicode-ci" "она"
837 (cond ((rxmatch #/О[А-Я]А/i "она") => rxmatch-substring)
838 (else #f)))
839 (test* "regexp/unicode-ci" "они"
840 (cond ((rxmatch #/[а-пР-Я][А-Пр-я]./i "они") => rxmatch-substring)
841 (else #f)))
842 (test* "regexp/unicode-ci" #f
843 (cond ((rxmatch #/о[а-я]а/ "ОНА") => rxmatch-substring)
844 (else #f)))
845 (test* "regexp/unicode-ci" #f
846 (cond ((rxmatch #/О[А-Я]А/ "она") => rxmatch-substring)
847 (else #f)))
848 (test* "regexp/unicode-ci" #f
849 (cond ((rxmatch #/[а-пР-Я][А-Пр-я]./ "они") => rxmatch-substring)
850 (else #f)))
851 (test* "regexp/unicode-ci" "она"
852 (cond ((rxmatch (string->regexp "о[А-Я]а" :case-fold #t) "она")
853 => rxmatch-substring)
854 (else #f)))
855 (test* "regexp/unicode-ci" #f
856 (cond ((rxmatch (string->regexp "о[А-Я]а") "она")
857 => rxmatch-substring)
858 (else #f)))
859 (test* "regexp/unicode-ci" "σζ\x01C5;"
860 (cond ((rxmatch (string->regexp "[[:LOWER:]]*" :case-fold #t) "σζ\x01C5;")
861 => rxmatch-substring)
862 (else #f)))
863
864 (test-end)