@@ -111,6 +111,98 @@ describe('Append tests', () => {
111111 } ) ;
112112} ) ;
113113
114+ describe ( 'Prepend tests' , ( ) => {
115+ it ( 'Test prepending string' , ( ) => {
116+ const buffer = new DynamicBuffer ( ) ;
117+ const str = 'Hello world' ;
118+
119+ const count = buffer . prepend ( str ) ;
120+
121+ assert . equal ( count , str . length ) ;
122+ assert . equal ( buffer . toString ( ) , str ) ;
123+ } ) ;
124+
125+ it ( 'Test prepending string twice time' , ( ) => {
126+ const buffer = new DynamicBuffer ( { size : 32 } ) ;
127+ let str = ' world' ;
128+ let count = buffer . prepend ( str ) ;
129+ assert . equal ( count , str . length ) ;
130+ assert . equal ( buffer . toString ( ) , str ) ;
131+
132+ str = 'Hello' ;
133+ count = buffer . prepend ( str ) ;
134+ assert . equal ( count , str . length ) ;
135+ assert . equal ( buffer . toString ( ) , 'Hello world' ) ;
136+ assert . equal ( buffer . length , 'Hello world' . length ) ;
137+ } ) ;
138+
139+ it ( 'Test prepending empty string' , ( ) => {
140+ const buffer = new DynamicBuffer ( ) ;
141+
142+ const count = buffer . prepend ( '' ) ;
143+
144+ assert . equal ( count , 0 ) ;
145+ assert . equal ( buffer . toString ( ) , '' ) ;
146+ } ) ;
147+
148+ it ( 'Test prepending without parameter' , ( ) => {
149+ const buffer = new DynamicBuffer ( ) ;
150+
151+ assert . throws ( ( ) => {
152+ // @ts -ignore
153+ buffer . prepend ( ) ;
154+ } ) ;
155+ } ) ;
156+
157+ it ( 'Test prepending null' , ( ) => {
158+ const buffer = new DynamicBuffer ( ) ;
159+
160+ assert . throws ( ( ) => {
161+ // @ts -ignore
162+ buffer . prepend ( null ) ;
163+ } ) ;
164+ } ) ;
165+
166+ it ( 'Test prepending a number' , ( ) => {
167+ const buffer = new DynamicBuffer ( ) ;
168+
169+ assert . throws ( ( ) => {
170+ // @ts -ignore
171+ buffer . prepend ( 65 ) ;
172+ } ) ;
173+ } ) ;
174+
175+ it ( 'Test prepending string with small length' , ( ) => {
176+ const buffer = new DynamicBuffer ( ' world' ) ;
177+
178+ const count = buffer . prepend ( 'Hello!!!!' , 5 , 'utf8' ) ;
179+
180+ assert . equal ( count , 5 ) ;
181+ assert . equal ( buffer . toString ( ) , 'Hello world' ) ;
182+ } ) ;
183+
184+ it ( 'Test prepending string with large length' , ( ) => {
185+ const buffer = new DynamicBuffer ( ' world' ) ;
186+ const str = 'Hello' ;
187+
188+ const count = buffer . prepend ( str , 16 , 'utf-8' ) ;
189+
190+ assert . equal ( count , str . length ) ;
191+ assert . equal ( buffer . toString ( ) , 'Hello world' ) ;
192+ } ) ;
193+
194+ it ( 'Test prepending string to zero size buffer' , ( ) => {
195+ const buffer = new DynamicBuffer ( { size : 0 } ) ;
196+ const str = 'Hello world' ;
197+
198+ const count = buffer . prepend ( str ) ;
199+
200+ assert . equal ( count , str . length ) ;
201+ assert . equal ( Reflect . get ( buffer , 'size' ) , str . length ) ;
202+ assert . equal ( buffer . toBuffer ( ) . toString ( ) , str ) ;
203+ } ) ;
204+ } ) ;
205+
114206describe ( 'Write tests' , ( ) => {
115207 it ( 'Test writing string without offset' , ( ) => {
116208 const buffer = new DynamicBuffer ( ) ;
0 commit comments