Skip to content

Commit 149442b

Browse files
committed
add unit test for _025 solution
1 parent 657138b commit 149442b

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.blankj.hard._025;
2+
import com.blankj.structure.ListNode;
3+
import org.hamcrest.CoreMatchers;
4+
import org.junit.jupiter.api.Test;
5+
import java.lang.ArithmeticException;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertThat;
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
public class SolutionTest {
12+
Solution solution = new Solution();
13+
14+
@Test
15+
public void TestReverseKGroupsWithHeadNull_1(){
16+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), -1);
17+
assertNull(actual);
18+
}
19+
20+
21+
@Test
22+
public void TestReverseKGroupsWithHeadNull_2(){
23+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), 0);
24+
assertNull(actual);
25+
}
26+
27+
28+
29+
30+
@Test
31+
public void TestReverseKGroupsWithHeadNull_3(){
32+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[]"), 1);
33+
assertNull(actual);
34+
}
35+
36+
@Test
37+
public void TestReverseKGroupWith_k_isZero() throws Exception {
38+
try {
39+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 0);
40+
fail("Not throw exception");
41+
} catch (Exception e) {
42+
assertThat(e, CoreMatchers.instanceOf(ArithmeticException.class));
43+
assertEquals(e.getMessage(), "/ by zero");
44+
}
45+
}
46+
47+
@Test
48+
public void TestReverseKGroupWith_k_isN() throws Exception {
49+
try {
50+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), -1);
51+
fail("Not throw exception");
52+
} catch (Exception e) {
53+
assertThat(e, CoreMatchers.instanceOf(ArithmeticException.class));
54+
assertEquals(e.getMessage(), "/ k must greater than 0");
55+
}
56+
}
57+
58+
@Test
59+
public void TestReverseKGroupsWith_k_(){
60+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 1);
61+
String actualString = actual.toString(actual);
62+
ListNode expect = ListNode.createTestData("[1]");
63+
String expectString = expect.toString(expect);
64+
assertEquals(expectString, actualString);
65+
}
66+
67+
@Test
68+
public void TestReverseKGroupsWith_k_greaterTh(){
69+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1]"), 2);
70+
String actualString = actual.toString(actual);
71+
ListNode expect = ListNode.createTestData("[1]");
72+
String expectString = expect.toString(expect);
73+
assertEquals(expectString, actualString);
74+
}
75+
76+
@Test
77+
public void TestReverseKGroupsWith_k_isSmThanLengthOfListNode(){
78+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3]"), 2);
79+
String actualString = actual.toString(actual);
80+
ListNode expect = ListNode.createTestData("[2,1,3]");
81+
String expectString = expect.toString(expect);
82+
assertEquals(expectString, actualString);
83+
}
84+
85+
@Test
86+
public void TestReverseKGroupsWith_k_isEqualToLength(){
87+
ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3]"), 3);
88+
String actualString = actual.toString(actual);
89+
ListNode expect = ListNode.createTestData("[3,2,1]");
90+
String expectString = expect.toString(expect);
91+
assertEquals(expectString, actualString);
92+
}
93+
94+
// public static void main(String[] args) {
95+
// Solution solution = new Solution();
96+
// ListNode actual = solution.reverseKGroup(ListNode.createTestData("[1,2,3,4,5,6,7,8]"), 3);
97+
// String actualString = actual.toString(actual);
98+
// ListNode expect = ListNode.createTestData("[3,2,1,6,5,4,7,8]");
99+
// String expectString = expect.toString(expect);
100+
// assertEquals(expectString, actualString);
101+
// assert
102+
// }
103+
}

0 commit comments

Comments
 (0)