diff --git a/MerchantRPGCSE2102/.classpath b/MerchantRPGCSE2102/.classpath index 91ee9a5..e1badea 100644 --- a/MerchantRPGCSE2102/.classpath +++ b/MerchantRPGCSE2102/.classpath @@ -2,5 +2,7 @@ + + diff --git a/MerchantRPGCSE2102/hamcrest-core-1.3.jar b/MerchantRPGCSE2102/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/MerchantRPGCSE2102/hamcrest-core-1.3.jar differ diff --git a/MerchantRPGCSE2102/junit-4.11.jar b/MerchantRPGCSE2102/junit-4.11.jar new file mode 100644 index 0000000..aaf7444 Binary files /dev/null and b/MerchantRPGCSE2102/junit-4.11.jar differ diff --git a/MerchantRPGCSE2102/src/queues/EmptyQueueException.java b/MerchantRPGCSE2102/src/queues/EmptyQueueException.java new file mode 100644 index 0000000..15d8b40 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/EmptyQueueException.java @@ -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); + } +} diff --git a/MerchantRPGCSE2102/src/queues/FIFOQ.java b/MerchantRPGCSE2102/src/queues/FIFOQ.java new file mode 100644 index 0000000..e2636f6 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/FIFOQ.java @@ -0,0 +1,38 @@ +/** + * + */ +package queues; + +/** + * @author tms08012 + * + */ +public interface FIFOQ { + /** + * 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; +} diff --git a/MerchantRPGCSE2102/src/queues/FIFOQImpl.java b/MerchantRPGCSE2102/src/queues/FIFOQImpl.java new file mode 100644 index 0000000..6c5d811 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/FIFOQImpl.java @@ -0,0 +1,46 @@ +package queues; + +import java.util.LinkedList; + +public class FIFOQImpl {//This instance of a queue is implemented with a java.util.LinkedList + + LinkedList _llist; + int _capacity; + int _nContents; + + public FIFOQImpl(){ + _llist = new LinkedList(); + _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; + } +} diff --git a/MerchantRPGCSE2102/src/queues/LIFOQ.java b/MerchantRPGCSE2102/src/queues/LIFOQ.java new file mode 100644 index 0000000..a265336 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/LIFOQ.java @@ -0,0 +1,38 @@ +/** + * + */ +package queues; + +/** + * @author tms08012 + * + */ +public interface LIFOQ { + /** + * 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; +} diff --git a/MerchantRPGCSE2102/src/queues/LIFOQImpl.java b/MerchantRPGCSE2102/src/queues/LIFOQImpl.java new file mode 100644 index 0000000..0a92c96 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/LIFOQImpl.java @@ -0,0 +1,64 @@ +package queues; + +import java.util.LinkedList; + +public class LIFOQImpl implements LIFOQ{//This instance of a queue is implemented with a java.util.LinkedList + + LinkedList _llist; + int _capacity; + int _nContents; + + public LIFOQImpl(){ + _llist = new LinkedList(); + _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; + } +} diff --git a/MerchantRPGCSE2102/src/queues/QueueMain.java b/MerchantRPGCSE2102/src/queues/QueueMain.java new file mode 100644 index 0000000..2c44c06 --- /dev/null +++ b/MerchantRPGCSE2102/src/queues/QueueMain.java @@ -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(); + + + } + +} diff --git a/MerchantRPGCSE2102/src/unittests/FIFOQTest.java b/MerchantRPGCSE2102/src/unittests/FIFOQTest.java new file mode 100644 index 0000000..b21597f --- /dev/null +++ b/MerchantRPGCSE2102/src/unittests/FIFOQTest.java @@ -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 _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(); + _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 = 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(); + _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(); + _mq.flush(); + boolean empty = false; + try{ + empty = (null ==_mq.dequeue()); + }catch(Exception e){ + System.out.println("FIFOQTest::testFailedDequeue: caught expected exception "+e); + } + + } + + +} diff --git a/MerchantRPGCSE2102/src/unittests/JunitTestSuite.java b/MerchantRPGCSE2102/src/unittests/JunitTestSuite.java new file mode 100644 index 0000000..743bb24 --- /dev/null +++ b/MerchantRPGCSE2102/src/unittests/JunitTestSuite.java @@ -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 { +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/unittests/LIFOQTest.java b/MerchantRPGCSE2102/src/unittests/LIFOQTest.java new file mode 100644 index 0000000..31d5e8b --- /dev/null +++ b/MerchantRPGCSE2102/src/unittests/LIFOQTest.java @@ -0,0 +1,105 @@ +package unittests; + +import org.junit.Test; + +import queues.EmptyQueueException; +import queues.FIFOQImpl; +import queues.LIFOQImpl; +import junit.framework.TestCase; + + +public class LIFOQTest extends TestCase { + + private int _value1; + private int _value2; + private LIFOQImpl _ms; + private int _capacity; + + public LIFOQTest(String testName) { + super(testName); + } + + protected void setUp() throws Exception { + super.setUp(); + System.out.println("LIFOQTest::setUp "); + _value1 = 3; + _value2 = 5; + } + + protected void tearDown() throws Exception { + super.tearDown(); + System.out.println("LIFOQTest::tearDown "); + _value1 = 0; + _value2 = 0; + } + + @Test + public void testPush() { + _ms = new LIFOQImpl(); + _ms.setCapacity(30); + int capacity = _ms.getCapacity(); + assertEquals(30,capacity); + boolean success = false;//this is not arbitrary, if the loop never runs violates assertion + for (int p = 0; p(); + _capacity = 30; + _ms.setCapacity(_capacity); + boolean success = true;//this is not arbitrary, if the loop never runs, violates assertion + for (int p = 0; p<_capacity; p++){ + success = _ms.push(_value1); + assertEquals(success, true); + } + success = _ms.push(_value1); + assertEquals(success, false); + } + + @Test + public void testPop() throws EmptyQueueException { + _ms = new LIFOQImpl(); + _capacity = 30; + _ms.setCapacity(_capacity); + for (int p = 1; p<_capacity+1; p++){ + boolean success = _ms.push(p);//first fill it up + assertEquals(success, true); + } + for (int p = _capacity; p>0; p--){ + int q = (int) _ms.pop(); //then empty it out + assertEquals(q, p); + } + } + + @Test + public void testFailedPop() throws EmptyQueueException { + _ms = new LIFOQImpl(); + int capacity = _ms.getCapacity(); + for (int p = 0; p<_capacity; p++){ + boolean success = _ms.push(p);//first fill it up + assertEquals(success, true); + } + for (int p = _capacity-1; p>0; p--){ + try{ + int q = (int) _ms.pop();//then empty it out + assertEquals(q, p); + }catch(Exception e){ + System.out.println("LIFOQTest::testFailedPop: caught unexpected exception "+e + p); + } + } + boolean empty = false; + try{ + empty = (null ==_ms.pop()); + }catch(Exception e){ + System.out.println("LIFOQTest::testFailedPop: caught expected exception "+e); + } + + } + + +} diff --git a/MerchantRPGCSE2102/src/unittests/Template4Test.java b/MerchantRPGCSE2102/src/unittests/Template4Test.java new file mode 100644 index 0000000..7160f92 --- /dev/null +++ b/MerchantRPGCSE2102/src/unittests/Template4Test.java @@ -0,0 +1,28 @@ +package unittests; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.Ignore; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +//Do not use any classes in junit.framework or junit.extensions + +/** + * Tests for {@link Foo}. + * + * @author user@example.com (John Doe) + */ +public class Template4Test { +//Usually, tests with JUnit4 do not need to extend anything + + @Test + public void thisAlwaysPasses() { + + } + + @Test + @Ignore + public void thisIsIgnored() { + } +} \ No newline at end of file diff --git a/MerchantRPGCSE2102/src/unittests/TestRunner.java b/MerchantRPGCSE2102/src/unittests/TestRunner.java new file mode 100644 index 0000000..a6917f5 --- /dev/null +++ b/MerchantRPGCSE2102/src/unittests/TestRunner.java @@ -0,0 +1,16 @@ +package unittests; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class TestRunner { + public static void main(String[] args) { + Result result = JUnitCore.runClasses(JunitTestSuite.class); + for (Failure failure : result.getFailures()) { + System.out.println("TestRunner:: had failure "+failure.toString()); + } + System.out.println("TestRunner:: successful = "+result.wasSuccessful()); + } +} +