-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample01.vbs
304 lines (270 loc) · 9.57 KB
/
sample01.vbs
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
297
298
299
300
301
302
303
304
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' VBScript example for SRELL.RegExp COM Wrapper (tests and benchmarks)
' Based on https://github.com/ZimProjects/SRELL's sample01.cpp
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Dim RegExpClass
RegExpClass = Replace("SRELL.RegExp", "SRELL", "VBScript", 1, WScript.Arguments.Named("builtin-regexp"))
Dim count
count = CInt(WScript.Arguments.Named("benchmark"))
Function Test(str, exp, max, expected)
Dim re, mr, sm, i, n, placeholder, matched, msg, num_of_failures, st, ed
Set re = CreateObject(RegExpClass)
re.Pattern = exp
st = Timer
For i = 1 To max
Set mr = re.Execute(str)
Next
ed = Timer
WScript.Echo vbTab & """" & str & """ =~ /" & exp & "/"
If max > 1 Then WScript.Echo vbTab & max & " times"
WScript.Echo vbTab & Replace("Not Found", "Not ", "", 1, mr.Count) & " (" & Int((ed - st) * 1000) & " msec)"
If mr.Count <> 0 Then
Set sm = mr(0).SubMatches
n = sm.Count
For i = 0 To n
placeholder = Replace("$&", "&", i, 1, i)
If i = 0 Then
matched = mr(0)
Else
matched = sm(i - 1)
End If
msg = vbTab & placeholder & " = """ & matched & """"
If i < UBound(expected) Then
if matched = expected(i) Or matched = "" And expected(i) = "(undefined)" Then
msg = msg & "; passed!"
Else
msg = msg & "; failed... (expected: """ & expected(i) & """)"
num_of_failures = num_of_failures + 1
End If
Else
msg = msg & "; failed..." ' should not exist.
num_of_failures = num_of_failures + 1
End If
If mr(0) <> "" And re.Replace(mr(0), placeholder) <> matched Then
msg = msg & "; replace failed..." ' should have yielded same result.
num_of_failures = num_of_failures + 1
End If
WScript.Echo msg
Next
Else
n = -1
End If
If num_of_failures = 0 And UBound(expected) <> n + 1 Then
num_of_failures = num_of_failures + 1
End If
WScript.Echo Replace("Result: passed.", "passed", "failed", 1, num_of_failures)
WScript.Echo
Test = 1 - Sgn(num_of_failures)
End Function
Function TestError(str, exp)
Dim re
Set re = CreateObject(RegExpClass)
re.Pattern = exp
On Error Resume Next
re.Execute str
WScript.Echo Err.Number & " from " & Err.Source & ": " & Err.Description
WScript.Echo Replace("Result: failed.", "failed", "passed", 1, Err.Number)
TestError = Sgn(Err.Number)
WScript.Echo
End Function
Function Main
Dim str, exp, expected
Dim num_of_tests
Dim num_of_tests_passed
Dim num_of_benches
Dim num_of_benches_passed
WScript.Echo "RegExpClass = " & RegExpClass
WScript.Echo
WScript.Echo "Exception Test"
str = "abc"
exp = "*"
num_of_tests_passed = num_of_tests_passed + TestError(str, exp)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 1 (ECMAScript 2021 Language Specification 22.2.2.3, NOTE)"
str = "abc"
exp = "((a)|(ab))((c)|(bc))"
ReDim expected(7)
expected(0) = "abc"
expected(1) = "a"
expected(2) = "a"
expected(3) = "(undefined)"
expected(4) = "bc"
expected(5) = "(undefined)"
expected(6) = "bc"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 2a (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 2)"
str = "abcdefghi"
exp = "a[a-z]{2,4}"
ReDim expected(1)
expected(0) = "abcde"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 2b (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 2)"
str = "abcdefghi"
exp = "a[a-z]{2,4}?"
expected(0) = "abc"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 3 (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 2)"
str = "aabaac"
exp = "(aa|aabaac|ba|b|c)*"
ReDim expected(2)
expected(0) = "aaba"
expected(1) = "ba"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 4 (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 3)"
str = "zaacbbbcac"
exp = "(z)((a+)?(b+)?(c))*"
ReDim expected(6)
expected(0) = "zaacbbbcac"
expected(1) = "z"
expected(2) = "ac"
expected(3) = "a"
expected(4) = "(undefined)"
expected(5) = "c"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 5a (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 4)"
str = "b"
exp = "(a*)*"
ReDim expected(2)
expected(0) = ""
expected(1) = ""
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 5b (ECMAScript 2021 Language Specification 22.2.2.5.1, NOTE 4)"
str = "baaaac"
exp = "(a*)b\1+"
expected(0) = "b"
expected(1) = ""
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 6a (ECMAScript 2021 Language Specification 22.2.2.8.2, NOTE 2)"
str = "baaabac"
exp = "(?=(a+))"
expected(0) = ""
expected(1) = "aaa"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 6b (ECMAScript 2021 Language Specification 22.2.2.8.2, NOTE 2)"
str = "baaabac"
exp = "(?=(a+))a*b\1"
expected(0) = "aba"
expected(1) = "a"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 7 (ECMAScript 2021 Language Specification 22.2.2.8.2, NOTE 3)"
str = "baaabaac"
exp = "(.*?)a(?!(a+)b\2c)\2(.*)"
ReDim expected(4)
expected(0) = "baaabaac"
expected(1) = "ba"
expected(2) = "(undefined)"
expected(3) = "abaac"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
WScript.Echo "Test 8 (from https://github.com/tc39/test262/tree/master/test/built-ins/RegExp/lookBehind/misc.js)"
str = "abc"
exp = "(abc\1)"
ReDim expected(2)
expected(0) = "abc"
expected(1) = "abc"
num_of_tests_passed = num_of_tests_passed + Test(str, exp, 1, expected)
num_of_tests = num_of_tests + 1
If count <> 0 Then
WScript.Echo "Benchmark 01"
'0123456'
str = "aaaabaa"
exp = "^(.*)*b\1$"
ReDim expected(2)
expected(0) = "aaaabaa"
expected(1) = "aa"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 02"
'012345678'
str = "aaaabaaaa"
exp = "^(.*)*b\1\1$"
expected(0) = "aaaabaaaa"
expected(1) = "aa"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 03"
'01'
str = "ab"
exp = "(.*?)*b\1"
expected(0) = "b"
expected(1) = ""
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 10, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 04"
'01234567'
str = "acaaabbb"
exp = "(a(.)a|\2(.)b){2}"
ReDim expected(4)
expected(0) = "aaabb"
expected(1) = "bb"
expected(2) = "(undefined)"
expected(3) = "b"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 10, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 05"
str = "aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaa"
exp = "(a*)(b)*\1\1\1"
ReDim expected(3)
expected(0) = "aabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaa"
expected(1) = "aa"
expected(2) = "b"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 06a"
str = "aaaaaaaaaab"
exp = "(.*)*b"
ReDim expected(2)
expected(0) = "aaaaaaaaaab"
expected(1) = "aaaaaaaaaa"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 10, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 06b"
str = "aaaaaaaaaab"
exp = "(.*)+b"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 10, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 06c"
str = "aaaaaaaaaab"
exp = "(.*){2,}b"
expected(1) = ""
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 10, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 07"
str = "aaaaaaaaaabc"
exp = "(?=(a+))(abc)"
ReDim expected(3)
expected(0) = "abc"
expected(1) = "a"
expected(2) = "abc"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 08"
str = "1234-5678-1234-456"
exp = "(\d{4}[-]){3}\d{3,4}"
ReDim expected(2)
expected(0) = "1234-5678-1234-456"
expected(1) = "1234-"
num_of_benches_passed = num_of_benches_passed + Test(str, exp, count * 5, expected)
num_of_benches = num_of_benches + 1
WScript.Echo "Benchmark 09"
str = "aaaaaaaaaaaaaaaaaaaaa"
exp = "(.*)*b"
ReDim expected(0)
num_of_benches_passed = num_of_benches_passed + Test(str, exp, 1, expected)
num_of_benches = num_of_benches + 1
End If
WScript.Echo "Results of tests: " & num_of_tests_passed & "/" & num_of_tests & " passed."
WScript.Echo "Results of benchmarks: " & num_of_benches_passed & "/" & num_of_benches & " passed."
Main = num_of_tests - num_of_tests_passed
End Function
wscript.Quit Main()