michael@0: michael@0: /** michael@0: * A test case spec is an array of objects of the following kind: michael@0: * { 'match': Num|Str|Null, michael@0: * 'body': Num|Str|Null, michael@0: * 'fallthrough': Boolean } michael@0: * michael@0: * If the 'match' is null, then it represents a 'default:' michael@0: * If the 'match' is not null, it represents a 'case X:' where X is the value. michael@0: * If the 'body' is null, then it means that the case body is empty. Otherwise, michael@0: * it means that the case body is a single 'arr.push(V);' where "arr" is an input michael@0: * array to the function containing the switch statement, and V is the value. michael@0: * If 'fallthrough' is false, then the body is terminated with a break, otherwise michael@0: * it is not. michael@0: * michael@0: * So a spec: [{'match':3, 'body':null, 'fallthrough':false}, {'match':null, 'body':"foo", 'fallthrough':true}] michael@0: * Represents a switch function: michael@0: * function(x, arr) { michael@0: * switch(x) { michael@0: * case 3: michael@0: * break; michael@0: * default: michael@0: * arr.push('foo'); michael@0: * } michael@0: * } michael@0: * michael@0: * GenerateSpecPermutes generates a bunch of relevant specs, using the given case match-values, michael@0: * and appends them to result the array passed into it. michael@0: * michael@0: * InterpretSwitch takes a spec, a value, and a result array, and behaves as if the switch specified michael@0: * by the spec had been called on the value and the result array. michael@0: * michael@0: * VerifySwitchSpec is there but not used in the code. I was using it while testing the test case michael@0: * generator. It verifies that a switch spec is sane. michael@0: * michael@0: * RunSpec uses eval to run the test directly. It's not used currently. michael@0: * michael@0: * GenerateSwitchCode generates a string of the form "function NAME(x, arg) { .... }" which michael@0: * contains the switch modeled by its input spec. michael@0: * michael@0: * RunTest is there to be used from within the generated script. Its code is dumped out michael@0: * to the generated script text, and invoked there. michael@0: * michael@0: * Hope all of this makes some sort of sense. michael@0: * -kannan michael@0: */ michael@0: michael@0: /** HELPERS **/ michael@0: michael@0: function ASSERT(cond, msg) { assertEq(cond, true, msg); } michael@0: michael@0: function IsUndef(x) { return typeof(x) == 'undefined'; } michael@0: function IsNull(x) { return typeof(x) == 'object' && x == null; } michael@0: function IsNum(x) { return typeof(x) == 'number'; } michael@0: function IsStr(x) { return typeof(x) == 'string'; } michael@0: function IsBool(x) { return typeof(x) == 'boolean'; } michael@0: michael@0: function Repr(x) { michael@0: ASSERT(IsNum(x) || IsStr(x), "Repr"); michael@0: if(IsNum(x)) { return ""+x; } michael@0: else { return "'"+x+"'"; } michael@0: } michael@0: michael@0: function RandBool() { return Math.random() >= 0.5; } michael@0: function RandInt(max) { michael@0: if(IsUndef(max)) { max = 0x7fffffff; } michael@0: return (Math.random() * max)|0; michael@0: } michael@0: michael@0: var CHARS = "abcdefghijklmnopqrstuvywxyzABCDEFGHIJKLMNOPQRSTUVYWXYZ0123456789~!@#$%^&*()-_=+{}[]"; michael@0: function RandStr() { michael@0: var arr = []; michael@0: var len = Math.floor(Math.random() * 10) + 1; michael@0: for(var i = 0; i < len; i++) { michael@0: var c = Math.floor(Math.random() * CHARS.length); michael@0: arr.push(CHARS[c]); michael@0: } michael@0: return arr.join(''); michael@0: } michael@0: michael@0: function RandVal() { return RandBool() ? RandInt() : RandStr(); } michael@0: michael@0: /** michael@0: * Compare two arrays and ensure they are equal. michael@0: */ michael@0: function ArraysEqual(arr1, arr2) { michael@0: ASSERT(arr1.length == arr2.length, "Lengths not equal"); michael@0: for(var i = 0; i < arr1.length; i++) { michael@0: ASSERT(typeof(arr1[i]) == typeof(arr2[i]), "Types not equal for position " + i); michael@0: ASSERT(arr1[i] == arr2[i], "Values not equal for position " + i); michael@0: } michael@0: } michael@0: michael@0: function VerifySwitchSpec(spec) { michael@0: var foundDefault = undefined; michael@0: for(var i = 0; i < spec.length; i++) { michael@0: var caseSpec = spec[i], michael@0: match = caseSpec.match, michael@0: body = caseSpec.body, michael@0: fallthrough = caseSpec.fallthrough; michael@0: ASSERT(IsNum(match) || IsStr(match) || IsNull(match), "Invalid case match for " + i); michael@0: ASSERT(IsNum(body) || IsStr(body) || IsNull(body), "Invalid case body for " + i); michael@0: ASSERT(IsBool(fallthrough), "Invalid fallthrough for " + i); michael@0: michael@0: if(IsNull(match)) { michael@0: ASSERT(IsUndef(foundDefault), "Duplicate default found at " + i); michael@0: foundDefault = i; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Do a manual interpretation of a particular spec, given an input michael@0: * and outputting to an output array. michael@0: */ michael@0: function InterpretSwitch(spec, input, outputArray) { michael@0: var foundMatch = undefined, foundDefault = undefined; michael@0: // Go through cases, trying to find a matching clause. michael@0: for(var i = 0; i < spec.length; i++) { michael@0: var caseSpec = spec[i], match = caseSpec.match; michael@0: michael@0: if(IsNull(match)) { michael@0: foundDefault = i; michael@0: continue; michael@0: } else if(match === input) { michael@0: foundMatch = i; michael@0: break; michael@0: } michael@0: } michael@0: // Select either matching clause or default. michael@0: var matchI = IsNum(foundMatch) ? foundMatch : foundDefault; michael@0: michael@0: // If match or default was found, interpret body from that point on. michael@0: if(IsNum(matchI)) { michael@0: for(var i = matchI; i < spec.length; i++) { michael@0: var caseSpec = spec[i], michael@0: match = caseSpec.match, michael@0: body = caseSpec.body, michael@0: fallthrough = caseSpec.fallthrough; michael@0: if(!IsNull(body)) { outputArray.push(body); } michael@0: if(!fallthrough) { break; } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Generate the code string for a javascript function containing the michael@0: * switch specified by the spec, in pure JS syntax. michael@0: */ michael@0: function GenerateSwitchCode(spec, name) { michael@0: var lines = []; michael@0: if(!name) { name = ""; } michael@0: michael@0: lines.push("function "+name+"(x, arr) {"); michael@0: lines.push(" switch(x) {"); michael@0: for(var i = 0; i < spec.length; i++) { michael@0: var caseSpec = spec[i], michael@0: match = caseSpec.match, michael@0: body = caseSpec.body, michael@0: fallthrough = caseSpec.fallthrough; michael@0: michael@0: if(IsNull(match)) { lines.push(" default:"); } michael@0: else { lines.push(" case "+Repr(match)+":"); } michael@0: michael@0: if(!IsNull(body)) { lines.push(" arr.push("+Repr(body)+");"); } michael@0: if(!fallthrough) { lines.push(" break;"); } michael@0: } michael@0: lines.push(" }"); michael@0: lines.push("}"); michael@0: return lines.join("\n"); michael@0: } michael@0: michael@0: /** michael@0: * Generates all possible specs for a given set of case match values. michael@0: */ michael@0: function GenerateSpecPermutes(matchVals, resultArray) { michael@0: ASSERT((0 < matchVals.length) && (matchVals.length <= 5), "Invalid matchVals"); michael@0: var maxPermuteBody = (1 << matchVals.length) - 1; michael@0: for(var bod_pm = 0; bod_pm <= maxPermuteBody; bod_pm++) { michael@0: var maxPermuteFallthrough = (1 << matchVals.length) - 1; michael@0: michael@0: for(var ft_pm = 0; ft_pm <= maxPermuteFallthrough; ft_pm++) { michael@0: // use bod_m and ft_pm to decide the placement of null vs value bodies, michael@0: // and the placement of fallthroughs vs breaks. michael@0: // Empty bodies always fall through, so fallthrough bitmask 1s must be michael@0: // a subset of the body bitmask 1s. michael@0: if((bod_pm | ft_pm) != bod_pm) { michael@0: continue; michael@0: } michael@0: michael@0: var spec = []; michael@0: for(var k = 0; k < matchVals.length; k++) { michael@0: var match = matchVals[k]; michael@0: var body = ((bod_pm & (1 << k)) > 0) ? null : RandVal(); michael@0: var fallthrough = ((ft_pm & (1 << k)) > 0) ? true : false; michael@0: var caseSpec = {'match':match, 'body':body, 'fallthrough':fallthrough}; michael@0: spec.push(caseSpec); michael@0: } michael@0: michael@0: // Variant specs for following cases: michael@0: michael@0: // Default with empty body, fallthrough michael@0: GenerateDefaultPermutes(spec, null, true, resultArray); michael@0: // Default with nonempty body, fallthrough michael@0: GenerateDefaultPermutes(spec, RandVal(), true, resultArray); michael@0: // Default with nonempty body, break michael@0: GenerateDefaultPermutes(spec, RandVal(), false, resultArray); michael@0: } michael@0: } michael@0: } michael@0: function GenerateDefaultPermutes(spec, body, fallthrough, resultArray) { michael@0: if(spec.length <= 2) { michael@0: for(var i = 0; i <= spec.length; i++) { michael@0: var copy = CopySpec(spec); michael@0: if(IsNull(body)) { michael@0: copy.splice(i,0,{'match':null, 'body':null, 'fallthrough':true}); michael@0: } else { michael@0: copy.splice(i,0,{'match':null, 'body':body, 'fallthrough':fallthrough}); michael@0: } michael@0: resultArray.push(copy); michael@0: } michael@0: } else { michael@0: var posns = [0, Math.floor(spec.length / 2), spec.length]; michael@0: posns.forEach(function (i) { michael@0: var copy = CopySpec(spec); michael@0: if(IsNull(body)) { michael@0: copy.splice(i,0,{'match':null, 'body':null, 'fallthrough':true}); michael@0: } else { michael@0: copy.splice(i,0,{'match':null, 'body':body, 'fallthrough':fallthrough}); michael@0: } michael@0: resultArray.push(copy); michael@0: }); michael@0: } michael@0: } michael@0: function CopySpec(spec) { michael@0: var newSpec = []; michael@0: for(var i = 0; i < spec.length; i++) { michael@0: var caseSpec = spec[i]; michael@0: newSpec.push({'match':caseSpec.match, michael@0: 'body':caseSpec.body, michael@0: 'fallthrough':caseSpec.fallthrough}); michael@0: } michael@0: return newSpec; michael@0: } michael@0: michael@0: michael@0: function RunSpec(spec, matchVals) { michael@0: var code = GenerateSwitchCode(spec); michael@0: michael@0: // Generate roughly 200 inputs for the test case spec, exercising michael@0: // every match value, as well as 3 randomly generated values for every michael@0: // iteration of the match values. michael@0: var inputs = []; michael@0: while(inputs.length < 500) { michael@0: for(var i = 0; i < matchVals.length; i++) { inputs.push(matchVals[i]); } michael@0: for(var i = 0; i < 3; i++) { inputs.push(RandVal()); } michael@0: } michael@0: michael@0: // Interpret the lookupswitch with the inputs. michael@0: var interpResults = []; michael@0: for(var i = 0; i < inputs.length; i++) { michael@0: InterpretSwitch(spec, inputs[i], interpResults); michael@0: } michael@0: michael@0: // Run compiled lookupswitch with the inputs. michael@0: var fn = eval("_ = " + code); michael@0: print("Running spec: " + code); michael@0: var compileResults = RunCompiled(fn, inputs); michael@0: print("Done Running spec"); michael@0: michael@0: // Verify that they produce the same output. michael@0: ASSERT(interpResults.length == compileResults.length, "Length mismatch"); michael@0: for(var i = 0; i < interpResults.length; i++) { michael@0: ASSERT(interpResults[i] == compileResults[i], "Value mismatch"); michael@0: } michael@0: } michael@0: function RunCompiled(fn, inputs) { michael@0: var results = []; michael@0: var len = inputs.length; michael@0: for(var i = 0; i < len; i++) { fn(inputs[i], results); } michael@0: return results; michael@0: } michael@0: michael@0: function PrintSpec(spec, inputs, fname) { michael@0: var code = GenerateSwitchCode(spec, fname); michael@0: var input_s = fname + ".INPUTS = [" + inputs.map(Repr).join(', ') + "];"; michael@0: var spec_s = fname + ".SPEC = " + JSON.stringify(spec) + ";"; michael@0: print(code + "\n" + input_s + "\n" + spec_s); michael@0: } michael@0: michael@0: function RunTest(test) { michael@0: // Exercise every test case as well as one case which won't match with any of the michael@0: // ("But what if it randomly generates a string case match whose value is michael@0: // UNMATCHED_CASE?!", you ask incredulously. Well, RandStr limits itself to 11 chars. michael@0: // So there.) michael@0: var inputs = test.INPUTS; michael@0: inputs.push("UNMATCHED_CASE"); michael@0: var spec = test.SPEC; michael@0: michael@0: var results1 = []; michael@0: for(var i = 0; i < 80; i++) { michael@0: for(var j = 0; j < inputs.length; j++) { michael@0: test(inputs[j], results1); michael@0: } michael@0: } michael@0: michael@0: var results2 = []; michael@0: for(var i = 0; i < 80; i++) { michael@0: for(var j = 0; j < inputs.length; j++) { michael@0: InterpretSwitch(spec, inputs[j], results2); michael@0: } michael@0: } michael@0: ArraysEqual(results1, results2); michael@0: } michael@0: michael@0: // NOTES: michael@0: // * RunTest is used within the generated test script. michael@0: // * InterpretSwitch is printed out into the generated test script. michael@0: michael@0: print("/////////////////////////////////////////"); michael@0: print("// This is a generated file!"); michael@0: print("// See jit-tests/etc/generate-lookupswitch-tests.js for the code"); michael@0: print("// that generated this code!"); michael@0: print("/////////////////////////////////////////"); michael@0: print(""); michael@0: print("/////////////////////////////////////////"); michael@0: print("// PRELUDE //"); michael@0: print("/////////////////////////////////////////"); michael@0: print(""); michael@0: print("// Avoid eager compilation of the global-scope."); michael@0: print("try{} catch (x) {};"); michael@0: print(""); michael@0: print(ASSERT); michael@0: print(IsNull); michael@0: print(IsNum); michael@0: print(ArraysEqual); michael@0: print(InterpretSwitch); michael@0: print(RunTest); michael@0: print(""); michael@0: print("/////////////////////////////////////////"); michael@0: print("// TEST CASES //"); michael@0: print("/////////////////////////////////////////"); michael@0: print(""); michael@0: print("var TESTS = [];"); michael@0: var MATCH_SETS = [["foo", "bar", "zing"]]; michael@0: var count = 0; michael@0: for(var i = 0; i < MATCH_SETS.length; i++) { michael@0: var matchSet = MATCH_SETS[i]; michael@0: var specs = []; michael@0: GenerateSpecPermutes(matchSet, specs); michael@0: for(var j = 0; j < specs.length; j++) { michael@0: count++; michael@0: PrintSpec(specs[j], matchSet.slice(), 'test_'+count); michael@0: print("TESTS.push(test_"+count+");\n"); michael@0: } michael@0: } michael@0: michael@0: print(""); michael@0: print("/////////////////////////////////////////"); michael@0: print("// RUNNER //"); michael@0: print("/////////////////////////////////////////"); michael@0: print(""); michael@0: print("for(var i = 0; i < TESTS.length; i++) {"); michael@0: print(" RunTest(TESTS[i]);"); michael@0: print("}");