-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvector.test.ts
244 lines (242 loc) · 8.69 KB
/
vector.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { SEALLibrary } from '../implementation/seal'
import SEAL from '../throws_wasm_node_umd'
let seal: SEALLibrary
beforeAll(async () => {
seal = await SEAL()
})
describe('Vector', () => {
test('It should be a factory', () => {
expect(seal.Vector).toBeDefined()
expect(typeof seal.Vector.constructor).toBe('function')
expect(seal.Vector).toBeInstanceOf(Object)
expect(seal.Vector.constructor).toBe(Function)
expect(seal.Vector.constructor.name).toBe('Function')
})
test('It should construct an instance', () => {
const Constructor = jest.fn(seal.Vector)
Constructor()
expect(Constructor).toHaveBeenCalledWith()
})
test('It should construct an instance with of uint8', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(Uint8Array.from([]))
expect(spyOn).toHaveBeenCalledWith(Uint8Array.from([]))
expect(vector.type).toEqual('Uint8Array')
})
test('It should construct an instance with of int32', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(Int32Array.from([]))
expect(spyOn).toHaveBeenCalledWith(Int32Array.from([]))
expect(vector.type).toEqual('Int32Array')
})
test('It should construct an instance with of uint32', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(Uint32Array.from([]))
expect(spyOn).toHaveBeenCalledWith(Uint32Array.from([]))
expect(vector.type).toEqual('Uint32Array')
})
test('It should construct an instance with of float64', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(Float64Array.from([]))
expect(spyOn).toHaveBeenCalledWith(Float64Array.from([]))
expect(vector.type).toEqual('Float64Array')
})
test('It should construct an instance with of bigint64', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(BigInt64Array.from([]))
expect(spyOn).toHaveBeenCalledWith(BigInt64Array.from([]))
expect(vector.type).toEqual('BigInt64Array')
})
test('It should construct an instance with of biguint64', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(BigUint64Array.from([]))
expect(spyOn).toHaveBeenCalledWith(BigUint64Array.from([]))
expect(vector.type).toEqual('BigUint64Array')
})
test('It should construct an instance with of modulus', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
vector.from(BigUint64Array.from([]), 'Modulus')
expect(spyOn).toHaveBeenCalledWith(BigUint64Array.from([]), 'Modulus')
expect(vector.type).toEqual('Modulus')
})
test('It should fail with unsupported type', () => {
const vector = seal.Vector()
const spyOn = jest.spyOn(vector, 'from')
expect(() =>
vector.from(Float32Array.from([]) as unknown as Int32Array)
).toThrow(Error)
expect(spyOn).toHaveBeenCalledWith(Float32Array.from([]))
})
test('It should have properties', () => {
const item = seal.Vector()
item.from(Int32Array.from([]))
// Test properties
expect(item).toHaveProperty('instance')
expect(item).toHaveProperty('unsafeInject')
expect(item).toHaveProperty('delete')
expect(item).toHaveProperty('type')
expect(item).toHaveProperty('size')
expect(item).toHaveProperty('getValue')
expect(item).toHaveProperty('resize')
expect(item).toHaveProperty('toArray')
})
test('It should not have an instance', () => {
const item = seal.Vector()
expect(item.instance).toBeUndefined()
})
test('It should inject', () => {
const item = seal.Vector()
item.from(Int32Array.from([3]))
const newItem = seal.Vector()
newItem.delete()
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item.instance)
expect(spyOn).toHaveBeenCalledWith(item.instance)
expect(newItem.getValue(0)).toEqual(3)
})
test('It should delete the old instance and inject', () => {
const item = seal.Vector()
item.from(Int32Array.from([3]))
const item2 = seal.Vector()
item2.from(Int32Array.from([7]))
const newItem = seal.Vector()
newItem.unsafeInject(item.instance)
const spyOn = jest.spyOn(newItem, 'unsafeInject')
newItem.unsafeInject(item2.instance)
expect(spyOn).toHaveBeenCalledWith(item2.instance)
expect(newItem.getValue(0)).toEqual(7)
})
test("It should delete it's instance", () => {
const item = seal.Vector()
item.from(Int32Array.from([3]))
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
expect(() => item.getValue(0)).toThrow(TypeError)
})
test('It should skip deleting twice', () => {
const item = seal.Vector()
item.from(Int32Array.from([3]))
item.delete()
const spyOn = jest.spyOn(item, 'delete')
item.delete()
expect(spyOn).toHaveBeenCalled()
expect(item.instance).toBeUndefined()
expect(() => item.getValue(0)).toThrow(TypeError)
})
test('It should return its size', () => {
const item = seal.Vector()
item.from(Int32Array.from([3]))
expect(item.size).toEqual(1)
})
test('It should return size 0', () => {
const item = seal.Vector()
item.from(Int32Array.from([]))
expect(item.size).toEqual(0)
})
test('It should get a value from a specified index', () => {
const item = seal.Vector()
item.from(Int32Array.from({ length: 4096 }, (_, i) => i))
const spyOn = jest.spyOn(item, 'getValue')
const value = item.getValue(0)
expect(spyOn).toHaveBeenCalledWith(0)
expect(value).toEqual(0)
})
test('It should resize to a specified number', () => {
const item = seal.Vector()
item.from(Int32Array.from({ length: 4096 }, _ => 3))
const spyOn = jest.spyOn(item, 'resize')
item.resize(8192, 3)
expect(spyOn).toHaveBeenCalledWith(8192, 3)
})
test('It should fail to resize to a specified number', () => {
const item = seal.Vector()
item.from(Int32Array.from({ length: 4096 }, _ => 3))
const spyOn = jest.spyOn(item, 'resize')
expect(() => item.resize(-1, 3)).toThrow()
expect(spyOn).toHaveBeenCalledWith(-1, 3)
})
test('It should return a Uint8Array', () => {
const item = seal.Vector()
const arr = Uint8Array.from({ length: 4096 }, _ => 3)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an Int32Array', () => {
const item = seal.Vector()
const arr = Int32Array.from({ length: 4096 }, _ => -3)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an Uint32Array', () => {
const item = seal.Vector()
const arr = Uint32Array.from({ length: 4096 }, _ => 3)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an Float64Array', () => {
const item = seal.Vector()
const arr = Float64Array.from({ length: 4096 }, _ => 3.33)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an BigInt64Array', () => {
const item = seal.Vector()
const arr = BigInt64Array.from({ length: 4096 }, _ =>
BigInt('9223372036854775807')
)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an BigUint64Array (2^64 - 1)', () => {
const item = seal.Vector()
const arr = BigUint64Array.from({ length: 4096 }, _ =>
BigInt('18446744073709551615')
)
item.from(arr)
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should return an BigUint64Array, Modulus, (2^61 - 1)', () => {
const item = seal.Vector()
const arr = BigUint64Array.from({ length: 4096 }, _ =>
BigInt('2305843009213693951')
)
item.from(arr, 'Modulus')
const spyOn = jest.spyOn(item, 'toArray')
const res = item.toArray()
expect(spyOn).toHaveBeenCalledWith()
expect(res).toEqual(arr)
})
test('It should throw if not initialized', () => {
const item = seal.Vector()
const spyOn = jest.spyOn(item, 'toArray')
expect(() => item.toArray()).toThrow()
expect(spyOn).toHaveBeenCalledWith()
})
})