Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit149442b

Browse files
committed
add unit test for _025 solution
1 parent657138b commit149442b

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
packagecom.blankj.hard._025;
2+
importcom.blankj.structure.ListNode;
3+
importorg.hamcrest.CoreMatchers;
4+
importorg.junit.jupiter.api.Test;
5+
importjava.lang.ArithmeticException;
6+
importjava.util.List;
7+
8+
importstaticorg.junit.Assert.assertThat;
9+
importstaticorg.junit.jupiter.api.Assertions.*;
10+
11+
publicclassSolutionTest {
12+
Solutionsolution =newSolution();
13+
14+
@Test
15+
publicvoidTestReverseKGroupsWithHeadNull_1(){
16+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[]"), -1);
17+
assertNull(actual);
18+
}
19+
20+
21+
@Test
22+
publicvoidTestReverseKGroupsWithHeadNull_2(){
23+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[]"),0);
24+
assertNull(actual);
25+
}
26+
27+
28+
29+
30+
@Test
31+
publicvoidTestReverseKGroupsWithHeadNull_3(){
32+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[]"),1);
33+
assertNull(actual);
34+
}
35+
36+
@Test
37+
publicvoidTestReverseKGroupWith_k_isZero()throwsException {
38+
try {
39+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1]"),0);
40+
fail("Not throw exception");
41+
}catch (Exceptione) {
42+
assertThat(e,CoreMatchers.instanceOf(ArithmeticException.class));
43+
assertEquals(e.getMessage(),"/ by zero");
44+
}
45+
}
46+
47+
@Test
48+
publicvoidTestReverseKGroupWith_k_isN()throwsException {
49+
try {
50+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1]"), -1);
51+
fail("Not throw exception");
52+
}catch (Exceptione) {
53+
assertThat(e,CoreMatchers.instanceOf(ArithmeticException.class));
54+
assertEquals(e.getMessage(),"/ k must greater than 0");
55+
}
56+
}
57+
58+
@Test
59+
publicvoidTestReverseKGroupsWith_k_(){
60+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1]"),1);
61+
StringactualString =actual.toString(actual);
62+
ListNodeexpect =ListNode.createTestData("[1]");
63+
StringexpectString =expect.toString(expect);
64+
assertEquals(expectString,actualString);
65+
}
66+
67+
@Test
68+
publicvoidTestReverseKGroupsWith_k_greaterTh(){
69+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1]"),2);
70+
StringactualString =actual.toString(actual);
71+
ListNodeexpect =ListNode.createTestData("[1]");
72+
StringexpectString =expect.toString(expect);
73+
assertEquals(expectString,actualString);
74+
}
75+
76+
@Test
77+
publicvoidTestReverseKGroupsWith_k_isSmThanLengthOfListNode(){
78+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1,2,3]"),2);
79+
StringactualString =actual.toString(actual);
80+
ListNodeexpect =ListNode.createTestData("[2,1,3]");
81+
StringexpectString =expect.toString(expect);
82+
assertEquals(expectString,actualString);
83+
}
84+
85+
@Test
86+
publicvoidTestReverseKGroupsWith_k_isEqualToLength(){
87+
ListNodeactual =solution.reverseKGroup(ListNode.createTestData("[1,2,3]"),3);
88+
StringactualString =actual.toString(actual);
89+
ListNodeexpect =ListNode.createTestData("[3,2,1]");
90+
StringexpectString =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)

[8]ページ先頭

©2009-2025 Movatter.jp