From 37595cec0ff849be16dcc6a2136ec680ba94b426 Mon Sep 17 00:00:00 2001 From: Mark Ryan <676615845@qq.com> Date: Sat, 25 Feb 2017 17:14:03 +0800 Subject: [PATCH] default --- group14/676615845/algo/pom.xml | 18 ++ .../algo/src/main/java/algo/BinarySearch.java | 17 ++ .../src/main/java/com/coding/basic/Array.java | 16 ++ .../main/java/com/coding/basic/ArrayList.java | 73 ++++++++ .../java/com/coding/basic/BinaryTreeNode.java | 41 +++++ .../main/java/com/coding/basic/Iterator.java | 7 + .../java/com/coding/basic/LinkedList.java | 164 ++++++++++++++++++ .../src/main/java/com/coding/basic/List.java | 9 + .../src/main/java/com/coding/basic/Queue.java | 22 +++ .../src/main/java/com/coding/basic/Stack.java | 26 +++ .../src/test/java/algo/BinarySearchTest.java | 26 +++ .../java/com/coding/basic/ArrayListTest.java | 146 ++++++++++++++++ .../test/java/com/coding/basic/ArrayTest.java | 30 ++++ .../com/coding/basic/BinaryTreeNodeTest.java | 64 +++++++ .../java/com/coding/basic/LinkedListTest.java | 115 ++++++++++++ .../test/java/com/coding/basic/QueueTest.java | 58 +++++++ .../test/java/com/coding/basic/StackTest.java | 70 ++++++++ 17 files changed, 902 insertions(+) create mode 100644 group14/676615845/algo/pom.xml create mode 100644 group14/676615845/algo/src/main/java/algo/BinarySearch.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Array.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/List.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Queue.java create mode 100644 group14/676615845/algo/src/main/java/com/coding/basic/Stack.java create mode 100644 group14/676615845/algo/src/test/java/algo/BinarySearchTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java create mode 100644 group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java diff --git a/group14/676615845/algo/pom.xml b/group14/676615845/algo/pom.xml new file mode 100644 index 0000000000..d2e1f258a0 --- /dev/null +++ b/group14/676615845/algo/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + com.mark + algo + 1.0-SNAPSHOT + + + + junit + junit + 4.12 + + + \ No newline at end of file diff --git a/group14/676615845/algo/src/main/java/algo/BinarySearch.java b/group14/676615845/algo/src/main/java/algo/BinarySearch.java new file mode 100644 index 0000000000..3144a37181 --- /dev/null +++ b/group14/676615845/algo/src/main/java/algo/BinarySearch.java @@ -0,0 +1,17 @@ +package algo; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +/** + * Created by mark on 17/2/23. + */ +public class BinarySearch { + + public static int rank(int key, int[] a) { + List list = new ArrayList(); + list = new LinkedList(); + return -1; + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Array.java b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java new file mode 100644 index 0000000000..44afce6c25 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Array.java @@ -0,0 +1,16 @@ +package com.coding.basic; + +import java.util.Arrays; + +/** + * Created by mark on 17/2/24. + */ +public class Array { + + public static Object[] grow(Object[] src, int size) { + return Arrays.copyOf(src, src.length + size); +// Object[] target = new Object[src.length + size]; +// System.arraycopy(src, 0, target, 0, src.length); + + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java new file mode 100644 index 0000000000..148bd6da96 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/ArrayList.java @@ -0,0 +1,73 @@ +package com.coding.basic; + +public class ArrayList implements List { + + private int size; // ArrayList 中的实际元素个数 + private Object[] elementData; + + public ArrayList() { + size = 0; + elementData = new Object[100]; + } + + public void add(Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + elementData[size++] = o; + } + + public void add(int index, Object o){ + if (size >= elementData.length) { + elementData = Array.grow(elementData, 100); + } + + if (index > size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = o; + size++; + } + + public Object get(int index){ + if (index > size) throw new ArrayIndexOutOfBoundsException(); + return elementData[index]; + } + + public Object remove(int index){ + + if (index >= size || index < 0) throw new ArrayIndexOutOfBoundsException(); + + Object result = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[--size] = null; + return result; + } + + public int size() { + return size; + } + + public Iterator iterator(){ + + return new Iterator() { + + private int next = 0; // 下一个返回元素所在的位置 + + public boolean hasNext() { + return next < size; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + return elementData[next++]; + } + + public Object remove() { + if (next <= 0) throw new IllegalStateException(); + return ArrayList.this.remove(--next); + } + }; + } + +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..5ddd6f5f8a --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/BinaryTreeNode.java @@ -0,0 +1,41 @@ +package com.coding.basic; + +public class BinaryTreeNode implements Comparable { + + private Object data; + private BinaryTreeNode left; + private BinaryTreeNode right; + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinaryTreeNode getLeft() { + return left; + } + + public void setLeft(BinaryTreeNode left) { + this.left = left; + } + + public BinaryTreeNode getRight() { + return right; + } + + public void setRight(BinaryTreeNode right) { + this.right = right; + } + + public BinaryTreeNode insert(Object o){ + return null; + } + + public int compareTo(Object obj) { + if (obj == null || obj.getClass() != Integer.class) throw new IllegalArgumentException(); + return Integer.compare(((Integer) data).intValue(), ((Integer) obj).intValue()); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java new file mode 100644 index 0000000000..a0b91b1a82 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Iterator.java @@ -0,0 +1,7 @@ +package com.coding.basic; + +public interface Iterator { + boolean hasNext(); + Object next(); + Object remove(); +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java new file mode 100644 index 0000000000..a1548a0c23 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/LinkedList.java @@ -0,0 +1,164 @@ +package com.coding.basic; + +public class LinkedList implements List { + + private Node first = null; + private Node last = null; + private int size = 0; + + public void add(Object o){ + Node node = new Node(o); + if (first == null) { + first = node; + } else { + last.next = node; + node.prev = last; + } + last = node; + size++; + } + + public void add(int index , Object o) { + if (index < 0 || index > size) throw new ArrayIndexOutOfBoundsException(); + + Node node = new Node(o); + + if (first == null) { + first = node; + last = node; + } else { + if (index == 0) { + node.next = first; + first.prev = node; + first = node; + } else if (index == size) { + last.next = node; + node.prev = last; + last = node; + } else { + Node temp = first; + while (--index > 0) { + temp = temp.next; + } + node.next = temp.next; + temp.next.prev = node; + temp.next = node; + node.prev = temp; + } + } + size++; + } + public Object get(int index){ + if (index < 0 || index > size - 1) throw new ArrayIndexOutOfBoundsException(); + Node node = first; + while (index-- > 0) { + node = node.next; + } + return node.data; + } + + public Object remove(int index){ + if (index < 0 || index >= size) throw new ArrayIndexOutOfBoundsException(); + + Node node = null; + if (index == 0) { + node = first; + if (size == 1) { + first = null; + last = null; + } else { + first = first.next; + first.prev = null; + } + } else if (index == size - 1) { + node = last; + last = last.prev; + last.next = null; + } else { + node = first; + Node temp = null; + while (index-- > 0) { + node = node.next; + } + temp = node.prev; + temp.next = node.next; + node.next.prev = temp; + } + size--; + return node.data; + } + + public int size(){ + return size; + } + + public void addFirst(Object obj){ + add(0, obj); + } + + public void addLast(Object obj){ + add(size, obj); + } + + public Object removeFirst(){ + return remove(0); + } + + public Object removeLast(){ + return remove(size - 1); + } + + public Iterator iterator(){ + + if (first == null || last == null) throw new IllegalStateException(); + + return new InnerIterator(); + } + + private class InnerIterator implements Iterator { + + private Node nextNode = first; + + public boolean hasNext() { + return nextNode != null; + } + + public Object next() { + if (!hasNext()) throw new ArrayIndexOutOfBoundsException(); + Node node = nextNode; + nextNode = nextNode.next; + return node.data; + } + + public Object remove() { + if (nextNode == first) throw new IllegalStateException(); + + Node node = nextNode.prev; + if (nextNode == first.next) { + first = nextNode; + first.prev = null; + } else if (nextNode == null) { + node = last; + last = last.prev; + last.next = null; + } else { + node.prev = node.next; + node.next.prev = node.prev; + } + return node.data; + } + } + + private static class Node{ + + Object data; + Node next; + Node prev; + + public Node(Object data) { + this.data = data; + next = null; + prev = null; + } + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/List.java b/group14/676615845/algo/src/main/java/com/coding/basic/List.java new file mode 100644 index 0000000000..6d380288e5 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/List.java @@ -0,0 +1,9 @@ +package com.coding.basic; + +public interface List { + void add(Object o); + void add(int index, Object o); + Object get(int index); + Object remove(int index); + int size(); +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java new file mode 100644 index 0000000000..60345ca4f6 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Queue.java @@ -0,0 +1,22 @@ +package com.coding.basic; + +public class Queue { + + private LinkedList linkedList = new LinkedList(); + + public void enQueue(Object o){ + linkedList.add(o); + } + + public Object deQueue(){ + return linkedList.removeFirst(); + } + + public boolean isEmpty(){ + return linkedList.size() == 0; + } + + public int size(){ + return linkedList.size(); + } +} diff --git a/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java new file mode 100644 index 0000000000..a9f0d009f3 --- /dev/null +++ b/group14/676615845/algo/src/main/java/com/coding/basic/Stack.java @@ -0,0 +1,26 @@ +package com.coding.basic; + +public class Stack { + + private ArrayList elementData = new ArrayList(); + + public void push(Object o){ + elementData.add(o); + } + + public Object pop(){ + return elementData.remove(elementData.size() - 1); + } + + public Object peek(){ + return elementData.get(elementData.size() - 1); + } + + public boolean isEmpty(){ + return elementData.size() == 0; + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java new file mode 100644 index 0000000000..6308e23251 --- /dev/null +++ b/group14/676615845/algo/src/test/java/algo/BinarySearchTest.java @@ -0,0 +1,26 @@ +package algo; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class BinarySearchTest { + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void rank() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java new file mode 100644 index 0000000000..64fd31eb33 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayListTest.java @@ -0,0 +1,146 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayListTest { + + private static ArrayList list; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + list = new ArrayList(); + } + + @After + public void tearDown() throws Exception { + list = null; + } + + @Test + public void add() throws Exception { + // 可以加入元素 + list.add("hello"); + Assert.assertEquals(1, list.size()); + + // 可以自动扩容 + for (int i=0; i<150; i++) { + list.add(i); + } + Assert.assertEquals(151, list.size()); + Assert.assertTrue(149 == ((Integer) list.get(150)).intValue()); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(0, "zero"); + list.add(50, "fifty"); + list.add(102, "102"); + Assert.assertEquals("zero", list.get(0)); + Assert.assertEquals("fifty", list.get(50)); + Assert.assertEquals("102", list.get(102)); + + list = new ArrayList(); + for (int i=0; i<100; i++) { + list.add(i); + } + list.add(100, "100"); + Assert.assertEquals("100", list.get(100)); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.add(102, "102"); + } + + @Test + public void get() throws Exception { + list.add("hello"); + Object obj = list.get(0); + Assert.assertTrue("hello".equals(obj)); + } + + @Test + public void remove() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(99, ((Integer) list.remove(99)).intValue()); + Assert.assertEquals(99, list.size()); + + thrown.expect(ArrayIndexOutOfBoundsException.class); + list.remove(100); + list.remove(-1); + } + + @Test + public void size() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + Assert.assertEquals(100, list.size()); + list.add("hello"); + Assert.assertEquals(101, list.size()); + } + + @Test + public void iterator() throws Exception { + for (int i=0; i<100; i++) { + list.add(i); + } + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + Assert.assertNotNull(iterator.next()); + } +// Assert.assertNotNull(iterator.next()); + + Object[] target = new Object[list.size()]; + int i = 0; + iterator = list.iterator(); + while (iterator.hasNext()) { + target[i++] = iterator.next(); + } + Assert.assertEquals(100, target.length); + + for (int j = 0; j < 100; j++) { + Assert.assertEquals(j, ((Integer) target[j]).intValue()); + } + + // 测试迭代器的 remove() 方法 + list = new ArrayList(); + for (int k=0; k<100; k++) { + list.add(k); + } + iterator = list.iterator(); +// thrown.expect(IllegalStateException.class); +// iterator.remove(); + + iterator.next(); + Object i0 = iterator.remove(); + Assert.assertEquals(0, ((Integer) i0).intValue()); + + for (int j=0; j<50; j++) { + iterator.next(); + } + Object i50 = iterator.remove(); + Assert.assertEquals(50, ((Integer)i50).intValue()); + + for (int j = 0; j < 48; j++) { + iterator.next(); + } + Object i99 = iterator.remove(); + Assert.assertEquals(98, ((Integer)i99).intValue()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java new file mode 100644 index 0000000000..579b98c585 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/ArrayTest.java @@ -0,0 +1,30 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/24. + */ +public class ArrayTest { + private Object[] src; + + @Before + public void setUp() throws Exception { + src = new Object[10]; + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void grow() throws Exception { + src = Array.grow(src, 20); + Assert.assertEquals(30, src.length); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..b3d3c7d557 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/BinaryTreeNodeTest.java @@ -0,0 +1,64 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class BinaryTreeNodeTest { + + private BinaryTreeNode tree; + + @Before + public void setUp() throws Exception { + tree = new BinaryTreeNode(); + } + + @After + public void tearDown() throws Exception { + tree = null; + } + + @Test + public void getData() throws Exception { + + } + + @Test + public void setData() throws Exception { + + } + + @Test + public void getLeft() throws Exception { + + } + + @Test + public void setLeft() throws Exception { + + } + + @Test + public void getRight() throws Exception { + + } + + @Test + public void setRight() throws Exception { + + } + + @Test + public void insert() throws Exception { + tree.insert("8"); + tree.insert("1"); + tree.insert("2"); + tree.insert("10"); + tree.insert("4"); + tree.insert("34"); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java new file mode 100644 index 0000000000..24c2a84367 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/LinkedListTest.java @@ -0,0 +1,115 @@ +package com.coding.basic; + +import org.junit.*; +import org.junit.rules.ExpectedException; + +/** + * Created by mark on 17/2/24. + */ +public class LinkedListTest { + + private LinkedList linkedList; + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + linkedList = new LinkedList(); + } + + @After + public void tearDown() throws Exception { + linkedList = null; + } + + @Test + public void add() throws Exception { + linkedList.add("first"); + Assert.assertEquals(1, linkedList.size()); + Assert.assertEquals("first", linkedList.get(0)); + + linkedList.add("second"); + linkedList.add("third"); + Assert.assertEquals("third", linkedList.get(2)); + } + + @Test + public void add1() throws Exception { + for (int i=0; i<10; i++) { + linkedList.add(i); + } + linkedList.add(5, "Five"); + Assert.assertEquals("Five", linkedList.get(5)); + Assert.assertEquals(11, linkedList.size()); + + linkedList.add(0, "Zero"); + Assert.assertEquals("Zero", linkedList.get(0)); + + linkedList.add(12, "Last"); + Assert.assertEquals("Last", linkedList.get(12)); + } + + @Test + public void get() throws Exception { + + linkedList.add("hello"); + Assert.assertEquals("hello", linkedList.get(0)); + + linkedList.add("two"); + Assert.assertEquals("two", linkedList.get(1)); + + linkedList = new LinkedList(); + thrown.expect(ArrayIndexOutOfBoundsException.class); + linkedList.get(0); + } + + @Test + public void remove() throws Exception { + Object data = null; + + for (int i=0; i<10; i++) { + linkedList.add("" + i); + } + + data = linkedList.remove(0); + Assert.assertEquals("0", data); + + data = linkedList.remove(8); + Assert.assertEquals("9", data); + + data = linkedList.remove(4); + Assert.assertEquals("5", data); + } + + @Test + public void size() throws Exception { + linkedList.add(0); + Assert.assertEquals(1, linkedList.size()); + } + + @Test + public void addFirst() throws Exception { + + } + + @Test + public void addLast() throws Exception { + + } + + @Test + public void removeFirst() throws Exception { + + } + + @Test + public void removeLast() throws Exception { + + } + + @Test + public void iterator() throws Exception { + + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java new file mode 100644 index 0000000000..76ecb28a48 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/QueueTest.java @@ -0,0 +1,58 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class QueueTest { + + private Queue queue; + + @Before + public void setUp() throws Exception { + queue = new Queue(); + } + + @After + public void tearDown() throws Exception { + queue = null; + } + + @Test + public void enQueue() throws Exception { + queue.enQueue("first"); + queue.enQueue("second"); + queue.enQueue("third"); + Assert.assertEquals("first", queue.deQueue()); + Assert.assertEquals("second", queue.deQueue()); + Assert.assertEquals("third", queue.deQueue()); + } + + @Test + public void deQueue() throws Exception { + + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, queue.isEmpty()); + queue.enQueue("first"); + Assert.assertEquals(false, queue.isEmpty()); + queue.deQueue(); + Assert.assertEquals(true, queue.isEmpty()); + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, queue.size()); + queue.enQueue("first"); + Assert.assertEquals(1, queue.size()); + queue.deQueue(); + Assert.assertEquals(0, queue.size()); + } + +} \ No newline at end of file diff --git a/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java new file mode 100644 index 0000000000..21192c9036 --- /dev/null +++ b/group14/676615845/algo/src/test/java/com/coding/basic/StackTest.java @@ -0,0 +1,70 @@ +package com.coding.basic; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by mark on 17/2/25. + */ +public class StackTest { + + private Stack stack; + + @Before + public void setUp() throws Exception { + stack = new Stack(); + } + + @After + public void tearDown() throws Exception { + stack = null; + } + + @Test + public void push() throws Exception { + stack.push("first"); + stack.push("second"); + Assert.assertEquals("second", stack.pop()); + Assert.assertEquals("first", stack.pop()); + Assert.assertEquals(0, stack.size()); + } + + @Test + public void pop() throws Exception { + + } + + @Test + public void peek() throws Exception { + stack.push("first"); + stack.push("second"); + Assert.assertEquals("second", stack.peek()); + stack.pop(); + Assert.assertEquals("first", stack.peek()); + } + + @Test + public void isEmpty() throws Exception { + Assert.assertEquals(true, stack.isEmpty()); + stack.push("first"); + Assert.assertEquals(false, stack.isEmpty()); + stack.pop(); + Assert.assertEquals(true, stack.isEmpty()); + + } + + @Test + public void size() throws Exception { + Assert.assertEquals(0, stack.size()); + stack.push("first"); + Assert.assertEquals(1, stack.size()); + stack.push("second"); + Assert.assertEquals(2, stack.size()); + stack.pop(); + stack.pop(); + Assert.assertEquals(0, stack.size()); + } + +} \ No newline at end of file