|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
|
2
|
+import java.util.NoSuchElementException;
|
|
|
3
|
+import static org.assertj.core.api.Assertions.assertThat;
|
|
|
4
|
+import org.junit.Test;
|
|
|
5
|
+
|
|
|
6
|
+public class SimpleLinkedListTest {
|
|
|
7
|
+
|
|
|
8
|
+ @Test
|
|
|
9
|
+ public void aNewListIsEmpty() {
|
|
|
10
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
11
|
+ assertThat(list.size()).isEqualTo(0);
|
|
|
12
|
+ }
|
|
|
13
|
+
|
|
|
14
|
+ @Test
|
|
|
15
|
+ public void canCreateFromArray() {
|
|
|
16
|
+ Integer[] values = new Integer[]{1, 2, 3};
|
|
|
17
|
+ SimpleLinkedList list = new SimpleLinkedList(values);
|
|
|
18
|
+ assertThat(list.size()).isEqualTo(3);
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+ @Test(expected = NoSuchElementException.class)
|
|
|
22
|
+ public void popOnEmptyListWillThrow() {
|
|
|
23
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
24
|
+ list.pop();
|
|
|
25
|
+ }
|
|
|
26
|
+
|
|
|
27
|
+ @Test
|
|
|
28
|
+ public void popReturnsLastAddedElement() {
|
|
|
29
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
30
|
+ list.push(9);
|
|
|
31
|
+ list.push(8);
|
|
|
32
|
+ assertThat(list.size()).isEqualTo(2);
|
|
|
33
|
+ assertThat(list.pop()).isEqualTo(8);
|
|
|
34
|
+ assertThat(list.pop()).isEqualTo(9);
|
|
|
35
|
+ assertThat(list.size()).isEqualTo(0);
|
|
|
36
|
+ }
|
|
|
37
|
+
|
|
|
38
|
+ @Test
|
|
|
39
|
+ public void reverseReversesList() {
|
|
|
40
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
41
|
+ list.push(9);
|
|
|
42
|
+ list.push(8);
|
|
|
43
|
+ list.push(7);
|
|
|
44
|
+ list.push(6);
|
|
|
45
|
+ list.push(5);
|
|
|
46
|
+ list.reverse();
|
|
|
47
|
+ assertThat(list.pop()).isEqualTo(9);
|
|
|
48
|
+ assertThat(list.pop()).isEqualTo(8);
|
|
|
49
|
+ assertThat(list.pop()).isEqualTo(7);
|
|
|
50
|
+ assertThat(list.pop()).isEqualTo(6);
|
|
|
51
|
+ assertThat(list.pop()).isEqualTo(5);
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ @Test
|
|
|
55
|
+ public void canReturnListAsArray() {
|
|
|
56
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
57
|
+ list.push(9);
|
|
|
58
|
+ list.push(8);
|
|
|
59
|
+ list.push(7);
|
|
|
60
|
+ list.push(6);
|
|
|
61
|
+ list.push(5);
|
|
|
62
|
+ Integer[] expected = {5, 6, 7, 8, 9};
|
|
|
63
|
+ assertThat(list.asArray(Integer.class)).containsExactly(expected);
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ @Test
|
|
|
67
|
+ public void canReturnEmptyListAsEmptyArray() {
|
|
|
68
|
+ SimpleLinkedList list = new SimpleLinkedList();
|
|
|
69
|
+ Object[] expected = {};
|
|
|
70
|
+ assertThat(list.asArray(Object.class)).containsExactly(expected);
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+}
|