Skip to content

Commit 5353b64

Browse files
committed
Fix: Don't hard-code errors & format JS
1 parent 3200e32 commit 5353b64

File tree

2 files changed

+140
-100
lines changed

2 files changed

+140
-100
lines changed

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
"xtree": "cpp",
123123
"xutility": "cpp",
124124
"execution": "cpp",
125-
"text_encoding": "cpp"
125+
"text_encoding": "cpp",
126+
"__functional_03": "cpp"
126127
},
127128
"cSpell.words": [
128129
"allclose",
@@ -230,5 +231,11 @@
230231
"editor.formatOnSave": true,
231232
"editor.defaultFormatter": "golang.go"
232233
},
234+
"editor.tabSize": 4,
235+
"editor.insertSpaces": true,
236+
"prettier.singleQuote": true,
237+
"prettier.tabWidth": 4,
238+
"prettier.useTabs": false
239+
233240
"dotnet.defaultSolution": "csharp/Cloud.Unum.USearch.sln"
234241
}

javascript/usearch.test.js

Lines changed: 132 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function assertAlmostEqual(actual, expected, tolerance = 1e-6) {
1414
);
1515
}
1616

17-
1817
test('Single-entry operations', async (t) => {
1918
await t.test('index info', () => {
2019
const index = new usearch.Index(2, 'l2sq');
@@ -31,12 +30,24 @@ test('Single-entry operations', async (t) => {
3130
index.add(16n, new Float32Array([10, 25]));
3231

3332
assert.equal(index.size(), 2, 'size after adding elements should be 2');
34-
assert.equal(index.contains(15), true, 'entry must be present after insertion');
33+
assert.equal(
34+
index.contains(15),
35+
true,
36+
'entry must be present after insertion'
37+
);
3538

3639
const results = index.search(new Float32Array([13, 14]), 2);
3740

38-
assert.deepEqual(results.keys, new BigUint64Array([15n, 16n]), 'keys should be 15 and 16');
39-
assert.deepEqual(results.distances, new Float32Array([45, 130]), 'distances should be 45 and 130');
41+
assert.deepEqual(
42+
results.keys,
43+
new BigUint64Array([15n, 16n]),
44+
'keys should be 15 and 16'
45+
);
46+
assert.deepEqual(
47+
results.distances,
48+
new Float32Array([45, 130]),
49+
'distances should be 45 and 130'
50+
);
4051
});
4152

4253
await t.test('remove', () => {
@@ -49,12 +60,24 @@ test('Single-entry operations', async (t) => {
4960

5061
assert.equal(index.remove(15n), 1);
5162

52-
assert.equal(index.size(), 3, 'size after removing elements should be 3');
53-
assert.equal(index.contains(15), false, 'entry must be absent after insertion');
63+
assert.equal(
64+
index.size(),
65+
3,
66+
'size after removing elements should be 3'
67+
);
68+
assert.equal(
69+
index.contains(15),
70+
false,
71+
'entry must be absent after insertion'
72+
);
5473

5574
const results = index.search(new Float32Array([13, 14]), 2);
5675

57-
assert.deepEqual(results.keys, new BigUint64Array([16n, 25n]), 'keys should not include 15');
76+
assert.deepEqual(
77+
results.keys,
78+
new BigUint64Array([16n, 25n]),
79+
'keys should not include 15'
80+
);
5881
});
5982
});
6083

@@ -63,15 +86,30 @@ test('Batch operations', async (t) => {
6386
const indexBatch = new usearch.Index(2, 'l2sq');
6487

6588
const keys = [15n, 16n];
66-
const vectors = [new Float32Array([10, 20]), new Float32Array([10, 25])];
89+
const vectors = [
90+
new Float32Array([10, 20]),
91+
new Float32Array([10, 25]),
92+
];
6793

6894
indexBatch.add(keys, vectors);
69-
assert.equal(indexBatch.size(), 2, 'size after adding batch should be 2');
95+
assert.equal(
96+
indexBatch.size(),
97+
2,
98+
'size after adding batch should be 2'
99+
);
70100

71101
const results = indexBatch.search(new Float32Array([13, 14]), 2);
72102

73-
assert.deepEqual(results.keys, new BigUint64Array([15n, 16n]), 'keys should be 15 and 16');
74-
assert.deepEqual(results.distances, new Float32Array([45, 130]), 'distances should be 45 and 130');
103+
assert.deepEqual(
104+
results.keys,
105+
new BigUint64Array([15n, 16n]),
106+
'keys should be 15 and 16'
107+
);
108+
assert.deepEqual(
109+
results.distances,
110+
new Float32Array([45, 130]),
111+
'distances should be 45 and 130'
112+
);
75113
});
76114

77115
await t.test('remove', () => {
@@ -82,22 +120,30 @@ test('Batch operations', async (t) => {
82120
new Float32Array([10, 20]),
83121
new Float32Array([10, 25]),
84122
new Float32Array([20, 40]),
85-
new Float32Array([20, 45])
123+
new Float32Array([20, 45]),
86124
];
87125
indexBatch.add(keys, vectors);
88126

89-
assert.deepEqual(indexBatch.remove([15n, 25n]), [1, 1])
90-
assert.equal(indexBatch.size(), 2, 'size after removing batch should be 2');
127+
assert.deepEqual(indexBatch.remove([15n, 25n]), [1, 1]);
128+
assert.equal(
129+
indexBatch.size(),
130+
2,
131+
'size after removing batch should be 2'
132+
);
91133

92134
const results = indexBatch.search(new Float32Array([13, 14]), 2);
93135

94-
assert.deepEqual(results.keys, new BigUint64Array([16n, 26n]), 'keys should not include 15 and 25');
136+
assert.deepEqual(
137+
results.keys,
138+
new BigUint64Array([16n, 26n]),
139+
'keys should not include 15 and 25'
140+
);
95141
});
96142
});
97143

98-
test("Expected results", () => {
144+
test('Expected results', () => {
99145
const index = new usearch.Index({
100-
metric: "l2sq",
146+
metric: 'l2sq',
101147
connectivity: 16,
102148
dimensions: 3,
103149
});
@@ -137,69 +183,50 @@ test('Operations with invalid values', () => {
137183
const keys = [NaN, 16n];
138184
const vectors = [new Float32Array([10, 30]), new Float32Array([1, 5])];
139185

140-
assert.throws(
141-
() => indexBatch.add(keys, vectors),
142-
{
143-
name: 'Error',
144-
message: 'All keys must be positive integers or bigints.'
145-
}
146-
);
186+
// All keys must be positive integers or bigints
187+
assert.throws(() => indexBatch.add(keys, vectors));
147188

148-
assert.throws(
149-
() => indexBatch.search(NaN, 2),
150-
{
151-
name: 'Error',
152-
message: 'Vectors must be a TypedArray or an array of arrays.'
153-
}
154-
);
189+
// Vectors must be a TypedArray or an array of arrays
190+
assert.throws(() => indexBatch.search(NaN, 2));
155191
});
156192

157193
test('Invalid operations', async (t) => {
158194
await t.test('Add the same keys', () => {
159195
const index = new usearch.Index({
160-
metric: "l2sq",
196+
metric: 'l2sq',
161197
connectivity: 16,
162198
dimensions: 3,
163199
});
164200
index.add(42n, new Float32Array([0.2, 0.6, 0.4]));
165-
assert.throws(
166-
() => index.add(42n, new Float32Array([0.2, 0.6, 0.4])),
167-
{
168-
name: 'Error',
169-
message: '<key:42 message:Duplicate keys not allowed in high-level wrappers>'
170-
}
171-
);
201+
assert.throws(() => index.add(42n, new Float32Array([0.2, 0.6, 0.4])));
172202
});
173203

174204
await t.test('Batch add containing the same key', () => {
175205
const index = new usearch.Index({
176-
metric: "l2sq",
206+
metric: 'l2sq',
177207
connectivity: 16,
178208
dimensions: 3,
179209
});
180210
index.add(42n, new Float32Array([0.2, 0.6, 0.4]));
181-
assert.throws(
182-
() => {
183-
index.add(
184-
[41n, 42n, 43n],
185-
[[0.1, 0.6, 0.4], [0.2, 0.6, 0.4], [0.3, 0.6, 0.4]]
186-
);
187-
},
188-
{
189-
name: 'Error',
190-
message: '<key:42 message:Duplicate keys not allowed in high-level wrappers>'
191-
}
192-
);
211+
assert.throws(() => {
212+
index.add(
213+
[41n, 42n, 43n],
214+
[
215+
[0.1, 0.6, 0.4],
216+
[0.2, 0.6, 0.4],
217+
[0.3, 0.6, 0.4],
218+
]
219+
);
220+
});
193221
});
194222
});
195223

196-
197224
test('Serialization', async (t) => {
198-
const indexPath = path.join(os.tmpdir(), 'usearch.test.index')
225+
const indexPath = path.join(os.tmpdir(), 'usearch.test.index');
199226

200227
t.beforeEach(() => {
201228
const index = new usearch.Index({
202-
metric: "l2sq",
229+
metric: 'l2sq',
203230
connectivity: 16,
204231
dimensions: 3,
205232
});
@@ -213,7 +240,7 @@ test('Serialization', async (t) => {
213240

214241
await t.test('load', () => {
215242
const index = new usearch.Index({
216-
metric: "l2sq",
243+
metric: 'l2sq',
217244
connectivity: 16,
218245
dimensions: 3,
219246
});
@@ -228,49 +255,55 @@ test('Serialization', async (t) => {
228255
// todo: Skip as the test fails only on windows.
229256
// The following error in afterEach().
230257
// `error: "EBUSY: resource busy or locked, unlink`
231-
await t.test('view: Read data', { skip: process.platform === 'win32' }, () => {
232-
const index = new usearch.Index({
233-
metric: "l2sq",
234-
connectivity: 16,
235-
dimensions: 3,
236-
});
237-
index.view(indexPath);
238-
const results = index.search(new Float32Array([0.2, 0.6, 0.4]), 10);
239-
240-
assert.equal(index.size(), 1);
241-
assert.deepEqual(results.keys, new BigUint64Array([42n]));
242-
assertAlmostEqual(results.distances[0], new Float32Array([0]));
243-
});
258+
await t.test(
259+
'view: Read data',
260+
{ skip: process.platform === 'win32' },
261+
() => {
262+
const index = new usearch.Index({
263+
metric: 'l2sq',
264+
connectivity: 16,
265+
dimensions: 3,
266+
});
267+
index.view(indexPath);
268+
const results = index.search(new Float32Array([0.2, 0.6, 0.4]), 10);
269+
270+
assert.equal(index.size(), 1);
271+
assert.deepEqual(results.keys, new BigUint64Array([42n]));
272+
assertAlmostEqual(results.distances[0], new Float32Array([0]));
273+
}
274+
);
244275

245-
await t.test('view: Invalid operations: add', { skip: process.platform === 'win32' }, () => {
246-
const index = new usearch.Index({
247-
metric: "l2sq",
248-
connectivity: 16,
249-
dimensions: 3,
250-
});
251-
index.view(indexPath);
252-
assert.throws(
253-
() => index.add(43n, new Float32Array([0.2, 0.6, 0.4])),
254-
{
255-
name: 'Error',
256-
message: "<key:43 message:Can't add to an immutable index>"
257-
}
258-
);
259-
});
276+
await t.test(
277+
'view: Invalid operations: add',
278+
{ skip: process.platform === 'win32' },
279+
() => {
280+
const index = new usearch.Index({
281+
metric: 'l2sq',
282+
connectivity: 16,
283+
dimensions: 3,
284+
});
285+
index.view(indexPath);
286+
287+
// Can't add to an immutable index
288+
assert.throws(() =>
289+
index.add(43n, new Float32Array([0.2, 0.6, 0.4]))
290+
);
291+
}
292+
);
260293

261-
await t.test('view: Invalid operations: remove', { skip: process.platform === 'win32' }, () => {
262-
const index = new usearch.Index({
263-
metric: "l2sq",
264-
connectivity: 16,
265-
dimensions: 3,
266-
});
267-
index.view(indexPath);
268-
assert.throws(
269-
() => index.remove(42n),
270-
{
271-
name: 'Error',
272-
message: "Can't remove from an immutable index"
273-
}
274-
);
275-
});
294+
await t.test(
295+
'view: Invalid operations: remove',
296+
{ skip: process.platform === 'win32' },
297+
() => {
298+
const index = new usearch.Index({
299+
metric: 'l2sq',
300+
connectivity: 16,
301+
dimensions: 3,
302+
});
303+
index.view(indexPath);
304+
305+
// Can't remove from an immutable index
306+
assert.throws(() => index.remove(42n));
307+
}
308+
);
276309
});

0 commit comments

Comments
 (0)