Skip to content

Commit 71b3352

Browse files
authored
Merge pull request onlyliuxin#3 from eloiseSJTU/master
add Test junit
2 parents 3648573 + 075a0bd commit 71b3352

File tree

18 files changed

+1047
-0
lines changed

18 files changed

+1047
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/18.
5+
*/
6+
public class MyArrayList<T> {
7+
8+
private Object[] initialArray = {};
9+
private Object[] dataArray;
10+
private int initSize = 10;
11+
private int arraySize;
12+
public MyArrayList() {
13+
dataArray = initialArray;
14+
}
15+
16+
public MyArrayList(int init) {
17+
dataArray = new Object[init];
18+
}
19+
20+
public void ensureCapacity(int newCapacity) {
21+
if (newCapacity < arraySize)
22+
return;
23+
24+
Object[] old = dataArray;
25+
dataArray = new Object[newCapacity];
26+
for (int i = 0; i < size(); i++) {
27+
dataArray[i] = old[i];
28+
}
29+
}
30+
31+
public void add(T element) {
32+
add(size(), element);
33+
}
34+
35+
public void add(int index, T element) {
36+
if (size() == dataArray.length) {
37+
ensureCapacity(size()*2 + 1);
38+
}
39+
for(int i=arraySize;i>index;i--) {
40+
dataArray[i] = dataArray[i - 1];
41+
}
42+
dataArray[index] = element;
43+
arraySize++;
44+
}
45+
46+
public T delete(int index) {
47+
if (index < 0 || index > arraySize) {
48+
throw new ArrayIndexOutOfBoundsException();
49+
}
50+
T removeElement = (T)dataArray[index];
51+
for (int i = index; i < size() -1; i++) {
52+
dataArray[i] = dataArray[i + 1];
53+
}
54+
arraySize--;
55+
return removeElement;
56+
}
57+
58+
public T get(int index) {
59+
if (index < 0 || index > arraySize) {
60+
throw new ArrayIndexOutOfBoundsException();
61+
}
62+
return (T)dataArray[index];
63+
}
64+
65+
public T set(int index, T newElement) {
66+
if (index < 0 || index > arraySize) {
67+
throw new ArrayIndexOutOfBoundsException();
68+
}
69+
T oldElement = (T) dataArray[index];
70+
dataArray[index] = newElement;
71+
72+
return oldElement;
73+
}
74+
75+
public int size() {
76+
return arraySize;
77+
}
78+
79+
public boolean isEmpty() {
80+
return size() == 0;
81+
}
82+
83+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/18.
5+
*/
6+
public class MyLinkedList<T> {
7+
private class Node<T> {
8+
public Node<T> pre;
9+
public Node<T> next;
10+
public T data;
11+
12+
public Node(Node<T> pre,Node<T> next,T data) {
13+
this.pre = pre;
14+
this.next = next;
15+
this.data = data;
16+
}
17+
}
18+
19+
private int dataSize;
20+
21+
private Node head;
22+
private Node tail;
23+
24+
public MyLinkedList() {
25+
head = new Node<T>(null,null,null);
26+
tail = new Node(head, null, null);
27+
head.next = tail;
28+
dataSize = 0;
29+
}
30+
31+
public void add(T t) {
32+
// add(size(), t);
33+
Node<T> newNode = new Node<>(null, tail, t);
34+
newNode.pre = tail.pre;
35+
tail.pre.next = newNode;
36+
tail.pre = newNode;
37+
dataSize++;
38+
39+
}
40+
41+
/**
42+
* 根据索引添加没有实现
43+
* @param index
44+
* @param element
45+
*/
46+
public void add(int index,T element) {
47+
//TODO 根据索引添加元素
48+
// addBefore(getNode(index,0,size()-1),element);
49+
// if (index == dataSize) {
50+
// add(element);
51+
// } else {
52+
//
53+
// }
54+
}
55+
56+
public T get(int index) {
57+
return getNode(index).data;
58+
}
59+
60+
public T set(int index, T newValue) {
61+
Node<T> node = getNode(index);
62+
T oldData = node.data;
63+
node.data = newValue;
64+
return oldData;
65+
}
66+
67+
public T remove(int index) {
68+
Node<T> node = getNode(index);
69+
node.next.pre = node.pre;
70+
node.pre.next = node.next;
71+
dataSize--;
72+
73+
return node.data;
74+
75+
}
76+
77+
private void addBefore(Node<T> node, T element) {
78+
// newNode.pre.next = newNode;
79+
// node.pre = newNode;
80+
Node<T> pre = node.pre;
81+
Node<T> newNode = new Node<>(node.pre, node, element);
82+
node.pre = newNode;
83+
pre.next = newNode;
84+
85+
dataSize++;
86+
}
87+
88+
private Node<T> getNode(int index) {
89+
return getNode(index, 0, size());
90+
}
91+
92+
private Node<T> getNode(int index, int lower, int upper) {
93+
Node<T> p;
94+
if (index < lower || index > upper) {
95+
throw new IndexOutOfBoundsException();
96+
}
97+
98+
if (index < size() / 2) {
99+
p = head.next;
100+
for (int i = 0; i < index; i++) {
101+
p = p.next;
102+
}
103+
} else {
104+
p = tail.pre;
105+
for (int i = size()-1; i > index; i--) {
106+
p = p.pre;
107+
}
108+
}
109+
return p;
110+
}
111+
112+
public int size() {
113+
return dataSize;
114+
}
115+
116+
public boolean isEmpty() {
117+
return size() == 0;
118+
}
119+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/22.
5+
*/
6+
public class MyQueue<T> {
7+
private MyLinkedList<T> link = new MyLinkedList<>();
8+
9+
public void enQueue(T t) {
10+
link.add(t);
11+
}
12+
13+
public T deQueue() {
14+
if (size() <= 0) {
15+
return null;
16+
}
17+
T t = link.get(0);
18+
link.remove(0);
19+
return t;
20+
}
21+
22+
public boolean isEmpty() {
23+
return size() == 0;
24+
}
25+
26+
public int size() {
27+
return link.size();
28+
}
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
/**
4+
* Created by andy on 2017/2/22.
5+
*/
6+
public class MyStack<T> {
7+
private MyArrayList<T> list = new MyArrayList<>();
8+
9+
public void push(T t) {
10+
list.add(t);
11+
}
12+
13+
public T pop() {
14+
if (size() <= 0) {
15+
throw new IndexOutOfBoundsException();
16+
}
17+
return list.delete(size() - 1);
18+
}
19+
20+
public T peek() {
21+
return list.get(size() - 1);
22+
}
23+
24+
public boolean isEmpty() {
25+
return list.size() == 0;
26+
}
27+
public int size() {
28+
return list.size();
29+
}
30+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.github.lhpmatlab.coding2017.basic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
public class MyArrayListTest {
9+
private MyArrayList<String> list;
10+
11+
@Before
12+
public void init(){
13+
list = new MyArrayList<>();
14+
}
15+
16+
@Test
17+
public void testEnsureCapacity() {
18+
assertEquals("init list size is 0 ", list.size(), 0);
19+
list.add("1");
20+
list.ensureCapacity(10);
21+
assertEquals("ensureCapacity size is 10 ", list.size(),1);
22+
}
23+
24+
/**
25+
* 在列表的末尾添加元素
26+
*/
27+
@Test
28+
public void testAddT() {
29+
assertEquals("init list size is 0 ", list.size(), 0);
30+
list.add("1");
31+
list.add("2");
32+
assertEquals("add list size ", list.size(), 2);
33+
for (int i=0; i<list.size(); i++) {
34+
assertEquals("index of"+i,list.get(i),""+(i+1));
35+
}
36+
}
37+
38+
/**
39+
* 测试在list的任意索引处添加元素
40+
*/
41+
@Test
42+
public void testAddIntT() {
43+
assertEquals("init list size is 0 ", list.size(), 0);
44+
list.add("1");
45+
list.add("3");
46+
list.add(1,"2");
47+
assertEquals("add list size ", list.size(), 3);
48+
assertEquals("add index 1 element is" ,list.get(1),"2");
49+
for (int i=0; i<list.size(); i++) {
50+
assertEquals("index of"+i,list.get(i),""+(i+1));
51+
}
52+
}
53+
54+
@Test
55+
public void testDelete() {
56+
assertEquals("init list size is 0 ", list.size(), 0);
57+
list.add("1");
58+
list.add("3");
59+
list.add(1,"2");
60+
assertEquals("add list size ", list.size(), 3);
61+
list.delete(1);
62+
assertEquals("after delete index 1 ",list.get(1),"3");
63+
}
64+
65+
@Test
66+
public void testGet() {
67+
assertEquals("init list size is 0 ", list.size(), 0);
68+
list.add("1");
69+
list.add("2");
70+
list.add("3");
71+
for(int i=0; i<list.size();i++) {
72+
assertEquals("index of"+i,list.get(i),""+(i+1));
73+
}
74+
}
75+
76+
@Test
77+
public void testSet() {
78+
assertEquals("init list size is 0 ", list.size(), 0);
79+
list.add("1");
80+
list.add("3");
81+
list.add("3");
82+
list.set(1, "2");
83+
for(int i=0; i<list.size();i++) {
84+
assertEquals("index of"+i,list.get(i),""+(i+1));
85+
}
86+
}
87+
88+
@Test
89+
public void testSize() {
90+
assertEquals("init list size is 0 ", list.size(), 0);
91+
list.add("1");
92+
list.add("3");
93+
list.add("3");
94+
assertEquals("after add list size is ", list.size(),3);
95+
}
96+
97+
/**
98+
* 正确的判空用例
99+
*/
100+
@Test
101+
public void testIsEmpty() {
102+
assertEquals("list is empty", list.isEmpty(),true);
103+
}
104+
105+
/**
106+
* 失败的判空用例
107+
*/
108+
@Test
109+
public void testIsEmptyNot() {
110+
list.add("1");
111+
assertEquals("list is empty", list.isEmpty(),false);
112+
}
113+
}

0 commit comments

Comments
 (0)