Skip to content

Commit

Permalink
week5: testException
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 18, 2019
1 parent 6c7c018 commit 45ea5a1
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 9 deletions.
4 changes: 2 additions & 2 deletions junit/beforeAfter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ all: compile

compile:
mkdir -p bin
javac testcase/TestBeforeAfter.java testcase/TestRunner.java -d bin
javac -classpath /usr/share/java/junit4.jar testcase/TestBeforeAfter.java testcase/TestRunner.java -d bin

test:
cd bin ; java TestRunner
cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner

clean:
rm bin/*.class
2 changes: 1 addition & 1 deletion junit/junit_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

sudo apt update
sudo apt install openjdk-8-jdk-headless junit4 make
export CLASSPATH=/usr/share/java/junit4.jar:.
#export CLASSPATH=/usr/share/java/junit4.jar:.
4 changes: 2 additions & 2 deletions junit/onlyTest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ all: compile

compile:
mkdir -p bin
javac src/TestJunit.java src/TestRunner.java -d bin
javac -classpath /usr/share/java/junit4.jar src/TestJunit.java src/TestRunner.java -d bin

test:
cd bin ; java TestRunner
cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner

clean:
rm bin/*.class
4 changes: 2 additions & 2 deletions junit/stack/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ compile:

compileTest:
mkdir -p bin
javac src/Stack.java testcase/TestStack.java testcase/TestRunner.java -d bin
javac -classpath /usr/share/java/junit4.jar src/Stack.java testcase/TestStack.java testcase/TestRunner.java -d bin

test:
cd bin ; java TestRunner
cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner

clean:
rm bin/*.class
4 changes: 2 additions & 2 deletions junit/testCode/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ compile:

compileTest:
mkdir -p bin
javac src/Distant.java testcase/TestDistant.java testcase/TestRunner.java -d bin
javac -classpath /usr/share/java/junit4.jar src/Distant.java testcase/TestDistant.java testcase/TestRunner.java -d bin

test:
cd bin ; java TestRunner
cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner

clean:
rm bin/*.class
17 changes: 17 additions & 0 deletions junit/testException/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
all: compile compileTest

compile:
mkdir -p bin
javac src/Stack.java src/EmptyStackException.java src/FullStackException.java -d bin

compileTest:
mkdir -p bin
javac -classpath /usr/share/java/junit4.jar src/Stack.java \
src/EmptyStackException.java src/FullStackException.java \
testcase/TestException.java testcase/TestRunner.java -d bin

test:
cd bin ; java -classpath .:/usr/share/java/junit4.jar TestRunner

clean:
rm bin/*.class
5 changes: 5 additions & 0 deletions junit/testException/src/EmptyStackException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class EmptyStackException extends Exception{
public EmptyStackException (String msg){
super (msg);
}
}
5 changes: 5 additions & 0 deletions junit/testException/src/FullStackException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public class FullStackException extends Exception{
public FullStackException (String msg){
super (msg);
}
}
45 changes: 45 additions & 0 deletions junit/testException/src/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class Stack {
private Node head;
private int size;
private int max_size;

class Node {
Object data;
Node next;
}

public Stack(int max_size) {
head = null;
size = 0;
this.max_size = max_size;
}

public Stack() {
this(10);
}

public int size() {
return this.size;
}

public void push(Object obj) throws FullStackException {
if (size >= max_size) {
throw new FullStackException("Push a full stack");
}
Node tmp = new Node();
tmp.next = head;
tmp.data = obj;
size++;
head = tmp;
}

public Object pop() throws EmptyStackException {
if (head == null) {
throw new EmptyStackException("Pop an empty stack");
}
Object tmp = head.data;
head = head.next;
size--;
return tmp;
}
}
67 changes: 67 additions & 0 deletions junit/testException/testcase/TestException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import org.junit.Test;
import org.junit.*; // before after ...
import static org.junit.Assert.assertEquals;
import org.junit.rules.ExpectedException;

public class TestException {
private static Stack stack;

@BeforeClass
public static void beforeClass() {
stack = new Stack();
}

@Before
public void before() throws Exception {
// initial some node in stack structure
stack.push(3);
stack.push(2);
}

@After
public void after() throws Exception {
// clean all node in stack
while (stack.size() != 0) {
stack.pop();
}
// try to create some exception
//stack.pop();
}

@AfterClass
public static void afterClass() {
stack = null;
}

@Test
public void CaseInt() throws Exception {
int value = 0xc8763;
stack.push(value);
assertEquals(value, stack.pop());
}

@Test
public void CaseMagic() throws Exception {
String magic = "c8763";
stack.push(magic);
assertEquals(magic, stack.pop());
}

@Test (expected = FullStackException.class)
public void CaseFullException() throws Exception {
// try to overflow
while (true) {
stack.push("overflow");
}
}

@Test (expected = EmptyStackException.class)
public void CaseEmptyException() throws Exception {
// clean all node in stack
while (stack.size() != 0) {
stack.pop();
}
// try to create some exception
stack.pop();
}
}
13 changes: 13 additions & 0 deletions junit/testException/testcase/TestRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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(TestException.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
48 changes: 48 additions & 0 deletions junit/testException/testcase/TestStack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import org.junit.Test;
import org.junit.*; // before after ...
import static org.junit.Assert.assertEquals;

public class TestStack {
private static Stack stack;

@BeforeClass
public static void beforeClass() {
stack = new Stack();
}

@Before
public void before() {
// initial some node in stack structure
stack.push(3);
stack.push(2);
}

@After
public void after() throws Exception {
// clean all node in stack
while (stack.size() != 0) {
stack.pop();
}
// try to create some exception
//stack.pop();
}

@AfterClass
public static void afterClass() {
stack = null;
}

@Test
public void CaseInt() throws Exception {
int value = 0xc8763;
stack.push(value);
assertEquals(value, stack.pop());
}

@Test
public void CaseMagic() throws Exception {
String magic = "c8763";
stack.push(magic);
assertEquals(magic, stack.pop());
}
}

0 comments on commit 45ea5a1

Please sign in to comment.