-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
map.test.js
55 lines (53 loc) · 1.31 KB
/
map.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
import * as map from './map.js'
import * as math from './math.js'
import * as t from './testing.js'
/**
* @param {t.TestCase} _tc
*/
export const testMap = _tc => {
/**
* @type {Map<number, number>}
*/
const m = map.create()
m.set(1, 2)
m.set(2, 3)
t.assert(map.map(m, (value, key) => value * 2 + key).reduce(math.add) === 13)
let numberOfWrites = 0
const createT = () => ++numberOfWrites
map.setIfUndefined(m, 3, createT)
map.setIfUndefined(m, 3, createT)
map.setIfUndefined(m, 3, createT)
t.compare(map.copy(m), m)
t.assert(numberOfWrites === 1)
t.assert(map.any(m, (v, k) => k === 1 && v === 2))
t.assert(map.any(m, (v, k) => k === 2 && v === 3))
t.assert(!map.any(m, () => false))
t.assert(!map.all(m, (v, k) => k === 1 && v === 2))
t.assert(map.all(m, (v) => v === 2 || v === 3 || v === numberOfWrites))
}
/**
* @param {t.TestCase} _tc
*/
export const testTypeDefinitions = _tc => {
// setIfUndefined supports inheritance properly: See https://github.com/dmonad/lib0/issues/82
class A {
constructor () {
this.a = 4
}
}
class B extends A {
constructor () {
super()
this.b = 4
}
}
/**
* @type {Map<number, A>}
*/
const m = map.create()
/**
* @type {B}
*/
const b = map.setIfUndefined(m, 0, () => new B())
console.log(b)
}