forked from schwers-zz/RedRacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstest.js
224 lines (188 loc) · 5.82 KB
/
jstest.js
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
// Testing javascript regexp times
var numtests = 20;
var all_at_once = true;
// Run some tests and give output
function test(re, stringbuilder, text, check_for, max, min) {
max = max || 20;
min = min || 10;
var results = new Array();
var test_regexp = all_at_once ? all_at_once_inner : individual_inner;
print("\n\n");
for (var i = min; i <= max; i++) {
var current_result = test_regexp(re, stringbuilder, i, check_for);
results.push(current_result);
}
print_results(results, text);
}
/* regexp * (int -> string) * int * bool -> [int, int, int, bool] */
function all_at_once_inner(re, stringbuilder, i, shouldbe){
var string = stringbuilder(i);
var result = shouldbe;
var times = new Array();
for (var k = 0; k < numtests; k++){
var t1 = (new Date()).getTime();
for (var j = 0; j < numtests; j++){
result &= (shouldbe == re.test(string));
}
var t2 = (new Date()).getTime();
times.push((t2 - t1) / numtests);
}
var temp = [string.length];
mean_variance(times, temp);
temp.push(result);
return temp;
}
/* regexp * (int -> string) * int * bool -> [int, int, int, bool] */
function individual_inner(re, stringbuilder, i, shouldbe){
var string = stringbiulder(i);
var result = shouldbe;
var times = new Array();
for (var j = 0; j < numtests; j++) {
var t1 = (new Date()).getTime();
result &= (shouldbe == re.test(string));
var t2 = (new Date()).getTime();
times.push(t2 - t2);
}
var temp = [string.length];
mean_variance(times, temp);
temp.push(result);
return temp;
}
function mean_variance(numbers, place){
var length = numbers.length;
var xs = 0; var x2s = 0;
for (var i = 0; i < length; i++) {
var x = numbers[i];
xs += x; x2s = ( x * x );
}
var mean = (xs / length);
var vari = (x2s / length) - (mean * mean);
place.push(mean);
place.push(vari);
}
/* Expects an array of sub-array with four elements [sizemean,variance,passed?] */
function print_results(result_array, test_description){
var line0 = doublequote("Size of Input:") + ";";
var line1 = doublequote("Mean time:") + ";";
var line2 = doublequote("Vari time:") + ";";
var line3 = doublequote("Test passed?:") + ";";
var length = result_array.length;
for (var i = 0; i < length; i++){
var res = result_array[i];
line0 += (doublequote(res[0]) + ";");
line1 += (doublequote(res[1]) + ";");
line2 += (doublequote(res[2]) + ";");
line3 += (doublequote(passfail(res[3])) + ";");
}
print(doublequote("Test" + test_description) + "\n" +
line0 + "\n" +
line1 + "\n" +
line2 + "\n" +
line3);
}
function doublequote(val){ return "\"" + val + "\""; }
function passfail(bool){ if (bool) { return "Passed"; } { return "Failed"; } }
// build large strings by powers of two of the input string
function buildByTwos(str, pow) {
if (pow <= 0) { return str;}
else { return buildByTwos(str + str, pow - 1);}
}
// swap the first character with a random one
function swap(str) {
var pos = Math.floor(Math.random() * (str.length - 1)) + 1;
var chr1 = str.substring(0,1);
var chr2 = str.substring(pos,pos + 1);
var middle = str.substring(1,pos);
var end = str.substring(pos + 1,str.length);
return chr2 + middle + chr1 + end;
}
// make a random permuation of a string by swapping all
// the way down a string
function permute(str) {
var res = str;
var len = res.length;
var temp = "";
for (var i = 0; i < (len - 1); i++){
temp = res.substring(0,i) + swap(res.substring(i,len));
res = temp;
}
return res;
}
function buildPermutations(str,n){
var res = [str];
for (var i = 1; i < n; i++){
res[i] = permute(str);
}
return res;
}
function insertRandom(str,ins){
var r = (Math.random() * (str.length - 1)) + 1;
return str.substring(0,r) + ins + str.substring(r+1,str.length);
}
function insertMiddle(str,ins){
var len = str.length;
var mid = Math.round(len / 2);
return str.substring(0,mid) + ins + str.substring(mid, len);
}
function permutesThenInsert(str, ins, n){
var res = buildPermutations(str,n);
for (var i = 0; i < res.length; i++){
res[i] = insertRandom(res[i], ins);
}
return res;
}
function insertToAll(strs,ins){
for (var i = 0; i < strs.length; i++){
strs[i] = insertMiddle(strs[i], ins);
}
return strs;
}
function beforeAndAfter(str,pre,post){
return pre + str + post;
}
function windAll(strs,pre,post){
for (var i = 0; i < strs.length; i++){
strs[i] = beforeAndAfter(strs[i],pre,post);
}
return strs;
}
function permutesInMiddle(str,pre,post,n){
var res = buildPermutations(str,n);
for (var i = 0; i < res.length; i++){
res[i] = beforeAndAfter(res[i],pre,post);
}
return res;
}
var re0 = /.*[email protected].*$/;
var re1 = /^a*/;
var re2 = /\(a+\)/;
var re3 = /^www\.[a-z]+\.[a-z]{2,4}/;
var re4 = /[a-z0-9\_\%\+\-\.]+@[a-z0-9\.\_]+\.[a-z]{2,4}/;
function as(n){
return buildByTwos("a", n);
}
function aemailsa(n){
var s = as(n);
return s + "[email protected]" + s;
}
function parens(n){
var s = as(n);
return "(" + s + ")";
}
function urls(n){
var s = buildByTwos("lambdafxxfxoisadf", n);
return "www." + s + ".com";
}
function emails(n){
var s = buildByTwos("this.is.a.test.right", n);
return s + "@yup.com";
}
function runTests(){
/* make sure the regexs are compiled */
re0.compile; re1.compile; re2.compile; re3.compile; re4.compile;
test(re0, aemailsa, " a*emaila*", true);
test(re1, as, " a* only", true);
test(re2, parens, " (a+)", true);
test(re4, emails, " email validation", true);
test(re3, urls, " url validation", true);
}