Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using MCC standard for structure testing to Solution 028 #31

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions ListNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.blankj.structure;

/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/05/18
* desc :
* </pre>
*/
public class ListNode {

public int val;
public ListNode next;

public ListNode(int x) {
val = x;
}

/**
* 创建测试数据
*
* @param data [XX,XX,XX]
* @return {@link ListNode}
*/
public static ListNode createTestData(String data) {
if (data.equals("[]")) return null;
data = data.substring(1, data.length() - 1);
String[] split = data.split(",");
int len = split.length;
ListNode[] listNode = new ListNode[len + 1];
listNode[0] = new ListNode(Integer.valueOf(split[0]));
for (int i = 1; i < len; i++) {
listNode[i] = new ListNode(Integer.valueOf(split[i]));
listNode[i - 1].next = listNode[i];
}
return listNode[0];
}

public static void print(ListNode listNode) {
if (listNode == null) {
System.out.println("null");
return;
}
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
ListNode p = listNode.next;
while (p != null) {
str.append(",").append(String.valueOf(p.val));
p = p.next;
}
System.out.println(str.append("]"));
}

public String toString(ListNode listNode) {
if (listNode == null) {
System.out.println("null");
return "";
}
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
ListNode p = listNode.next;
while (p != null) {
str.append(",").append(String.valueOf(p.val));
p = p.next;
}
str.append("]");
return str.toString();
}
}
103 changes: 103 additions & 0 deletions SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
// }
}
52 changes: 52 additions & 0 deletions SolutionTest028.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//test use MCC cover

package com.blankj.easy._028;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class SolutionTest {
Solution solution = new Solution();
@Test
public void TestImplementStrWithNeedleIsPartOfHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2");
Integer expected = 0;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithNeedleIsNotPartOfHayStack1(){
Integer actual = solution.strStr("1,2,3,4", "2,1");
Integer expected = -1;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithNeedleIsNone(){
Integer actual = solution.strStr("1,2,3,4", "");
Integer expected = 0;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithLenNeedleGreaterThanLenHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4,5");
Integer expected = -1;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithLenNeedleEqualToLenHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4");
Integer expected = 0;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithNeedleIsNotPartOfHayStack2(){
Integer actual = solution.strStr("1,2,3,4", "3,4,5");
Integer expected = -1;
assertEquals(actual, expected);
}

}
19 changes: 19 additions & 0 deletions TestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import com.blankj.hard._025.Solution;
import com.blankj.hard._025.SolutionTest;
import com.blankj.structure.ListNode;
import org.junit.jupiter.api.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(SolutionTest.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}

System.out.println(result.wasSuccessful());
}
}
43 changes: 43 additions & 0 deletions UnitTest028
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.blankj.easy._028;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class SolutionTest {
Solution solution = new Solution();
@Test
public void TestImplementStrWithNeedleIsPartOfHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2");
Integer expected = 0;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithNeedleIsNotPartOfHayStack(){
Integer actual = solution.strStr("1,2,3,4", "2,1");
Integer expected = -1;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithNeedleIsNone(){
Integer actual = solution.strStr("1,2,3,4", "");
Integer expected = 0;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithLenNeedleGreaterThanLenHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4,5");
Integer expected = -1;
assertEquals(actual, expected);
}

@Test
public void TestImplementStrWithLenNeedleEqualToLenHayStack(){
Integer actual = solution.strStr("1,2,3,4", "1,2,3,4");
Integer expected = 0;
assertEquals(actual, expected);
}

}
15 changes: 15 additions & 0 deletions src/com/blankj/structure/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ public static void print(ListNode listNode) {
}
System.out.println(str.append("]"));
}

public String toString(ListNode listNode) {
if (listNode == null) {
System.out.println("null");
return "";
}
StringBuilder str = new StringBuilder("[" + String.valueOf(listNode.val));
ListNode p = listNode.next;
while (p != null) {
str.append(",").append(String.valueOf(p.val));
p = p.next;
}
str.append("]");
return str.toString();
}
}
Loading