From 3ea180fc331621bbeff3684cc9ee023009682e6c Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 26 Feb 2017 13:04:07 +0800 Subject: [PATCH] commit --- group12/382266293/.classpath | 7 + group12/382266293/.gitignore | 1 + group12/382266293/.project | 17 ++ .../src/Collection/AbstractList.java | 43 ++++ .../src/Collection/Concrete/ArrayList.java | 148 ++++++++++++ .../Collection/Concrete/BinaryTreeNode.java | 126 +++++++++++ .../src/Collection/Concrete/LinkedList.java | 196 ++++++++++++++++ .../src/Collection/Concrete/Queue.java | 84 +++++++ .../src/Collection/Concrete/Stack.java | 106 +++++++++ .../382266293/src/Collection/Iterator.java | 7 + group12/382266293/src/Collection/List.java | 16 ++ .../src/TestCollection/AllTests.java | 11 + .../src/TestCollection/ArrayListTest.java | 173 ++++++++++++++ .../TestCollection/BinaryTreeNodeTest.java | 54 +++++ .../src/TestCollection/LinkedListTest.java | 212 ++++++++++++++++++ .../src/TestCollection/QueueTest.java | 98 ++++++++ .../src/TestCollection/StackTest.java | 129 +++++++++++ group12/382266293/src/test.java | 15 ++ group12/382266293/src/util/Print.java | 14 ++ group12/382266293/src/util/TestUtil.java | 78 +++++++ 20 files changed, 1535 insertions(+) create mode 100644 group12/382266293/.classpath create mode 100644 group12/382266293/.gitignore create mode 100644 group12/382266293/.project create mode 100644 group12/382266293/src/Collection/AbstractList.java create mode 100644 group12/382266293/src/Collection/Concrete/ArrayList.java create mode 100644 group12/382266293/src/Collection/Concrete/BinaryTreeNode.java create mode 100644 group12/382266293/src/Collection/Concrete/LinkedList.java create mode 100644 group12/382266293/src/Collection/Concrete/Queue.java create mode 100644 group12/382266293/src/Collection/Concrete/Stack.java create mode 100644 group12/382266293/src/Collection/Iterator.java create mode 100644 group12/382266293/src/Collection/List.java create mode 100644 group12/382266293/src/TestCollection/AllTests.java create mode 100644 group12/382266293/src/TestCollection/ArrayListTest.java create mode 100644 group12/382266293/src/TestCollection/BinaryTreeNodeTest.java create mode 100644 group12/382266293/src/TestCollection/LinkedListTest.java create mode 100644 group12/382266293/src/TestCollection/QueueTest.java create mode 100644 group12/382266293/src/TestCollection/StackTest.java create mode 100644 group12/382266293/src/test.java create mode 100644 group12/382266293/src/util/Print.java create mode 100644 group12/382266293/src/util/TestUtil.java diff --git a/group12/382266293/.classpath b/group12/382266293/.classpath new file mode 100644 index 0000000000..3e0fb272a8 --- /dev/null +++ b/group12/382266293/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/group12/382266293/.gitignore b/group12/382266293/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/group12/382266293/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/group12/382266293/.project b/group12/382266293/.project new file mode 100644 index 0000000000..1282023911 --- /dev/null +++ b/group12/382266293/.project @@ -0,0 +1,17 @@ + + + 382266293Learning + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/group12/382266293/src/Collection/AbstractList.java b/group12/382266293/src/Collection/AbstractList.java new file mode 100644 index 0000000000..a81e76b587 --- /dev/null +++ b/group12/382266293/src/Collection/AbstractList.java @@ -0,0 +1,43 @@ +package Collection; + +public abstract class AbstractList implements List { + + protected static final String PREFIX = "["; + protected static final String SUFFIX = "]"; + protected static final String SEPERATOR = ", "; + protected static final int MAX_SIZE = Integer.MAX_VALUE/3; + + protected void checkIndex(int i) { + if( i < 0 || i > Math.min(size(), MAX_SIZE)) + throw new IndexOutOfBoundsException("Size :" + size()+", Index: " + i); + } + + public boolean isEmpty() { + return size() == 0; + } + + + public int indexOf(E e) { + for (int i = 0; i < size()-1; i++) { + if (get(i).equals(e)) + return i; + } + return -1; + } + + protected abstract Iterator iterator(); + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(PREFIX); + for (int i = 0; i < size(); i++) { + sb.append(get(i)); + if (i < size()-1) + sb.append(SEPERATOR); + } + sb.append(SUFFIX); + return sb.toString(); + } + +} diff --git a/group12/382266293/src/Collection/Concrete/ArrayList.java b/group12/382266293/src/Collection/Concrete/ArrayList.java new file mode 100644 index 0000000000..3db5ab47e6 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/ArrayList.java @@ -0,0 +1,148 @@ +package Collection.Concrete; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class ArrayList extends AbstractList { + + private E[] elements; + private int size; + private static final int INITIAL_SIZE = 16; + + + public ArrayList() { + elements = (E[]) new Object[INITIAL_SIZE]; + size = 0; + } + + @Override + public void add(E e) { + checkCapacity(); + elements[size++] = e; + } + + private void checkCapacity() { + if (size >= MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max size"); + + if (elements.length - size < INITIAL_SIZE) + grow(); + } + + synchronized private void grow() { + + int newCapacity = size * 2; + newCapacity = (newCapacity < 0 || newCapacity - MAX_SIZE > 0) ? MAX_SIZE : newCapacity; + E[] target = (E[]) new Object[newCapacity]; + System.arraycopy(elements, 0, target, 0, size); + elements = target; + + } + + public void add(int index, E e) { + checkCapacity(); + if (index == size) { + add(e); + return; + } + checkIndex(index); + synchronized (this) { + System.arraycopy(elements, index, elements, index+1, size-index+1); + elements[index] = e; + size++; + } + } + + @Override + public E get(int index) { + checkIndex(index); + return elements[index]; + } + + + public E getLast() { + return get(size-1); + } + + public void addLast(E e) { + add(e); + } + + public E removeLast() { + return elements[--size]; + } + + public E remove(int index) { + checkIndex(index); + E result = elements[index]; + synchronized (this) { + System.arraycopy(elements, index+1, elements, index, size-index-1); + elements[--size] = null; + } + return result; + } + + @Override + public int size() { + return size; + } + + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Arrays.hashCode(elements); + result = prime * result + size; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ArrayList other = (ArrayList) obj; + if (!Arrays.equals(elements, other.elements)) + return false; + if (size != other.size) + return false; + return true; + } + + public Iterator iterator(){ + return new ArrayListIterator(this); + } + + private class ArrayListIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public ArrayListIterator(ArrayList arrayList) { + myArrayList = arrayList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) elements[pos++]; + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java new file mode 100644 index 0000000000..34a9d4253e --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/BinaryTreeNode.java @@ -0,0 +1,126 @@ +package Collection.Concrete; + +public class BinaryTreeNode> { + + private E data; + private BinaryTreeNode left; + private BinaryTreeNode right; + private int size; + private ArrayList myList; + + + + public BinaryTreeNode() { + this.data = null; + this.left = null; + this.right = null; + } + + public BinaryTreeNode(E data) { + this.data = data; + this.left = null; + this.right = null; + } + + public boolean isEmpty() { + return data == null; + } + + public int size() { + return size; + } + + public BinaryTreeNode insert(E o) { + BinaryTreeNode res; + if (isEmpty()) { + data = o; + size++; + return this; + } else { + BinaryTreeNode p = this; + res = new BinaryTreeNode(o); + while (true) { + if (res.getData().compareTo(p.getData()) < 0) { + if (p.left == null) { + p.setLeft(res); + break; + } + p = p.left; + } else if (res.getData().compareTo(p.getData()) > 0) { + if (p.right == null) { + p.setRight(res); + break; + } + p = p.right; + } else { + return null; + } + } + size++; + } + return res; + } + + + + public ArrayList preOrderTraversal(BinaryTreeNode node) { + + if (node != null) { + preOrderTraversal(node.left); + myList.add(node.data); + preOrderTraversal(node.right); + } + return myList; + } + + @Override + public String toString() { + myList = new ArrayList(); + return preOrderTraversal(this).toString(); + } + + public E getData() { + return data; + } + public void setData(E 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; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BinaryTreeNode other = (BinaryTreeNode) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + +} \ No newline at end of file diff --git a/group12/382266293/src/Collection/Concrete/LinkedList.java b/group12/382266293/src/Collection/Concrete/LinkedList.java new file mode 100644 index 0000000000..66dbd9d8d0 --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/LinkedList.java @@ -0,0 +1,196 @@ +package Collection.Concrete; + +import java.util.NoSuchElementException; +import Collection.AbstractList; +import Collection.Iterator; + + +public class LinkedList extends AbstractList { + + private Node head; + private int size; + + public LinkedList() { + this.head = new Node(null); + this.size = 0; + } + + @Override + public void add(E e) { + addLast(e); + } + + + @Override + public E get(int index) { + checkIndex(index); + return getNode(index).data; + } + + public E getFirst() { + return get(0); + } + + public E getLast() { + return get(size-1); + } + + + public void add(int index, E e) { + if (index == size) { + addLast(e); + return; + } + + if (index == 0) { + addFirst(e); + return; + } + + checkIndex(index); + Node pNode = new Node(e); + Node p = getNode(index); + synchronized (this) { + getNode(index-1).next = pNode; + pNode.next = p; + size++; + } + } + + public void addFirst(E e){ + checkCapacity(); + Node pNode = new Node(e); + Node oldHead = head; + head = pNode; + pNode.next = oldHead; + size++; + return; + } + + public void addLast(E e){ + if (size == 0) { + addFirst(e); + return; + } + + checkCapacity(); + Node res = new Node(e); + setLastNode(res); + size++; + return; + } + + public E removeFirst(){ + return remove(0); + } + public E removeLast(){ + return remove(size-1); + } + + public E remove(int index) { + checkIndex(index); + Node pNode = getNode(index); + if (index == 0) { + head = head.next; + } else if (index == size-1 ) { + getNode(index-1).next = null; + } else { + getNode(index-1).next = getNode(index+1); + } + size--; + return pNode.data; + } + + @Override + public int size() { + return size; + } + + public Iterator iterator(){ + return new LinkedListIterator(this); + } + + private void checkCapacity() { + if (size > MAX_SIZE) + throw new IndexOutOfBoundsException("Reached max capacity: "+ MAX_SIZE); + } + + private Node getNode(int index) { + if (size == 0) + return head; + + Node pNode = head; + for ( int i = 0; i < index ; i++) { + pNode = pNode.next; + } + return pNode; + } + + private void setLastNode(Node res) { + getNode(size-1).next = res; + } + + private static class Node { + E data; + Node next; + + public Node(E data) { + this.data = data; + this.next = null; + } + + @Override + public String toString() { + return data.toString(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((data == null) ? 0 : data.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Node other = (Node) obj; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; + return true; + } + } + + @SuppressWarnings("hiding") + private class LinkedListIterator implements Iterator { + + private LinkedList myLinkedList; + private int pos; + + public LinkedListIterator(LinkedList linkedList) { + myLinkedList = linkedList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size; + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos++); + throw new NoSuchElementException(); + } + } +} diff --git a/group12/382266293/src/Collection/Concrete/Queue.java b/group12/382266293/src/Collection/Concrete/Queue.java new file mode 100644 index 0000000000..300fb633af --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Queue.java @@ -0,0 +1,84 @@ +package Collection.Concrete; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Queue extends AbstractList { + + private LinkedList myList; + + public Queue() { + this.myList = new LinkedList(); + } + + public void enQueue(E e){ + myList.addLast(e); + } + + public E deQueue(){ + if (0 == size()) + return null; + return myList.removeFirst(); + } + + @Override + public void add(E e) { + enQueue(e); + } + + @Override + public E get(int index) { + if (0 == size()) + return null; + return myList.get(index); + } + + public E element() { + if (0 == size()) + return null; + return myList.getFirst(); + } + + + @Override + public int size() { + return myList.size(); + } + + @Override + protected Iterator iterator() { + return myList.iterator(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Queue other = (Queue) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + + + + + +} diff --git a/group12/382266293/src/Collection/Concrete/Stack.java b/group12/382266293/src/Collection/Concrete/Stack.java new file mode 100644 index 0000000000..17ec4364fa --- /dev/null +++ b/group12/382266293/src/Collection/Concrete/Stack.java @@ -0,0 +1,106 @@ +package Collection.Concrete; + +import java.util.EmptyStackException; +import java.util.NoSuchElementException; + +import Collection.AbstractList; +import Collection.Iterator; + +public class Stack extends AbstractList { + + private ArrayList myList; + + public Stack() { + this.myList = new ArrayList(); + } + + public void push(E e){ + myList.addLast(e); + } + + public E pop(){ + checkEmpty(); + return myList.removeLast(); + } + + private void checkEmpty() { + if (0 == size()) + throw new EmptyStackException(); + } + + public E peek(){ + checkEmpty(); + return myList.getLast(); + } + + public int size(){ + return myList.size(); + } + + @Override + public void add(E e) { + push(e); + } + + @Override + public E get(int index) { + checkEmpty(); + return myList.get(size() - index - 1); + } + + @Override + protected Iterator iterator() { + return new StackIterator(myList); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((myList == null) ? 0 : myList.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Stack other = (Stack) obj; + if (myList == null) { + if (other.myList != null) + return false; + } else if (!myList.equals(other.myList)) + return false; + return true; + } + + private class StackIterator implements Iterator { + + private ArrayList myArrayList; + private int pos; + + public StackIterator(ArrayList myList) { + myArrayList = myList; + pos = 0; + } + + @Override + public boolean hasNext() { + return pos < size(); + } + + @Override + public E next() { + if (hasNext()) + return (E) get(pos); + throw new NoSuchElementException(); + } + } + + + +} diff --git a/group12/382266293/src/Collection/Iterator.java b/group12/382266293/src/Collection/Iterator.java new file mode 100644 index 0000000000..d51656a3a8 --- /dev/null +++ b/group12/382266293/src/Collection/Iterator.java @@ -0,0 +1,7 @@ +package Collection; + +public interface Iterator { + + public boolean hasNext(); + public E next(); +} diff --git a/group12/382266293/src/Collection/List.java b/group12/382266293/src/Collection/List.java new file mode 100644 index 0000000000..7ddb685cac --- /dev/null +++ b/group12/382266293/src/Collection/List.java @@ -0,0 +1,16 @@ +package Collection; + +public interface List { + + public void add(E e); + + public int size(); + + public E get(int index); + + public boolean isEmpty(); + + public int indexOf(E e); + + +} diff --git a/group12/382266293/src/TestCollection/AllTests.java b/group12/382266293/src/TestCollection/AllTests.java new file mode 100644 index 0000000000..cd4eeadb58 --- /dev/null +++ b/group12/382266293/src/TestCollection/AllTests.java @@ -0,0 +1,11 @@ +package TestCollection; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayListTest.class, BinaryTreeNodeTest.class, LinkedListTest.class, QueueTest.class, StackTest.class }) +public class AllTests { + +} diff --git a/group12/382266293/src/TestCollection/ArrayListTest.java b/group12/382266293/src/TestCollection/ArrayListTest.java new file mode 100644 index 0000000000..f29580616f --- /dev/null +++ b/group12/382266293/src/TestCollection/ArrayListTest.java @@ -0,0 +1,173 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import java.util.Random; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.ArrayList; +import junit.framework.TestCase; + +public class ArrayListTest extends TestCase { + + + private ArrayList myAL; + private static Random rnd = new Random(); + + @Before + public void setUp() throws Exception { + + myAL = new ArrayList(); + assertEquals(true,myAL.isEmpty()); + + } + + @After + public void tearDown() throws Exception { + myAL = null; + } + + @Test + public void testRawTypeArrayList() { + + List rawList = new ArrayList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + } + + @Test + public void testEmpty() { + + assertEquals(true,myAL.isEmpty()); + + myAL.add(5); + assertEquals(false,myAL.isEmpty()); + + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(false,myAL.isEmpty()); + + } + + @Test + public void testAddIntAutoBoxing() { + + myAL.add(5); + myAL.add(5); + myAL.add(5); + myAL.add(1,10); + int c = myAL.get(1); + assertEquals(10,c); + + assertEquals(4,myAL.size()); + myAL.add(4,15); + int a = myAL.get(0); + Integer b = myAL.get(1); + c = myAL.get(2); + int d = myAL.get(3); + int e = myAL.get(4); + assertEquals(5,a); + assertEquals(new Integer(10),b); + assertEquals(5,c); + assertEquals(5,d); + assertEquals(15,e); + } + + @Test + public void testGet() { + + int[] result = addRandomInt(myAL, getRandomNumber()); + + int actual,expected; + + for (int i = 0; i < result.length; i++) { + actual = myAL.get(i); + expected = result[i]; + assertEquals(expected, actual); + } + + } + + @Test + public void testRemove() { + + addIntWithNatureOrder(myAL, 100); + + testRemoveAndGetFromTail(myAL); + try { + myAL.remove(10); + fail("no exception thrown"); + } catch (Exception e) { + assertEquals(IndexOutOfBoundsException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + + assertEquals(0,myAL.size()); + int num = getRandomNumber(); + addIntWithNatureOrder(myAL, num); + assertEquals(num,myAL.size()); + } + + + + @Test + public void testGrow() { + + int actualSize = 12345; + + addIntWithNatureOrder(myAL, actualSize); + + assertEquals(actualSize,myAL.size()); + } + + + @Test + public void testIterator() { + + addIntWithNatureOrder(myAL,100); + + Iterator it = myAL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + int actual = it.next(); + assertEquals(i,actual); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + @Test + public void testIndexOf() { + + int num = 200; + addIntWithNatureOrder(myAL,num); + + int expected,actual; + for (int i = 0; i < num-1; i++) { + expected = i; + actual = myAL.indexOf(i); + assertEquals(expected, actual); + } + + assertEquals(-1, myAL.indexOf(-1*getRandomNumber())); + + } + +} diff --git a/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java new file mode 100644 index 0000000000..275ef59484 --- /dev/null +++ b/group12/382266293/src/TestCollection/BinaryTreeNodeTest.java @@ -0,0 +1,54 @@ +package TestCollection; + +import static util.Print.*; +import java.util.Random; +import java.util.Set; +import java.util.TreeSet; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.BinaryTreeNode; +import junit.framework.TestCase; + +public class BinaryTreeNodeTest extends TestCase { + + private BinaryTreeNode myTree; + + @Before + public void setUp() throws Exception { + myTree = new BinaryTreeNode(); + assertEquals(0, myTree.size()); + } + + @After + public void tearDown() throws Exception { + myTree = null; + } + + @Test + public void testInsert() { + Set expected = new TreeSet(); + int size = getRandomNumber(); + int j = 0 ; + while (expected.size() != size) { + j = getRandomNumber(); + expected.add(j); + myTree.insert(j); + } + + assertEquals(size,myTree.size()); + assertEquals(expected.toString(),myTree.toString()); + } + + public void testSize() { + + for (int i = 0; i < getRandomNumber(); i++) { + myTree.insert(18); + myTree.insert(-19); + myTree.insert(1); + assertEquals(3,myTree.size()); + } + } +} \ No newline at end of file diff --git a/group12/382266293/src/TestCollection/LinkedListTest.java b/group12/382266293/src/TestCollection/LinkedListTest.java new file mode 100644 index 0000000000..8b9fad15eb --- /dev/null +++ b/group12/382266293/src/TestCollection/LinkedListTest.java @@ -0,0 +1,212 @@ +package TestCollection; + +import static util.Print.*; +import static util.TestUtil.*; +import java.util.Date; +import java.util.NoSuchElementException; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Iterator; +import Collection.List; +import Collection.Concrete.LinkedList; +import junit.framework.TestCase; + + +public class LinkedListTest extends TestCase { + + LinkedList myLL; + + @Before + public void setUp() throws Exception { + myLL = new LinkedList(); + assertEquals(0,myLL.size()); + } + + @After + public void tearDown() throws Exception { + myLL = null; + } + + @Test + public void testLinkedList() { + List rawList = new LinkedList(); + assertEquals(rawList.size(), 0); + rawList.add(new Date()); + assertEquals(1, rawList.size()); + + } + + @Test + public void testAddE() { + myLL.add("s"); + assertEquals(1,myLL.size()); + assertEquals(false,myLL.isEmpty()); + } + + @Test + public void testAddStringE() { + String a; + + addString(myLL,30); + println(myLL.get(0)); + +// for (int i = 0 ; i < 29; i++) { +// a = "" + i; +// assertEquals(myLL.get(i),a); +// } + } + + @Test + public void testAddIndex() { + String a; + for (int i = 0 ; i < 30; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "ss"; + myLL.add(3,ss); + assertEquals(myLL.get(3), ss); + assertEquals(myLL.get(2), "2"); + assertEquals(myLL.get(4), "3"); + + } + + public void testAddFirst() { + String a; + for (int i = 0 ; i < 20; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "bba"; + myLL.addFirst(ss); + assertEquals(ss,myLL.get(0)); + assertEquals(21, myLL.size()); + + ; + for (int i = 1 ; i < myLL.size(); i++) { + a = (i-1) + ""; + assertEquals(a, myLL.get(i)); + } + } + + public void testAddLast() { + String a; + for (int i = 0 ; i < 25; i++) { + a = "" + i; + myLL.add(a); + } + + String ss = "25"; + myLL.addLast(ss); + int size = myLL.size(); + assertEquals(26, size); + + for (int i = 0 ; i < size; i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + } + + @Test + public void testRemoveFirst() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeFirst(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i+1 + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = i+1 +""; + result = myLL.removeFirst(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + } + + @Test + public void testRemoveLast() { + + String a = ""; + String result = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + myLL.removeLast(); + assertEquals(9, myLL.size()); + + for(int i = 0; i < myLL.size(); i++) { + a = i + ""; + assertEquals(a, myLL.get(i)); + } + + int size = myLL.size(); + for(int i = 0; i < size; i++) { + a = myLL.size()-1 +""; + result = myLL.removeLast(); + assertEquals(a, result); + } + + assertEquals(0, myLL.size()); + + } + + + @Test + public void testRemove() { + + String res = ""; + String a = ""; + for(int i = 0; i < 10; i++) { + myLL.add(i+""); + } + + for(int i = myLL.size()-1; i >= 0; i--) { + a = myLL.get(i); + res = myLL.remove(i); + assertEquals(i, myLL.size()); + assertEquals(a,res); + } + } + + @Test + public void testSize() { + assertEquals(0,myLL.size()); + } + + @Test + public void testIterator() { + for(int i = 0; i<10; i++) { + myLL.add(i+""); + } + Iterator it = myLL.iterator(); + + for(int i = 0; it.hasNext(); i++){ + String res = it.next(); + assertEquals(i+"",res); + } + + try { + it.next(); + } catch (NoSuchElementException ex) { + assertEquals(ex.getClass(),NoSuchElementException.class); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/QueueTest.java b/group12/382266293/src/TestCollection/QueueTest.java new file mode 100644 index 0000000000..01a9aa31f2 --- /dev/null +++ b/group12/382266293/src/TestCollection/QueueTest.java @@ -0,0 +1,98 @@ +package TestCollection; + +import static util.Print.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import static util.TestUtil.*; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + +public class QueueTest extends TestCase { + + private Queue myQueue; + + @Before + public void setUp() throws Exception { + myQueue= new Queue(); + } + + @After + public void tearDown() throws Exception { + myQueue = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myQueue.isEmpty()); + myQueue.enQueue(getRandomNumber()); + assertEquals(false, myQueue.isEmpty()); + } + + @Test + public void testEnQueue() { + + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + + } + + @Test + public void testDeQueue() { + enQueueIntWithNatureOrder(myQueue, getRandomNumber()); + int size = myQueue.size(); + for (int i = 0; i < size ; i++) { + assertEquals(size-i, myQueue.size()); + int expect = i; + int actual = myQueue.deQueue(); + assertEquals(expect, actual); + } + + assertEquals(null, myQueue.deQueue()); + assertEquals(null, myQueue.element()); + assertEquals(null, myQueue.get(0)); + + } + + @Test + public void testelement() { + + int expected = 0; + int element1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myQueue.enQueue(i); + expected = i; + + element1 = myQueue.element(); + assertEquals(expected, element1); + + for (int j = 0; j < i; j++) { + repeated = myQueue.element(); + assertEquals(expected, repeated); + } + myQueue.deQueue(); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myQueue.size()); + myQueue.enQueue(i); + } + } + + @Test + public void testAdd() { + for (int i = 0; i < 10; i++) { + myQueue.add(i); + Integer actual = new Integer(myQueue.get(i)); + Integer expected = new Integer(i); + assertEquals(expected, actual); + } + } + + +} diff --git a/group12/382266293/src/TestCollection/StackTest.java b/group12/382266293/src/TestCollection/StackTest.java new file mode 100644 index 0000000000..3784a9b972 --- /dev/null +++ b/group12/382266293/src/TestCollection/StackTest.java @@ -0,0 +1,129 @@ +package TestCollection; + +import static util.Print.*; + +import java.util.EmptyStackException; +import static util.TestUtil.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import Collection.Concrete.Stack; +import junit.framework.TestCase; + +public class StackTest extends TestCase { + + Stack myStack; + + @Before + public void setUp() throws Exception { + myStack= new Stack(); + } + + @After + public void tearDown() throws Exception { + myStack = null; + } + + @Test + public void testIsEmpty() { + assertEquals(true, myStack.isEmpty()); + myStack.push(getRandomNumber()); + assertEquals(false, myStack.isEmpty()); + } + + @Test + public void testPush() { + for (int i = 0; i < 10; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testPop() { + testPush(); + int size = myStack.size(); + for (int i = size; i > 0; i--) { + assertEquals(i, myStack.size()); + int expect = i-1; + assertEquals(i, myStack.size()); + int actual = myStack.pop(); + assertEquals(expect, actual); + } + + try { + myStack.pop(); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + } + + @Test + public void testPeek() { + + int expected = 0; + int peek1 = 0; + int repeated = 0; + + for (int i = 0; i < 10; i++) { + myStack.push(i); + expected = i; + + peek1 = myStack.peek(); + assertEquals(expected, peek1); + + for (int j = 0; j < i; j++) { + repeated = myStack.peek(); + assertEquals(expected, repeated); + } + } + + } + + + public void testGet() { + + try { + myStack.get(getRandomNumber()); + fail("no exception throw"); + } catch (Exception e) { + assertEquals(EmptyStackException.class, e.getClass()); + } + + } + + @Test + public void testSize() { + for (int i = 0; i < 10000; i++) { + assertEquals(i, myStack.size()); + myStack.push(i); + } + } + + @Test + public void testAdd() { + myStack.push(5); + myStack.push(10); + myStack.push(15); + println(myStack.get(0)); + println(myStack.get(1)); + println(myStack.get(2)); + + } + + @Test + public void testPopPushAndPeek() { + for (int i = 0; i < 10; i++) { + int expected = i; + myStack.push(i); + int a = 0; + myStack.push(a); + myStack.pop(); + int actual = myStack.peek(); + assertEquals(expected, actual); + } + } + +} diff --git a/group12/382266293/src/test.java b/group12/382266293/src/test.java new file mode 100644 index 0000000000..e4e7fead2d --- /dev/null +++ b/group12/382266293/src/test.java @@ -0,0 +1,15 @@ +import java.util.ArrayList; +import java.util.Queue; +import java.util.Stack; +import java.util.concurrent.PriorityBlockingQueue; +import static util.Print.*; + +public class test { + + public static void main(String[] args) { + Queue queue = new PriorityBlockingQueue(); + println(queue.poll()); + + } + +} diff --git a/group12/382266293/src/util/Print.java b/group12/382266293/src/util/Print.java new file mode 100644 index 0000000000..b2ae48552b --- /dev/null +++ b/group12/382266293/src/util/Print.java @@ -0,0 +1,14 @@ +package util; + + +public class Print { + + public static void print(Object o){ + System.out.print(o); + } + + public static void println(Object o){ + System.out.println(o); + } + +} diff --git a/group12/382266293/src/util/TestUtil.java b/group12/382266293/src/util/TestUtil.java new file mode 100644 index 0000000000..2dfeeade7f --- /dev/null +++ b/group12/382266293/src/util/TestUtil.java @@ -0,0 +1,78 @@ +package util; + +import java.util.Random; + +import Collection.List; +import Collection.Concrete.ArrayList; +import Collection.Concrete.LinkedList; +import Collection.Concrete.Queue; +import junit.framework.TestCase; + + +public class TestUtil extends TestCase { + + private static Random random = new Random(); + private static final int RANDOM_BOUND = 2<<15; + private static final int RANDOM_SIZE = 500; + + + public static int getRandomNumber() { + return random.nextInt(RANDOM_SIZE); + } + + public static void addIntWithNatureOrder(List myList, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myList.add(acutal); + } + } + + public static int[] addRandomInt(List myList, int numbers) { + + int actual = 0; + int[] result = new int[numbers]; + for (int i = 0; i < numbers ; i++) { + actual = random.nextInt(RANDOM_BOUND); + result[i] = actual; + myList.add(actual); + } + return result; + } + + public static void addString(List myList, int num) { + + String actual; + for (int index = 0; index < num ; index++) { + actual = index + ""; + myList.add(actual); + } + } + + public static void testRemoveAndGetFromTail(ArrayList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void testRemoveAndGetFromTail(LinkedList myList) { + E get; + E remove; + for(int i = myList.size()-1; i >= 0; i--) { + get = myList.get(i); + remove = myList.remove(i); + assertEquals(get,remove); + } + } + + public static void enQueueIntWithNatureOrder(Queue myQueue, int numbers) { + + for (int acutal = 0; acutal < numbers ; acutal++) { + myQueue.enQueue(acutal); + } + } + +}