Commit d845bb4b authored by Adam Blank's avatar Adam Blank
Browse files

Merge branch 'fixes' into 'master'

Fixes

See merge request !1
Pipeline #76080 failed with stage
in 0 seconds
Showing with 313 additions and 179 deletions
+313 -179
package edu.caltech.cs2.helpers;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestHint {
String value();
}
package edu.caltech.cs2.interfaces;
import edu.caltech.cs2.helpers.DependsOn;
import edu.caltech.cs2.helpers.TestDescription;
import edu.caltech.cs2.interfaces.ICollection;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
......@@ -23,6 +26,8 @@ public interface ICollectionTests {
@Order(collectionTestLevel)
@DisplayName("Simple tests of various ICollection functions")
@ParameterizedTest(name = "Test add(), size(), isEmpty(), contains(), and clear() on [{arguments}]")
@DependsOn({"constructor", "iterator", "size", "addBack"})
@TestDescription("This test checks the built-in functions we gave you by using your iterator and size methods.")
@ValueSource(strings = {
"",
"1",
......@@ -70,6 +75,8 @@ public interface ICollectionTests {
@Order(collectionTestLevel)
@Test
@DisplayName("Test repeated emptying and filling of ICollection with single element")
@DependsOn({"constructor", "size", "addBack", "removeBack"})
@TestDescription("This test repeatedly adds and clears the data structure.")
default void testFillEmptyCollection() {
ICollection<Object> impl = newCollection();
for (int i = 0; i < 10; i ++) {
......@@ -83,6 +90,8 @@ public interface ICollectionTests {
@Order(collectionTestLevel)
@DisplayName("Stress test for add(...)")
@ParameterizedTest(name = "Test add()ing {1} random numbers with seed = {0}")
@DependsOn({"constructor", "size", "addBack", "iterator"})
@TestDescription("This test adds a ton of random numbers to your data structure and checks that they're all actually in there.")
@CsvSource({
"100, 3000", "42, 1000"
})
......@@ -108,6 +117,8 @@ public interface ICollectionTests {
@Order(collectionTestLevel)
@DisplayName("Stress test for contains(...)")
@ParameterizedTest(name = "Test contains() with {1} random numbers and seed = {0}")
@DependsOn({"constructor", "size", "addBack", "iterator"})
@TestDescription("This test adds a ton of random numbers to your data structure and checks that they're all actually in there.")
@CsvSource({
"100, 3000", "42, 1000"
})
......
package edu.caltech.cs2.interfaces;
import edu.caltech.cs2.helpers.DependsOn;
import edu.caltech.cs2.helpers.Reflection;
import edu.caltech.cs2.helpers.TestDescription;
import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.IsEmptyIterable;
import org.junit.jupiter.api.DisplayName;
......@@ -18,12 +21,14 @@ import java.util.Random;
import static edu.caltech.cs2.project03.Project03TestOrdering.*;
import static org.junit.jupiter.api.Assertions.*;
public interface IDequeTests extends ICollectionTests {
public interface IDequeTests {
IDeque<Object> newDeque();
@Order(dequeTestLevel)
@DisplayName("Test Deque Iterator")
@ParameterizedTest(name = "Test deque iterator on [{arguments}]")
@DependsOn({"constructor", "iterator", "size", "addBack", "removeBack"})
@TestDescription("This test checks that your iterator works on various sizes of data structures.")
@ValueSource(strings = {
"",
"1",
......@@ -51,6 +56,8 @@ public interface IDequeTests extends ICollectionTests {
@Test
@Order(dequeTestLevel)
@DependsOn({"constructor", "iterator", "size", "addFront", "removeFront"})
@TestDescription("This test checks that small deques work correctly when added at the front and removed at the front.")
@DisplayName("Test Deque addFront / removeFront edge cases")
default void testRepeatedAddFrontRemoveFront() {
ArrayDeque<Object> ref = new ArrayDeque<>();
......@@ -74,6 +81,8 @@ public interface IDequeTests extends ICollectionTests {
@Test
@Order(dequeTestLevel)
@DisplayName("Test Deque addFront / removeBack edge cases")
@DependsOn({"constructor", "iterator", "size", "addFront", "removeBack"})
@TestDescription("This test checks that small deques work correctly when added at the front and removed at the back.")
default void testRepeatedAddFrontRemoveBack() {
ArrayDeque<Object> ref = new ArrayDeque<>();
IDeque<Object> impl = newDeque();
......@@ -96,6 +105,8 @@ public interface IDequeTests extends ICollectionTests {
@Test
@Order(dequeTestLevel)
@DisplayName("Test Deque addBack / removeFront edge cases")
@DependsOn({"constructor", "iterator", "size", "addBack", "removeFront"})
@TestDescription("This test checks that small deques work correctly when added at the back and removed at the front.")
default void testRepeatedAddBackRemoveFront() {
ArrayDeque<Object> ref = new ArrayDeque<>();
IDeque<Object> impl = newDeque();
......@@ -117,6 +128,8 @@ public interface IDequeTests extends ICollectionTests {
@Test
@Order(dequeTestLevel)
@DependsOn({"constructor", "iterator", "size", "addBack", "removeBack"})
@TestDescription("This test checks that small deques work correctly when added at the back and removed at the back.")
@DisplayName("Test Deque addBack / removeBack edge cases")
default void testRepeatedAddBackRemoveBack() {
ArrayDeque<Object> ref = new ArrayDeque<>();
......@@ -139,6 +152,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Stress test for addFront(...) and peekFront(...)")
@ParameterizedTest(name = "Test addFront()ing {1} random numbers with seed = {0}")
@DependsOn({"constructor", "iterator", "size", "peekFront", "addFront", "toString"})
@TestDescription("This test checks uses large amounts of random data to test that your adds work correctly.")
@CsvSource({
"100, 300", "42, 500"
})
......@@ -161,6 +176,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Stress test for addBack(...) and peekBack(...)")
@DependsOn({"constructor", "iterator", "size", "peekBack", "addBack", "toString"})
@TestDescription("This test checks uses large amounts of random data to test that your adds work correctly.")
@ParameterizedTest(name = "Test addBack()ing {1} random numbers with seed = {0}")
@CsvSource({
"100, 300", "42, 500"
......@@ -185,6 +202,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Stress test for removeFront(...)")
@ParameterizedTest(name = "Test removeFront()ing {1} random numbers with seed = {0}")
@DependsOn({"constructor", "iterator", "size", "peekFront", "addFront", "removeFront", "toString"})
@TestDescription("This test checks uses large amounts of random data to test that your adds and removes work correctly.")
@CsvSource({
"101, 300", "45, 500"
})
......@@ -211,6 +230,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Stress test for removeBack(...)")
@ParameterizedTest(name = "Test removeBack()ing {1} random numbers with seed = {0}")
@DependsOn({"constructor", "iterator", "size", "peekBack", "addBack", "removeBack", "toString"})
@TestDescription("This test checks uses large amounts of random data to test that your adds and removes work correctly.")
@CsvSource({
"101, 300", "45, 500"
})
......@@ -237,6 +258,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Stress test full IDeque")
@ParameterizedTest(name = "Test all IDeque methods {1} random numbers with seed = {0}")
@DependsOn({"everything"})
@TestDescription("This test checks uses large amounts of random data to test that all your methods in the deque work correctly.")
@CsvSource({
"102, 300", "52, 500"
})
......@@ -278,6 +301,8 @@ public interface IDequeTests extends ICollectionTests {
@Order(dequeTestLevel)
@DisplayName("Test for addAll(...)")
@ParameterizedTest(name = "Test addAll with {1} random numbers and seed = {0}")
@DependsOn({"constructor", "iterator", "addBack", "toString"})
@TestDescription("This test checks that the built-in addAll method works using your implementation of the iterator and addBack.")
@CsvSource({
"99, 300", "48, 500"
})
......@@ -295,4 +320,21 @@ public interface IDequeTests extends ICollectionTests {
MatcherAssert.assertThat("IDeque has incorrect elements / order", impl, IsIterableContainingInOrder.contains(expected));
}
@Order(toStringTestLevel)
@DisplayName("toString() matches java.util.ArrayDeque")
@DependsOn({"fields", "constructors", "toString", "addBack"})
@ParameterizedTest(name = "Test toString() on [{arguments}]")
@ValueSource(strings = {
"0, 1, 2, 3", "5, 4, 3, 2, 1", "8, 3, 5, 7, 4, 3, 12, 12, 1"
})
default void testToString(String inputs) {
java.util.ArrayDeque<String> reference = new java.util.ArrayDeque<String>();
edu.caltech.cs2.interfaces.IDeque<Object> me = newDeque();
for (String value : inputs.trim().split(", ")) {
assertEquals(reference.toString(), me.toString(), "toString outputs should be the same");
reference.addLast(value);
me.addBack(value);
}
}
}
......@@ -2,6 +2,7 @@ package edu.caltech.cs2.interfaces;
import edu.caltech.cs2.datastructures.CircularArrayFixedSizeQueue;
import edu.caltech.cs2.helpers.Reflection;
import edu.caltech.cs2.helpers.TestHint;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
......@@ -19,6 +20,7 @@ public interface IFixedSizeQueueTests extends IQueueTests {
@Order(fixedSizeQueueLevel)
@DisplayName("Overflow test for enqueue(...)")
@TestHint("This test is likely failing because you don't have correct wrap-around behavior.\n Remember that everything should advance; so, it's possible that your start wraps around.")
@ParameterizedTest(name = "Test randomly enqueue()ing/dequeue()ing {1} random numbers with seed = {0} and fixed array size = {2}")
@CsvSource({
"97, 3000, 100", "38, 5000, 10"
......
package edu.caltech.cs2.interfaces;
import edu.caltech.cs2.helpers.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import java.util.List;
import static edu.caltech.cs2.project03.Project03TestOrdering.classSpecificTestLevel;
public interface IStyleTests {
String getSource();
Class<?> getClazz();
List<String> getPublicInterface();
int getMaxFields();
@Order(classSpecificTestLevel)
@DisplayName("The overall number of fields is small")
@TestHint("It's good style to use the smallest number of fields that you can in each class.")
@Test
default void testSmallNumberOfFields() {
Reflection.assertFieldsLessThan(getClazz(), "private", getMaxFields());
}
@Order(classSpecificTestLevel)
@DisplayName("The public interface is correct")
@TestDescription("This test checks that you haven't declared any extra public methods.")
@TestHint("You are welcome to make any extra methods you want, but they have to be private!")
@Test
default void testPublicInterface() {
Reflection.assertPublicInterface(getClazz(), getPublicInterface());
}
@Order(classSpecificTestLevel)
@DisplayName("Does not use or import disallowed classes")
@TestHint("Remember that you're not allowed to use anything in java.util except Iterator and Random!")
@Test
default void testForInvalidClasses() {
List<String> regexps = List.of("java\\.util\\.(?!Iterator|Random|function.Function)", "java\\.lang\\.reflect", "java\\.io");
Inspection.assertNoImportsOf(getSource(), regexps);
Inspection.assertNoUsageOf(getSource(), regexps);
}
@Order(classSpecificTestLevel)
@DisplayName("Does not attempt to get around constructor counts")
@Test
default void testForAvoidCounters() {
List<String> regexps = List.of("NUM_CALLS");
Inspection.assertNoUsageOf(getSource(), regexps);
}
@Order(classSpecificTestLevel)
@DisplayName("Uses this(...) notation in all but one constructor")
@TestDescription("This test is checking that all of your constructors except one call the other constructors rather than duplicating code.")
@Test
default void testForThisConstructors() {
Inspection.assertConstructorHygiene(getSource());
}
@Order(classSpecificTestLevel)
@DisplayName("All fields have modifiers")
@TestDescription("This test checks that every field is private or public.")
@Test
default void testFieldModifiers() {
Reflection.assertFieldModifiers(getClazz());
}
@Order(classSpecificTestLevel)
@DisplayName("There are no public fields")
@TestHint("It's good style to never use public fields whenever avoidable.")
@Test
default void testNoPublicFields() {
Reflection.assertNoPublicFields(getClazz());
}
@Order(classSpecificTestLevel)
@DisplayName("There are no protected fields")
@TestHint("It's good style to never use protected fields whenever avoidable.")
@Test
default void testNoProtectedFields() {
Reflection.assertNoProtectedFields(getClazz());
}
}
\ No newline at end of file
package edu.caltech.cs2.project03;
import edu.caltech.cs2.datastructures.CircularArrayFixedSizeQueue;
import edu.caltech.cs2.helpers.Inspection;
import edu.caltech.cs2.helpers.Reflection;
import edu.caltech.cs2.helpers.TestDescription;
import edu.caltech.cs2.helpers.TestExtension;
import edu.caltech.cs2.helpers.TestHint;
import edu.caltech.cs2.interfaces.IFixedSizeQueue;
import edu.caltech.cs2.interfaces.IStyleTests;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.File;
import java.io.FileNotFoundException;
......@@ -17,7 +19,6 @@ import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.stream.Stream;
import java.util.function.Function;
import static edu.caltech.cs2.project03.Project03TestOrdering.*;
import static java.lang.Math.abs;
......@@ -26,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
@Tag("A")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ExtendWith(TestExtension.class)
public class GuitarStringTests {
private static String STRING_SOURCE = "src/edu/caltech/cs2/project03/CircularArrayFixedSizeQueueGuitarString.java";
......@@ -36,203 +38,180 @@ public class GuitarStringTests {
public static IFixedSizeQueue<Double> getQueueFromString(CircularArrayFixedSizeQueueGuitarString string) {
String queueName = Reflection.getFieldByType(CircularArrayFixedSizeQueueGuitarString.class, IFixedSizeQueue.class)
.getName();
.getName();
return Reflection.getFieldValue(CircularArrayFixedSizeQueueGuitarString.class, queueName, string);
}
@DisplayName("Style")
@Nested
class StyleTests implements IStyleTests {
@Order(classSpecificTestLevel)
@DisplayName("There are three static fields: the two double constants and a random value generator")
@TestDescription("This test is checking that you have three constant fields for the \"magic numbers\" and the Random instance.")
@Test
public void testStaticFields() {
Reflection.assertFieldsEqualTo(CircularArrayFixedSizeQueueGuitarString.class, "static", 3);
Stream<Field> fields = Reflection.getFields(CircularArrayFixedSizeQueueGuitarString.class);
fields.filter(Reflection.hasModifier("static")).forEach((field) -> {
Reflection.checkFieldModifiers(field, List.of("private", "static"));
assertTrue(Reflection.hasModifier("final").test(field) || field.getType().equals(Random.class),
"non-final static class must be a random value generator");
});
}
@Order(classSpecificTestLevel)
@DisplayName("Does not use or import disallowed classes")
@Test
public void testForInvalidClasses() {
List<String> regexps = List.of("java\\.util\\.(?!Random|function.Function)", "java\\.lang\\.reflect", "java\\.io");
Inspection.assertNoImportsOf(STRING_SOURCE, regexps);
Inspection.assertNoUsageOf(STRING_SOURCE, regexps);
}
@Order(classSpecificTestLevel)
@DisplayName("Uses this(...) notation in all but one constructor")
@Test
public void testForThisConstructors() {
Inspection.assertConstructorHygiene(STRING_SOURCE);
}
@Order(classSpecificTestLevel)
@DisplayName("There are three static fields: the two double constants and a random value generator")
@Test
public void testStaticFields() {
Reflection.assertFieldsEqualTo(CircularArrayFixedSizeQueueGuitarString.class, "static", 3);
Stream<Field> fields = Reflection.getFields(CircularArrayFixedSizeQueueGuitarString.class);
fields.filter(Reflection.hasModifier("static")).forEach((field) -> {
Reflection.checkFieldModifiers(field, List.of("private", "static"));
assertTrue(Reflection.hasModifier("final").test(field) || field.getType().equals(Random.class),
"non-final static class must be a random value generator");
});
}
@Order(classSpecificTestLevel)
@DisplayName("The overall number of fields is small")
@Test
public void testSmallNumberOfFields() {
Reflection.assertFieldsLessThan(CircularArrayFixedSizeQueueGuitarString.class, "private", 5);
}
@Order(classSpecificTestLevel)
@DisplayName("There are no public fields")
@Test
public void testNoPublicFields() {
Reflection.assertNoPublicFields(CircularArrayFixedSizeQueueGuitarString.class);
}
@Order(classSpecificTestLevel)
@DisplayName("There are no protected fields")
@Test
public void testNoProtectedFields() {
Reflection.assertNoProtectedFields(CircularArrayFixedSizeQueueGuitarString.class);
}
@Order(classSpecificTestLevel)
@DisplayName("All fields in CircularArrayFixedSizeQueueGuitarString have modifiers")
@Test
public void testFieldModifiers() {
Reflection.assertFieldModifiers(CircularArrayFixedSizeQueueGuitarString.class);
}
@Order(classSpecificTestLevel)
@DisplayName("The public interface is correct")
@Test
public void testPublicInterface() {
Reflection.assertPublicInterface(CircularArrayFixedSizeQueueGuitarString.class,
List.of("length", "pluck", "tic", "sample"));
}
public int getMaxFields() {
return 3;
}
@Order(classSpecificTestLevel)
@DisplayName("The constructor correctly sets up the queue")
@ParameterizedTest(name = "Test constructor with CircularArrayFixedSizeQueue and a frequency of {0} Hz; expected queue size is {1}")
@CsvSource({ "110, 401", "340, 130", "512, 87", "600.5, 74", "880, 51" })
public void testConstructor(double frequency, int expectedSize) {
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
assertEquals(expectedSize, queue.size(), "Queue size is not equal to expected size");
for (double val : queue) {
assertEquals(0, val, "All values in queue should be equal to 0");
public List<String> getPublicInterface() {
return List.of("length", "pluck", "tic", "sample");
}
}
@Order(guitarStringTestLevel)
@DisplayName("The pluck() method randomizes the values in the queue")
@ParameterizedTest(name = "Test pluck() with CircularArrayFixedSizeQueue and a frequency of {0} Hz")
@CsvSource({ "100", "50", "10", "8", "5" })
public void testPluck(double frequency) {
final double DELTA = 0.05;
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Checks all values are initially 0
for (double val : queue) {
assertEquals(0, val, "initial values must be 0");
@Override
public String getSource() {
return STRING_SOURCE;
}
string.pluck();
queue = getQueueFromString(string);
double sum = 0;
double absSum = 0;
for (double val : queue) {
sum += val;
absSum += abs(val);
@Override
public Class<?> getClazz() {
return CircularArrayFixedSizeQueueGuitarString.class;
}
assertEquals(0, sum / queue.size(), DELTA, "average value of uniform distribution should be near 0");
assertEquals(0.25, absSum / queue.size(), DELTA, "average magnitude of uniform distribution should be near 0.25");
}
@Order(guitarStringTestLevel)
@DisplayName("The tic() method correctly applies the Karplus-Strong algorithm")
@ParameterizedTest(name = "Test tic() with CircularArrayFixedSizeQueue and a frequency of {0} Hz; data file {1}.txt")
@CsvSource({ "10000, ticStates1", "8000, ticStates2", "5000, ticStates3" })
public void testTic(double frequency, String filename) {
// Set up scanner
String filepath = "tests/data/" + filename + ".txt";
Scanner in;
try {
in = new Scanner(new File(filepath));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(filepath + " is not a valid trace file.");
@DisplayName("Implementation Tests")
@Nested
class ImplementationTests {
@Order(classSpecificTestLevel)
@DisplayName("The constructor correctly sets up the queue")
@TestHint("Make sure that your queue is the correct size according to the specification and filled with zeroes.")
@ParameterizedTest(name = "Test constructor with CircularArrayFixedSizeQueue and a frequency of {0} Hz; expected queue size is {1}")
@CsvSource({"110, 401", "340, 130", "512, 87", "600.5, 74", "880, 51"})
public void testConstructor(double frequency, int expectedSize) {
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
assertEquals(expectedSize, queue.size(), "Queue size is not equal to expected size");
for (double val : queue) {
assertEquals(0, val, "All values in queue should be equal to 0");
}
}
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Reinitialize queue with new data
for (int i = 0; i < queue.size(); i++) {
queue.dequeue();
queue.enqueue(in.nextDouble());
}
int initSize = queue.size();
// Pass through the same number of tics as elements in the array
for (int i = 0; i < initSize; i++) {
string.tic();
@Order(guitarStringTestLevel)
@DisplayName("The pluck() method randomizes the values in the queue")
@ParameterizedTest(name = "Test pluck() with CircularArrayFixedSizeQueue and a frequency of {0} Hz")
@TestHint("Make sure that you're centering your distribution around the correct number.")
@CsvSource({"100", "50", "10", "8", "5"})
public void testPluck(double frequency) {
final double DELTA = 0.05;
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Checks all values are initially 0
for (double val : queue) {
assertEquals(0, val, "initial values must be 0");
}
string.pluck();
queue = getQueueFromString(string);
assertEquals(initSize, queue.size(), "queue size must remain the same");
double sum = 0;
double absSum = 0;
for (double val : queue) {
sum += val;
absSum += abs(val);
}
assertEquals(0, sum / queue.size(), DELTA, "average value of uniform distribution should be near 0");
assertEquals(0.25, absSum / queue.size(), DELTA, "average magnitude of uniform distribution should be near 0.25");
}
// Compare peek() values with the expected values in the files
while (in.hasNext()) {
string.tic();
queue = getQueueFromString(string);
assertEquals(initSize, queue.size(), "queue size must remain the same");
assertEquals(in.nextDouble(), queue.peek(), "next expected value not at front of queue");
@Order(guitarStringTestLevel)
@DisplayName("The tic() method correctly applies the Karplus-Strong algorithm")
@ParameterizedTest(name = "Test tic() with CircularArrayFixedSizeQueue and a frequency of {0} Hz; data file {1}.txt")
@CsvSource({"10000, ticStates1", "8000, ticStates2", "5000, ticStates3"})
public void testTic(double frequency, String filename) {
// Set up scanner
String filepath = "tests/data/" + filename + ".txt";
Scanner in;
try {
in = new Scanner(new File(filepath));
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(filepath + " is not a valid trace file.");
}
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Reinitialize queue with new data
for (int i = 0; i < queue.size(); i++) {
queue.dequeue();
queue.enqueue(in.nextDouble());
}
int initSize = queue.size();
// Pass through the same number of tics as elements in the array
for (int i = 0; i < initSize; i++) {
string.tic();
queue = getQueueFromString(string);
assertEquals(initSize, queue.size(), "queue size must remain the same");
}
// Compare peek() values with the expected values in the files
while (in.hasNext()) {
string.tic();
queue = getQueueFromString(string);
assertEquals(initSize, queue.size(), "queue size must remain the same");
assertEquals(in.nextDouble(), queue.peek(), "next expected value not at front of queue");
}
}
}
@Order(guitarStringTestLevel)
@DisplayName("The length() method correctly gives the length of the queue")
@ParameterizedTest(name = "Test length() with CircularArrayFixedSizeQueue and a frequency of {0} Hz; expected length = {1}; iterations = {2}")
@CsvSource({ "110, 401, 1000", "340, 130, 500", "512, 87, 200", "600.5, 74, 150", "880, 51, 100" })
public void testLength(double frequency, int expectedLength, int iterations) {
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Pluck and make sure length doesn't change
int initSize = queue.size();
assertEquals(expectedLength, string.length(), "Length should be same as expected");
assertEquals(queue.size(), string.length(), "Length should be same as queue size");
string.pluck();
queue = getQueueFromString(string);
assertEquals(initSize, string.length(), "Length should not have changed from beginning");
assertEquals(queue.size(), string.length(), "Length should be same as queue size");
// Run through many iterations, making sure both the queue size and length are
// constant
for (int i = 0; i < iterations; i++) {
string.tic();
@Order(guitarStringTestLevel)
@DisplayName("The length() method correctly gives the length of the queue")
@ParameterizedTest(name = "Test length() with CircularArrayFixedSizeQueue and a frequency of {0} Hz; expected length = {1}; iterations = {2}")
@CsvSource({"110, 401, 1000", "340, 130, 500", "512, 87, 200", "600.5, 74, 150", "880, 51, 100"})
public void testLength(double frequency, int expectedLength, int iterations) {
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Pluck and make sure length doesn't change
int initSize = queue.size();
assertEquals(expectedLength, string.length(), "Length should be same as expected");
assertEquals(queue.size(), string.length(), "Length should be same as queue size");
string.pluck();
queue = getQueueFromString(string);
assertEquals(initSize, string.length(), "Length should not have changed from beginning");
assertEquals(queue.size(), string.length(), "Length should be same as queue size");
}
}
// Run through many iterations, making sure both the queue size and length are
// constant
for (int i = 0; i < iterations; i++) {
string.tic();
queue = getQueueFromString(string);
assertEquals(initSize, string.length(), "Length should not have changed from beginning");
assertEquals(queue.size(), string.length(), "Length should be same as queue size");
}
}
@Order(guitarStringTestLevel)
@DisplayName("The sample() method gives the same values as peek()ing the queue")
@ParameterizedTest(name = "Test sample() with CircularArrayFixedSizeQueue and a frequency of {0} Hz")
@CsvSource({ "110, 1000", "340, 500", "512, 200", "600.5, 150", "880, 100" })
public void testSample(double frequency, int iterations) {
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Pluck and make sure initial samples are correct
assertEquals(0, string.sample(), "Sample should return 0 before plucking");
assertEquals(queue.peek(), string.sample(), "Sample should same as peek()ing queue");
string.pluck();
queue = getQueueFromString(string);
assertEquals(queue.peek(), string.sample(), "Sample should same as peek()ing queue");
// Run through many iterations, making sure sample() matches peek()
for (int i = 0; i < iterations; i++) {
string.tic();
@Order(guitarStringTestLevel)
@DisplayName("The sample() method gives the same values as peek()ing the queue")
@ParameterizedTest(name = "Test sample() with CircularArrayFixedSizeQueue and a frequency of {0} Hz")
@CsvSource({"110, 1000", "340, 500", "512, 200", "600.5, 150", "880, 100"})
public void testSample(double frequency, int iterations) {
// Set up class and retrieve queue
CircularArrayFixedSizeQueueGuitarString string = constructGuitarString(frequency);
IFixedSizeQueue<Double> queue = getQueueFromString(string);
// Pluck and make sure initial samples are correct
assertEquals(0, string.sample(), "Sample should return 0 before plucking");
assertEquals(queue.peek(), string.sample(), "Sample should same as peek()ing queue");
string.pluck();
queue = getQueueFromString(string);
assertEquals(queue.peek(), string.sample(), "Sample should same as peek()ing queue");
}
// Run through many iterations, making sure sample() matches peek()
for (int i = 0; i < iterations; i++) {
string.tic();
queue = getQueueFromString(string);
assertEquals(queue.peek(), string.sample(), "Sample should same as peek()ing queue");
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment