Skip to content

Commit 02b009b

Browse files
committed
test(NODE-6534): add spec test runner for Binary vector
1 parent dd990ef commit 02b009b

File tree

1 file changed

+7
-65
lines changed

1 file changed

+7
-65
lines changed

test/node/bson_binary_vector.spec.test.ts

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ import { expect } from 'chai';
55

66
const { toHex, fromHex } = BSON.onDemand.ByteUtils;
77

8-
const FLOAT = new Float64Array(1);
9-
const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
10-
11-
FLOAT[0] = -1;
12-
// Little endian [0, 0, 0, 0, 0, 0, 240, 191]
13-
// Big endian [191, 240, 0, 0, 0, 0, 0, 0]
14-
const isBigEndian = FLOAT_BYTES[7] === 0;
15-
168
type VectorHexType = '0x03' | '0x27' | '0x10';
179
type VectorTest = {
1810
description: string;
@@ -24,36 +16,6 @@ type VectorTest = {
2416
};
2517
type VectorSuite = { description: string; test_key: string; tests: VectorTest[] };
2618

27-
function validateVector(vector: Binary): void {
28-
const VECTOR_TYPE = Object.freeze({
29-
Int8: 0x03,
30-
Float32: 0x27,
31-
PackedBit: 0x10
32-
} as const);
33-
34-
if (vector.sub_type !== 9) return;
35-
36-
const size = vector.position;
37-
const d_type = vector.buffer[0] ?? 0;
38-
const padding = vector.buffer[1] ?? 0;
39-
40-
if ((d_type === VECTOR_TYPE.Float32 || d_type === VECTOR_TYPE.Int8) && padding !== 0) {
41-
throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
42-
}
43-
44-
if (d_type === VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
45-
throw new BSONError(
46-
'Invalid Vector: padding must be zero for packed bit vectors that are empty'
47-
);
48-
}
49-
50-
if (d_type === VECTOR_TYPE.PackedBit && padding > 7) {
51-
throw new BSONError(
52-
`Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`
53-
);
54-
}
55-
}
56-
5719
function fixFloats(f: string | number): number {
5820
if (typeof f === 'number') {
5921
return f;
@@ -90,31 +52,15 @@ function fixBits(f: number | string): number {
9052
function make(vector: (number | string)[], dtype_hex: VectorHexType, padding?: number): Binary {
9153
let binary: Binary;
9254
switch (dtype_hex) {
93-
case '0x10': /* packed_bit */
94-
case '0x03': /* int8 */ {
95-
const array = new Int8Array(vector.map(dtype_hex === '0x03' /* int8 */ ? fixInt8s : fixBits));
96-
const buffer = new Uint8Array(array.byteLength + 2);
97-
buffer.set(new Uint8Array(array.buffer), 2);
98-
binary = new Binary(buffer, 9);
55+
case '0x10' /* packed_bit */:
56+
binary = Binary.fromPackedBits(new Uint8Array(vector.map(fixBits)), padding);
9957
break;
100-
}
101-
102-
case '0x27': /* float32 */ {
103-
const array = new Float32Array(vector.map(fixFloats));
104-
const buffer = new Uint8Array(array.byteLength + 2);
105-
if (isBigEndian) {
106-
for (let i = 0; i < array.length; i++) {
107-
const bytes = new Uint8Array(array.buffer, i * 4, 4);
108-
bytes.reverse();
109-
buffer.set(bytes, i * 4 + 2);
110-
}
111-
} else {
112-
buffer.set(new Uint8Array(array.buffer), 2);
113-
}
114-
binary = new Binary(buffer, 9);
58+
case '0x03' /* int8 */:
59+
binary = Binary.fromInt8Array(new Int8Array(vector.map(fixInt8s)));
60+
break;
61+
case '0x27' /* float32 */:
62+
binary = Binary.fromFloat32Array(new Float32Array(vector.map(fixFloats)));
11563
break;
116-
}
117-
11864
default:
11965
throw new Error(`Unknown dtype_hex: ${dtype_hex}`);
12066
}
@@ -206,8 +152,6 @@ describe('BSON Binary Vector spec tests', () => {
206152
try {
207153
const bin = make(test.vector, test.dtype_hex, test.padding);
208154
BSON.serialize({ bin });
209-
// TODO(NODE-6537): The following validation MUST be a part of serialize
210-
validateVector(bin);
211155
} catch (error) {
212156
thrownError = error;
213157
}
@@ -229,8 +173,6 @@ describe('BSON Binary Vector spec tests', () => {
229173
try {
230174
const bin = make(test.vector, test.dtype_hex, test.padding);
231175
BSON.EJSON.stringify({ bin });
232-
// TODO(NODE-6537): The following validation MUST be a part of stringify
233-
validateVector(bin);
234176
} catch (error) {
235177
thrownError = error;
236178
}

0 commit comments

Comments
 (0)