Skip to content

Commit

Permalink
Adding Testing Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Gavin Li committed Jan 31, 2015
1 parent e0cb0f4 commit 553e754
Show file tree
Hide file tree
Showing 14 changed files with 475 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MerchantRPGCSE2102/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="lib" path="hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="junit-4.11.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added MerchantRPGCSE2102/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added MerchantRPGCSE2102/junit-4.11.jar
Binary file not shown.
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/queues/EmptyQueueException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package queues;

public class EmptyQueueException extends Exception {

public EmptyQueueException(){
this("no message");
}
public EmptyQueueException(String s){
System.err.println("EmptyQueueException "+s);
}
}
38 changes: 38 additions & 0 deletions MerchantRPGCSE2102/src/queues/FIFOQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
*
*/
package queues;

/**
* @author tms08012
*
*/
public interface FIFOQ<E> {
/**
* Returns the number of elements in the queue
* @return number of elements in the queue
*/
public int size();
/**
* Returns whether the queue is empty
* @return true if the queue is empty, false otherwise.
*/
public boolean isEmpty();
/**
* Inspects the element at the front of the queue.
* @return elements at the front of the queue
* @exception EmptyQueueException if the queue is empty.
*/
public E front() throws EmptyQueueException;
/**
* Inserts an elements at the rear of the queue.
* @param element new element to be inserted/
*/
public void enqueue(E element);
/**
* Removes the element at the front of the queue.
* @return element removed.
* @exception EmptyQueueException if the queue is empty
*/
public E dequeue() throws EmptyQueueException;
}
46 changes: 46 additions & 0 deletions MerchantRPGCSE2102/src/queues/FIFOQImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package queues;

import java.util.LinkedList;

public class FIFOQImpl<E> {//This instance of a queue is implemented with a java.util.LinkedList

LinkedList<E> _llist;
int _capacity;
int _nContents;

public FIFOQImpl(){
_llist = new LinkedList<E>();
_capacity = 10;
_nContents = 0;
}

public boolean enqueue(E item){
if (_nContents<_capacity){
_llist.add(item);
_nContents++;
return true;
}
else {
return false;
}
}
public E dequeue() throws EmptyQueueException{
if(_nContents>0){
_nContents--;
return(_llist.get(_nContents));
}
else {
throw new EmptyQueueException("FIFOQImpl::dequeue from empty");
}
}
public int getCapacity(){
return _capacity;
}
public void setCapacity(int c){
_capacity = c;
}
public void flush(){
_llist.clear();
_nContents = 0;
}
}
38 changes: 38 additions & 0 deletions MerchantRPGCSE2102/src/queues/LIFOQ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
*
*/
package queues;

/**
* @author tms08012
*
*/
public interface LIFOQ<E> {
/**
* Returns the number of elements in the queue
* @return number of elements in the queue
*/
public int size();
/**
* Returns whether the queue is empty
* @return true if the queue is empty, false otherwise.
*/
public boolean isEmpty();
/**
* Inspects the element at the top of the queue.
* @return elements at the top of the queue
* @exception EmptyQueueException if the queue is empty.
*/
public E top() throws EmptyQueueException;
/**
* Inserts an elements at the top of the queue.
* @param element new element to be inserted/
*/
public boolean push(E element);
/**
* Removes the element at the top of the queue.
* @return element removed.
* @exception EmptyQueueException if the queue is empty
*/
public E pop() throws EmptyQueueException;
}
64 changes: 64 additions & 0 deletions MerchantRPGCSE2102/src/queues/LIFOQImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package queues;

import java.util.LinkedList;

public class LIFOQImpl<E> implements LIFOQ<E>{//This instance of a queue is implemented with a java.util.LinkedList

LinkedList<E> _llist;
int _capacity;
int _nContents;

public LIFOQImpl(){
_llist = new LinkedList<E>();
_capacity = 10;
_nContents = 0;
}

public boolean push(E item){
if (_nContents<_capacity){
_llist.add(item);
_nContents++;
return true;
}
else {
return false;
}
}
public E pop() throws EmptyQueueException{
if(_nContents>0){
_nContents--;
return(_llist.get(_nContents));
}
else {
throw new EmptyQueueException("LIFOQImpl::pop from empty");

}
}
public int getCapacity(){
return _capacity;
}
public void setCapacity(int c){
_capacity = c;
}
public void flush(){
_llist.clear();
}

@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}

@Override
public E top() throws EmptyQueueException {
// TODO Auto-generated method stub
return null;
}
}
14 changes: 14 additions & 0 deletions MerchantRPGCSE2102/src/queues/QueueMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package queues;

import java.util.*;

public class QueueMain {

public static void main(String[] args) {
// This QueueMain instantiates a queue
FIFOQImpl q = new FIFOQImpl();


}

}
102 changes: 102 additions & 0 deletions MerchantRPGCSE2102/src/unittests/FIFOQTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package unittests;

import org.junit.Test;

import queues.EmptyQueueException;
import queues.FIFOQImpl;
import queues.LIFOQImpl;
import junit.framework.TestCase;


public class FIFOQTest extends TestCase {

private int _value1;
private int _value2;
private FIFOQImpl<Integer> _mq;
private int _capacity;

public FIFOQTest(String testName) {
super(testName);
}

protected void setUp() throws Exception {
super.setUp();
System.out.println("FIFOQTest::setUp ");
_value1 = 3;
_value2 = 5;
}

protected void tearDown() throws Exception {
super.tearDown();
System.out.println("FIFOQTest::tearDown ");
_value1 = 0;
_value2 = 0;
}

@Test
public void testEnqueue() {
_mq = new FIFOQImpl<Integer>();
_mq.setCapacity(30);
int capacity = _mq.getCapacity();
assertEquals(30,capacity);
boolean success = false;//this is not arbitrary, if the loop never runs violates assertion
for (int p = 1; p<capacity; p++){
success = _mq.enqueue(_value1);
assertEquals(success, true);
}
assertEquals(success, true);
}

@Test
public void testFailedEnqueue() {
_mq = new FIFOQImpl<Integer>();
_capacity = 30;
_mq.setCapacity(_capacity);
boolean success = true;//this is not arbitrary, if the loop never runs, violates assertion
for (int p = 0; p<(_capacity+1); p++){
success = _mq.enqueue(_value1);
if (p<_capacity){
assertEquals(success, true);
}
else {
assertEquals(success, false);
}
}

}

@Test
public void testDequeue() throws EmptyQueueException {
_mq = new FIFOQImpl<Integer>();
_capacity = 30;
_mq.setCapacity(_capacity);
System.out.println("FIFOQTest::testDequeue ");
for (int p = 0; p<_capacity; p++){
boolean success = _mq.enqueue(p);//first fill it up
System.out.println("FIFOQTest::testDequeue "+ p);
assertEquals(success, true);
}
for (int p = _capacity; p>0; p--){
int q = (int) _mq.dequeue(); //then empty it out
q=q+1;
System.out.println("FIFOQTest::testDequeue "+ p + q);
assertEquals(q, p);
}

}

@Test
public void testFailedDequeue() throws EmptyQueueException {
_mq = new FIFOQImpl<Integer>();
_mq.flush();
boolean empty = false;
try{
empty = (null ==_mq.dequeue());
}catch(Exception e){
System.out.println("FIFOQTest::testFailedDequeue: caught expected exception "+e);
}

}


}
11 changes: 11 additions & 0 deletions MerchantRPGCSE2102/src/unittests/JunitTestSuite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package unittests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
FIFOQTest.class,
LIFOQTest.class
})
public class JunitTestSuite {
}
Loading

0 comments on commit 553e754

Please sign in to comment.