-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexptest.scm
208 lines (178 loc) · 6.98 KB
/
regexptest.scm
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
;;; Ben Zinberg
;;; Regex combinators
;;; (Assignment for 6.945 Advanced Symbolic Programming)
;;; Test suite
(load "regexp.scm")
(define (list-add . lists)
(apply map + lists))
(define (display-with-newline string)
(display string)
(newline))
(define (display-indented string)
(display " ")
(display-with-newline string))
(define (make-test-case name object)
(list name object))
(define test-case-name car)
(define test-case-object cadr)
(define test-cases '())
(define (add-test-case name object)
(let ((test-case (make-test-case name object)))
(set! test-cases (append test-cases
(list test-case)))))
(define (run-tests)
; Returns (1 0) on success, (0 1) on failure
(define (run-test test-case)
(let* ((name (test-case-name test-case))
(object (test-case-object test-case))
(bre (fluid-let ((USE_ERE #f))
(r:compile object)))
(ere (fluid-let ((USE_ERE #t))
(r:compile object)))
(grep-matches (r:grep* bre "tests.txt"))
(egrep-matches (r:egrep* ere "tests.txt")))
(display "Test case: ")
(display name)
(newline)
(display-with-newline "Compiled BRE:")
(display-indented bre)
(display-with-newline "Compiled ERE:")
(display-indented ere)
(if (equal? grep-matches egrep-matches)
(begin
(display-with-newline "grep and egrep results match:")
(for-each display-indented grep-matches)
(newline)
'(1 0))
(begin
(display-with-newline "grep and egrep results don't match.")
(display-with-newline "grep results:")
(for-each display-indented grep-matches)
(display-with-newline "egrep results:")
(for-each display-indented egrep-matches)
(newline)
'(0 1)))))
(let* ((results
(apply list-add
(map run-test test-cases)))
(num-successes (car results))
(num-failures (cadr results)))
(display (string-append "Testing complete: "
(number->string num-successes)
" passed, "
(number->string num-failures)
" failed (as far as I can tell)"))
(newline)))
(add-test-case "Literal brackets and parentheses"
(r:quote "()--[]"))
(add-test-case "Alternating literal brackets and parentheses with a backreference"
(r:seq
(r:attach-name 'left
(r:alt (r:quote "()")
(r:quote "[]")))
(r:quote "--")
(r:backref 'left)))
(add-test-case "Alternation with a pipe"
(r:repeat 5
5
(r:alt
(r:quote "|")
(r:quote "e"))))
(add-test-case "Repeated literal left brace"
(r:repeat 1
2
(r:quote "{")))
(add-test-case "Repeated literal right brace"
(r:repeat 1
2
(r:quote "}")))
(add-test-case "Repeated repeat"
(r:repeat 3
#f
(r:repeat 2
3
(r:quote "c"))))
(add-test-case "Repeated literal expression that itself looks like a repeat designation, with a backreference"
(r:seq
(r:attach-name 'fake-repeat
(r:repeat 1
2
(r:quote "{1,2}")))
(r:quote " and ")
(r:backref 'fake-repeat)))
(add-test-case "Possibly mismatched literal parentheses, brackets and braces, because hey, why not"
(r:seq
(r:repeat 3
3
(r:alt (r:quote "(")
(r:quote "[")
(r:quote "{")))
(r:repeat 3
3
(r:alt (r:quote ")")
(r:quote "]")
(r:quote "}")))))
(add-test-case "char-from \"a]\""
(r:seq (r:quote "CHARFROM ")
(r:char-from "a]")))
(add-test-case "char-not-from \"^\""
(r:seq (r:quote "CHARFROM ")
(r:char-not-from "^")))
(add-test-case "char-from \"^-\" (the weird exception case)"
(r:seq (r:quote "CHARFROM ")
(r:char-from "^-")))
(add-test-case "char-from \"a-z^\" (note that 'b' should not be matched here)"
(r:seq (r:quote "CHARFROM ")
(r:char-from "a-z^")))
(add-test-case "BOL and EOL with literal ^ and $"
(r:seq
(r:bol)
(r:quote "^")
(r:quote "$")
(r:eol)))
(add-test-case "Empty line"
(r:seq
(r:bol)
(r:eol)))
(add-test-case "Lines beginning with $"
(r:seq
(r:bol)
(r:quote "$")))
(add-test-case "Lines ending with ^"
(r:seq
(r:quote "^")
(r:eol)))
(add-test-case "Backreference with nested parentheses, both syntactic and literal"
(r:seq
(r:attach-name 'first-sentence
(r:seq
(r:quote "I'm (I am) going to ")
(r:alt (r:quote "run to")
(r:quote "urinate in"))
(r:quote " the ")
(r:attach-name 'room-type
(r:repeat 1
#f
(r:char-not-from " ")))
(r:quote "room.")))
(r:quote " I need to use the ")
(r:backref 'room-type)
(r:quote ". ")
(r:backref 'first-sentence)))
(add-test-case "Combining a bunch of small terms with alternation and concatenation, backreferences, and unused names"
(r:seq
(r:attach-name
'chunk1
(r:alt
(r:repeat 1
2
(r:attach-name 'chunk2
(r:quote "{1,2}(((")))
(r:seq (r:char-from "a]")
(r:char-not-from "^"))))
(r:attach-name 'laughter
(r:quote "haha"))
(r:quote "HAHA")
(r:backref 'laughter)
(r:backref 'chunk1)))
(run-tests)