Skip to content

Commit 57d69f8

Browse files
committed
using MCC standar for structure tesing to Solution028
1 parent b222c9a commit 57d69f8

File tree

4 files changed

+242
-0
lines changed

4 files changed

+242
-0
lines changed

ListNode.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.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+
public class ListNode {
12+
13+
public int val;
14+
public ListNode next;
15+
16+
public ListNode(int x) {
17+
val = x;
18+
}
19+
20+
/**
21+
* 创建测试数据
22+
*
23+
* @param data [XX,XX,XX]
24+
* @return {@link ListNode}
25+
*/
26+
public static ListNode createTestData(String data) {
27+
if (data.equals("[]")) return null;
28+
data = data.substring(1, data.length() - 1);
29+
String[] split = data.split(",");
30+
int len = split.length;
31+
ListNode[] listNode = new ListNode[len + 1];
32+
listNode[0] = new ListNode(Integer.valueOf(split[0]));
33+
for (int i = 1; i < len; i++) {
34+
listNode[i] = new ListNode(Integer.valueOf(split[i]));
35+
listNode[i - 1].next = listNode[i];
36+
}
37+
return listNode[0];
38+
}
39+
40+
public static void print(ListNode listNode) {
41+
if (listNode == null) {
42+
System.out.println("null");
43+
return;
44+
}
45+
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
46+
ListNode p = 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+
public String toString(ListNode listNode) {
55+
if (listNode == null) {
56+
System.out.println("null");
57+
return "";
58+
}
59+
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
60+
ListNode p = listNode.next;
61+
while (p != null) {
62+
str.append(",").append(String.valueOf(p.val));
63+
p = p.next;
64+
}
65+
str.append("]");
66+
return str.toString();
67+
}
68+
}

SolutionTest.java

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

SolutionTest028.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//test use MCC cover
2+
3+
package com.blankj.easy._028;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
public class SolutionTest {
9+
Solution solution = new Solution();
10+
@Test
11+
public void TestImplementStrWithNeedleIsPartOfHayStack(){
12+
Integer actual = solution.strStr("1,2,3,4", "1,2");
13+
Integer expected = 0;
14+
assertEquals(actual, expected);
15+
}
16+
17+
@Test
18+
public void TestImplementStrWithNeedleIsNotPartOfHayStack1(){
19+
Integer actual = solution.strStr("1,2,3,4", "2,1");
20+
Integer expected = -1;
21+
assertEquals(actual, expected);
22+
}
23+
24+
@Test
25+
public void TestImplementStrWithNeedleIsNone(){
26+
Integer actual = solution.strStr("1,2,3,4", "");
27+
Integer expected = 0;
28+
assertEquals(actual, expected);
29+
}
30+
31+
@Test
32+
public void TestImplementStrWithLenNeedleGreaterThanLenHayStack(){
33+
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4,5");
34+
Integer expected = -1;
35+
assertEquals(actual, expected);
36+
}
37+
38+
@Test
39+
public void TestImplementStrWithLenNeedleEqualToLenHayStack(){
40+
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4");
41+
Integer expected = 0;
42+
assertEquals(actual, expected);
43+
}
44+
45+
@Test
46+
public void TestImplementStrWithNeedleIsNotPartOfHayStack2(){
47+
Integer actual = solution.strStr("1,2,3,4", "3,4,5");
48+
Integer expected = -1;
49+
assertEquals(actual, expected);
50+
}
51+
52+
}

TestRunner.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import com.blankj.hard._025.Solution;
2+
import com.blankj.hard._025.SolutionTest;
3+
import com.blankj.structure.ListNode;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.runner.JUnitCore;
6+
import org.junit.runner.Result;
7+
import org.junit.runner.notification.Failure;
8+
9+
public class TestRunner {
10+
public static void main(String[] args) {
11+
Result result = JUnitCore.runClasses(SolutionTest.class);
12+
13+
for (Failure failure : result.getFailures()) {
14+
System.out.println(failure.toString());
15+
}
16+
17+
System.out.println(result.wasSuccessful());
18+
}
19+
}

0 commit comments

Comments
 (0)