-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBidirectionalMap.test.ts
149 lines (130 loc) · 4.18 KB
/
BidirectionalMap.test.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import './index.js'
describe('BidirectionalMap', () => {
const sampleMap = new BidirectionalMap<string, string>([
['key1', 'value1'],
['key2', 'value2'],
])
describe('constructor', () => {
it('should create a map', () => {
expect(sampleMap.get('key1')).toEqual('value1')
expect(sampleMap.getKey('value1')).toEqual('key1')
expect(sampleMap.get('key2')).toEqual('value2')
expect(sampleMap.getKey('value2')).toEqual('key2')
})
})
describe('Symbol.toStringTag', () => {
it('should return correct tag', () => {
expect(sampleMap[Symbol.toStringTag]).toEqual('BidirectionalMap')
expect(sampleMap.toString()).toEqual('[object BidirectionalMap]')
})
})
describe('keys', () => {
it('should iterate over keys', () => {
const keys = [...sampleMap.keys()]
expect(keys).toEqual(['key1', 'key2'])
})
})
describe('values', () => {
it('should iterate over values', () => {
const values = [...sampleMap.values()]
expect(values).toEqual(['value1', 'value2'])
})
})
describe('entries', () => {
it('should iterate over key-value pairs', () => {
const entries = [...sampleMap.entries()]
expect(entries).toEqual([['key1', 'value1'], ['key2', 'value2']])
})
it('should iterate using Symbol.iterator', () => {
const entries = [...sampleMap]
expect(entries).toEqual([['key1', 'value1'], ['key2', 'value2']])
})
})
describe('forEach', () => {
it('should iterate over key-value pairs', () => {
const entries = new Array<[string, string]>()
sampleMap.forEach((value, key) => entries.push([key, value]))
expect(entries).toEqual([['key1', 'value1'], ['key2', 'value2']])
})
})
describe('set', () => {
it('should add key-value pairs', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
expect(map.get('key1')).toEqual('value1')
expect(map.getKey('value1')).toEqual('key1')
})
it('should update value for existing key', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
map.set('key1', 'value2')
expect(map.get('key1')).toEqual('value2')
expect(map.getKey('value1')).toBeUndefined()
expect(map.getKey('value2')).toEqual('key1')
})
it('should throw error when value is not unique', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
expect(() => map.set('key2', 'value1')).toThrow()
})
})
describe('delete', () => {
it('should delete key-value pairs', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
map.delete('key1')
expect(map.get('key1')).toBeUndefined()
expect(map.getKey('value1')).toBeUndefined()
})
it('should return false when key does not exist', () => {
const map = new BidirectionalMap<string, string>()
expect(map.delete('key1')).toBeFalse()
})
})
describe('deleteValue', () => {
it('should delete value-key pairs', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
map.deleteValue('value1')
expect(map.get('key1')).toBeUndefined()
expect(map.getKey('value1')).toBeUndefined()
})
it('should return false when value does not exist', () => {
const map = new BidirectionalMap<string, string>()
expect(map.deleteValue('value1')).toBeFalse()
})
})
describe('clear', () => {
it('should clear the map', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
map.set('key2', 'value2')
map.clear()
expect(map.size).toEqual(0)
})
})
describe('size', () => {
it('should return correct size', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
map.set('key2', 'value2')
expect(map.size).toEqual(2)
})
})
describe('has', () => {
it('should check if a key exists', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
expect(map.has('key1')).toBeTrue()
expect(map.has('key2')).toBeFalse()
})
})
describe('hasValue', () => {
it('should check if a value exists', () => {
const map = new BidirectionalMap<string, string>()
map.set('key1', 'value1')
expect(map.hasValue('value1')).toBeTrue()
expect(map.hasValue('value2')).toBeFalse()
})
})
})