Debian Codegraph @

other-libs/pregexp-test.ss

raw
1 #!/usr/bin/env scheme-script
2 ;;; ported to Ikarus by Abdulaziz Ghuloum on Dec 19 2007.
3
4 ;last change by dorai on 2005-04-24
5
6
7 (import (ikarus) (pregexp))
8
9 (define *failed* 0)
10
11 (define-syntax test
12 (syntax-rules ()
13 [(_) #t]
14 [(_ q a rest ...)
15 (begin
16 (test-each q a)
17 (test rest ...))]))
18
19 (define-syntax test-each
20 (syntax-rules ()
21 [(_ expr expected-answer)
22 (begin
23 (display "Trying ")
24 (write 'expr)
25 (newline)
26 (display " --> ")
27 (let ((__actual-answer expr))
28 (write __actual-answer)
29 (display " ... ")
30 (if (equal? __actual-answer 'expected-answer)
31 (display "OK")
32 (begin
33 (set! *failed* (+ *failed* 1))
34 (display "FAILED!!!")
35 (newline)
36 (display " ;;; expected ")
37 (write 'expected-answer))))
38 (newline))]))
39
40 (define bottomline
41 (lambda ()
42 (newline)
43 (if (= *failed* 0)
44 (display "All tests succeeded! :-) :-) :-)")
45 (begin (display *failed*)
46 (display " test")
47 (if (> *failed* 1) (display "s"))
48 (display " failed! :-( :-( :-(")))
49 (newline)))
50
51 ;keeping the document honest
52
53 (test
54
55 (pregexp "c.r")
56 (:sub (:or (:seq #\c :any #\r)))
57
58 (pregexp-match-positions "brain" "bird")
59 #f
60
61 (pregexp-match-positions "needle" "hay needle stack")
62 ((4 . 10))
63
64 (pregexp-match-positions "needle"
65 "his hay needle stack -- my hay needle stack -- her hay needle stack"
66 24 43)
67 ((31 . 37))
68
69 (pregexp-match "brain" "bird")
70 #f
71
72 (pregexp-match "needle" "hay needle stack")
73 ("needle")
74
75 (pregexp-split ":" "/bin:/usr/bin:/usr/bin/X11:/usr/local/bin")
76 ("/bin" "/usr/bin" "/usr/bin/X11" "/usr/local/bin")
77
78 (pregexp-split " " "pea soup")
79 ("pea" "soup")
80
81 (pregexp-split "" "smithereens")
82 ("s" "m" "i" "t" "h" "e" "r" "e" "e" "n" "s")
83
84 (pregexp-split " +" "split pea soup")
85 ("split" "pea" "soup")
86
87 (pregexp-split " *" "split pea soup")
88 ("s" "p" "l" "i" "t" "p" "e" "a" "s" "o" "u" "p")
89
90 (pregexp-replace "te" "liberte" "ty")
91 "liberty"
92
93 (pregexp-replace* "te" "liberte egalite fraternite" "ty")
94 "liberty egality fratyrnity"
95
96 (pregexp-match-positions "^contact" "first contact")
97 #f
98
99 (pregexp-match-positions "laugh$" "laugh laugh laugh laugh")
100 ((18 . 23))
101
102 (pregexp-match-positions "yack\\b" "yackety yack")
103 ((8 . 12))
104
105 (pregexp-match-positions "an\\B" "an analysis")
106 ((3 . 5))
107
108 (pregexp-match "p.t" "pet")
109 ("pet")
110
111
112 (pregexp-match "\\d\\d" "0 dear, 1 have to read catch 22 before 9")
113 ("22")
114
115 (pregexp-match "[[:alpha:]_]" "--x--")
116 ("x")
117
118 (pregexp-match "[[:alpha:]_]" "--_--")
119 ("_")
120
121 (pregexp-match "[[:alpha:]_]" "--:--")
122 #f
123
124 (pregexp-match "[:alpha:]" "--a--")
125 ("a")
126
127 (pregexp-match "[:alpha:]" "--_--")
128 #f
129
130 (pregexp-match-positions "c[ad]*r" "cadaddadddr")
131 ((0 . 11))
132
133 (pregexp-match-positions "c[ad]*r" "cr")
134 ((0 . 2))
135
136 (pregexp-match-positions "c[ad]+r" "cadaddadddr")
137 ((0 . 11))
138
139 (pregexp-match-positions "c[ad]+r" "cr")
140 #f
141
142 (pregexp-match-positions "c[ad]?r" "cadaddadddr")
143 #f
144
145 (pregexp-match-positions "c[ad]?r" "cr")
146 ((0 . 2))
147
148 (pregexp-match-positions "c[ad]?r" "car")
149 ((0 . 3))
150
151 (pregexp-match "[aeiou]{3}" "vacuous")
152 ("uou")
153
154 (pregexp-match "[aeiou]{3}" "evolve")
155 #f
156
157 (pregexp-match "[aeiou]{2,3}" "evolve")
158 #f
159
160 (pregexp-match "[aeiou]{2,3}" "zeugma")
161 ("eu")
162
163 (pregexp-match "<.*>" "<tag1> <tag2> <tag3>")
164 ("<tag1> <tag2> <tag3>")
165
166
167 (pregexp-match "<.*?>" "<tag1> <tag2> <tag3>")
168 ("<tag1>")
169
170 (pregexp-match "([a-z]+) ([0-9]+), ([0-9]+)" "jan 1, 1970")
171 ("jan 1, 1970" "jan" "1" "1970")
172
173 (pregexp-match "(poo )*" "poo poo platter")
174 ("poo poo " "poo ")
175
176 (pregexp-match "([a-z ]+;)*" "lather; rinse; repeat;")
177 ("lather; rinse; repeat;" " repeat;")
178
179 )
180
181 (define date-re
182 ;match `month year' or `month day, year'.
183 ;subpattern matches day, if present
184 (pregexp "([a-z]+) +([0-9]+,)? *([0-9]+)"))
185
186 (test
187
188 (pregexp-match date-re "jan 1, 1970")
189 ("jan 1, 1970" "jan" "1," "1970")
190
191 (pregexp-match date-re "jan 1970")
192 ("jan 1970" "jan" #f "1970")
193
194 (pregexp-replace "_(.+?)_"
195 "the _nina_, the _pinta_, and the _santa maria_"
196 "*\\1*")
197 "the *nina*, the _pinta_, and the _santa maria_"
198
199 (pregexp-replace* "_(.+?)_"
200 "the _nina_, the _pinta_, and the _santa maria_"
201 "*\\1*")
202 "the *nina*, the *pinta*, and the *santa maria*"
203
204 (pregexp-replace "(\\S+) (\\S+) (\\S+)"
205 "eat to live"
206 "\\3 \\2 \\1")
207 "live to eat"
208
209 (pregexp-match "([a-z]+) and \\1"
210 "billions and billions")
211 ("billions and billions" "billions")
212
213 (pregexp-match "([a-z]+) and \\1"
214 "billions and millions")
215 #f
216
217 (pregexp-replace* "(\\S+) \\1"
218 "now is the the time for all good men to to come to the aid of of the party"
219 "\\1")
220 "now is the time for all good men to come to the aid of the party"
221
222 (pregexp-replace* "(\\d+)\\1"
223 "123340983242432420980980234"
224 "{\\1,\\1}")
225 "12{3,3}40983{24,24}3242{098,098}0234"
226
227 (pregexp-match "^(?:[a-z]*/)*([a-z]+)$" "/usr/local/bin/mzscheme")
228 ("/usr/local/bin/mzscheme" "mzscheme")
229
230 (pregexp-match "(?i:hearth)" "HeartH")
231 ("HeartH")
232
233 (pregexp-match "(?x: a lot)" "alot")
234 ("alot")
235
236 (pregexp-match "(?x: a \\ lot)" "a lot")
237 ("a lot")
238
239 (pregexp-match "(?x:
240 a \\ man \\; \\ ; ignore
241 a \\ plan \\; \\ ; me
242 a \\ canal ; completely
243 )"
244 "a man; a plan; a canal")
245 ("a man; a plan; a canal")
246
247 (pregexp-match "(?ix:
248 a \\ man \\; \\ ; ignore
249 a \\ plan \\; \\ ; me
250 a \\ canal ; completely
251 )"
252 "A Man; a Plan; a Canal")
253 ("A Man; a Plan; a Canal")
254
255 (pregexp-match "(?i:the (?-i:TeX)book)"
256 "The TeXbook")
257 ("The TeXbook")
258
259 (pregexp-match "f(ee|i|o|um)" "a small, final fee")
260 ("fi" "i")
261
262 (pregexp-replace* "([yi])s(e[sdr]?|ing|ation)"
263 "it is energising to analyse an organisation pulsing with noisy organisms"
264 "\\1z\\2")
265 "it is energizing to analyze an organization pulsing with noisy organisms"
266
267 (pregexp-match "f(?:ee|i|o|um)" "fun for all")
268 ("fo")
269
270 (pregexp-match "call|call-with-current-continuation"
271 "call-with-current-continuation")
272 ("call")
273
274 (pregexp-match "call-with-current-continuation|call"
275 "call-with-current-continuation")
276 ("call-with-current-continuation")
277
278 (pregexp-match "(?:call|call-with-current-continuation) constrained"
279 "call-with-current-continuation constrained")
280 ("call-with-current-continuation constrained")
281
282 (pregexp-match "(?>a+)." "aaaa")
283 #f
284
285 (pregexp-match-positions "grey(?=hound)"
286 "i left my grey socks at the greyhound")
287 ((28 . 32))
288
289 (pregexp-match-positions "grey(?!hound)"
290 "the gray greyhound ate the grey socks")
291 ((27 . 31))
292
293 (pregexp-match-positions "(?<=grey)hound"
294 "the hound in the picture is not a greyhound")
295 ((38 . 43))
296
297 (pregexp-match-positions "(?<!grey)hound"
298 "the greyhound in the picture is not a hound")
299 ((38 . 43))
300
301 )
302
303 (define n0-255
304 "(?x:
305 \\d ; 0 through 9
306 | \\d\\d ; 00 through 99
307 | [01]\\d\\d ;000 through 199
308 | 2[0-4]\\d ;200 through 249
309 | 25[0-5] ;250 through 255
310 )")
311
312 (define ip-re1
313 (string-append
314 "^" ;nothing before
315 n0-255 ;the first n0-255,
316 "(?x:" ;then the subpattern of
317 "\\." ;a dot followed by
318 n0-255 ;an n0-255,
319 ")" ;which is
320 "{3}" ;repeated exactly 3 times
321 "$" ;with nothing following
322 ))
323
324 (test
325
326 (pregexp-match ip-re1
327 "1.2.3.4")
328 ("1.2.3.4")
329
330 (pregexp-match ip-re1
331 "55.155.255.265")
332 #f
333
334 (pregexp-match ip-re1
335 "0.00.000.00")
336 ("0.00.000.00")
337
338 )
339
340
341 (define ip-re
342 (string-append
343 "(?=[1-9])" ;ensure there's a non-0 digit
344 ip-re1))
345
346 (test
347
348 (pregexp-match ip-re
349 "1.2.3.4")
350 ("1.2.3.4")
351
352 (pregexp-match ip-re
353 "0.0.0.0")
354 #f
355
356 )
357
358 (set! ip-re
359 (string-append
360 "(?![0.]*$)" ;not just zeros and dots
361 ;dot is not metachar inside []
362 ip-re1))
363
364 (test
365
366 (pregexp-match ip-re
367 "1.2.3.4")
368 ("1.2.3.4")
369
370 (pregexp-match ip-re
371 "0.0.0.0")
372 #f
373
374 ;misc
375
376 (pregexp-match "a[^a]*b" "glauber")
377 ("aub")
378
379 (pregexp-match "a([^a]*)b" "glauber")
380 ("aub" "u")
381
382 (pregexp-match "a([^a]*)b" "ababababab")
383 ("ab" "")
384
385 (pregexp-match "(?x: s e * k )" "seeeeek")
386 ("seeeeek")
387
388 (pregexp-match "(?x: t ;matches t
389 h ; matches h
390 e ;;; matches e
391 \\ ; ; ; matches space
392 \\; ; matches ;
393 )"
394 "the ;")
395 ("the ;")
396
397 (pregexp-replace* "^(.*)$" "foobar" "\\1abc")
398 "foobarabc"
399
400 (pregexp-replace* "^(.*)$" "foobar" "abc\\1")
401 "abcfoobar"
402
403 (pregexp-replace* "(.*)$" "foobar" "abc\\1")
404 "abcfoobar"
405
406 )
407
408 (test
409
410 ;PLT bug 6095 from Neil W. Van Dyke
411 (pregexp "[a-z-]")
412 (:sub (:or (:seq (:one-of-chars (:char-range #\a #\z) #\-))))
413 ;
414 (pregexp "[-a-z]")
415 (:sub (:or (:seq (:one-of-chars #\- (:char-range #\a #\z)))))
416
417 ;PLT bug 6442 from David T. Pierson
418 (pregexp-match-positions "(a(b))?c" "abc")
419 ((0 . 3) (0 . 2) (1 . 2))
420 ;
421 (pregexp-match-positions "(a(b))?c" "c")
422 ((0 . 1) #f #f)
423
424 ;PLT bug 7233 from Edi Weitz
425 (length (pregexp-match "(a)|(b)" "b"))
426 3
427
428 ;PLT bug 7232 from Neil Van Dyke
429 (pregexp "[-a]")
430 (:sub (:or (:seq (:one-of-chars #\- #\a))))
431 ;
432 (pregexp "[a-]")
433 (:sub (:or (:seq (:one-of-chars #\a #\-))))
434
435 )
436
437 (bottomline)