Skip to content

Commit

Permalink
Merge pull request #1 from happo/dynamic-drift
Browse files Browse the repository at this point in the history
Apply drift for lcs algorithm dynamically (based on image height)
  • Loading branch information
trotzig authored Jul 4, 2019
2 parents 1dd17ef + cafcbc1 commit 0e075b0
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 3 deletions.
10 changes: 10 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:10
working_directory: ~/repo
steps:
- checkout
- run: yarn install
- run: yarn test
Binary file added snapshots/logo/after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/logo/before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/logo/diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/long-example/after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/long-example/before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added snapshots/long-example/diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions src/__tests__/snapshots-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const crypto = require('crypto');
const childProcess = require('child_process');
const fs = require('fs');
const path = require('path');

const Jimp = require('jimp');

const imageDiff = require('../');

function hashFunction(data) {
return crypto
.createHash('md5')
.update(data)
.digest('hex');
}

const snapshots = childProcess
.execSync('ls snapshots', { encoding: 'utf-8' })
.split(/\n/)
.filter(Boolean);

jest.setTimeout(60000);

describe('snapshot tests', () => {
snapshots.forEach(snapshot => {
it(snapshot, async () => {
console.log('Starting', snapshot);
const [image1, image2] = await Promise.all([
Jimp.read(path.resolve('snapshots', snapshot, 'before.png')),
Jimp.read(path.resolve('snapshots', snapshot, 'after.png')),
]);
console.log('Images ready', snapshot);
const diffImage = imageDiff(image1.bitmap, image2.bitmap, {
hashFunction,
});

console.log('Created diff image', snapshot);
const pathToDiff = path.resolve('snapshots', snapshot, 'diff.png');

if (!fs.existsSync(pathToDiff)) {
console.log(
`No previous diff image for ${snapshot} found -- saving diff.png.`,
);
const newDiffImage = await new Jimp(diffImage);
await newDiffImage.write(pathToDiff);
}
const expectedDiffImage = (await Jimp.read(pathToDiff)).bitmap;
const diffHash = hashFunction(diffImage.data);
const expectedHash = hashFunction(expectedDiffImage.data);
if (diffHash !== expectedHash) {
console.log(
`Diff image did not match existing diff image. Remove this image and run again to re-generate:\n${pathToDiff}`,
);
}
expect(diffHash).toEqual(expectedHash);
});
});
});
10 changes: 7 additions & 3 deletions src/alignArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const MOVEMENT = {
};

const PLACEHOLDER = '+';
const DRIFT_RANGE = 200;
const MIN_DRIFT_RANGE = 200;

/**
* Creates a 2d matrix of a certain size.
Expand Down Expand Up @@ -38,11 +38,15 @@ function longestCommonSubsequence(a, b) {
const memo = initMatrix(aLength + 1, bLength + 1);
const solution = initMatrix(aLength + 1, bLength + 1);

const usedDriftRange = Math.abs(
Math.max(Math.max(aLength, bLength) / 10, MIN_DRIFT_RANGE),
);

// Loop and find the solution
for (let i = 1; i <= aLength; i += 1) {
for (
let j = Math.max(1, i - (DRIFT_RANGE / 2));
j <= Math.min(bLength, i + (DRIFT_RANGE / 2));
let j = Math.max(1, i - usedDriftRange / 2);
j <= Math.min(bLength, i + usedDriftRange / 2);
j += 1
) {
if (a[i - 1] === b[j - 1]) {
Expand Down

0 comments on commit 0e075b0

Please sign in to comment.