@@ -92,3 +92,93 @@ describe('IndexOf tests', () => {
9292 assert . equal ( buf . indexOf ( '' , 32 ) , 0 ) ;
9393 } ) ;
9494} ) ;
95+
96+ describe ( 'Includes tests' , ( ) => {
97+ it ( 'Test includes with empty buffer and empty value.' , ( ) => {
98+ const buf = new DynamicBuffer ( ) ;
99+
100+ assert . equal ( buf . includes ( 'Hello' ) , false ) ;
101+ assert . equal ( buf . includes ( '' ) , true ) ;
102+ assert . equal ( buf . includes ( 0 ) , false ) ;
103+ assert . equal ( buf . includes ( Buffer . alloc ( 0 ) ) , true ) ;
104+ assert . equal ( buf . includes ( new Uint8Array ( ) ) , true ) ;
105+ assert . equal ( buf . includes ( new DynamicBuffer ( ) ) , true ) ;
106+ } ) ;
107+
108+ it ( 'Test includes with empty value.' , ( ) => {
109+ const buf = new DynamicBuffer ( ) ;
110+ buf . append ( 'Hello world' ) ;
111+
112+ assert . equal ( buf . includes ( '' ) , true ) ;
113+ assert . equal ( buf . includes ( 0 ) , false ) ;
114+ assert . equal ( buf . includes ( Buffer . alloc ( 0 ) ) , true ) ;
115+ assert . equal ( buf . includes ( new Uint8Array ( ) ) , true ) ;
116+ assert . equal ( buf . includes ( new DynamicBuffer ( ) ) , true ) ;
117+ } ) ;
118+
119+ it ( 'Test includes with string.' , ( ) => {
120+ const buf = new DynamicBuffer ( ) ;
121+ buf . append ( 'Hello world' ) ;
122+
123+ assert . equal ( buf . includes ( 'Hello' ) , true ) ;
124+ assert . equal ( buf . includes ( 'world' ) , true ) ;
125+ assert . equal ( buf . includes ( 'no' ) , false ) ;
126+ } ) ;
127+
128+ it ( 'Test includes with number.' , ( ) => {
129+ const buf = new DynamicBuffer ( ) ;
130+ buf . append ( 'Hello world' ) ;
131+
132+ assert . equal ( buf . includes ( 72 ) , true ) ; // H
133+ assert . equal ( buf . includes ( 119 ) , true ) ; // w
134+ assert . equal ( buf . includes ( 97 ) , false ) ; // a
135+ } ) ;
136+
137+ it ( 'Test includes with Buffer.' , ( ) => {
138+ const buf = new DynamicBuffer ( ) ;
139+ buf . append ( 'Hello world' ) ;
140+
141+ assert . equal ( buf . includes ( Buffer . from ( 'Hello' ) ) , true ) ;
142+ assert . equal ( buf . includes ( Buffer . from ( 'world' ) ) , true ) ;
143+ assert . equal ( buf . includes ( Buffer . from ( 'no' ) ) , false ) ;
144+ } ) ;
145+
146+ it ( 'Test includes with Uint8Array.' , ( ) => {
147+ const buf = new DynamicBuffer ( ) ;
148+ buf . append ( 'Hello world' ) ;
149+
150+ assert . equal ( buf . includes ( new Uint8Array ( [ 0x48 , 0x65 , 0x6c , 0x6c , 0x6f ] ) ) , true ) ;
151+ assert . equal ( buf . includes ( new Uint8Array ( [ 0x77 , 0x6f , 0x72 , 0x6c , 0x64 ] ) ) , true ) ;
152+ assert . equal ( buf . includes ( new Uint8Array ( [ 0x6e , 0x6c ] ) ) , false ) ;
153+ } ) ;
154+
155+ it ( 'Test includes with DynamicBuffer.' , ( ) => {
156+ const buf = new DynamicBuffer ( ) ;
157+ buf . append ( 'Hello world' ) ;
158+
159+ const buf1 = new DynamicBuffer ( ) ;
160+ buf1 . append ( 'Hello' ) ;
161+ assert . equal ( buf . includes ( buf1 ) , true ) ;
162+
163+ const buf2 = new DynamicBuffer ( ) ;
164+ buf2 . append ( 'world' ) ;
165+ assert . equal ( buf . includes ( buf2 ) , true ) ;
166+
167+ const buf3 = new DynamicBuffer ( ) ;
168+ buf3 . append ( 'no' ) ;
169+ assert . equal ( buf . includes ( buf3 ) , false ) ;
170+ } ) ;
171+
172+ it ( 'Test includes with byteOffset parameter.' , ( ) => {
173+ const buf = new DynamicBuffer ( ) ;
174+ buf . append ( 'Hello world' ) ;
175+
176+ assert . equal ( buf . includes ( 'Hello' ) , true ) ;
177+ assert . equal ( buf . includes ( 'Hello' , 0 ) , true ) ;
178+ assert . equal ( buf . includes ( 'Hello' , 1 ) , false ) ;
179+ assert . equal ( buf . includes ( 'Hello' , - 1 ) , false ) ;
180+ assert . equal ( buf . includes ( 'Hello' , - 11 ) , true ) ; // -11 equals 0
181+ assert . equal ( buf . includes ( 'Hello' , 32 ) , false ) ;
182+ assert . equal ( buf . includes ( '' , 32 ) , true ) ;
183+ } ) ;
184+ } ) ;
0 commit comments