Debian Codegraph @

test/typedregex1.awk

raw
1 # This file describes the semantics for hard regex constants
2 # As much as possible it's executable code so that it can be used
3 # (or split into) test cases for development and regression testing.
4
5 function simple_tests( fbre, numresult, strresult)
6 {
7 # usable as case value
8 switch ("foobaaar") {
9 case @/fo+ba+r/:
10 print "switch-case: ok"
11 break
12 default:
13 print "switch-case: fail"
14 break
15 }
16
17 # usable with ~ and !~
18 if ("foobaaar" ~ @/fo+ba+r/)
19 print "match ~: ok"
20 else
21 print "match ~: fail"
22
23 if ("quasimoto" !~ @/fo+ba+r/)
24 print "match !~: ok"
25 else
26 print "match !~: fail"
27
28 # assign to variable, use in match
29 fbre = @/fo+ba+r/
30 if ("foobaaar" ~ fbre)
31 print "variable match ~: ok"
32 else
33 print "variable match ~: fail"
34
35 if ("quasimoto" !~ fbre)
36 print "variable match !~: ok"
37 else
38 print "variable match !~: fail"
39
40 # Use as numeric value, should be zero
41 numresult = fbre + 42
42 if (numresult == 42)
43 print "variable as numeric value: ok"
44 else
45 print "variable as numeric value: fail"
46
47 # Use as string value, should be string value of regexp text
48 strresult = "<" fbre ">"
49 if (strresult == "<fo+ba+r>")
50 print "variable as string value: ok"
51 else
52 print "variable as string value: fail", strresult
53
54 # typeof should work
55 if (typeof(@/fo+ba+r/) == "regexp")
56 print "typeof constant: ok"
57 else
58 print "typeof constant: fail"
59
60 if (typeof(fbre) == "regexp")
61 print "typeof variable: ok"
62 else
63 print "typeof variable: fail"
64
65 # conversion to number, works. should it be fatal?
66 fbre++
67 if (fbre == 1)
68 print "conversion to number: ok"
69 else
70 print "conversion to number: fail"
71
72 if (typeof(fbre) == "number")
73 print "typeof variable after conversion: ok"
74 else
75 print "typeof variable after conversion: fail"
76 }
77
78 function match_tests( fbre, fun)
79 {
80 if (match("foobaaar", @/fo+ba+r/))
81 print "match(constant): ok"
82 else
83 print "match(constant): fail"
84
85 fbre = @/fo+ba+r/
86 if (match("foobaaar", fbre))
87 print "match(variable): ok"
88 else
89 print "match(variable): fail"
90
91 fun = "match"
92 if (@fun("foobaaar", @/fo+ba+r/))
93 print "match(constant) indirect: ok"
94 else
95 print "match(constant) indirect: fail"
96
97 if (@fun("foobaaar", fbre))
98 print "match(variable) indirect: ok"
99 else
100 print "match(variable) indirect: fail"
101 }
102
103 function sub_tests( fbre, count, target, fun)
104 {
105 target = "abc foobaar def foobar ghi"
106 count = sub(@/fo+ba+r/, "XX", target)
107 if (count == 1 && target == "abc XX def foobar ghi")
108 print "sub(constant): ok"
109 else
110 print "sub(constant): fail"
111
112 fbre = @/fo+ba+r/
113 target = "abc foobaar def foobar ghi"
114 count = sub(fbre, "XX", target)
115 if (count == 1 && target == "abc XX def foobar ghi")
116 print "sub(variable): ok"
117 else
118 print "sub(variable): fail"
119
120 fun = "sub"
121 $0 = "abc foobaar def foobar ghi"
122 count = @fun(@/fo+ba+r/, "XX")
123 if (count == 1 && $0 == "abc XX def foobar ghi")
124 print "sub(constant) indirect: ok"
125 else
126 print "sub(constant) indirect: fail"
127
128 $0 = "abc foobaar def foobar ghi"
129 count = @fun(fbre, "XX")
130 if (count == 1 && $0 == "abc XX def foobar ghi")
131 print "sub(variable) indirect: ok"
132 else
133 print "sub(variable) indirect: fail"
134 }
135
136 function gsub_tests( fbre, count, target, fun)
137 {
138 target = "abc foobaar def foobar ghi"
139 count = gsub(@/fo+ba+r/, "XX", target)
140 if (count == 2 && target == "abc XX def XX ghi")
141 print "gsub(constant): ok"
142 else
143 print "gsub(constant): fail"
144
145 fbre = @/fo+ba+r/
146 target = "abc foobaar def foobar ghi"
147 count = gsub(fbre, "XX", target)
148 if (count == 2 && target == "abc XX def XX ghi")
149 print "gsub(variable): ok"
150 else
151 print "gsub(variable): fail"
152
153 fun = "gsub"
154 $0 = "abc foobaar def foobar ghi"
155 count = @fun(@/fo+ba+r/, "XX")
156 if (count == 2 && $0 == "abc XX def XX ghi")
157 print "gsub(constant) indirect: ok"
158 else
159 print "gsub(constant) indirect: fail"
160
161 $0 = "abc foobaar def foobar ghi"
162 count = @fun(fbre, "XX")
163 if (count == 2 && $0 == "abc XX def XX ghi")
164 print "gsub(variable) indirect: ok"
165 else
166 print "gsub(variable) indirect: fail"
167 }
168
169 function gensub_tests( fbre, result, target, fun)
170 {
171 target = "abc foobaar def foobar ghi"
172 result = gensub(@/fo+ba+r/, "XX", "g", target)
173 if (result == "abc XX def XX ghi")
174 print "gensub(constant): ok"
175 else
176 print "gensub(constant): fail"
177
178 fbre = @/fo+ba+r/
179 target = "abc foobaar def foobar ghi"
180 result = gensub(fbre, "XX", "g", target)
181 if (result == "abc XX def XX ghi")
182 print "gensub(variable): ok"
183 else
184 print "gensub(variable): fail"
185
186 fun = "gensub"
187 $0 = "abc foobaar def foobar ghi"
188 result = @fun(@/fo+ba+r/, "XX", "g")
189 if (result == "abc XX def XX ghi")
190 print "gensub(constant) indirect: ok"
191 else
192 print "gensub(constant) indirect: fail"
193
194 $0 = "abc foobaar def foobar ghi"
195 result = @fun(fbre, "XX", "g")
196 if (result == "abc XX def XX ghi")
197 print "gensub(variable) indirect: ok"
198 else
199 print "gensub(variable) indirect: fail"
200
201 result = @fun(@/fo+ba+r/, "XX", "g", target)
202 if (result == "abc XX def XX ghi")
203 print "gensub(constant) indirect 2: ok"
204 else
205 print "gensub(constant) indirect 2: fail"
206
207 result = @fun(fbre, "XX", "g", target)
208 if (result == "abc XX def XX ghi")
209 print "gensub(variable) indirect 2: ok"
210 else
211 print "gensub(variable) indirect 2: fail"
212 }
213
214 function split_tests( fbre, data, seps, fun, b1)
215 {
216 delete data
217 delete seps
218 b1 = split("a:b:c:d", data, @/:/, seps)
219 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
220 print "split(constant): ok"
221 else
222 print "split(constant): fail"
223
224 delete data
225 delete seps
226 fbre = @/:/
227 b1 = split("a:b:c:d", data, fbre, seps)
228 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
229 print "split(variable): ok"
230 else
231 print "split(variable): fail"
232
233 fun = "split"
234 delete data
235 delete seps
236 b1 = @fun("a:b:c:d", data, @/:/, seps)
237 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
238 print "split(constant) indirect: ok"
239 else
240 print "split(constant) indirect: fail"
241
242 delete data
243 delete seps
244 b1 = @fun("a:b:c:d", data, fbre, seps)
245 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
246 print "split(variable) indirect: ok"
247 else
248 print "split(variable) indirect: fail"
249 }
250
251 function patsplit_tests( fbre, data, seps, fun, b1)
252 {
253 delete data
254 delete seps
255 b1 = patsplit("a:b:c:d", data, @/[a-z]+/, seps)
256 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
257 print "patsplit(constant): ok"
258 else
259 print "patsplit(constant): fail"
260
261 delete data
262 delete seps
263 fbre = @/[a-z]+/
264 b1 = patsplit("a:b:c:d", data, fbre, seps)
265 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
266 print "patsplit(variable): ok"
267 else
268 print "patsplit(variable): fail"
269
270 fun = "patsplit"
271 delete data
272 delete seps
273 b1 = @fun("a:b:c:d", data, @/[a-z]+/, seps)
274 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
275 print "patsplit(constant) indirect: ok"
276 else
277 print "patsplit(constant) indirect: fail"
278
279 delete data
280 delete seps
281 b1 = @fun("a:b:c:d", data, fbre, seps)
282 if (b1 == 4 && data[1] == "a" && seps[1] == ":")
283 print "patsplit(variable) indirect: ok"
284 else
285 print "patsplit(variable) indirect: fail"
286 }
287
288 BEGIN {
289 simple_tests()
290 match_tests()
291 sub_tests()
292 gsub_tests()
293 gensub_tests()
294 split_tests()
295 patsplit_tests()
296 }