Skip to content

Commit c726f00

Browse files
committed
linked-list: exercise generics via tests
1 parent 3e55b7d commit c726f00

1 file changed

Lines changed: 48 additions & 38 deletions

File tree

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,78 @@
1-
import org.junit.Before;
21
import org.junit.Ignore;
32
import org.junit.Test;
43

5-
import static org.hamcrest.CoreMatchers.*;
6-
import static org.junit.Assert.*;
4+
import static org.hamcrest.CoreMatchers.is;
5+
import static org.junit.Assert.assertThat;
76

87
public class DoublyLinkedListTest {
9-
private DoublyLinkedList<Integer> subject;
10-
11-
@Before
12-
public void setUp() {
13-
subject = new DoublyLinkedList<>();
14-
}
15-
168

179
@Test
1810
public void testPushPop() {
19-
subject.push(10);
20-
subject.push(20);
21-
assertThat(subject.pop(), is(20));
22-
assertThat(subject.pop(), is(10));
11+
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
12+
13+
list.push(10);
14+
list.push(20);
15+
16+
assertThat(list.pop(), is(20));
17+
assertThat(list.pop(), is(10));
2318
}
2419

2520
@Ignore("Remove to run test")
2621
@Test
2722
public void testPushShift() {
28-
subject.push(10);
29-
subject.push(20);
30-
assertThat(subject.shift(), is(10));
31-
assertThat(subject.shift(), is(20));
23+
DoublyLinkedList<String> list = new DoublyLinkedList<>();
24+
25+
list.push("10");
26+
list.push("20");
27+
28+
assertThat(list.shift(), is("10"));
29+
assertThat(list.shift(), is("20"));
3230
}
3331

3432
@Ignore("Remove to run test")
3533
@Test
3634
public void testUnshiftShift() {
37-
subject.unshift(10);
38-
subject.unshift(20);
39-
assertThat(subject.shift(), is(20));
40-
assertThat(subject.shift(), is(10));
35+
DoublyLinkedList<Character> list = new DoublyLinkedList<>();
36+
37+
list.unshift('1');
38+
list.unshift('2');
39+
40+
assertThat(list.shift(), is('2'));
41+
assertThat(list.shift(), is('1'));
4142
}
4243

4344
@Ignore("Remove to run test")
4445
@Test
4546
public void testUnshiftPop() {
46-
subject.unshift(10);
47-
subject.unshift(20);
48-
assertThat(subject.pop(), is(10));
49-
assertThat(subject.pop(), is(20));
47+
DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
48+
49+
list.unshift(10);
50+
list.unshift(20);
51+
52+
assertThat(list.pop(), is(10));
53+
assertThat(list.pop(), is(20));
5054
}
5155

5256
@Ignore("Remove to run test")
5357
@Test
5458
public void testExample() {
55-
subject.push(10);
56-
subject.push(20);
57-
assertThat(subject.pop(), is(20));
58-
59-
subject.push(30);
60-
assertThat(subject.shift(), is(10));
61-
62-
subject.unshift(40);
63-
subject.push(50);
64-
assertThat(subject.shift(), is(40));
65-
assertThat(subject.pop(), is(50));
66-
assertThat(subject.shift(), is(30));
59+
DoublyLinkedList<String> list = new DoublyLinkedList<>();
60+
61+
list.push("ten");
62+
list.push("twenty");
63+
64+
assertThat(list.pop(), is("twenty"));
65+
66+
list.push("thirty");
67+
68+
assertThat(list.shift(), is("ten"));
69+
70+
list.unshift("forty");
71+
list.push("fifty");
72+
73+
assertThat(list.shift(), is("forty"));
74+
assertThat(list.pop(), is("fifty"));
75+
assertThat(list.shift(), is("thirty"));
6776
}
77+
6878
}

0 commit comments

Comments
 (0)