Skip to content

Commit 9197183

Browse files
Stack and Queue test pass
1 parent b073bb8 commit 9197183

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public boolean isEmpty(){
2727
public int size(){
2828
return elementData.size();
2929
}
30+
31+
public void clear(){
32+
elementData.clear();
33+
}
3034
}

group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public boolean isEmpty(){
2525
public int size(){
2626
return elementData.size();
2727
}
28+
29+
public void clear(){
30+
elementData.clear();
31+
}
2832
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.github.congcongcong250.coding2017.basicTest;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import com.github.congcongcong250.coding2017.basic.Queue;
12+
13+
public class QueueTest implements testCase {
14+
15+
Queue testqueue = new Queue();
16+
17+
@Override
18+
@Before
19+
public void setUp() {
20+
21+
for(int i = 0; i < 20; i++){
22+
testqueue.enQueue(i);
23+
}
24+
}
25+
26+
@Override
27+
@After
28+
public void tearDown() {
29+
testqueue.clear();
30+
}
31+
32+
33+
@Override
34+
@Test
35+
public void testAdd() {
36+
assertEquals(20,testqueue.size());
37+
assertEquals(0,testqueue.peek());
38+
assertEquals(20,testqueue.size());
39+
assertFalse(testqueue.isEmpty());
40+
}
41+
42+
@Override
43+
@Test
44+
public void testRemove() {
45+
assertEquals(20,testqueue.size());
46+
assertEquals(0,testqueue.deQueue());
47+
assertEquals(19,testqueue.size());
48+
assertEquals(1,testqueue.peek());
49+
assertFalse(testqueue.isEmpty());
50+
}
51+
52+
@Override
53+
@Test
54+
public void testFunctional() {
55+
for(int i = 0; i < 20; i++){
56+
testqueue.deQueue();
57+
}
58+
assertTrue(testqueue.isEmpty());
59+
testqueue.enQueue(100);
60+
testqueue.enQueue(200);
61+
assertEquals(100,testqueue.deQueue());
62+
testqueue.enQueue(400);
63+
assertEquals(200,testqueue.deQueue());
64+
assertFalse(testqueue.isEmpty());
65+
assertEquals(400,testqueue.deQueue());
66+
67+
boolean hasExp = false;
68+
try{
69+
testqueue.deQueue();
70+
}catch (IndexOutOfBoundsException e){
71+
hasExp = true;
72+
}
73+
assertTrue(hasExp);
74+
}
75+
76+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.congcongcong250.coding2017.basicTest;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import com.github.congcongcong250.coding2017.basic.Stack;
10+
11+
public class StackTest implements testCase {
12+
13+
Stack teststack = new Stack();
14+
15+
@Override
16+
@Before
17+
public void setUp() {
18+
19+
for(int i = 0; i < 20; i++){
20+
teststack.push(i);
21+
}
22+
}
23+
24+
@Override
25+
@After
26+
public void tearDown() {
27+
teststack.clear();
28+
}
29+
30+
31+
@Override
32+
@Test
33+
public void testAdd() {
34+
assertEquals(20,teststack.size());
35+
assertEquals(19,teststack.peek());
36+
assertEquals(20,teststack.size());
37+
assertFalse(teststack.isEmpty());
38+
}
39+
40+
@Override
41+
@Test
42+
public void testRemove() {
43+
assertEquals(20,teststack.size());
44+
assertEquals(19,teststack.pop());
45+
assertEquals(19,teststack.size());
46+
assertEquals(18,teststack.peek());
47+
assertFalse(teststack.isEmpty());
48+
}
49+
50+
@Override
51+
@Test
52+
public void testFunctional() {
53+
for(int i = 0; i < 20; i++){
54+
teststack.pop();
55+
}
56+
assertTrue(teststack.isEmpty());
57+
teststack.push(100);
58+
teststack.push(200);
59+
assertEquals(200,teststack.pop());
60+
teststack.push(400);
61+
assertEquals(400,teststack.pop());
62+
assertFalse(teststack.isEmpty());
63+
assertEquals(100,teststack.pop());
64+
65+
66+
boolean hasExp = false;
67+
try{
68+
teststack.pop();
69+
}catch (IndexOutOfBoundsException e){
70+
hasExp = true;
71+
}
72+
assertTrue(hasExp);
73+
}
74+
75+
}

0 commit comments

Comments
 (0)