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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# This file describes the semantics for hard regex constants
# As much as possible it's executable code so that it can be used
# (or split into) test cases for development and regression testing.
function simple_tests( fbre, numresult, strresult)
{
# usable as case value
switch ("foobaaar") {
case @/fo+ba+r/:
print "switch-case: ok"
break
default:
print "switch-case: fail"
break
}
# usable with ~ and !~
if ("foobaaar" ~ @/fo+ba+r/)
print "match ~: ok"
else
print "match ~: fail"
if ("quasimoto" !~ @/fo+ba+r/)
print "match !~: ok"
else
print "match !~: fail"
# assign to variable, use in match
fbre = @/fo+ba+r/
if ("foobaaar" ~ fbre)
print "variable match ~: ok"
else
print "variable match ~: fail"
if ("quasimoto" !~ fbre)
print "variable match !~: ok"
else
print "variable match !~: fail"
# Use as numeric value, should be zero
numresult = fbre + 42
if (numresult == 42)
print "variable as numeric value: ok"
else
print "variable as numeric value: fail"
# Use as string value, should be string value of regexp text
strresult = "<" fbre ">"
if (strresult == "<fo+ba+r>")
print "variable as string value: ok"
else
print "variable as string value: fail", strresult
# typeof should work
if (typeof(@/fo+ba+r/) == "regexp")
print "typeof constant: ok"
else
print "typeof constant: fail"
if (typeof(fbre) == "regexp")
print "typeof variable: ok"
else
print "typeof variable: fail"
# conversion to number, works. should it be fatal?
fbre++
if (fbre == 1)
print "conversion to number: ok"
else
print "conversion to number: fail"
if (typeof(fbre) == "scalar_n")
print "typeof variable after conversion: ok"
else
print "typeof variable after conversion: fail"
}
function match_tests( fbre, fun)
{
if (match("foobaaar", @/fo+ba+r/))
print "match(constant): ok"
else
print "match(constant): fail"
fbre = @/fo+ba+r/
if (match("foobaaar", fbre))
print "match(variable): ok"
else
print "match(variable): fail"
fun = "match"
if (@fun("foobaaar", @/fo+ba+r/))
print "match(constant) indirect: ok"
else
print "match(constant) indirect: fail"
if (@fun("foobaaar", fbre))
print "match(variable) indirect: ok"
else
print "match(variable) indirect: fail"
}
function sub_tests( fbre, count, target, fun)
{
target = "abc foobaar def foobar ghi"
count = sub(@/fo+ba+r/, "XX", target)
if (count == 1 && target == "abc XX def foobar ghi")
print "sub(constant): ok"
else
print "sub(constant): fail"
fbre = @/fo+ba+r/
target = "abc foobaar def foobar ghi"
count = sub(fbre, "XX", target)
if (count == 1 && target == "abc XX def foobar ghi")
print "sub(variable): ok"
else
print "sub(variable): fail"
fun = "sub"
$0 = "abc foobaar def foobar ghi"
count = @fun(@/fo+ba+r/, "XX")
if (count == 1 && $0 == "abc XX def foobar ghi")
print "sub(constant) indirect: ok"
else
print "sub(constant) indirect: fail"
$0 = "abc foobaar def foobar ghi"
count = @fun(fbre, "XX")
if (count == 1 && $0 == "abc XX def foobar ghi")
print "sub(variable) indirect: ok"
else
print "sub(variable) indirect: fail"
}
function gsub_tests( fbre, count, target, fun)
{
target = "abc foobaar def foobar ghi"
count = gsub(@/fo+ba+r/, "XX", target)
if (count == 2 && target == "abc XX def XX ghi")
print "gsub(constant): ok"
else
print "gsub(constant): fail"
fbre = @/fo+ba+r/
target = "abc foobaar def foobar ghi"
count = gsub(fbre, "XX", target)
if (count == 2 && target == "abc XX def XX ghi")
print "gsub(variable): ok"
else
print "gsub(variable): fail"
fun = "gsub"
$0 = "abc foobaar def foobar ghi"
count = @fun(@/fo+ba+r/, "XX")
if (count == 2 && $0 == "abc XX def XX ghi")
print "gsub(constant) indirect: ok"
else
print "gsub(constant) indirect: fail"
$0 = "abc foobaar def foobar ghi"
count = @fun(fbre, "XX")
if (count == 2 && $0 == "abc XX def XX ghi")
print "gsub(variable) indirect: ok"
else
print "gsub(variable) indirect: fail"
}
function gensub_tests( fbre, result, target, fun)
{
target = "abc foobaar def foobar ghi"
result = gensub(@/fo+ba+r/, "XX", "g", target)
if (result == "abc XX def XX ghi")
print "gensub(constant): ok"
else
print "gensub(constant): fail"
fbre = @/fo+ba+r/
target = "abc foobaar def foobar ghi"
result = gensub(fbre, "XX", "g", target)
if (result == "abc XX def XX ghi")
print "gensub(variable): ok"
else
print "gensub(variable): fail"
fun = "gensub"
$0 = "abc foobaar def foobar ghi"
result = @fun(@/fo+ba+r/, "XX", "g")
if (result == "abc XX def XX ghi")
print "gensub(constant) indirect: ok"
else
print "gensub(constant) indirect: fail"
$0 = "abc foobaar def foobar ghi"
result = @fun(fbre, "XX", "g")
if (result == "abc XX def XX ghi")
print "gensub(variable) indirect: ok"
else
print "gensub(variable) indirect: fail"
result = @fun(@/fo+ba+r/, "XX", "g", target)
if (result == "abc XX def XX ghi")
print "gensub(constant) indirect 2: ok"
else
print "gensub(constant) indirect 2: fail"
result = @fun(fbre, "XX", "g", target)
if (result == "abc XX def XX ghi")
print "gensub(variable) indirect 2: ok"
else
print "gensub(variable) indirect 2: fail"
}
function split_tests( fbre, data, seps, fun, b1)
{
delete data
delete seps
b1 = split("a:b:c:d", data, @/:/, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "split(constant): ok"
else
print "split(constant): fail"
delete data
delete seps
fbre = @/:/
b1 = split("a:b:c:d", data, fbre, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "split(variable): ok"
else
print "split(variable): fail"
fun = "split"
delete data
delete seps
b1 = @fun("a:b:c:d", data, @/:/, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "split(constant) indirect: ok"
else
print "split(constant) indirect: fail"
delete data
delete seps
b1 = @fun("a:b:c:d", data, fbre, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "split(variable) indirect: ok"
else
print "split(variable) indirect: fail"
}
function patsplit_tests( fbre, data, seps, fun, b1)
{
delete data
delete seps
b1 = patsplit("a:b:c:d", data, @/[a-z]+/, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "patsplit(constant): ok"
else
print "patsplit(constant): fail"
delete data
delete seps
fbre = @/[a-z]+/
b1 = patsplit("a:b:c:d", data, fbre, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "patsplit(variable): ok"
else
print "patsplit(variable): fail"
fun = "patsplit"
delete data
delete seps
b1 = @fun("a:b:c:d", data, @/[a-z]+/, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "patsplit(constant) indirect: ok"
else
print "patsplit(constant) indirect: fail"
delete data
delete seps
b1 = @fun("a:b:c:d", data, fbre, seps)
if (b1 == 4 && data[1] == "a" && seps[1] == ":")
print "patsplit(variable) indirect: ok"
else
print "patsplit(variable) indirect: fail"
}
BEGIN {
simple_tests()
match_tests()
sub_tests()
gsub_tests()
gensub_tests()
split_tests()
patsplit_tests()
}
|