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

Commit57d69f8

Browse files
committed
using MCC standar for structure tesing to Solution028
1 parentb222c9a commit57d69f8

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

‎ListNode.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
packagecom.blankj.structure;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/05/18
8+
* desc :
9+
* </pre>
10+
*/
11+
publicclassListNode {
12+
13+
publicintval;
14+
publicListNodenext;
15+
16+
publicListNode(intx) {
17+
val =x;
18+
}
19+
20+
/**
21+
* 创建测试数据
22+
*
23+
* @param data [XX,XX,XX]
24+
* @return {@link ListNode}
25+
*/
26+
publicstaticListNodecreateTestData(Stringdata) {
27+
if (data.equals("[]"))returnnull;
28+
data =data.substring(1,data.length() -1);
29+
String[]split =data.split(",");
30+
intlen =split.length;
31+
ListNode[]listNode =newListNode[len +1];
32+
listNode[0] =newListNode(Integer.valueOf(split[0]));
33+
for (inti =1;i <len;i++) {
34+
listNode[i] =newListNode(Integer.valueOf(split[i]));
35+
listNode[i -1].next =listNode[i];
36+
}
37+
returnlistNode[0];
38+
}
39+
40+
publicstaticvoidprint(ListNodelistNode) {
41+
if (listNode ==null) {
42+
System.out.println("null");
43+
return;
44+
}
45+
StringBuilderstr =newStringBuilder("[" +String.valueOf(listNode.val));
46+
ListNodep =listNode.next;
47+
while (p !=null) {
48+
str.append(",").append(String.valueOf(p.val));
49+
p =p.next;
50+
}
51+
System.out.println(str.append("]"));
52+
}
53+
54+
publicStringtoString(ListNodelistNode) {
55+
if (listNode ==null) {
56+
System.out.println("null");
57+
return"";
58+
}
59+
StringBuilderstr =newStringBuilder("[" +String.valueOf(listNode.val));
60+
ListNodep =listNode.next;
61+
while (p !=null) {
62+
str.append(",").append(String.valueOf(p.val));
63+
p =p.next;
64+
}
65+
str.append("]");
66+
returnstr.toString();
67+
}
68+
}

‎SolutionTest.java

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+
}

‎SolutionTest028.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//test use MCC cover
2+
3+
packagecom.blankj.easy._028;
4+
importorg.junit.jupiter.api.Test;
5+
6+
importstaticorg.junit.jupiter.api.Assertions.*;
7+
8+
publicclassSolutionTest {
9+
Solutionsolution =newSolution();
10+
@Test
11+
publicvoidTestImplementStrWithNeedleIsPartOfHayStack(){
12+
Integeractual =solution.strStr("1,2,3,4","1,2");
13+
Integerexpected =0;
14+
assertEquals(actual,expected);
15+
}
16+
17+
@Test
18+
publicvoidTestImplementStrWithNeedleIsNotPartOfHayStack1(){
19+
Integeractual =solution.strStr("1,2,3,4","2,1");
20+
Integerexpected = -1;
21+
assertEquals(actual,expected);
22+
}
23+
24+
@Test
25+
publicvoidTestImplementStrWithNeedleIsNone(){
26+
Integeractual =solution.strStr("1,2,3,4","");
27+
Integerexpected =0;
28+
assertEquals(actual,expected);
29+
}
30+
31+
@Test
32+
publicvoidTestImplementStrWithLenNeedleGreaterThanLenHayStack(){
33+
Integeractual =solution.strStr("1,2,3,4","1,2,3,4,5");
34+
Integerexpected = -1;
35+
assertEquals(actual,expected);
36+
}
37+
38+
@Test
39+
publicvoidTestImplementStrWithLenNeedleEqualToLenHayStack(){
40+
Integeractual =solution.strStr("1,2,3,4","1,2,3,4");
41+
Integerexpected =0;
42+
assertEquals(actual,expected);
43+
}
44+
45+
@Test
46+
publicvoidTestImplementStrWithNeedleIsNotPartOfHayStack2(){
47+
Integeractual =solution.strStr("1,2,3,4","3,4,5");
48+
Integerexpected = -1;
49+
assertEquals(actual,expected);
50+
}
51+
52+
}

‎TestRunner.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
importcom.blankj.hard._025.Solution;
2+
importcom.blankj.hard._025.SolutionTest;
3+
importcom.blankj.structure.ListNode;
4+
importorg.junit.jupiter.api.Test;
5+
importorg.junit.runner.JUnitCore;
6+
importorg.junit.runner.Result;
7+
importorg.junit.runner.notification.Failure;
8+
9+
publicclassTestRunner {
10+
publicstaticvoidmain(String[]args) {
11+
Resultresult =JUnitCore.runClasses(SolutionTest.class);
12+
13+
for (Failurefailure :result.getFailures()) {
14+
System.out.println(failure.toString());
15+
}
16+
17+
System.out.println(result.wasSuccessful());
18+
}
19+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp