-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathSolutionTest.java
103 lines (87 loc) · 3.63 KB
/
SolutionTest.java
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.blankj.hard._025;
import com.blankj.structure.ListNode;
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import java.lang.ArithmeticException;
import java.util.List;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
public class SolutionTest {
Solution solution = new Solution();
@Test
public void TestReverseKGroupsWithHeadNull_1(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), -1);
assertNull(actual);
}
@Test
public void TestReverseKGroupsWithHeadNull_2(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), 0);
assertNull(actual);
}
@Test
public void TestReverseKGroupsWithHeadNull_3(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), 1);
assertNull(actual);
}
@Test
public void TestReverseKGroupWith_k_isZero() throws Exception {
try {
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 0);
fail("Not throw exception");
} catch (Exception e) {
assertThat(e, CoreMatchers.instanceOf(ArithmeticException.class));
assertEquals(e.getMessage(), "/ by zero");
}
}
@Test
public void TestReverseKGroupWith_k_isN() throws Exception {
try {
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), -1);
fail("Not throw exception");
} catch (Exception e) {
assertThat(e, CoreMatchers.instanceOf(ArithmeticException.class));
assertEquals(e.getMessage(), "/ k must greater than 0");
}
}
@Test
public void TestReverseKGroupsWith_k_(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 1);
String actualString = actual.toString(actual);
ListNode expect = ListNode.createTestData("[1]");
String expectString = expect.toString(expect);
assertEquals(expectString, actualString);
}
@Test
public void TestReverseKGroupsWith_k_greaterTh(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 2);
String actualString = actual.toString(actual);
ListNode expect = ListNode.createTestData("[1]");
String expectString = expect.toString(expect);
assertEquals(expectString, actualString);
}
@Test
public void TestReverseKGroupsWith_k_isSmThanLengthOfListNode(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3]"), 2);
String actualString = actual.toString(actual);
ListNode expect = ListNode.createTestData("[2,1,3]");
String expectString = expect.toString(expect);
assertEquals(expectString, actualString);
}
@Test
public void TestReverseKGroupsWith_k_isEqualToLength(){
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3]"), 3);
String actualString = actual.toString(actual);
ListNode expect = ListNode.createTestData("[3,2,1]");
String expectString = expect.toString(expect);
assertEquals(expectString, actualString);
}
// public static void main(String[] args) {
// Solution solution = new Solution();
// ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3,4,5,6,7,8]"), 3);
// String actualString = actual.toString(actual);
// ListNode expect = ListNode.createTestData("[3,2,1,6,5,4,7,8]");
// String expectString = expect.toString(expect);
// assertEquals(expectString, actualString);
// assert
// }
}