-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gavin Li
committed
Jan 31, 2015
1 parent
e0cb0f4
commit 553e754
Showing
14 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
Oops, something went wrong.