-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ubuntu
committed
Mar 18, 2019
1 parent
6c7c018
commit 45ea5a1
Showing
12 changed files
with
209 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |