77
88public class SimpleLinkedListTest {
99
10- @ Test
11- @ DisplayName ("A new list is empty" )
12- public void aNewListIsEmpty () {
13- SimpleLinkedList <Integer > list = new SimpleLinkedList <>();
14- assertThat (list .size ()).isEqualTo (0 );
15- }
16-
17- @ Disabled ("Remove to run test" )
18- @ Test
19- @ DisplayName ("Create list from array" )
20- public void canCreateFromArray () {
21- Character [] values = new Character []{'1' , '2' , '3' };
22- SimpleLinkedList <Character > list = new SimpleLinkedList <Character >(values );
23- assertThat (list .size ()).isEqualTo (3 );
24- }
25-
26- @ Disabled ("Remove to run test" )
27- @ Test
28- @ DisplayName ("Popping an empty list throws NoSuchElementException" )
29- public void popOnEmptyListWillThrow () {
30- SimpleLinkedList <String > list = new SimpleLinkedList <String >();
31- assertThatExceptionOfType (NoSuchElementException .class ).isThrownBy (list ::pop );
32- }
33-
34- @ Disabled ("Remove to run test" )
35- @ Test
36- @ DisplayName ("Pop returns last added element (LIFO)" )
37- public void popReturnsLastAddedElement () {
38- SimpleLinkedList <Integer > list = new SimpleLinkedList <Integer >();
39- list .push (9 );
40- list .push (8 );
41- assertThat (list .size ()).isEqualTo (2 );
42- assertThat (list .pop ()).isEqualTo (8 );
43- assertThat (list .pop ()).isEqualTo (9 );
44- assertThat (list .size ()).isEqualTo (0 );
45- }
46-
47- @ Disabled ("Remove to run test" )
48- @ Test
49- @ DisplayName ("Reverse reverses the list order" )
50- public void reverseReversesList () {
51- SimpleLinkedList <String > list = new SimpleLinkedList <String >();
52- list .push ("9" );
53- list .push ("8" );
54- list .push ("7" );
55- list .push ("6" );
56- list .push ("5" );
57- list .reverse ();
58- assertThat (list .pop ()).isEqualTo ("9" );
59- assertThat (list .pop ()).isEqualTo ("8" );
60- assertThat (list .pop ()).isEqualTo ("7" );
61- assertThat (list .pop ()).isEqualTo ("6" );
62- assertThat (list .pop ()).isEqualTo ("5" );
63- }
64-
65- @ Disabled ("Remove to run test" )
66- @ Test
67- @ DisplayName ("Can return list as an array" )
68- public void canReturnListAsArray () {
69- SimpleLinkedList <Character > list = new SimpleLinkedList <Character >();
70- list .push ('9' );
71- list .push ('8' );
72- list .push ('7' );
73- list .push ('6' );
74- list .push ('5' );
75- Character [] expected = {'5' , '6' , '7' , '8' , '9' };
76- assertThat (list .asArray (Character .class )).isEqualTo (expected );
77- }
78-
79- @ Disabled ("Remove to run test" )
80- @ Test
81- @ DisplayName ("Can return empty list as an empty array" )
82- public void canReturnEmptyListAsEmptyArray () {
83- SimpleLinkedList <Object > list = new SimpleLinkedList <Object >();
84- Object [] expected = {};
85- assertThat (list .asArray (Object .class )).isEqualTo (expected );
86- }
87-
88- // count tests
89-
90- @ Disabled ("Remove to run test" )
9110 @ Test
9211 @ DisplayName ("count -> Empty list has length of zero" )
9312 public void countEmptyListHasLengthOfZero () {
@@ -111,8 +30,6 @@ public void countNonEmptyListHasCorrectLength() {
11130 assertThat (list .size ()).isEqualTo (3 );
11231 }
11332
114- // pop tests
115-
11633 @ Disabled ("Remove to run test" )
11734 @ Test
11835 @ DisplayName ("pop -> Pop from empty list is an error" )
@@ -158,8 +75,6 @@ public void popUpdatesTheCount() {
15875 assertThat (list .size ()).isEqualTo (0 );
15976 }
16077
161- // push tests
162-
16378 @ Disabled ("Remove to run test" )
16479 @ Test
16580 @ DisplayName ("push -> Can push to an empty list" )
@@ -202,8 +117,6 @@ public void pushAndPop() {
202117 assertThat (list .size ()).isEqualTo (0 );
203118 }
204119
205- // peek tests
206-
207120 @ Disabled ("Remove to run test" )
208121 @ Test
209122 @ DisplayName ("peek -> Peek on empty list is an error" )
@@ -251,8 +164,6 @@ public void canPeekAfterAPopAndPush() {
251164 assertThat (list .peek ()).isEqualTo (3 );
252165 }
253166
254- // toList tests
255-
256167 @ Disabled ("Remove to run test" )
257168 @ Test
258169 @ DisplayName ("toList LIFO -> Empty linked list to list is empty" )
@@ -282,37 +193,6 @@ public void toListLifoToListAfterAPop() {
282193 assertThat (list .toList ()).containsExactly (4 , 2 , 1 );
283194 }
284195
285- @ Disabled ("Remove to run test" )
286- @ Test
287- @ DisplayName ("toList FIFO -> Empty linked list to list is empty" )
288- public void toListFifoEmptyLinkedListToListIsEmpty () {
289- SimpleLinkedList <Integer > list = new SimpleLinkedList <>();
290- assertThat (list .toList ()).isEmpty ();
291- }
292-
293- @ Disabled ("Remove to run test" )
294- @ Test
295- @ DisplayName ("toList FIFO -> To list with multiple values" )
296- public void toListFifoToListWithMultipleValues () {
297- SimpleLinkedList <Integer > list = new SimpleLinkedList <>(new Integer []{1 , 2 , 3 });
298- assertThat (list .toList ()).containsExactly (1 , 2 , 3 );
299- }
300-
301- @ Disabled ("Remove to run test" )
302- @ Test
303- @ DisplayName ("toList FIFO -> To list after a pop" )
304- public void toListFifoToListAfterAPop () {
305- SimpleLinkedList <Integer > list = new SimpleLinkedList <>();
306- list .push (1 );
307- list .push (2 );
308- list .push (3 );
309- assertThat (list .pop ()).isEqualTo (3 );
310- list .push (4 );
311- assertThat (list .toList ()).containsExactly (4 , 2 , 1 );
312- }
313-
314- // reverse tests
315-
316196 @ Disabled ("Remove to run test" )
317197 @ Test
318198 @ DisplayName ("reverse -> Reversed empty list has same values" )
@@ -354,4 +234,36 @@ public void doubleReverse() {
354234 assertThat (list .pop ()).isEqualTo (2 );
355235 assertThat (list .pop ()).isEqualTo (3 );
356236 }
237+
238+ @ Disabled ("Remove to run test" )
239+ @ Test
240+ @ DisplayName ("Can return list as an array" )
241+ public void canReturnListAsArray () {
242+ SimpleLinkedList <Character > list = new SimpleLinkedList <Character >();
243+ list .push ('9' );
244+ list .push ('8' );
245+ list .push ('7' );
246+ list .push ('6' );
247+ list .push ('5' );
248+ Character [] expected = {'5' , '6' , '7' , '8' , '9' };
249+ assertThat (list .asArray (Character .class )).isEqualTo (expected );
250+ }
251+
252+ @ Disabled ("Remove to run test" )
253+ @ Test
254+ @ DisplayName ("Can return empty list as an empty array" )
255+ public void canReturnEmptyListAsEmptyArray () {
256+ SimpleLinkedList <Object > list = new SimpleLinkedList <Object >();
257+ Object [] expected = {};
258+ assertThat (list .asArray (Object .class )).isEqualTo (expected );
259+ }
260+
261+ @ Disabled ("Remove to run test" )
262+ @ Test
263+ @ DisplayName ("Create list from array" )
264+ public void canCreateFromArray () {
265+ Character [] values = new Character []{'1' , '2' , '3' };
266+ SimpleLinkedList <Character > list = new SimpleLinkedList <Character >(values );
267+ assertThat (list .size ()).isEqualTo (3 );
268+ }
357269}
0 commit comments