@@ -113,3 +113,69 @@ describe('Exports to JSON object', () => {
113113 assert . equal ( JSON . stringify ( buf ) , '{"type":"Buffer","data":[72,101,108,108,111]}' ) ;
114114 } ) ;
115115} ) ;
116+
117+ describe ( 'Copy to another storage' , ( ) => {
118+ it ( 'Test copy()' , ( ) => {
119+ const str = 'hello world' ;
120+ const buf1 = new DynamicBuffer ( str ) ;
121+ const buf2 = new DynamicBuffer ( ) ;
122+
123+ assert . equal ( buf1 . copy ( buf2 ) , str . length ) ;
124+ assert . equal ( buf2 . toString ( ) , 'hello world' ) ;
125+ } ) ;
126+
127+ it ( 'Test copy() with builtin Buffer' , ( ) => {
128+ const str = 'hello world' ;
129+ const buf1 = new DynamicBuffer ( str ) ;
130+ const buf2 = Buffer . alloc ( 15 , '.' ) ;
131+
132+ assert . equal ( buf1 . copy ( buf2 ) , str . length ) ;
133+ assert . equal ( buf2 . toString ( ) , 'hello world....' ) ;
134+ } ) ;
135+
136+ it ( 'Test copy() with builtin Uint8Array' , ( ) => {
137+ const str = 'hello world' ;
138+ const buf1 = new DynamicBuffer ( str ) ;
139+ const buf2 = new Uint8Array ( str . length ) ;
140+
141+ assert . equal ( buf1 . copy ( buf2 ) , str . length ) ;
142+ for ( let i = 0 ; i < buf2 . length ; i += 1 ) {
143+ assert . equal ( buf2 [ i ] , str . charCodeAt ( i ) ) ;
144+ }
145+ } ) ;
146+
147+ it ( 'Test copy() with invalid target start' , ( ) => {
148+ const buf1 = new DynamicBuffer ( 'hello world' ) ;
149+ const buf2 = new DynamicBuffer ( ) ;
150+
151+ assert . throws ( ( ) => {
152+ buf1 . copy ( buf2 , - 1 ) ;
153+ } ) ;
154+ } ) ;
155+
156+ it ( 'Test copy() with invalid source start' , ( ) => {
157+ const buf1 = new DynamicBuffer ( 'hello world' ) ;
158+ const buf2 = new DynamicBuffer ( ) ;
159+
160+ assert . throws ( ( ) => {
161+ buf1 . copy ( buf2 , 0 , - 1 ) ;
162+ } ) ;
163+ } ) ;
164+
165+ it ( 'Test copy() with big source end' , ( ) => {
166+ const str = '!hello world' ;
167+ const buf1 = new DynamicBuffer ( str ) ;
168+ const buf2 = new DynamicBuffer ( '...' ) ;
169+
170+ assert . equal ( buf1 . copy ( buf2 , 1 , 1 , 100 ) , str . length - 1 ) ;
171+ assert . equal ( buf2 . toString ( ) , '.hello world' ) ;
172+ } ) ;
173+
174+ it ( 'Test copy() with empty buffer' , ( ) => {
175+ const buf1 = new DynamicBuffer ( ) ;
176+ const buf2 = Buffer . alloc ( 5 , '.' ) ;
177+
178+ assert . equal ( buf1 . copy ( buf2 ) , 0 ) ;
179+ assert . equal ( buf2 . toString ( ) , '.....' ) ;
180+ } ) ;
181+ } ) ;
0 commit comments