-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.test.js
80 lines (72 loc) · 2.8 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { expect } from '@jest/globals';
import { decode } from 'blurhash';
import { getBlurHashAverageColor } from '.';
import { decodeBlurHash } from '.';
const hashes = [
'LEHV6nWB2yk8pyo0adR*.7kCMdnj',
'LKO2?U%2Tw=w]~RBVZRi};RPxuwH',
'g6Pj0^i_.AyE8^m+%g_3t7t7R*WBs,of*0o#DgR4.Tt,IT_3R*D%xt%MIpRjMcV@%itSI9R5x]',
'gHF5]+Yk^6#M9wKSW@@-5b,1J5O[V=R:@[or[k6.O[TLtJ};FxngOZE3NgNHjMFxS#OtcXnzRj',
'|HF5]+Yk^6#M9wKSW@j=#*@-5b,1J5O[V=R:s;w[@[or[k6.O[TLtJnNnO};FxngOZE3NgNHsps,jMFxS#OtcXnzRjxZxHj]OYNeR:JCs9xunhwIbeIpNaxHNGr;v}aeo0Xmt6XS$et6#*$ft6nhxHnNV@w{nOenwfNHo0',
'|EHV6nWB2yk8$NxujFNGt6pyoJadR*=ss:I[R%of.7kCMdnjx]S2NHs:i_S#M|%1%2ENRis9a$%1Sis.slNHW:WBxZ%2NbogaekBW;ofo0NHS4j?W?WBsloLR+oJofS2s:ozj@s:jaR*Wps.j[RkT0of%2afR*fkoJjZof',
];
const sizes = [
{ width: 2, height: 2 },
{ width: 5, height: 4 },
{ width: 32, height: 32 },
{ width: 64, height: 64 },
{ width: 100, height: 50 },
{ width: 1920, height: 1080 },
];
/**
* Calculates mean square error
* @param {number[]} received
* @param {number[]} expected
* @returns number
*/
function mse(received, expected) {
let sum = 0;
for (let i = 0; i < received.length; i++) {
sum += (received[i] - expected[i]) ** 2;
}
return sum / received.length;
}
describe('fast-blurhash', () => {
describe('decodeBlurHash', () => {
sizes.forEach(({ width, height }) => {
hashes.forEach((hash, i) => {
it(`decodes image ${width}x${height} correctly, hash #${i}`, () => {
const expected = decode(hash, 5, 4);
const received = decodeBlurHash(hash, 5, 4);
expect(received.length).toEqual(expected.length);
// fast-blurhash uses approximate math calculations for perf reasons
// let's check that diff between fast-blurhash is small enough
expect(mse(received, expected)).toBeLessThan(0.5);
});
it(`decodes image ${width}x${height} correctly, hash #${i} with punch`, () => {
const expected = decode(hash, 5, 4, 10);
const received = decodeBlurHash(hash, 5, 4, 10);
expect(mse(received, expected)).toBeLessThan(0.5);
});
});
});
});
describe('getBlurHashAverageColor', () => {
it(`returns correct average color`, () => {
expect(getBlurHashAverageColor(hashes[0])).toMatchInlineSnapshot(`
[
151,
150,
149,
]
`);
expect(getBlurHashAverageColor(hashes[1])).toMatchInlineSnapshot(`
[
209,
178,
163,
]
`);
});
});
});