@@ -3,6 +3,15 @@ import { BSON, Double } from '../register-bson';
3
3
4
4
import { BSON_DATA_NUMBER , BSON_DATA_INT } from '../../src/constants' ;
5
5
import { inspect } from 'node:util' ;
6
+ import { bufferFromHexArray } from './tools/utils' ;
7
+
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 ;
6
15
7
16
describe ( 'BSON Double Precision' , function ( ) {
8
17
context ( 'class Double' , function ( ) {
@@ -297,4 +306,22 @@ describe('BSON Double Precision', function () {
297
306
} ) ;
298
307
} ) ;
299
308
} ) ;
309
+
310
+ context . only ( `handles ${ isBigEndian ? 'big' : 'little' } endianness correctly` , ( ) => {
311
+ const bsonWithFloat = bufferFromHexArray ( [
312
+ '01' , // double
313
+ '6100' , // 'a'
314
+ '00' . repeat ( 6 ) + 'f0bf' // 8 byte LE float equal to -1
315
+ ] ) ;
316
+
317
+ it ( 'deserialize should return -1' , ( ) => {
318
+ const res = BSON . deserialize ( bsonWithFloat ) ;
319
+ expect ( res ) . to . have . property ( 'a' , - 1 ) ;
320
+ } ) ;
321
+
322
+ it ( 'serialize should set bytes to -1 in little endian format' , ( ) => {
323
+ const res = BSON . serialize ( { a : new Double ( - 1 ) } ) ;
324
+ expect ( res ) . to . deep . equal ( bsonWithFloat ) ;
325
+ } ) ;
326
+ } ) ;
300
327
} ) ;
0 commit comments