Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proximity as a feedback loop #14

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions lib/addRequireHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function addDefaultIgnores() {
]);
shallowIgnore.add([
'test',
'*.spec.js'
'*.spec.js',
'../../unexpected-check/lib/unexpected-check.js'
]);
}

Expand Down Expand Up @@ -60,8 +61,8 @@ let locations;
let prevLocation;

function initialize() {
global.recordLocation.locations = locations = {};
global.recordProximity.proximity = [];
global.recordLocation.locations = locations = Object.create(null);
global.recordProximity.proximity = Object.create(null);
prevLocation = 0;
}

Expand Down Expand Up @@ -91,20 +92,25 @@ function evaluate(left, operator, right) {
}

function calculateNumberProximity(left, operator, right) {
var difference = Math.abs(left - right);
if (difference > 1000) {
return null;
}

switch (operator) {
case '!==':
return Number.isInteger(left) && Number.isInteger(right) ? 1 : null;
case '===':
case '==':
return Number.isInteger(left) && Number.isInteger(right)
? Math.abs(left - right)
? difference
: null;
case '<':
case '>':
return Math.abs(left - right);
return difference;
case '<=':
case '>=':
return Math.abs(left - right) + 1;
return difference + 1;
default:
return null;
}
Expand All @@ -117,15 +123,18 @@ function calculateStringProximity(left, operator, right) {
case '===':
case '==':
const lengthDifference = Math.abs(left.length - right.length);
const limit = 30;

let difference = lengthDifference;
for (var i = 0; i < Math.min(left.length, right.length) && lengthDifference <= 100; i += 1) {
if (left[i] !== right[i]) {
difference++;
if (difference >= limit) {
for (var i = 0; i < Math.min(left.length, right.length); i += 1) {
if (left[i] !== right[i]) {
difference++;
}
}
}

return difference <= 100 ? difference : null;
return difference > limit ? null : difference;
default:
return null;
}
Expand All @@ -136,18 +145,20 @@ global.recordProximity = (left, operator, right) => {
const leftType = typeof left;
const rightType = typeof right;

if (leftType === 'number' && rightType === 'number') {
const proximity = result ? 0 : calculateNumberProximity(left, operator, right);
if (proximity !== null) {
global.recordProximity.proximity.push(proximity);
}
} else if (leftType === 'string' && rightType === 'string') {
const proximity = result ? 0 : calculateStringProximity(left, operator, right);
if (proximity !== null) {
global.recordProximity.proximity.push(proximity);
let proximity = null;

if (!result) {
if (leftType === 'number' && rightType === 'number') {
proximity = calculateNumberProximity(left, operator, right);
} else if (leftType === 'string' && rightType === 'string') {
proximity = calculateStringProximity(left, operator, right);
}
}

if (proximity !== null && proximity > 0) {
global.recordProximity.proximity[proximity] = (global.recordProximity.proximity[proximity] || 0) + 1;
}

return result;
};

Expand Down Expand Up @@ -182,6 +193,7 @@ require.extensions['.js'] = function (module, absoluteFileName) {
if (deepIgnore.ignores(fileNameToCheck) || (module.parent && module.parent._unexpectedCheckDeepIgnored)) {
module._unexpectedCheckDeepIgnored = true;
} else if (!shallowIgnore.ignores(fileNameToCheck)) {
console.log('instrument', fileNameToCheck);
const ast = esprima.parseScript(code, {
source: pathModule.relative(process.cwd(), absoluteFileName)
});
Expand Down
66 changes: 47 additions & 19 deletions lib/unexpected-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,42 @@
}

function copyProximity() {
return [].concat(global.recordProximity.proximity).sort(function (a, b) {
var proximity = global.recordProximity.proximity;
var aKeys = Object.keys(proximity).map(Number).sort(function (a, b) {
return a - b;
});

return aKeys.reduce(function (result, key) {
result[key] = proximity[key];
return result;
}, {});
}

function isProximityLessThan(a, b) {
for (var i = 0; i < Math.min(a.length, b.length); i += 1) {
if (a[i] < b[i]) {
// keys are lready ordered
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);

for (var i = 0; i < aKeys.length; i += 1) {
var aKey = Number(aKeys[i]);
var bKey = Number(bKeys[i]);

if (aKey < bKey) {
return true;
} else if (a[i] > b[i]) {
} else if (aKey > bKey) {
return false;
}

var aCount = a[aKey];
var bCount = b[bKey] || 0;
if (aCount > bCount) {
return true;
} else if (aCount < bCount) {
return false;
}
}
return a.length > b.length;

return aKeys.length > bKeys.length;
}

return {
Expand Down Expand Up @@ -151,10 +173,10 @@
var j = 0;

function calculateInvestigations() {
return Math.floor((maxIterations - i) * 0.1);
return Math.min(Math.floor((maxIterations - i) * 0.1), 10000);
}

var investigations = Math.ceil(maxIterations * 0.01);
var investigations = Math.min(Math.ceil(maxIterations * 0.01), 1000);

return promiseLoop(function () {
return (
Expand Down Expand Up @@ -183,32 +205,38 @@
investigations--;

if (interestingInput) {
if (investigations > 0) {
var proximity = copyProximity();
if (isProximityLessThan(proximity, interestingInput.proximity)) {
interestingInput.deadEnd = false;
interestingInput.input = task.args;
interestingInput.proximity = proximity;
}
}

if (interestingInput.deadEnd || investigations === 0) {
investigations = calculateInvestigations();

// TODO check the iterations
interestingInput.deadEnd = true;

var keys = Object.keys(interestingInputs).filter(function (key) {
return !interestingInputs[key].deadEnd;
});
var keys = Object.keys(interestingInputs);

if (keys.length > 0) {
var newInterestingInput = interestingInputs[keys[keys.length - 1]];
var newInterestingInput = null;
for (var i = keys.length - 1; i >= 0 && !newInterestingInput; i--) {
var candidate = interestingInputs[keys[i]];
if (!candidate.deadEnd) {
newInterestingInput = candidate;
}
}

if (newInterestingInput) {
generators = (options.generators || []).map(function (g, i) {
return g.expand ? g.expand(newInterestingInput.input[i]) : g;
});
} else {
generators = (options.generators || []);
}
} else {
var proximity = copyProximity();
if (isProximityLessThan(proximity, interestingInput.proximity)) {
// TODO consider to remove the deadEnd flag as well.
interestingInput.input = task.args;
interestingInput.proximity = proximity;
}
}
} else {
interestingInputs[key] = {
Expand Down