Skip to content

Commit

Permalink
port tests to blur1/blur2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jun 30, 2022
1 parent 5690f27 commit 19a22da
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions test/blur-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,52 @@ import {blur1, blur2} from "../src/index.js";
// assert.equal(h.value(), undefined);
// });

it.only("blur(values, options) blurs in 1D", () => {
it("blur1(values, options) blurs in 1D", () => {
const V = [0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0];
assert.strictEqual(blur1(V, {r: 1, width: 0}), V);
assert.deepStrictEqual([0, 0, 0, 1, 3, 6, 7, 6, 3, 1, 0, 0, 0, 0], V);
assert.deepStrictEqual(blur1(V, {r: 1}), [0, 0, 0, 1, 3, 6, 7, 6, 3, 1, 0, 0, 0, 0]);
});

it("blur() blurs in 2D", () => {
const h = blur();
assert.deepEqual(
h.width(4).radiusX(1).radiusY(1)([0, 0, 0, 0, 729, 0, 0, 0, 0, 0, 0, 0]),
Object.assign(Float32Array.from([117, 81, 36, 9, 117, 81, 36, 9, 117, 81, 36, 9]), { width: 4, height: 3 })
);
it("blur1(values, options) blurs in place", () => {
const V = [0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0];
assert.strictEqual(blur1(V, {r: 1}), V);
});

it("blur().radiusY(0) blurs horizontally", () => {
const h = blur();
assert.deepEqual(
h.width(4).radiusX(1).radiusY(0)([27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
Object.assign(Float32Array.from([13, 9, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0]), { width: 4, height: 3 })
);
it("blur2(values, width, options) blurs in 2D", () => {
const V = [0, 0, 0, 0, 729, 0, 0, 0, 0, 0, 0, 0];
assert.deepStrictEqual(blur2(V, 4, {r: 1}), [117, 81, 36, 9, 117, 81, 36, 9, 117, 81, 36, 9]);
});

it("blur().radiusX(0) blurs vertically", () => {
const h = blur();
assert.deepEqual(
h.width(4).radiusX(0).radiusY(1)([0, 0, 0, 27, 3, -9, 0, 0, 0, 0, 0, 0]),
Object.assign(Float32Array.from([1, -3, 0, 13, 1, -3, 0, 9, 1, -3, 0, 5]), { width: 4, height: 3 })
);
it("blur2(values, width, options) blurs in place", () => {
const V = [0, 0, 0, 0, 729, 0, 0, 0, 0, 0, 0, 0];
assert.strictEqual(blur2(V, 4, {r: 1}), V);
});

it("blur().radius(0.5) does a fraction of blur", () => {
const h = blur().width(5), V = [0,0,0,0,0, 0,0,5184,0,0, 0,0,0,0,0];
assert.deepEqual(
h.radius(0.5)(V),
Object.assign(Float32Array.from([64, 96, 544, 96, 64, 256, 384, 2176, 384, 256, 64, 96, 544, 96, 64]), { width: 5, height: 3 })
);
it("blur2(… {ry: 0}) blurs horizontally", () => {
const V = [0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0, 0, 0];
assert.deepStrictEqual(blur2(V, 4, {rx: 1, ry: 0}), [13, 9, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0]);
});

it("blur().radius(1.2) does a fraction of blur", () => {
const h = blur(), V = [0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0];
const V1 = h.radius(1)(V);
const V2 = h.radius(2)(V);
for (let i = 0; i < V1.length; i++) V1[i] = 0.8 * V1[i] + 0.2 * V2[i];
assert.deepEqual(Array.from(h.radius(1.2)(V)), Array.from(V1));
it("blur2(…, {rx: 0}) blurs vertically", () => {
const V = [0, 0, 0, 27, 3, -9, 0, 0, 0, 0, 0, 0];
assert.deepStrictEqual(blur2(V, 4, {rx: 0, ry: 1}), [1, -3, 0, 13, 1, -3, 0, 9, 1, -3, 0, 5]);
});

it("blur().radius() returns the (average) radius", () => {
const h = blur();
assert.equal(h.width(2).radiusX(1).radiusY(1).radius(), 1);
assert.equal(h.width(2).radius(2).radius(), 2);
assert.equal(h.width(2).radiusX(1).radiusY(5).radius(), 3);
it("blur1(…, {r: 0.5}) does a fraction of blur", () => {
const V = [0,0,0,0,0, 0,0,5184,0,0, 0,0,0,0,0];
assert.deepStrictEqual(blur1(V, {r: 0.5}), [64, 96, 544, 96, 64, 256, 384, 2176, 384, 256, 64, 96, 544, 96, 64]);
});

it("blur() accepts an iterable", () => {
const h = blur().radius(1);
assert.deepEqual(h(new Set([27, 0, -27])), Object.assign(Float32Array.from([8, 0, -8]), { width: 3, height: 1 }));
assert.deepEqual(h.value(d => d.a)(new Set([{a: 27}, {a: 0}, {a: -27}])), Object.assign(Float32Array.from([8, 0, -8]), { width: 3, height: 1 }));
it("blur1(…, {r: 1.2}) does a fraction of blur", () => {
const V = [0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0];
const V1 = blur1(V.slice(), {r: 1});
const V2 = blur1(V.slice(), {r: 2});
for (let i = 0; i < V1.length; i++) V1[i] = 0.8 * V1[i] + 0.2 * V2[i];
blur1(V, {r: 1.2});
assert.deepEqual(V, V1);
});

// it("blur() accepts an iterable", () => {
// const h = blur().radius(1);
// assert.deepEqual(h(new Set([27, 0, -27])), Object.assign(Float32Array.from([8, 0, -8]), { width: 3, height: 1 }));
// assert.deepEqual(h.value(d => d.a)(new Set([{a: 27}, {a: 0}, {a: -27}])), Object.assign(Float32Array.from([8, 0, -8]), { width: 3, height: 1 }));
// });

0 comments on commit 19a22da

Please sign in to comment.