Repository navigation
Expand file tree
/
Copy pathReverseListTest.java
More file actions
74 lines (54 loc) · 2.35 KB
/
Copy pathReverseListTest.java
File metadata and controls
74 lines (54 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package list.reverse;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
public class ReverseListTest {
@Test
public void reverse_ReverseTwice_Identity() {
LinkedList list = ReverseListMain.create("abcdef");
list.reverse();
assertThat(list.toString()).isEqualTo("f -> e -> d -> c -> b -> a");
list.reverse();
assertThat(list.toString()).isEqualTo("a -> b -> c -> d -> e -> f");
}
@Test
public void reverseRecursive_List_Reversed() {
LinkedList list = ReverseListMain.create("abcdef");
list.reverseRecursive();
assertThat(list.toString()).isEqualTo("f -> e -> d -> c -> b -> a");
list.reverseRecursive();
assertThat(list.toString()).isEqualTo("a -> b -> c -> d -> e -> f");
}
@Test
public void reversePairs_OddList_PairsReversed() {
LinkedList listOdd = ReverseListMain.create("abcde");
listOdd.reversePairs();
assertThat(listOdd.toString()).isEqualTo("d -> e -> b -> c -> a");
listOdd.reversePairs();
assertThat(listOdd.toString()).isEqualTo("c -> a -> e -> b -> d");
}
@Test
public void reversePairs_EvenList_PairsReversed() {
LinkedList listEven = ReverseListMain.create("abcdef");
listEven.reversePairs();
assertThat(listEven.toString()).isEqualTo("e -> f -> c -> d -> a -> b");
listEven.reversePairs();
assertThat(listEven.toString()).isEqualTo("a -> b -> c -> d -> e -> f");
}
@Test
public void reverseAlternateK_Lists_ReversedAlternateK() {
LinkedList list = ReverseListMain.create("abc");
list.reverseAlternateK(3);
assertThat(list.toString()).isEqualTo("c -> b -> a");
list = ReverseListMain.create("abcde");
list.reverseAlternateK(2);
assertThat(list.toString()).isEqualTo("b -> a -> c -> d -> e");
list = ReverseListMain.create("abcdefghi");
list.reverseAlternateK(3);
assertThat(list.toString()).isEqualTo("c -> b -> a -> d -> e -> f -> i -> h -> g");
list.reverseAlternateK(3);
assertThat(list.toString()).isEqualTo("a -> b -> c -> d -> e -> f -> g -> h -> i");
list = ReverseListMain.create("abcdef");
list.reverseAlternateK(5);
assertThat(list.toString()).isEqualTo("e -> d -> c -> b -> a -> f");
}
}