summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/etc/generate-lookupswitch-tests.js
blob: 2fd2b73d1cf17e484a1342da48518f7bbd414eed (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
 * A test case spec is an array of objects of the following kind:
 *  { 'match': Num|Str|Null,
 *    'body': Num|Str|Null,
 *    'fallthrough': Boolean }
 *
 * If the 'match' is null, then it represents a 'default:'
 * If the 'match' is not null, it represents a 'case X:' where X is the value.
 * If the 'body' is null, then it means that the case body is empty.  Otherwise,
 * it means that the case body is a single 'arr.push(V);' where "arr" is an input
 * array to the function containing the switch statement, and V is the value.
 * If 'fallthrough' is false, then the body is terminated with a break, otherwise
 * it is not.
 *
 * So a spec: [{'match':3, 'body':null, 'fallthrough':false}, {'match':null, 'body':"foo", 'fallthrough':true}]
 * Represents a switch function:
 *  function(x, arr) {
 *      switch(x) {
 *      case 3:
 *          break;
 *      default:
 *          arr.push('foo');
 *      }
 *  }
 *
 * GenerateSpecPermutes generates a bunch of relevant specs, using the given case match-values,
 * and appends them to result the array passed into it.
 *
 * InterpretSwitch takes a spec, a value, and a result array, and behaves as if the switch specified
 * by the spec had been called on the value and the result array.
 *
 * VerifySwitchSpec is there but not used in the code.  I was using it while testing the test case
 * generator.  It verifies that a switch spec is sane.
 *
 * RunSpec uses eval to run the test directly.  It's not used currently.
 *
 * GenerateSwitchCode generates a string of the form "function NAME(x, arg) { .... }" which
 * contains the switch modeled by its input spec.
 *
 * RunTest is there to be used from within the generated script.  Its code is dumped out
 * to the generated script text, and invoked there.
 *
 * Hope all of this makes some sort of sense.
 *  -kannan
 */

/** HELPERS **/

function ASSERT(cond, msg) { assertEq(cond, true, msg); }

function IsUndef(x) { return typeof(x) == 'undefined'; }
function IsNull(x) { return typeof(x) == 'object' && x == null; }
function IsNum(x) { return typeof(x) == 'number'; }
function IsStr(x) { return typeof(x) == 'string'; }
function IsBool(x) { return typeof(x) == 'boolean'; }

function Repr(x) {
    ASSERT(IsNum(x) || IsStr(x), "Repr");
    if(IsNum(x)) { return ""+x; }
    else { return "'"+x+"'"; }
}

function RandBool() { return Math.random() >= 0.5; }
function RandInt(max) {
    if(IsUndef(max)) { max = 0x7fffffff; }
    return (Math.random() * max)|0;
}

var CHARS = "abcdefghijklmnopqrstuvywxyzABCDEFGHIJKLMNOPQRSTUVYWXYZ0123456789~!@#$%^&*()-_=+{}[]";
function RandStr() {
    var arr = [];
    var len = Math.floor(Math.random() * 10) + 1;
    for(var i = 0; i < len; i++) {
        var c = Math.floor(Math.random() * CHARS.length);
        arr.push(CHARS[c]);
    }
    return arr.join('');
}

function RandVal() { return RandBool() ? RandInt() : RandStr(); }

/**
 * Compare two arrays and ensure they are equal.
 */
function ArraysEqual(arr1, arr2) {
    ASSERT(arr1.length == arr2.length, "Lengths not equal");
    for(var i = 0; i < arr1.length; i++) {
        ASSERT(typeof(arr1[i]) == typeof(arr2[i]), "Types not equal for position " + i);
        ASSERT(arr1[i] == arr2[i], "Values not equal for position " + i);
    }
}

function VerifySwitchSpec(spec) {
    var foundDefault = undefined;
    for(var i = 0; i < spec.length; i++) {
        var caseSpec = spec[i],
            match = caseSpec.match,
            body = caseSpec.body,
            fallthrough = caseSpec.fallthrough;
        ASSERT(IsNum(match) || IsStr(match) || IsNull(match), "Invalid case match for " + i);
        ASSERT(IsNum(body) || IsStr(body) || IsNull(body), "Invalid case body for " + i);
        ASSERT(IsBool(fallthrough), "Invalid fallthrough for " + i);

        if(IsNull(match)) {
            ASSERT(IsUndef(foundDefault), "Duplicate default found at " + i);
            foundDefault = i;
        }
    }
}

/**
 * Do a manual interpretation of a particular spec, given an input
 * and outputting to an output array.
 */
function InterpretSwitch(spec, input, outputArray) {
    var foundMatch = undefined, foundDefault = undefined;
    // Go through cases, trying to find a matching clause.
    for(var i = 0; i < spec.length; i++) {
        var caseSpec = spec[i], match = caseSpec.match;

        if(IsNull(match)) {
            foundDefault = i;
            continue;
        } else if(match === input) {
            foundMatch = i;
            break;
        }
    }
    // Select either matching clause or default.
    var matchI = IsNum(foundMatch) ? foundMatch : foundDefault;

    // If match or default was found, interpret body from that point on.
    if(IsNum(matchI)) {
        for(var i = matchI; i < spec.length; i++) {
            var caseSpec = spec[i],
                match = caseSpec.match,
                body = caseSpec.body,
                fallthrough = caseSpec.fallthrough;
            if(!IsNull(body)) { outputArray.push(body); }
            if(!fallthrough) { break; }
        }
    }
}

/**
 * Generate the code string for a javascript function containing the
 * switch specified by the spec, in pure JS syntax.
 */
function GenerateSwitchCode(spec, name) {
    var lines = [];
    if(!name) { name = ""; }

    lines.push("function "+name+"(x, arr) {");
    lines.push("    switch(x) {");
    for(var i = 0; i < spec.length; i++) {
        var caseSpec = spec[i],
            match = caseSpec.match,
            body = caseSpec.body,
            fallthrough = caseSpec.fallthrough;

        if(IsNull(match))   { lines.push("    default:"); }
        else                { lines.push("    case "+Repr(match)+":"); }

        if(!IsNull(body))   { lines.push("        arr.push("+Repr(body)+");"); }
        if(!fallthrough)    { lines.push("        break;"); }
    }
    lines.push("    }");
    lines.push("}");
    return lines.join("\n");
}

/**
 * Generates all possible specs for a given set of case match values.
 */
function GenerateSpecPermutes(matchVals, resultArray) {
    ASSERT((0 < matchVals.length) && (matchVals.length <= 5), "Invalid matchVals");
    var maxPermuteBody = (1 << matchVals.length) - 1;
    for(var bod_pm = 0; bod_pm <= maxPermuteBody; bod_pm++) {
        var maxPermuteFallthrough = (1 << matchVals.length) - 1;

        for(var ft_pm = 0; ft_pm <= maxPermuteFallthrough; ft_pm++) {
            // use bod_m and ft_pm to decide the placement of null vs value bodies,
            // and the placement of fallthroughs vs breaks.
            // Empty bodies always fall through, so fallthrough bitmask 1s must be
            // a subset of the body bitmask 1s.
            if((bod_pm | ft_pm) != bod_pm) {
                continue;
            }

            var spec = [];
            for(var k = 0; k < matchVals.length; k++) {
                var match = matchVals[k];
                var body = ((bod_pm & (1 << k)) > 0) ? null : RandVal();
                var fallthrough = ((ft_pm & (1 << k)) > 0) ? true : false;
                var caseSpec = {'match':match, 'body':body, 'fallthrough':fallthrough};
                spec.push(caseSpec);
            }

            // Variant specs for following cases:

            // Default with empty body, fallthrough
            GenerateDefaultPermutes(spec, null, true, resultArray);
            // Default with nonempty body, fallthrough
            GenerateDefaultPermutes(spec, RandVal(), true, resultArray);
            // Default with nonempty body, break
            GenerateDefaultPermutes(spec, RandVal(), false, resultArray);
        }
    }
}
function GenerateDefaultPermutes(spec, body, fallthrough, resultArray) {
    if(spec.length <= 2) {
        for(var i = 0; i <= spec.length; i++) {
            var copy = CopySpec(spec);
            if(IsNull(body)) {
                copy.splice(i,0,{'match':null, 'body':null, 'fallthrough':true});
            } else {
                copy.splice(i,0,{'match':null, 'body':body, 'fallthrough':fallthrough});
            }
            resultArray.push(copy);
        }
    } else {
        var posns = [0, Math.floor(spec.length / 2), spec.length];
        posns.forEach(function (i) {
            var copy = CopySpec(spec);
            if(IsNull(body)) {
                copy.splice(i,0,{'match':null, 'body':null, 'fallthrough':true});
            } else {
                copy.splice(i,0,{'match':null, 'body':body, 'fallthrough':fallthrough});
            }
            resultArray.push(copy);
        });
    }
}
function CopySpec(spec) {
    var newSpec = [];
    for(var i = 0; i < spec.length; i++) {
        var caseSpec = spec[i];
        newSpec.push({'match':caseSpec.match,
                      'body':caseSpec.body,
                      'fallthrough':caseSpec.fallthrough});
    }
    return newSpec;
}


function RunSpec(spec, matchVals) {
    var code = GenerateSwitchCode(spec);

    // Generate roughly 200 inputs for the test case spec, exercising
    // every match value, as well as 3 randomly generated values for every
    // iteration of the match values.
    var inputs = [];
    while(inputs.length < 500) {
        for(var i = 0; i < matchVals.length; i++) { inputs.push(matchVals[i]); }
        for(var i = 0; i < 3; i++) { inputs.push(RandVal()); }
    }

    // Interpret the lookupswitch with the inputs.
    var interpResults = [];
    for(var i = 0; i < inputs.length; i++) {
        InterpretSwitch(spec, inputs[i], interpResults);
    }

    // Run compiled lookupswitch with the inputs.
    var fn = eval("_ = " + code);
    print("Running spec: " + code);
    var compileResults = RunCompiled(fn, inputs);
    print("Done Running spec");

    // Verify that they produce the same output.
    ASSERT(interpResults.length == compileResults.length, "Length mismatch");
    for(var i = 0; i < interpResults.length; i++) {
        ASSERT(interpResults[i] == compileResults[i], "Value mismatch");
    }
}
function RunCompiled(fn, inputs) {
    var results = [];
    var len = inputs.length;
    for(var i = 0; i < len; i++) { fn(inputs[i], results); }
    return results;
}

function PrintSpec(spec, inputs, fname) {
    var code = GenerateSwitchCode(spec, fname);
    var input_s = fname + ".INPUTS = [" + inputs.map(Repr).join(', ') + "];";
    var spec_s = fname + ".SPEC = " + JSON.stringify(spec) + ";";
    print(code + "\n" + input_s + "\n" + spec_s);
}

function RunTest(test) {
    // Exercise every test case as well as one case which won't match with any of the
    // ("But what if it randomly generates a string case match whose value is
    //   UNMATCHED_CASE?!", you ask incredulously.  Well, RandStr limits itself to 11 chars.
    //   So there.)
    var inputs = test.INPUTS;
    inputs.push("UNMATCHED_CASE");
    var spec = test.SPEC;

    var results1 = [];
    for(var i = 0; i < 80; i++) {
        for(var j = 0; j < inputs.length; j++) {
            test(inputs[j], results1);
        }
    }

    var results2 = [];
    for(var i = 0; i < 80; i++) {
        for(var j = 0; j < inputs.length; j++) {
            InterpretSwitch(spec, inputs[j], results2);
        }
    }
    ArraysEqual(results1, results2);
}

// NOTES:
//  * RunTest is used within the generated test script.
//  * InterpretSwitch is printed out into the generated test script.

print("/////////////////////////////////////////");
print("// This is a generated file!");
print("// See jit-tests/etc/generate-lookupswitch-tests.js for the code");
print("// that generated this code!");
print("/////////////////////////////////////////");
print("");
print("/////////////////////////////////////////");
print("// PRELUDE                             //");
print("/////////////////////////////////////////");
print("");
print("// Avoid eager compilation of the global-scope.");
print("try{} catch (x) {};");
print("");
print(ASSERT);
print(IsNull);
print(IsNum);
print(ArraysEqual);
print(InterpretSwitch);
print(RunTest);
print("");
print("/////////////////////////////////////////");
print("// TEST CASES                          //");
print("/////////////////////////////////////////");
print("");
print("var TESTS = [];");
var MATCH_SETS = [["foo", "bar", "zing"]];
var count = 0;
for(var i = 0; i < MATCH_SETS.length; i++) {
    var matchSet = MATCH_SETS[i];
    var specs = [];
    GenerateSpecPermutes(matchSet, specs);
    for(var j = 0; j < specs.length; j++) {
        count++;
        PrintSpec(specs[j], matchSet.slice(), 'test_'+count);
        print("TESTS.push(test_"+count+");\n");
    }
}

print("");
print("/////////////////////////////////////////");
print("// RUNNER                              //");
print("/////////////////////////////////////////");
print("");
print("for(var i = 0; i < TESTS.length; i++) {");
print("  RunTest(TESTS[i]);");
print("}");