@@ -182,3 +182,93 @@ describe('Includes tests', () => {
182182 assert . equal ( buf . includes ( '' , 32 ) , true ) ;
183183 } ) ;
184184} ) ;
185+
186+ describe ( 'LastIndexOf tests' , ( ) => {
187+ it ( 'Test lastIndexOf with empty buffer and empty value.' , ( ) => {
188+ const buf = new DynamicBuffer ( ) ;
189+
190+ assert . equal ( buf . lastIndexOf ( 'ABC' ) , - 1 ) ;
191+ assert . equal ( buf . lastIndexOf ( '' ) , 0 ) ;
192+ assert . equal ( buf . lastIndexOf ( 0 ) , - 1 ) ;
193+ assert . equal ( buf . lastIndexOf ( Buffer . alloc ( 0 ) ) , 0 ) ;
194+ assert . equal ( buf . lastIndexOf ( new Uint8Array ( ) ) , 0 ) ;
195+ assert . equal ( buf . lastIndexOf ( new DynamicBuffer ( ) ) , 0 ) ;
196+ } ) ;
197+
198+ it ( 'Test lastIndexOf with empty value.' , ( ) => {
199+ const buf = new DynamicBuffer ( ) ;
200+ buf . append ( 'ABCABCABC' ) ;
201+
202+ assert . equal ( buf . lastIndexOf ( '' ) , 9 ) ;
203+ assert . equal ( buf . lastIndexOf ( 0 ) , - 1 ) ;
204+ assert . equal ( buf . lastIndexOf ( Buffer . alloc ( 0 ) ) , 9 ) ;
205+ assert . equal ( buf . lastIndexOf ( new Uint8Array ( ) ) , 9 ) ;
206+ assert . equal ( buf . lastIndexOf ( new DynamicBuffer ( ) ) , 9 ) ;
207+ } ) ;
208+
209+ it ( 'Test lastIndexOf with string.' , ( ) => {
210+ const buf = new DynamicBuffer ( ) ;
211+ buf . append ( 'ABCABCABC' ) ;
212+
213+ assert . equal ( buf . lastIndexOf ( 'ABC' ) , 6 ) ;
214+ assert . equal ( buf . lastIndexOf ( 'BCA' ) , 4 ) ;
215+ assert . equal ( buf . lastIndexOf ( 'abc' ) , - 1 ) ;
216+ } ) ;
217+
218+ it ( 'Test lastIndexOf with number.' , ( ) => {
219+ const buf = new DynamicBuffer ( ) ;
220+ buf . append ( 'ABCABCABC' ) ;
221+
222+ assert . equal ( buf . lastIndexOf ( 65 ) , 6 ) ; // A
223+ assert . equal ( buf . lastIndexOf ( 67 ) , 8 ) ; // C
224+ assert . equal ( buf . lastIndexOf ( 97 ) , - 1 ) ; // a
225+ } ) ;
226+
227+ it ( 'Test lastIndexOf with Buffer.' , ( ) => {
228+ const buf = new DynamicBuffer ( ) ;
229+ buf . append ( 'ABCABCABC' ) ;
230+
231+ assert . equal ( buf . lastIndexOf ( Buffer . from ( 'ABC' ) ) , 6 ) ;
232+ assert . equal ( buf . lastIndexOf ( Buffer . from ( 'BCA' ) ) , 4 ) ;
233+ assert . equal ( buf . lastIndexOf ( Buffer . from ( 'abc' ) ) , - 1 ) ;
234+ } ) ;
235+
236+ it ( 'Test lastIndexOf with Uint8Array.' , ( ) => {
237+ const buf = new DynamicBuffer ( ) ;
238+ buf . append ( 'ABCABCABC' ) ;
239+
240+ assert . equal ( buf . lastIndexOf ( new Uint8Array ( [ 0x41 , 0x42 , 0x43 ] ) ) , 6 ) ;
241+ assert . equal ( buf . lastIndexOf ( new Uint8Array ( [ 0x42 , 0x43 , 0x41 ] ) ) , 4 ) ;
242+ assert . equal ( buf . lastIndexOf ( new Uint8Array ( [ 0x61 , 0x62 , 0x63 ] ) ) , - 1 ) ;
243+ } ) ;
244+
245+ it ( 'Test lastIndexOf with DynamicBuffer.' , ( ) => {
246+ const buf = new DynamicBuffer ( ) ;
247+ buf . append ( 'ABCABCABC' ) ;
248+
249+ const buf1 = new DynamicBuffer ( ) ;
250+ buf1 . append ( 'ABC' ) ;
251+ assert . equal ( buf . lastIndexOf ( buf1 ) , 6 ) ;
252+
253+ const buf2 = new DynamicBuffer ( ) ;
254+ buf2 . append ( 'BCA' ) ;
255+ assert . equal ( buf . lastIndexOf ( buf2 ) , 4 ) ;
256+
257+ const buf3 = new DynamicBuffer ( ) ;
258+ buf3 . append ( 'abc' ) ;
259+ assert . equal ( buf . lastIndexOf ( buf3 ) , - 1 ) ;
260+ } ) ;
261+
262+ it ( 'Test lastIndexOf with byteOffset parameter.' , ( ) => {
263+ const buf = new DynamicBuffer ( ) ;
264+ buf . append ( 'ABCABCABC' ) ;
265+
266+ assert . equal ( buf . lastIndexOf ( 'ABC' ) , 6 ) ;
267+ assert . equal ( buf . lastIndexOf ( 'ABC' , 0 ) , 0 ) ;
268+ assert . equal ( buf . lastIndexOf ( 'ABC' , 1 ) , 0 ) ;
269+ assert . equal ( buf . lastIndexOf ( 'ABC' , - 1 ) , 6 ) ;
270+ assert . equal ( buf . lastIndexOf ( 'ABC' , - 9 ) , 0 ) ; // -11 equals 0
271+ assert . equal ( buf . lastIndexOf ( 'ABC' , 32 ) , 6 ) ;
272+ assert . equal ( buf . lastIndexOf ( '' , 32 ) , 9 ) ;
273+ } ) ;
274+ } ) ;
0 commit comments