@@ -259,3 +259,151 @@ describe('Set tests', () => {
259259 } ) ;
260260 } ) ;
261261} ) ;
262+
263+ describe ( 'More write methods test' , ( ) => {
264+ const writeCompare = ( func : ( buf : DynamicBuffer ) => any , expect : any ) => {
265+ const buf = new DynamicBuffer ( { size : 0 } ) ;
266+ func ( buf ) ;
267+ assert . deepEqual ( buf . toJSON ( ) . data , expect ) ;
268+ } ;
269+
270+ it ( 'Test write BigInt' , ( ) => {
271+ writeCompare (
272+ ( buf : DynamicBuffer ) => buf . writeBigInt64BE ( 0x0102030405060708n ) ,
273+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ] ,
274+ ) ;
275+
276+ writeCompare (
277+ ( buf : DynamicBuffer ) => buf . writeBigInt64LE ( 0x0102030405060708n ) ,
278+ [ 0x08 , 0x07 , 0x06 , 0x05 , 0x04 , 0x03 , 0x02 , 0x01 ] ,
279+ ) ;
280+
281+ writeCompare (
282+ ( buf : DynamicBuffer ) => buf . writeBigUInt64BE ( 0x0102030405060708n ) ,
283+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ] ,
284+ ) ;
285+
286+ writeCompare (
287+ ( buf : DynamicBuffer ) => buf . writeBigUInt64LE ( 0x0102030405060708n ) ,
288+ [ 0x08 , 0x07 , 0x06 , 0x05 , 0x04 , 0x03 , 0x02 , 0x01 ] ,
289+ ) ;
290+ } ) ;
291+
292+ it ( 'Test write double and float' , ( ) => {
293+ writeCompare (
294+ ( buf : DynamicBuffer ) => buf . writeFloatBE ( 2.387939260590663e-38 ) ,
295+ [ 0x01 , 0x02 , 0x03 , 0x04 ] ,
296+ ) ;
297+
298+ writeCompare (
299+ ( buf : DynamicBuffer ) => buf . writeFloatLE ( 1.539989614439558e-36 ) ,
300+ [ 0x01 , 0x02 , 0x03 , 0x04 ] ,
301+ ) ;
302+
303+ writeCompare (
304+ ( buf : DynamicBuffer ) => buf . writeDoubleBE ( 8.20788039913184e-304 ) ,
305+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ] ,
306+ ) ;
307+
308+ writeCompare (
309+ ( buf : DynamicBuffer ) => buf . writeDoubleLE ( 5.447603722011605e-270 ) ,
310+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ] ,
311+ ) ;
312+ } ) ;
313+
314+ it ( 'Test write int' , ( ) => {
315+ writeCompare (
316+ ( buf : DynamicBuffer ) => buf . writeInt8 ( - 2 ) ,
317+ [ 0xfe ] ,
318+ ) ;
319+
320+ writeCompare (
321+ ( buf : DynamicBuffer ) => buf . writeInt16BE ( 0x0102 ) ,
322+ [ 0x01 , 0x02 ] ,
323+ ) ;
324+
325+ writeCompare (
326+ ( buf : DynamicBuffer ) => buf . writeInt16LE ( 0x0102 ) ,
327+ [ 0x02 , 0x01 ] ,
328+ ) ;
329+
330+ writeCompare (
331+ ( buf : DynamicBuffer ) => buf . writeInt32BE ( 0x01020304 ) ,
332+ [ 0x01 , 0x02 , 0x03 , 0x04 ] ,
333+ ) ;
334+
335+ writeCompare (
336+ ( buf : DynamicBuffer ) => buf . writeInt32LE ( 0x01020304 ) ,
337+ [ 0x04 , 0x03 , 0x02 , 0x01 ] ,
338+ ) ;
339+ } ) ;
340+
341+ it ( 'Test write uint' , ( ) => {
342+ writeCompare (
343+ ( buf : DynamicBuffer ) => buf . writeUInt8 ( 1 ) ,
344+ [ 0x01 ] ,
345+ ) ;
346+
347+ writeCompare (
348+ ( buf : DynamicBuffer ) => buf . writeUInt16BE ( 0x0102 ) ,
349+ [ 0x01 , 0x02 ] ,
350+ ) ;
351+
352+ writeCompare (
353+ ( buf : DynamicBuffer ) => buf . writeUInt16LE ( 0x0102 ) ,
354+ [ 0x02 , 0x01 ] ,
355+ ) ;
356+
357+ writeCompare (
358+ ( buf : DynamicBuffer ) => buf . writeUInt32BE ( 0x01020304 ) ,
359+ [ 0x01 , 0x02 , 0x03 , 0x04 ] ,
360+ ) ;
361+
362+ writeCompare (
363+ ( buf : DynamicBuffer ) => buf . writeUInt32LE ( 0x01020304 ) ,
364+ [ 0x04 , 0x03 , 0x02 , 0x01 ] ,
365+ ) ;
366+ } ) ;
367+
368+ it ( 'Test write with byte length' , ( ) => {
369+ const v : number [ ] = [ 0x01 , 0x0102 , 0x010203 , 0x01020304 , 0x0102030405 , 0x010203040506 ] ;
370+ const be : number [ ] [ ] = [
371+ [ 0x01 ] ,
372+ [ 0x01 , 0x02 ] ,
373+ [ 0x01 , 0x02 , 0x03 ] ,
374+ [ 0x01 , 0x02 , 0x03 , 0x04 ] ,
375+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 ] ,
376+ [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 ] ,
377+ ] ;
378+ const le : number [ ] [ ] = [
379+ [ 0x01 ] ,
380+ [ 0x02 , 0x01 ] ,
381+ [ 0x03 , 0x02 , 0x01 ] ,
382+ [ 0x04 , 0x03 , 0x02 , 0x01 ] ,
383+ [ 0x05 , 0x04 , 0x03 , 0x02 , 0x01 ] ,
384+ [ 0x06 , 0x05 , 0x04 , 0x03 , 0x02 , 0x01 ] ,
385+ ] ;
386+
387+ for ( let i = 0 ; i < 6 ; i += 1 ) {
388+ writeCompare (
389+ ( buf : DynamicBuffer ) => buf . writeIntBE ( v [ i ] , 0 , i + 1 ) ,
390+ be [ i ] ,
391+ ) ;
392+
393+ writeCompare (
394+ ( buf : DynamicBuffer ) => buf . writeIntLE ( v [ i ] , 0 , i + 1 ) ,
395+ le [ i ] ,
396+ ) ;
397+
398+ writeCompare (
399+ ( buf : DynamicBuffer ) => buf . writeUIntBE ( v [ i ] , 0 , i + 1 ) ,
400+ be [ i ] ,
401+ ) ;
402+
403+ writeCompare (
404+ ( buf : DynamicBuffer ) => buf . writeUIntLE ( v [ i ] , 0 , i + 1 ) ,
405+ le [ i ] ,
406+ ) ;
407+ }
408+ } ) ;
409+ } ) ;
0 commit comments