Skip to content

Commit

Permalink
tweak ufuzz test case generation (#5615)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Aug 11, 2022
1 parent 503532c commit 612701a
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions test/ufuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,16 @@ var in_class = 0;
var called = Object.create(null);
var labels = 10000;

function rng(max) {
function rng(limit) {
var r = randomBytes(2).readUInt16LE(0) / 65536;
return Math.floor(max * r);
return Math.floor(limit * r);
}

function get_num(max) {
if (rng(max + 1) == 0) return 0;
var i = 1;
while (i < max && rng(2)) i++;
return i;
}

function strictMode() {
Expand Down Expand Up @@ -438,9 +445,8 @@ function createTopLevelCode() {
function createFunctions(n, recurmax, allowDefun, canThrow, stmtDepth) {
if (--recurmax < 0) { return ";"; }
var s = "";
while (n-- > 0) {
for (var i = get_num(n - 1) + 1; --i >= 0;) {
s += createFunction(recurmax, allowDefun, canThrow, stmtDepth) + "\n";
if (rng(2)) break;
}
return s;
}
Expand All @@ -456,7 +462,7 @@ function createParams(was_async, was_generator, noDuplicate) {
if (!generator) generator = was_generator;
var len = unique_vars.length;
var params = [];
for (var n = 3; --n >= 0 && rng(2);) {
for (var i = get_num(3); --i >= 0;) {
var name = createVarName(MANDATORY);
if (noDuplicate || in_class) unique_vars.push(name);
params.push(name);
Expand All @@ -471,7 +477,7 @@ function createArgs(recurmax, stmtDepth, canThrow, noTemplate) {
recurmax--;
if (SUPPORT.template && !noTemplate && rng(20) == 0) return createTemplateLiteral(recurmax, stmtDepth, canThrow);
var args = [];
for (var n = 3; --n >= 0 && rng(2);) switch (SUPPORT.spread ? rng(50) : 3) {
for (var i = get_num(3); --i >= 0;) switch (SUPPORT.spread ? rng(50) : 3) {
case 0:
case 1:
var name = getVarName();
Expand Down Expand Up @@ -881,11 +887,10 @@ function createFunction(recurmax, allowDefun, canThrow, stmtDepth) {
}

function _createStatements(n, recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) {
if (--recurmax < 0) { return ";"; }
if (--recurmax < 0) return ";";
var s = "";
while (--n > 0) {
for (var i = get_num(n); --i >= 0;) {
s += createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "\n";
if (rng(2)) break;
}
return s;
}
Expand Down Expand Up @@ -1078,7 +1083,7 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn
s += " * as " + createImportAlias();
} else {
var names = [];
for (var i = 3; --i >= 0 && rng(2);) {
for (var i = get_num(3); --i >= 0;) {
var name = createImportAlias();
names.push(rng(2) ? getDotKey() + " as " + name : name);
}
Expand Down Expand Up @@ -1418,7 +1423,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
var add_new_target = SUPPORT.new_target && VALUES.indexOf("new.target") < 0;
if (add_new_target) VALUES.push("new.target");
s.push(defns());
if (instantiate) for (var i = 3; --i >= 0 && rng(2);) {
if (instantiate) for (var i = get_num(3); --i >= 0;) {
s.push((in_class ? "if (this) " : "") + createThisAssignment(recurmax, stmtDepth, canThrow));
}
s.push(_createStatements(5, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth));
Expand Down Expand Up @@ -1567,7 +1572,7 @@ function _createExpression(recurmax, noComma, stmtDepth, canThrow) {
function createArrayLiteral(recurmax, stmtDepth, canThrow) {
recurmax--;
var arr = [];
for (var i = 5; --i >= 0 && rng(2);) switch (SUPPORT.spread ? rng(50) : 3 + rng(47)) {
for (var i = get_num(5); --i >= 0;) switch (SUPPORT.spread ? rng(50) : 3 + rng(47)) {
case 0:
case 1:
var name = getVarName();
Expand Down Expand Up @@ -1596,7 +1601,7 @@ function createTemplateLiteral(recurmax, stmtDepth, canThrow) {
recurmax--;
var s = [];
addText();
for (var i = 5; --i >= 0 && rng(2);) {
for (var i = get_num(5); --i >= 0;) {
s.push("${", createExpression(recurmax, COMMA_OK, stmtDepth, canThrow), "}");
addText();
}
Expand Down Expand Up @@ -1751,7 +1756,7 @@ function createObjectFunction(recurmax, stmtDepth, canThrow, internal, isClazz)
s.push(_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CANNOT_RETURN, stmtDepth));
if (internal == "super") s.push("super" + createArgs(recurmax, stmtDepth, canThrow, NO_TEMPLATE) + ";");
allow_this = save_allow;
if (/^(constructor|super)$/.test(internal) || rng(10) == 0) for (var i = 3; --i >= 0 && rng(2);) {
if (/^(constructor|super)$/.test(internal) || rng(10) == 0) for (var i = get_num(3); --i >= 0;) {
s.push(rng(2) ? createSuperAssignment(recurmax, stmtDepth, canThrow) : createThisAssignment(recurmax, stmtDepth, canThrow));
}
s.push(_createStatements(2, recurmax, canThrow, CANNOT_BREAK, CANNOT_CONTINUE, CAN_RETURN, stmtDepth), "}");
Expand All @@ -1770,7 +1775,7 @@ function createObjectLiteral(recurmax, stmtDepth, canThrow) {
var obj = [ "({" ];
var offset = SUPPORT.spread_object ? 0 : SUPPORT.computed_key ? 2 : 4;
var has_proto = false;
for (var i = 5; --i >= 0 && rng(2);) switch (offset + rng(50 - offset)) {
for (var i = get_num(5); --i >= 0;) switch (offset + rng(50 - offset)) {
case 0:
obj.push("..." + getVarName() + ",");
break;
Expand Down Expand Up @@ -1817,7 +1822,7 @@ function createClassLiteral(recurmax, stmtDepth, canThrow, name) {
}
s += " {\n";
var declared = [];
for (var i = 5; --i >= 0 && rng(2);) {
for (var i = get_num(5); --i >= 0;) {
var fixed = false;
if (rng(5) == 0) {
fixed = true;
Expand Down

0 comments on commit 612701a

Please sign in to comment.