Skip to content

Commit 3d5e9d6

Browse files
committed
add JUnit
1 parent 8b50476 commit 3d5e9d6

File tree

9 files changed

+256
-26
lines changed

9 files changed

+256
-26
lines changed

group07/476770768/MyDataStructure/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
56
<classpathentry kind="output" path="bin"/>
67
</classpath>

group07/476770768/MyDataStructure/src/com/coding/basic/MyArrayList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean isFull(){
8989
}
9090

9191
public void checkBounds(int index){
92-
if(index >= size || index < 0){
92+
if(index > size || index < 0){
9393
//System.out.println("From MyArrayList: Index out of bounds");
9494
throw new IndexOutOfBoundsException(OutOfBoundsMsg(index));
9595
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.coding.basic;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class MyArrayListTest {
8+
9+
10+
@Test
11+
public void testAddObject() {
12+
MyArrayList mal = new MyArrayList();
13+
assertEquals(0, mal.size());
14+
mal.add(new Integer(1));
15+
assertEquals(1, mal.size());
16+
}
17+
18+
@Test
19+
public void testAddIntObject() {
20+
MyArrayList mal = new MyArrayList();
21+
mal.add(0, new Integer(1));
22+
assertEquals(1, mal.size());
23+
int tmp = 0;
24+
try {
25+
mal.add(4, new Integer(4));
26+
} catch (IndexOutOfBoundsException e) {
27+
tmp = 1;
28+
assertEquals(tmp, 1);
29+
}
30+
31+
}
32+
33+
@Test
34+
public void testGet() {
35+
MyArrayList mal = new MyArrayList();
36+
mal.add(new Integer(1));
37+
assertEquals((Integer)mal.get(0),new Integer(1));
38+
int tmp = 0;
39+
try {
40+
mal.get(4);
41+
} catch (IndexOutOfBoundsException e) {
42+
tmp = 1;
43+
assertEquals(tmp, 1);
44+
}
45+
}
46+
47+
@Test
48+
public void testRemove() {
49+
MyArrayList mal = new MyArrayList();
50+
mal.add(new Integer(1));
51+
assertEquals((Integer)mal.get(0),new Integer(1));
52+
assertEquals(mal.size(),1);
53+
}
54+
55+
@Test
56+
public void testSize() {
57+
MyArrayList mal = new MyArrayList();
58+
assertEquals(0, mal.size());
59+
}
60+
61+
@Test
62+
public void testIsEmpty() {
63+
MyArrayList mal = new MyArrayList();
64+
assertTrue(mal.isEmpty());
65+
mal.add(new Integer(1));
66+
assertFalse(mal.isEmpty());
67+
}
68+
69+
}

group07/476770768/MyDataStructure/src/com/coding/basic/MyLinkedList.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ public void add(int index, Object o) {
3838
*/
3939
public void addFirst(Object o) {
4040
Node tmp = new Node(o);
41-
tmp.next = head;
42-
head.prov = tmp;
43-
head = tmp;
41+
if(head == null){
42+
head = tmp;
43+
}else{
44+
tmp.next = head;
45+
head.prov = tmp;
46+
head = tmp;
47+
}
4448
}
4549

4650
/**
@@ -97,7 +101,10 @@ public Object remove(int index) {
97101
public Node removeFirst() {
98102
Node tmp = head;
99103
head = head.next;
100-
head.prov = null;
104+
if(head != null){
105+
head.prov = null;
106+
}
107+
101108
return tmp;
102109
}
103110

@@ -199,22 +206,22 @@ public MyIterator iterator() {
199206
private class LinkedListIterator implements MyIterator{
200207

201208
private MyLinkedList eleIterator;
202-
private Node pos;
209+
private int pos;
203210

204211
private LinkedListIterator(MyLinkedList mll){
205212
this.eleIterator = mll;
206-
this.pos = eleIterator.get(0);
213+
this.pos = 0;
207214
}
208215

209216
@Override
210217
public boolean hasNext() {
211-
return pos != null;
218+
return pos <= size;
212219
}
213220

214221
@Override
215222
public Object next() {
216-
Node res = pos;
217-
pos = pos.next;
223+
Node res = eleIterator.get(pos);
224+
pos++;
218225
return res;
219226
}
220227

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.coding.basic;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class MyLinkedListTest {
8+
9+
@Test
10+
public void testAddObject() {
11+
MyLinkedList mll = new MyLinkedList();
12+
assertEquals(0, mll.size());
13+
mll.add(new Integer(1));
14+
assertEquals(1, mll.size());
15+
}
16+
17+
@Test
18+
public void testAddIntObject() {
19+
MyLinkedList mll = new MyLinkedList();
20+
mll.add(0, new Integer(1));
21+
assertEquals(1, mll.size());
22+
int tmp = 0;
23+
try {
24+
mll.add(4, new Integer(4));
25+
} catch (IndexOutOfBoundsException e) {
26+
tmp = 1;
27+
assertEquals(tmp, 1);
28+
}
29+
}
30+
31+
@Test
32+
public void testGet() {
33+
MyLinkedList mll = new MyLinkedList();
34+
mll.add(new Object());
35+
assertNotNull(mll.get(0));
36+
int tmp = 0;
37+
try {
38+
mll.get(4);
39+
} catch (IndexOutOfBoundsException e) {
40+
tmp = 1;
41+
assertEquals(tmp, 1);
42+
}
43+
}
44+
45+
@Test
46+
public void testRemove() {
47+
MyLinkedList mll = new MyLinkedList();
48+
mll.add(new Object());
49+
mll.remove(0);
50+
assertEquals(mll.size(),0);
51+
}
52+
53+
@Test
54+
public void testSize() {
55+
MyLinkedList mll = new MyLinkedList();
56+
assertEquals(0, mll.size());
57+
}
58+
59+
@Test
60+
public void testIsEmpty() {
61+
MyLinkedList mll = new MyLinkedList();
62+
assertTrue(mll.isEmpty());
63+
mll.add(new Object());
64+
assertFalse(mll.isEmpty());
65+
}
66+
67+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.coding.basic;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class MyQueueTest {
8+
9+
@Test
10+
public void testEnQueue() {
11+
MyQueue mq = new MyQueue();
12+
assertEquals(mq.size(), 0);
13+
mq.enQueue(new Object());
14+
assertEquals(mq.size(), 1);
15+
}
16+
17+
@Test
18+
public void testDeQueue() {
19+
MyQueue mq = new MyQueue();
20+
int tmp = 0;
21+
try {
22+
mq.deQueue();
23+
} catch (IndexOutOfBoundsException e) {
24+
tmp = 1;
25+
assertEquals(tmp, 1);
26+
}
27+
mq.enQueue(new Object());
28+
assertNotNull(mq.deQueue());
29+
}
30+
31+
@Test
32+
public void testIsEmpty() {
33+
MyQueue mq = new MyQueue();
34+
assertTrue(mq.isEmpty());
35+
mq.enQueue(new Object());
36+
assertFalse(mq.isEmpty());
37+
}
38+
39+
@Test
40+
public void testSize() {
41+
MyQueue mq = new MyQueue();
42+
assertEquals(mq.size(), 0);
43+
}
44+
45+
}

group07/476770768/MyDataStructure/src/com/coding/basic/MyStack.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Object peek(){
3131
}
3232

3333
public boolean isEmpty(){
34-
return top >= 0;
34+
return top < 0;
3535
}
3636

3737
public int size(){
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.coding.basic;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.EmptyStackException;
6+
7+
import org.junit.Test;
8+
9+
public class MyStackTest {
10+
11+
@Test
12+
public void testPush() {
13+
MyStack ms = new MyStack();
14+
assertEquals(0, ms.size());
15+
ms.push(new Object());
16+
assertEquals(1, ms.size());
17+
}
18+
19+
@Test
20+
public void testPop() {
21+
MyStack ms = new MyStack();
22+
ms.push(new Object());
23+
assertNotNull(ms.pop());
24+
assertEquals(0, ms.size());
25+
}
26+
27+
@Test
28+
public void testPeek() {
29+
MyStack ms = new MyStack();
30+
int tmp = 0;
31+
try {
32+
ms.peek();
33+
} catch (EmptyStackException e) {
34+
tmp = 1;
35+
assertEquals(1, tmp);
36+
}
37+
ms.push(new Object());
38+
assertNotNull(ms.peek());
39+
assertEquals(1, ms.size());
40+
}
41+
42+
@Test
43+
public void testIsEmpty() {
44+
MyStack ms = new MyStack();
45+
assertTrue(ms.isEmpty());
46+
ms.push(new Object());
47+
assertFalse(ms.isEmpty());
48+
}
49+
50+
@Test
51+
public void testSize() {
52+
MyStack ms = new MyStack();
53+
assertEquals(0, ms.size());
54+
}
55+
56+
}

group07/476770768/MyDataStructure/src/com/coding/basic/testFile.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,6 @@
33
public class testFile {
44

55
public static void main(String[] args) {
6-
MyLinkedList mll = new MyLinkedList();
7-
mll.add(new Integer(5));
8-
mll.add(new Integer(2));
9-
mll.add(new Integer(3));
10-
mll.add(new Integer(4));
11-
System.out.println(mll);
12-
MyIterator mIt = mll.iterator();
13-
while(mIt.hasNext()){
14-
System.out.println(mIt.next());
15-
}
16-
mll.remove(3);
17-
System.out.println(mll);
18-
19-
20-
216
}
227

238
}

0 commit comments

Comments
 (0)