-
Notifications
You must be signed in to change notification settings - Fork 88
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
Showing
8 changed files
with
368 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
target/ |
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,12 @@ | ||
# Testing Infrastructure in Java | ||
|
||
This repository is set up for testing with _JUnit_ and test coverage with _Jacoco_, both automated through _Maven_. | ||
|
||
You will likely want to explore IDE integration for test execution and coverage in your IDE, but also ensure that tests still execute with Maven in a Continuous Integration environment (e.g. github actions). | ||
|
||
The following commands might be useful: | ||
* `mvn clean` reset the build | ||
* `mvn test` execute tests and write a test coverage report in `target/site/jacoco/index.html` | ||
* `mvn site` build and test the project and write results in `target/site/index.html` (includes coverage and test results) | ||
|
||
See the comments in `pom.xml` for technical details of this setup. |
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,81 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.example</groupId> | ||
<artifactId>rec3</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- importing JUnit test framework --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- importing a jar file provided with this recitation (not usually needed) --> | ||
<dependency> | ||
<groupId>LinkedIntQueue</groupId> | ||
<artifactId>LinkedIntQueue</artifactId> | ||
<version>1.0</version> | ||
<scope>system</scope> | ||
<systemPath>${project.basedir}/lib/LinkedIntQueue.jar</systemPath> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<!-- adding and configuring a plugin to collect coverage information during test execution --> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<version>0.8.8</version> | ||
<executions> | ||
<execution> | ||
<id>prepare-agent</id> | ||
<goals> | ||
<goal>prepare-agent</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>report</id> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<!-- a plugin to support `mvn site` to generate a website summarizing the project --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<reporting> | ||
<plugins> | ||
<!-- a plugin to include test results in the `mvn site` report --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-report-plugin</artifactId> | ||
<version>2.22.2</version> | ||
</plugin> | ||
<!-- final plugin to include coverage information in the `mvn site` report --> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<version>0.8.8</version> | ||
</plugin> | ||
</plugins> | ||
</reporting> | ||
</project> |
108 changes: 108 additions & 0 deletions
108
Java/src/main/java/edu/cmu/cs/cs214/rec02/ArrayIntQueue.java
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,108 @@ | ||
package edu.cmu.cs.cs214.rec02; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* A resizable-array implementation of the {@link IntQueue} interface. The head of | ||
* the queue starts out at the head of the array, allowing the queue to grow and | ||
* shrink in constant time. | ||
* | ||
* TODO: This implementation contains three bugs! Use your tests to determine the | ||
* source of the bugs and correct them! | ||
* | ||
* @author Alex Lockwood | ||
* @author Ye Lu | ||
*/ | ||
public class ArrayIntQueue implements IntQueue { | ||
|
||
/** | ||
* An array holding this queue's data | ||
*/ | ||
private int[] elementData; | ||
|
||
/** | ||
* Index of the next dequeue-able value | ||
*/ | ||
private int head; | ||
|
||
/** | ||
* Current size of queue | ||
*/ | ||
private int size; | ||
|
||
/** | ||
* The initial size for new instances of ArrayQueue | ||
*/ | ||
private static final int INITIAL_SIZE = 10; | ||
|
||
/** | ||
* Constructs an empty queue with an initial capacity of ten. | ||
*/ | ||
public ArrayIntQueue() { | ||
elementData = new int[INITIAL_SIZE]; | ||
head = 0; | ||
size = 0; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public void clear() { | ||
Arrays.fill(elementData, 0); | ||
size = 0; | ||
head = 0; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public Integer dequeue() { | ||
if (isEmpty()) { | ||
return null; | ||
} | ||
Integer value = elementData[head]; | ||
head = (head + 1) % elementData.length; | ||
size--; | ||
return value; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public boolean enqueue(Integer value) { | ||
ensureCapacity(); | ||
int tail = (head + size) % elementData.length; | ||
elementData[tail] = value; | ||
size++; | ||
return true; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public boolean isEmpty() { | ||
return size >= 0; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public Integer peek() { | ||
return elementData[head]; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
public int size() { | ||
return size; | ||
} | ||
|
||
/** | ||
* Increases the capacity of this <tt>ArrayIntQueue</tt> instance, if | ||
* necessary, to ensure that it can hold at least size + 1 elements. | ||
*/ | ||
private void ensureCapacity() { | ||
if (size == elementData.length) { | ||
int oldCapacity = elementData.length; | ||
int newCapacity = 2 * oldCapacity + 1; | ||
int[] newData = new int[newCapacity]; | ||
for (int i = head; i < oldCapacity; i++) { | ||
newData[i - head] = elementData[i]; | ||
} | ||
for (int i = 0; i < head; i++) { | ||
newData[head - i] = elementData[i]; | ||
} | ||
elementData = newData; | ||
head = 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,55 @@ | ||
package edu.cmu.cs.cs214.rec02; | ||
|
||
/** | ||
* Interface describing a first-in, first-out structure for integers. Values are | ||
* added at the tail, and removed from the head. Queues are typically used to | ||
* process values in the order that they appear and to store the state of a | ||
* buffered object. | ||
* | ||
* @author Alex Lockwood | ||
*/ | ||
public interface IntQueue { | ||
|
||
/** | ||
* Remove all the elements from the queue. | ||
*/ | ||
void clear(); | ||
|
||
/** | ||
* Fetch and remove the element at the head of the queue. | ||
* | ||
* @return The element at the head of the queue. Returns | ||
* null if the queue is empty. | ||
*/ | ||
Integer dequeue(); | ||
|
||
/** | ||
* Add the element to the tail of the queue. | ||
* | ||
* @param value The element to place at the tail of the queue. | ||
* @return Whether the element was enqueued successfully. | ||
*/ | ||
boolean enqueue(Integer value); | ||
|
||
/** | ||
* Determine if the queue is empty. | ||
* | ||
* @return <tt>true</tt> if the queue is empty, <tt>false</tt> otherwise. | ||
*/ | ||
boolean isEmpty(); | ||
|
||
/** | ||
* Fetch the element at the head of the queue. | ||
* | ||
* @return The element at the head of the queue. Returns null if queue | ||
* is empty. | ||
*/ | ||
Integer peek(); | ||
|
||
/** | ||
* Determine the number of elements in the queue. | ||
* | ||
* @return The number of elements in the queue. | ||
*/ | ||
int size(); | ||
} |
109 changes: 109 additions & 0 deletions
109
Java/src/test/java/edu/cmu/cs/cs214/rec02/IntQueueTest.java
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,109 @@ | ||
package edu.cmu.cs.cs214.rec02; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
|
||
/** | ||
* TODO: | ||
* 1. The {@link LinkedIntQueue} has no bugs. We've provided you with some example test cases. | ||
* Write your own unit tests to test against IntQueue interface with specification testing method | ||
* using mQueue = new LinkedIntQueue(); | ||
* | ||
* 2. | ||
* Comment `mQueue = new LinkedIntQueue();` and uncomment `mQueue = new ArrayIntQueue();` | ||
* Use your test cases from part 1 to test ArrayIntQueue and find bugs in the {@link ArrayIntQueue} class | ||
* Write more unit tests to test the implementation of ArrayIntQueue, with structural testing method | ||
* Aim to achieve 100% line coverage for ArrayIntQueue | ||
* | ||
* @author Alex Lockwood, George Guo, Terry Li | ||
*/ | ||
public class IntQueueTest { | ||
|
||
private IntQueue mQueue; | ||
private List<Integer> testList; | ||
|
||
/** | ||
* Called before each test. | ||
*/ | ||
@Before | ||
public void setUp() { | ||
// comment/uncomment these lines to test each class | ||
mQueue = new LinkedIntQueue(); | ||
// mQueue = new ArrayIntQueue(); | ||
|
||
testList = new ArrayList<>(List.of(1, 2, 3)); | ||
} | ||
|
||
@Test | ||
public void testIsEmpty() { | ||
// This is an example unit test | ||
assertTrue(mQueue.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testNotEmpty() { | ||
// TODO: write your own unit test | ||
fail("Test not implemented"); | ||
} | ||
|
||
@Test | ||
public void testPeekEmptyQueue() { | ||
// TODO: write your own unit test | ||
fail("Test not implemented"); | ||
} | ||
|
||
@Test | ||
public void testPeekNoEmptyQueue() { | ||
// TODO: write your own unit test | ||
fail("Test not implemented"); | ||
} | ||
|
||
@Test | ||
public void testEnqueue() { | ||
// This is an example unit test | ||
for (int i = 0; i < testList.size(); i++) { | ||
mQueue.enqueue(testList.get(i)); | ||
assertEquals(testList.get(0), mQueue.peek()); | ||
assertEquals(i + 1, mQueue.size()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testDequeue() { | ||
// TODO: write your own unit test | ||
fail("Test not implemented"); | ||
} | ||
|
||
@Test | ||
public void testContent() throws IOException { | ||
// This is an example unit test | ||
InputStream in = new FileInputStream("src/test/resources/data.txt"); | ||
try (Scanner scanner = new Scanner(in)) { | ||
scanner.useDelimiter("\\s*fish\\s*"); | ||
|
||
List<Integer> correctResult = new ArrayList<>(); | ||
while (scanner.hasNextInt()) { | ||
int input = scanner.nextInt(); | ||
correctResult.add(input); | ||
System.out.println("enqueue: " + input); | ||
mQueue.enqueue(input); | ||
} | ||
|
||
for (Integer result : correctResult) { | ||
assertEquals(mQueue.dequeue(), result); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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 @@ | ||
1 fish 5 fish 2 fish 1 fish 4 fish |