@@ -3,6 +3,15 @@ import { BSON, Double } from '../register-bson';
33
44import { BSON_DATA_NUMBER , BSON_DATA_INT } from '../../src/constants' ;
55import { 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 ;
615
716describe ( 'BSON Double Precision' , function ( ) {
817 context ( 'class Double' , function ( ) {
@@ -297,4 +306,22 @@ describe('BSON Double Precision', function () {
297306 } ) ;
298307 } ) ;
299308 } ) ;
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+ } ) ;
300327} ) ;
0 commit comments