-
Notifications
You must be signed in to change notification settings - Fork 24
/
tests.js
85 lines (63 loc) · 5.05 KB
/
tests.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
81
82
83
84
85
import assert from 'assert/strict'
import { randomString } from './benchmark/benchmark.js'
import { minidenticon as minidenticon_CE } from './minidenticons.min.js'
import { minidenticon as minidenticon_NO_CE } from './no-custom-element.min.js'
const COLLISION_TESTS_NUMBER = 10_000
function alternateTerribleHash(str) {
return str.split('')
.reduce((hash, char) => hash + char.charCodeAt(0), 123456)
}
console.time("\nTests duration")
// minidenticon tests
new Array(minidenticon_CE, minidenticon_NO_CE).forEach(minidenticon => {
// non-integer hue normal for empty string
assert.equal(
minidenticon(""),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(40 95% 45%)"></svg>'
)
assert.equal(
minidenticon("laurent"),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(160 95% 45%)"><rect x="0" y="0" width="1" height="1"/><rect x="0" y="2" width="1" height="1"/><rect x="0" y="3" width="1" height="1"/><rect x="1" y="1" width="1" height="1"/><rect x="2" y="2" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="4" y="0" width="1" height="1"/><rect x="4" y="2" width="1" height="1"/><rect x="4" y="3" width="1" height="1"/><rect x="3" y="1" width="1" height="1"/></svg>'
)
assert.equal(
minidenticon("foo"),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(120 95% 45%)"><rect x="0" y="0" width="1" height="1"/><rect x="1" y="0" width="1" height="1"/><rect x="1" y="4" width="1" height="1"/><rect x="2" y="0" width="1" height="1"/><rect x="2" y="2" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="2" y="4" width="1" height="1"/><rect x="4" y="0" width="1" height="1"/><rect x="3" y="0" width="1" height="1"/><rect x="3" y="4" width="1" height="1"/></svg>'
)
assert.equal(
minidenticon("foo", 75),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(120 75% 45%)"><rect x="0" y="0" width="1" height="1"/><rect x="1" y="0" width="1" height="1"/><rect x="1" y="4" width="1" height="1"/><rect x="2" y="0" width="1" height="1"/><rect x="2" y="2" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="2" y="4" width="1" height="1"/><rect x="4" y="0" width="1" height="1"/><rect x="3" y="0" width="1" height="1"/><rect x="3" y="4" width="1" height="1"/></svg>'
)
assert.equal(
minidenticon("foo", undefined, 75),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(120 95% 75%)"><rect x="0" y="0" width="1" height="1"/><rect x="1" y="0" width="1" height="1"/><rect x="1" y="4" width="1" height="1"/><rect x="2" y="0" width="1" height="1"/><rect x="2" y="2" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="2" y="4" width="1" height="1"/><rect x="4" y="0" width="1" height="1"/><rect x="3" y="0" width="1" height="1"/><rect x="3" y="4" width="1" height="1"/></svg>'
)
assert.equal(
minidenticon("foo", undefined, undefined, alternateTerribleHash),
'<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(120 95% 45%)"><rect x="0" y="2" width="1" height="1"/><rect x="1" y="2" width="1" height="1"/><rect x="1" y="3" width="1" height="1"/><rect x="1" y="4" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="2" y="4" width="1" height="1"/><rect x="4" y="2" width="1" height="1"/><rect x="3" y="2" width="1" height="1"/><rect x="3" y="3" width="1" height="1"/><rect x="3" y="4" width="1" height="1"/></svg>'
)
assert.notEqual(
minidenticon("foo"),
minidenticon("foo", undefined, undefined, alternateTerribleHash)
)
for (let saturation = 0; saturation < 100; saturation += 5) {
for (let lightness = 0; lightness < COLLISION_TESTS_NUMBER; lightness += 5) {
assert.equal(
minidenticon("foo", saturation, lightness),
`<svg viewBox="-1.5 -1.5 8 8" xmlns="http://www.w3.org/2000/svg" fill="hsl(120 ${saturation}% ${lightness}%)"><rect x="0" y="0" width="1" height="1"/><rect x="1" y="0" width="1" height="1"/><rect x="1" y="4" width="1" height="1"/><rect x="2" y="0" width="1" height="1"/><rect x="2" y="2" width="1" height="1"/><rect x="2" y="3" width="1" height="1"/><rect x="2" y="4" width="1" height="1"/><rect x="4" y="0" width="1" height="1"/><rect x="3" y="0" width="1" height="1"/><rect x="3" y="4" width="1" height="1"/></svg>`
)
}
}
})
// collision tests
const minidenticon = minidenticon_CE
const seeds = new Set() // to ensure no duplicate random seed
const minidenticons = new Set()
for (let i = 0; i < COLLISION_TESTS_NUMBER; i++) {
const seed = randomString(15)
seeds.add(seed)
minidenticons.add(minidenticon(seed))
if ((i + 1) % ~~(COLLISION_TESTS_NUMBER / (i < (COLLISION_TESTS_NUMBER / 10) ? 100 : 10)) === 0)
console.log(`${seeds.size - minidenticons.size} collisions out of ${seeds.size}`,
`(${((seeds.size - minidenticons.size)/seeds.size * 100).toFixed(2)}%)`)
}
console.timeEnd("\nTests duration")