Skip to content

Commit 0e784be

Browse files
committed
test code
1 parent 306b41b commit 0e784be

File tree

9 files changed

+323
-14
lines changed

9 files changed

+323
-14
lines changed

group17/240094626/warm-up/src/com/coding/basic/impl/ArrayList.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.coding.basic.impl;
22

3-
import java.util.Arrays;
4-
53
import com.coding.basic.Iterator;
64
import com.coding.basic.List;
75

@@ -13,7 +11,6 @@
1311
*/
1412
public class ArrayList implements List {
1513

16-
1714
/**
1815
* @comment:元素数组
1916
*/
@@ -83,7 +80,7 @@ public void add(Object o) {
8380
@Override
8481
public void add(int index, Object o) {
8582
if( index > size || index < 0){
86-
throw new IndexOutOfBoundsException("Index:"+index);
83+
throw new IndexOutOfBoundsException("Index:"+index+",size:"+size);
8784
}
8885
grow(size+1);
8986
System.arraycopy(data, index, data, index+1, size-index);
@@ -117,8 +114,14 @@ public int size() {
117114

118115
@Override
119116
public String toString() {
120-
return "ArrayList [data=" + Arrays.toString(data) + ", size=" + size
121-
+ "]";
117+
StringBuilder sb = new StringBuilder();
118+
for(int i =0; i<size ;i++){
119+
if(i > 0){
120+
sb.append(",");
121+
}
122+
sb.append(data[i]);
123+
}
124+
return String.format("ArrayList {data=[%s], size=%d}", sb.toString(),size);
122125
}
123126

124127
public Iterator iterator(){

group17/240094626/warm-up/src/com/coding/basic/impl/BinaryTree.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@
77
*
88
*/
99
public class BinaryTree {
10+
/**根结点,初始化为空*/
1011
private Node rootNode = null;
1112

13+
14+
/**
15+
* 根据key值插入数据data为空的新节点
16+
* @param key
17+
* @return
18+
*/
1219
public Node insert(int key){
1320
return insert(key,null);
1421
}
1522

23+
/**
24+
* 根据key值插入数据data为o的新节点
25+
* @param key
26+
* @param o
27+
* @return
28+
*/
1629
public Node insert(int key ,Object o){
1730
Node newNode = new Node(key, o);
1831
if(rootNode == null){
@@ -34,13 +47,25 @@ public Node insert(int key ,Object o){
3447
}else{
3548
fatherNode.right = newNode;
3649
}
50+
size++;
3751
return newNode;
3852
}
3953

54+
/**
55+
* 根据key值查找结点
56+
* @param key
57+
* @return
58+
*/
4059
public Node getNode(int key){
4160
return get(rootNode, key);
4261
}
4362

63+
/**
64+
* 递归算法: 根据开始结点位置和key值查找节点
65+
* @param n
66+
* @param key
67+
* @return
68+
*/
4469
private Node get(Node n,int key){
4570
if(n == null){
4671
return null;
@@ -50,11 +75,13 @@ private Node get(Node n,int key){
5075
}else if(key > n.key){
5176
return get(n.left, key);
5277
}
53-
return null;
78+
return n;
5479
}
5580

56-
57-
81+
82+
83+
84+
5885

5986
private static class Node{
6087

@@ -77,6 +104,7 @@ public String toString() {
77104

78105

79106
}
107+
80108

81109

82110
}

group17/240094626/warm-up/src/com/coding/basic/impl/LinkedList.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ private Node getNode(int index) {
4949
}
5050
// 查找从header开始
5151
Node n = header;
52-
if(index < (size >> 1)){
52+
if(index <= (size >> 1)){
5353
// 往next方向找第index个节点
54-
for(int i=0; i < index; i++){
54+
for(int i=0; i <=index; i++){
5555
n = n.next;
5656
}
5757
}else{
5858
// 往pre方向找第size-index个节点
59-
for(int i=size-index; i > 0; i--){
59+
for(int i=size-index; i >0; i--){
6060
n = n.pre;
6161
}
6262
}
@@ -76,7 +76,6 @@ private Object remove(Node n){
7676
Object result = n.data;
7777
n.pre.next = n.next;
7878
n.next.pre = n.pre;
79-
n.next = n.pre = null;
8079
n.data = null;
8180
size--;
8281
return result;
@@ -137,6 +136,21 @@ public Iterator iterator(){
137136
return new LinkedListIterator();
138137
}
139138

139+
140+
@Override
141+
public String toString() {
142+
Iterator it = iterator();
143+
StringBuilder sb = new StringBuilder();
144+
while(it.hasNext()){
145+
if(sb.length() > 0){
146+
sb.append(",");
147+
}
148+
sb.append(it.next());
149+
}
150+
return "LinkedList {nodes=[" + sb + "], size=" + size + "}";
151+
}
152+
153+
140154
private static class Node{
141155
Object data;
142156
Node pre;
@@ -154,6 +168,13 @@ public Node(Object data, Node pre, Node next) {
154168
this.pre = pre;
155169
this.next = next;
156170
}
171+
172+
@Override
173+
public String toString() {
174+
return "Node {data=" + data + "}";
175+
}
176+
177+
157178

158179
}
159180

group17/240094626/warm-up/src/com/coding/basic/impl/Queue.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public int size(){
3838
return elementData.size();
3939
}
4040

41+
42+
@Override
43+
public String toString() {
44+
return "Queue {elementData=" + elementData + "}";
45+
}
46+
4147
public Iterator iterator(){
4248
return new QueueIterator();
4349
}

group17/240094626/warm-up/src/com/coding/basic/impl/Stack.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,17 @@ public int size(){
5050
return elementData.size();
5151
}
5252

53+
54+
55+
@Override
56+
public String toString() {
57+
return "Stack {elementData=" + elementData + "}";
58+
}
59+
5360
public Iterator iterator(){
5461
return new StackIterator();
5562
}
63+
5664
private class StackIterator implements Iterator{
5765
int index;
5866

@@ -70,7 +78,7 @@ public boolean hasNext() {
7078

7179
@Override
7280
public Object next() {
73-
return elementData.get(index);
81+
return elementData.get(index++);
7482
}
7583

7684

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.coding.basic.test;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.JUnitCore;
7+
import org.junit.runner.Result;
8+
import org.junit.runner.notification.Failure;
9+
10+
import com.coding.basic.impl.ArrayList;
11+
12+
/**
13+
* ArrayList 简单测试
14+
* @author 240094626
15+
*
16+
*/
17+
public class ArrayListTest {
18+
19+
20+
@Test
21+
public void test() {
22+
ArrayList list = new ArrayList();
23+
24+
System.out.println("******测试add(Object o ):添加第一个元素0******");
25+
list.add(0);
26+
System.out.println("ArrayList print:"+list.toString());
27+
28+
System.out.println("******测试add(int index,Object o):添加第二个元素1******");
29+
list.add(1, 1);
30+
System.out.println("ArrayList print:"+list.toString());
31+
32+
System.out.println("******测试remove(int index):删除第1个元素:0******");
33+
list.remove(0);
34+
System.out.println("ArrayList print:"+list.toString());
35+
36+
System.out.println("******测试add(int Object o):添加第三个元素2******");
37+
list.add(2);
38+
System.out.println("ArrayList print:"+list.toString());
39+
40+
41+
System.out.println("ArrayList size:"+list.size());
42+
43+
System.out.println("******测试get(int index):判断第1个元素是否为1******");
44+
assertEquals(1, list.get(0));
45+
}
46+
47+
public static void main(String[] args) {
48+
Result result = JUnitCore.runClasses(ArrayListTest.class);
49+
for(Failure failure : result.getFailures()){
50+
System.out.println(failure.toString());
51+
}
52+
System.out.println("test success!:"+result.wasSuccessful());
53+
}
54+
55+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.coding.basic.test;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.JUnitCore;
7+
import org.junit.runner.Result;
8+
import org.junit.runner.notification.Failure;
9+
10+
import com.coding.basic.impl.LinkedList;
11+
12+
/**
13+
* LinkedList 简单测试
14+
* @author 240094626
15+
*
16+
*/
17+
public class LinkedListTest {
18+
19+
@Test
20+
public void test() {
21+
LinkedList list = new LinkedList();
22+
23+
System.out.println("******测试add(Object o ):添加第一个元素0******");
24+
list.add(0);
25+
System.out.println("LinkedList print:"+list.toString());
26+
System.out.println("******测试add(int index,Object o):添加第二个元素1******");
27+
list.add(1);
28+
System.out.println("******测试addLast(Object o):往链表最后添加元素3******");
29+
list.addLast(3);
30+
System.out.println("******测试addFirst(Object o):往链表最前面添加元素5******");
31+
list.addFirst(5);
32+
System.out.println("LinkedList print:"+list.toString());
33+
34+
System.out.println("******测试remove(int index):删除第4个元素:index=3******");
35+
list.remove(3);
36+
System.out.println("LinkedList print:"+list.toString());
37+
38+
System.out.println("******测试addFirst(int Object o):链表最前面添加素2******");
39+
list.addFirst(2);
40+
System.out.println("LinkedList print:"+list.toString());
41+
42+
// 断言第一个元素为0
43+
assertEquals(2, list.get(0));
44+
45+
list.addLast(3);
46+
list.addFirst(5);
47+
System.out.println("LinkedList print:"+list.toString());
48+
// 断言最后一个元素为3
49+
assertEquals(3,list.get(list.size()-1));
50+
}
51+
52+
public static void main(String[] args) {
53+
Result result = JUnitCore.runClasses(LinkedListTest.class);
54+
for(Failure failure : result.getFailures()){
55+
System.out.println(failure.toString());
56+
}
57+
System.out.println("test success!:"+result.wasSuccessful());
58+
}
59+
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.coding.basic.test;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import org.junit.runner.JUnitCore;
7+
import org.junit.runner.Result;
8+
import org.junit.runner.notification.Failure;
9+
10+
import com.coding.basic.impl.Queue;
11+
12+
/**
13+
* Queue 简单测试
14+
* @author 240094626
15+
*
16+
*/
17+
public class QueueTest {
18+
19+
@Test
20+
public void test() {
21+
Queue queue = new Queue();
22+
23+
System.out.println("******测试enQueue(Object o):入队列元素a,b,c******");
24+
queue.enQueue("a");
25+
queue.enQueue("b");
26+
queue.enQueue("c");
27+
System.out.println("queue:"+queue.toString());
28+
29+
// 断言队列不为空
30+
assertEquals(false,queue.isEmpty());
31+
32+
// 断言出队列是a
33+
System.out.println("******测试deQueue(Object o):出队列元素a******");
34+
assertEquals("a",queue.deQueue());
35+
System.out.println("queue:"+queue.toString());
36+
37+
// 断言出队列是b
38+
System.out.println("******测试deQueue(Object o):出队列元素b******");
39+
assertEquals("b",queue.deQueue());
40+
System.out.println("queue:"+queue.toString());
41+
42+
// 断言出队列是c
43+
assertEquals("c",queue.deQueue());
44+
45+
46+
47+
48+
}
49+
50+
public static void main(String[] args) {
51+
Result result = JUnitCore.runClasses(QueueTest.class);
52+
for(Failure failure : result.getFailures()){
53+
System.out.println(failure.toString());
54+
}
55+
System.out.println("test success!:"+result.wasSuccessful());
56+
}
57+
}

0 commit comments

Comments
 (0)