@@ -63,3 +63,100 @@ describe('At tests', () => {
6363 assert . equal ( buffer . at ( - str . length - 1 ) , undefined ) ;
6464 } ) ;
6565} ) ;
66+
67+ describe ( 'More read test' , ( ) => {
68+ it ( 'Test read big int from buffer' , ( ) => {
69+ const buf = new DynamicBuffer ( ) ;
70+
71+ buf [ 4 ] = 0xff ;
72+ buf [ 5 ] = 0xff ;
73+ buf [ 6 ] = 0xff ;
74+ buf [ 7 ] = 0xff ;
75+ // 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
76+
77+ assert . equal ( buf . readBigInt64BE ( ) , 4294967295n ) ;
78+ assert . equal ( buf . readBigInt64LE ( ) , - 4294967296n ) ;
79+ assert . equal ( buf . readBigUInt64BE ( ) , 4294967295n ) ;
80+ assert . equal ( buf . readBigUInt64LE ( ) , 18446744069414584320n ) ;
81+ } ) ;
82+
83+ it ( 'Test read double and float from buffer' , ( ) => {
84+ const buf = new DynamicBuffer ( ) ;
85+
86+ for ( let i = 0 ; i < 8 ; i += 1 ) {
87+ buf [ i ] = i + 1 ;
88+ }
89+
90+ assert . equal ( buf . readDoubleBE ( ) , 8.20788039913184e-304 ) ;
91+ assert . equal ( buf . readDoubleLE ( ) , 5.447603722011605e-270 ) ;
92+ assert . equal ( buf . readFloatBE ( ) , 2.387939260590663e-38 ) ;
93+ assert . equal ( buf . readFloatLE ( ) , 1.539989614439558e-36 ) ;
94+ } ) ;
95+
96+ it ( 'Test read int from buffer' , ( ) => {
97+ const buf = new DynamicBuffer ( ) ;
98+ buf [ 0 ] = 0x12 ;
99+ buf [ 1 ] = 0x34 ;
100+ buf [ 2 ] = 0x56 ;
101+ buf [ 3 ] = 0x78 ;
102+ buf [ 4 ] = 0x90 ;
103+ buf [ 5 ] = 0xab ;
104+
105+ const be = [ 0x12 , 0x1234 , 0x123456 , 0x12345678 , 0x1234567890 , 0x1234567890ab ] ;
106+ const le = [ 0x12 , 0x3412 , 0x563412 , 0x78563412 , 0x9078563412 , 0xab9078563412 ] ;
107+
108+ assert . equal ( buf . readInt8 ( ) , be [ 0 ] ) ;
109+ assert . equal ( buf . readInt16BE ( ) , be [ 1 ] ) ;
110+ assert . equal ( buf . readInt16LE ( ) , le [ 1 ] ) ;
111+ assert . equal ( buf . readInt32BE ( ) , be [ 3 ] ) ;
112+ assert . equal ( buf . readInt32LE ( ) , le [ 3 ] ) ;
113+
114+ assert . equal ( buf . readUInt8 ( ) , be [ 0 ] ) ;
115+ assert . equal ( buf . readUInt16BE ( ) , be [ 1 ] ) ;
116+ assert . equal ( buf . readUInt16LE ( ) , le [ 1 ] ) ;
117+ assert . equal ( buf . readUInt32BE ( ) , be [ 3 ] ) ;
118+ assert . equal ( buf . readUInt32LE ( ) , le [ 3 ] ) ;
119+
120+ for ( let i = 1 ; i <= 6 ; i += 1 ) {
121+ assert . equal ( buf . readIntBE ( 0 , i ) , be [ i - 1 ] ) ;
122+ assert . equal ( buf . readUIntBE ( 0 , i ) , be [ i - 1 ] ) ;
123+ assert . equal ( buf . readUIntLE ( 0 , i ) , le [ i - 1 ] ) ;
124+
125+ if ( i === 5 ) {
126+ assert . equal ( buf . readIntLE ( 0 , i ) , - 479017421806 ) ;
127+ } else if ( i === 6 ) {
128+ assert . equal ( buf . readIntLE ( 0 , i ) , - 92837994154990 ) ;
129+ } else {
130+ assert . equal ( buf . readIntLE ( 0 , i ) , le [ i - 1 ] ) ;
131+ }
132+ }
133+ } ) ;
134+
135+ it ( 'Test read empty buffer' , ( ) => {
136+ const buf = new DynamicBuffer ( ) ;
137+
138+ assert . throws ( ( ) => buf . readBigInt64BE ( ) ) ;
139+ assert . throws ( ( ) => buf . readBigInt64LE ( ) ) ;
140+ assert . throws ( ( ) => buf . readBigUInt64BE ( ) ) ;
141+ assert . throws ( ( ) => buf . readBigUInt64LE ( ) ) ;
142+ assert . throws ( ( ) => buf . readDoubleBE ( ) ) ;
143+ assert . throws ( ( ) => buf . readDoubleLE ( ) ) ;
144+ assert . throws ( ( ) => buf . readFloatBE ( ) ) ;
145+ assert . throws ( ( ) => buf . readFloatLE ( ) ) ;
146+ assert . throws ( ( ) => buf . readInt8 ( ) ) ;
147+ assert . throws ( ( ) => buf . readInt16BE ( ) ) ;
148+ assert . throws ( ( ) => buf . readInt16LE ( ) ) ;
149+ assert . throws ( ( ) => buf . readInt32BE ( ) ) ;
150+ assert . throws ( ( ) => buf . readInt32LE ( ) ) ;
151+ assert . throws ( ( ) => buf . readUInt8 ( ) ) ;
152+ assert . throws ( ( ) => buf . readUInt16BE ( ) ) ;
153+ assert . throws ( ( ) => buf . readUInt16LE ( ) ) ;
154+ assert . throws ( ( ) => buf . readUInt32BE ( ) ) ;
155+ assert . throws ( ( ) => buf . readUInt32LE ( ) ) ;
156+
157+ assert . throws ( ( ) => buf . readIntBE ( 0 , 6 ) ) ;
158+ assert . throws ( ( ) => buf . readIntLE ( 0 , 6 ) ) ;
159+ assert . throws ( ( ) => buf . readUIntBE ( 0 , 6 ) ) ;
160+ assert . throws ( ( ) => buf . readUIntLE ( 0 , 6 ) ) ;
161+ } ) ;
162+ } ) ;
0 commit comments