@@ -171,6 +171,96 @@ describe('List', () => {
171171 } )
172172 } )
173173
174+ describe ( 'fromArray' , ( ) => {
175+ it ( 'should create a list from an array of elements' , ( ) => {
176+ const array = [ 1 , 2 , 3 , 4 ]
177+ const list = new List < number > ( )
178+ list . fromArray ( array )
179+
180+ const result = [ ] as number [ ]
181+ list . map ( ( node ) => result . push ( node . data ) )
182+
183+ expect ( result ) . toEqual ( array )
184+ } )
185+
186+ it ( 'should handle an empty array' , ( ) => {
187+ const array : number [ ] = [ ]
188+ const list = new List < number > ( )
189+ list . fromArray ( array )
190+
191+ expect ( list . head ) . toBeNull ( )
192+ expect ( list . tail ) . toBeNull ( )
193+ } )
194+
195+ it ( 'should correctly set head and tail for a single element array' , ( ) => {
196+ const array = [ 42 ]
197+ const list = new List < number > ( )
198+ list . fromArray ( array )
199+
200+ expect ( list . head ) . not . toBeNull ( )
201+ expect ( list . tail ) . not . toBeNull ( )
202+ expect ( list . head ) . toBe ( list . tail )
203+ expect ( list . head ?. data ) . toBe ( 42 )
204+ } )
205+
206+ it ( 'should correctly link nodes' , ( ) => {
207+ const array = [ 1 , 2 , 3 ]
208+ const list = new List < number > ( )
209+ list . fromArray ( array )
210+
211+ expect ( list . head ?. data ) . toBe ( 1 )
212+ expect ( list . head ?. next ?. data ) . toBe ( 2 )
213+ expect ( list . head ?. next ?. next ?. data ) . toBe ( 3 )
214+ expect ( list . head ?. next ?. next ?. next ) . toBeNull ( )
215+
216+ expect ( list . tail ?. data ) . toBe ( 3 )
217+ expect ( list . tail ?. prev ?. data ) . toBe ( 2 )
218+ expect ( list . tail ?. prev ?. prev ?. data ) . toBe ( 1 )
219+ expect ( list . tail ?. prev ?. prev ?. prev ) . toBeNull ( )
220+ } )
221+ } )
222+
223+ describe ( 'toArray' , ( ) => {
224+ it ( 'should return an array of all list nodes\' data' , ( ) => {
225+ const list = new List < number > ( )
226+ list . push ( new Node ( 1 ) )
227+ list . push ( new Node ( 2 ) )
228+ list . push ( new Node ( 3 ) )
229+
230+ const result = list . toArray ( )
231+
232+ expect ( result ) . toEqual ( [ 1 , 2 , 3 ] )
233+ } )
234+
235+ it ( 'should return an empty array when the list is empty' , ( ) => {
236+ const list = new List < number > ( )
237+
238+ const result = list . toArray ( )
239+
240+ expect ( result ) . toEqual ( [ ] )
241+ } )
242+
243+ it ( 'should handle a list with a single node' , ( ) => {
244+ const list = new List < number > ( )
245+ list . push ( new Node ( 42 ) )
246+
247+ const result = list . toArray ( )
248+
249+ expect ( result ) . toEqual ( [ 42 ] )
250+ } )
251+
252+ it ( 'should correctly handle a list with multiple nodes' , ( ) => {
253+ const list = new List < string > ( )
254+ list . push ( new Node ( 'a' ) )
255+ list . push ( new Node ( 'b' ) )
256+ list . push ( new Node ( 'c' ) )
257+
258+ const result = list . toArray ( )
259+
260+ expect ( result ) . toEqual ( [ 'a' , 'b' , 'c' ] )
261+ } )
262+ } )
263+
174264 describe ( 'toString' , ( ) => {
175265 it ( 'should return a concatenated string of JSON stringified node data' , ( ) => {
176266 const node1 = new Node ( { value : 1 } )
0 commit comments