Skip to content

Commit

Permalink
tests: cleanup/polish
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jan 14, 2022
1 parent fcfeef1 commit e4e6616
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 66 deletions.
11 changes: 6 additions & 5 deletions test/brightness.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ function createTest(color) {

let test_name = `test ${SRC_W}x${SRC_H} -> ${DEST_W}x${DEST_H} with color #${hexColor}${hexColor}${hexColor}`;

it(test_name, function () {
return pica.resizeBuffer({
it(test_name, async () => {
const result = await pica.resizeBuffer({
src: src,
width: SRC_W,
height: SRC_H,
toWidth: DEST_W,
toHeight: DEST_H
})
.then(result => assert.deepStrictEqual(result, correct));
});

assert.deepStrictEqual(result, correct);
});
}


describe('Brightness should not change', function () {
describe('Brightness should not change', () => {
createTest(255);
createTest(127);
});
2 changes: 1 addition & 1 deletion test/fixture_resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const FIXTURES_DIRECTORY = path.join(__dirname, 'fixtures');
const OUTPUT_DIRECTORY = path.join(__dirname, '..');


describe('Fixture resize', function () {
describe('Fixture resize', () => {

it.skip('.resizeCanvas() should be correct for the given fixture', async () => {
this.timeout(3000);
Expand Down
27 changes: 13 additions & 14 deletions test/pica.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ const _pica = require('../index.js');
const assert = require('assert');


describe('API', function () {
describe('API', () => {

// Need node 8 to run
it('Upscale (unexpected use) via wasm should not crash', function () {
it('Upscale (unexpected use) via wasm should not crash', async () => {
const p = _pica({ features: [ 'wasm' ] });

const input = new Uint8Array(500 * 500 * 4);

return p.init().then(() => p.resizeBuffer({
await p.resizeBuffer({
src: input,
width: 500,
height: 500,
toWidth: 1000,
toHeight: 1000
}));
});
});

it('Should return result in promise', function () {
it('Should return result in promise', async () => {
let src = document.createElement('canvas');

src.width = 1000;
Expand All @@ -33,23 +33,22 @@ describe('API', function () {
to.width = 100;
to.height = 100;

return _pica().resize(src, to).then(result => {
assert.strictEqual(result, to);
});
const result = await _pica().resize(src, to);
assert.strictEqual(result, to);
});

it('Resize with bad output size should fail', function () {
it('Resize with bad output size should fail', async () => {
let src = document.createElement('canvas');
src.width = 1000;
src.height = 1000;
let to = document.createElement('canvas');
to.width = 0;
to.height = 0;
return _pica().resize(src, to)
.then(() => { throw new Error('Resize should fail'); })
.catch(err => {
assert.strictEqual(err.message, 'Invalid output size: 0x0');
});

await assert.rejects(
async () => _pica().resize(src, to),
{ message: 'Invalid output size: 0x0' }
);
});

});
12 changes: 6 additions & 6 deletions test/stepper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const TILE_SIZE = 1024;
const TILE_BORDER = 3;


describe('createStages', function () {
it('1024x1024 -> 300x300 (1 stage)', function () {
describe('createStages', () => {
it('1024x1024 -> 300x300 (1 stage)', () => {
assert.deepStrictEqual(createStages(
1024,
1024,
Expand All @@ -20,7 +20,7 @@ describe('createStages', function () {
), [ [ 300, 300 ] ]);
});

it('1024x1024 -> 2x2 (2 stages)', function () {
it('1024x1024 -> 2x2 (2 stages)', () => {
assert.deepStrictEqual(createStages(
1024,
1024,
Expand All @@ -31,7 +31,7 @@ describe('createStages', function () {
), [ [ 45, 45 ], [ 2, 2 ] ]);
});

it('102400x100 -> 1x1 (3 stages)', function () {
it('102400x100 -> 1x1 (3 stages)', () => {
assert.deepStrictEqual(createStages(
102400,
100,
Expand All @@ -42,7 +42,7 @@ describe('createStages', function () {
), [ [ 2189, 22 ], [ 47, 5 ], [ 1, 1 ] ]);
});

it('20000x1 -> 1x20000 (magnifying along another axis)', function () {
it('20000x1 -> 1x20000 (magnifying along another axis)', () => {
assert.deepStrictEqual(createStages(
20000,
1,
Expand All @@ -53,7 +53,7 @@ describe('createStages', function () {
), [ [ 737, 27 ], [ 27, 737 ], [ 1, 20000 ] ]);
});

it('1x1 -> 20000x20000 (magnifying should always be single stage)', function () {
it('1x1 -> 20000x20000 (magnifying should always be single stage)', () => {
assert.deepStrictEqual(createStages(
1,
1,
Expand Down
58 changes: 25 additions & 33 deletions test/unsharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const pica = require('../index.js')();
const assert = require('assert');

describe('Unsharp mask', function () {
it('save random image untouched when amount, radius and threshold are equal to 0', function () {
describe('Unsharp mask', () => {
it('save random image untouched when amount, radius and threshold are equal to 0', async () => {
let width = 100,
height = 100,
size = width * height * 4,
Expand All @@ -15,55 +15,47 @@ describe('Unsharp mask', function () {
image[i] = srcImage[i] = (Math.random() * 0xff + 0.5) | 0;
}

return pica.init()
.then(() => {
pica.__mathlib.unsharp_mask(image, width, height, 1e-4, 0, 0);
await pica.init();
pica.__mathlib.unsharp_mask(image, width, height, 1e-4, 0, 0);

for (let i = 0; i < size; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});
for (let i = 0; i < size; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});

it('save white color untouched', function () {
it('save white color untouched', async () => {
let srcImage = [ 255, 255, 255, 255 ];
let image = [ 255, 255, 255, 255 ];

return pica.init()
.then(() => {
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);
await pica.init();
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);

for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});
for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});

it('save black color untouched', function () {
it('save black color untouched', async () => {
let srcImage = [ 0, 0, 0, 0 ];
let image = [ 0, 0, 0, 0 ];

return pica.init()
.then(() => {
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);
await pica.init();
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);

for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});
for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});

it('save red color untouched', function () {
it('save red color untouched', async () => {
let srcImage = [ 255, 0, 0, 0 ];
let image = [ 255, 0, 0, 0 ];

return pica.init()
.then(() => {
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);
await pica.init();
pica.__mathlib.unsharp_mask(image, 1, 1, 0, 0, 0);

for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});
for (var i = 0; i < image.length; i++) {
assert.strictEqual(image[i], srcImage[i]);
}
});
});
14 changes: 7 additions & 7 deletions test/unsharp_mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ function fill(target, arr) {
}


describe('unsharp_mask', function () {
describe('unsharp_mask', () => {

describe('glur_mono16', function () {
describe('glur_mono16', () => {

it('js', function () {
it('js', () => {
const glur_js = require('glur/mono16');

let sample = new Uint16Array(100 * 100);
Expand All @@ -30,7 +30,7 @@ describe('unsharp_mask', function () {
});


it('wasm', function () {
it('wasm', () => {
const glur_js = require('glur/mono16');
const mlib_wasm = mathlib_raw({ js: false }).use(require('../lib/mm_unsharp_mask'));

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('unsharp_mask', function () {
});


describe('unsharp_mask', function () {
describe('unsharp_mask', () => {

function createSample(width, height) {
const result = new Uint8Array(width * height * 4);
Expand All @@ -95,15 +95,15 @@ describe('unsharp_mask', function () {
}


it('js should not throw without wasm', function () {
it('js should not throw without wasm', () => {
const mlib = mathlib_raw({ wasm: false }).use(require('../lib/mm_unsharp_mask'));

let sample = createSample(100, 100);
mlib.unsharp_mask(sample, 100, 100, 80, 2, 2);
});


it('wasm', function () {
it('wasm', () => {
const mlib_js = mathlib_raw({ wasm: false }).use(require('../lib/mm_unsharp_mask'));
const mlib_wasm = mathlib_raw({ js: false }).use(require('../lib/mm_unsharp_mask'));

Expand Down

0 comments on commit e4e6616

Please sign in to comment.