diff --git a/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java b/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java index 2d6fae9b2ed85a3e9edbbf77259b01559fd1a4d8..93534150154c78e18648ebae639f180bdfa0d88f 100644 --- a/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java +++ b/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java @@ -15,17 +15,19 @@ import org.junit.jupiter.params.provider.ValueSource; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; import static edu.caltech.cs2.project03.Project03TestOrdering.*; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.jupiter.api.Assertions.assertEquals; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @Tag("C") public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { - private static String ARRAY_DEQUE_SOURCE = "src/edu/caltech/cs2/datastructures/ArrayDeque.java"; + private static String ARRAY_DEQUE_SOURCE ="src/edu/caltech/cs2/datastructures/ArrayDeque.java"; private Constructor arrayDequeConstructor = Reflection.getConstructor(ArrayDeque.class); @@ -42,7 +44,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { } public IQueue<Object> newQueue() { - return Reflection.newInstance(arrayDequeConstructor); + return Reflection.newInstance(arrayDequeConstructor); } public IQueue<Object> newQueue(int size) { @@ -104,8 +106,22 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @DisplayName("The public interface is correct") @Test public void testPublicInterface() { - Reflection.assertPublicInterface(ArrayDeque.class, List.of("addFront", "addBack", "removeFront", "removeBack", - "enqueue", "dequeue", "push", "pop", "peek", "peekFront", "peekBack", "iterator", "size", "toString")); + Reflection.assertPublicInterface(ArrayDeque.class, List.of( + "addFront", + "addBack", + "removeFront", + "removeBack", + "enqueue", + "dequeue", + "push", + "pop", + "peek", + "peekFront", + "peekBack", + "iterator", + "size", + "toString" + )); } @Order(classSpecificTestLevel) @@ -115,6 +131,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { Inspection.assertConstructorHygiene(ARRAY_DEQUE_SOURCE); } + // TOSTRING TESTS --------------------------------------------------- @Order(toStringTestLevel) @@ -127,7 +144,9 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @Order(toStringTestLevel) @DisplayName("toString() matches java.util.ArrayDeque") @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" }) + @ValueSource(strings = { + "0, 1, 2, 3", "5, 4, 3, 2, 1", "8, 3, 5, 7, 4, 3, 12, 12, 1" + }) public void testToString(String inputs) { java.util.ArrayDeque<String> reference = new java.util.ArrayDeque<String>(); edu.caltech.cs2.datastructures.ArrayDeque<String> me = new edu.caltech.cs2.datastructures.ArrayDeque<>(); @@ -142,6 +161,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("addFront() and removeFront() take linear time") + @Timeout(value = 20, unit = SECONDS) @Test() public void testFrontDequeOperationComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -155,12 +175,12 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { Consumer<IDeque<Integer>> removeFront = (IDeque<Integer> q) -> q.removeFront(); RuntimeInstrumentation.assertAtMost("addFront", RuntimeInstrumentation.ComplexityType.LINEAR, provide, addFront, 8); - RuntimeInstrumentation.assertAtMost("removeFront", RuntimeInstrumentation.ComplexityType.LINEAR, provide, - removeFront, 8); + RuntimeInstrumentation.assertAtMost("removeFront", RuntimeInstrumentation.ComplexityType.LINEAR, provide, removeFront, 8); } @Order(complexityTestLevel) @DisplayName("addBack() and removeBack() take linear time") + @Timeout(value = 20, unit = SECONDS) @Test public void testBackDequeOperationComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -174,12 +194,12 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { Consumer<IDeque<Integer>> removeBack = (IDeque<Integer> q) -> q.removeBack(); RuntimeInstrumentation.assertAtMost("addBack", RuntimeInstrumentation.ComplexityType.LINEAR, provide, addBack, 8); - RuntimeInstrumentation.assertAtMost("removeBack", RuntimeInstrumentation.ComplexityType.LINEAR, provide, removeBack, - 8); + RuntimeInstrumentation.assertAtMost("removeBack", RuntimeInstrumentation.ComplexityType.LINEAR, provide, removeBack, 8); } @Order(complexityTestLevel) @DisplayName("enqueue() and dequeue() take linear time") + @Timeout(value = 20, unit = SECONDS) @Test public void testQueueOperationComplexity() { Function<Integer, IQueue<Integer>> provide = (Integer numElements) -> { @@ -198,6 +218,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("push() and pop() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testStackOperationComplexity() { Function<Integer, IStack<Integer>> provide = (Integer numElements) -> { @@ -216,6 +237,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("peek() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testPeekComplexity() { Function<Integer, IStack<Integer>> provide = (Integer numElements) -> { @@ -232,6 +254,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("peekFront() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test() public void testPeekFrontComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -243,12 +266,12 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { }; Consumer<IDeque<Integer>> peekFront = (IDeque<Integer> q) -> q.peekFront(); - RuntimeInstrumentation.assertAtMost("peekFront", RuntimeInstrumentation.ComplexityType.CONSTANT, provide, peekFront, - 8); + RuntimeInstrumentation.assertAtMost("peekFront", RuntimeInstrumentation.ComplexityType.CONSTANT, provide, peekFront, 8); } @Order(complexityTestLevel) @DisplayName("peekBack() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testPeekBackComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -260,8 +283,7 @@ public class ArrayDequeTests implements DequeTests, StackTests, QueueTests { }; Consumer<IDeque<Integer>> peekBack = (IDeque<Integer> q) -> q.peekBack(); - RuntimeInstrumentation.assertAtMost("peekBack", RuntimeInstrumentation.ComplexityType.CONSTANT, provide, peekBack, - 8); + RuntimeInstrumentation.assertAtMost("peekBack", RuntimeInstrumentation.ComplexityType.CONSTANT, provide, peekBack, 8); } - + } \ No newline at end of file diff --git a/tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java b/tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java index d1127e60ac02e23c924db79d3312ce0a013f5d3f..c1ca90d88c626ead24b918f843850fecec74af22 100644 --- a/tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java +++ b/tests/edu/caltech/cs2/datastructures/CircularArrayFixedSizeQueueTests.java @@ -5,7 +5,6 @@ import edu.caltech.cs2.helpers.Reflection; import edu.caltech.cs2.helpers.RuntimeInstrumentation; import edu.caltech.cs2.interfaces.IFixedSizeQueue; import edu.caltech.cs2.interfaces.IQueue; -import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -17,6 +16,7 @@ import java.util.function.Consumer; import java.util.function.Function; import static edu.caltech.cs2.project03.Project03TestOrdering.*; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.jupiter.api.Assertions.*; @Tag("B") @@ -129,6 +129,7 @@ public class CircularArrayFixedSizeQueueTests implements FixedSizeQueueTests { @Order(complexityTestLevel) @DisplayName("enqueue() and dequeue() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test() public void testQueueOperationComplexity() { Function<Integer, IFixedSizeQueue<Integer>> provide = (Integer numElements) -> { @@ -148,6 +149,7 @@ public class CircularArrayFixedSizeQueueTests implements FixedSizeQueueTests { @Order(complexityTestLevel) @DisplayName("peek() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test() public void testPeekComplexity() { Function<Integer, IFixedSizeQueue<Integer>> provide = (Integer numElements) -> { @@ -164,8 +166,8 @@ public class CircularArrayFixedSizeQueueTests implements FixedSizeQueueTests { } @Order(fixedSizeQueueLevel) - @DisplayName("Test iterator matches reference") - @ParameterizedTest(name = "Test iterator with {1} random values with seed = {0} and fixed array size = {2}") + @DisplayName("Test iterator matches reference for wraparound values") + @ParameterizedTest(name = "Test iterator and wraparound behavior with {1} random values with seed = {0} and fixed array size = {2}") @CsvSource({ "69, 200, 20", "21, 300, 200" }) public void testWrapAround(int seed, int numVals, int queueSize) { Random r = new Random(seed); @@ -188,5 +190,5 @@ public class CircularArrayFixedSizeQueueTests implements FixedSizeQueueTests { assertIterableEquals(reference, me, "Reference and implemented queues are not equal"); } } - + } diff --git a/tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java b/tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java index f9898b5019e7d578c206ded4c646b90cc4e896d4..40961504a967197edf47cd69a78d77dd7861366a 100644 --- a/tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java +++ b/tests/edu/caltech/cs2/datastructures/LinkedDequeTests.java @@ -21,6 +21,7 @@ import java.util.function.Consumer; import java.util.function.Function; import static edu.caltech.cs2.project03.Project03TestOrdering.*; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -152,6 +153,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("addFront() and removeFront() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testFrontDequeOperationComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -172,6 +174,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("addBack() and removeBack() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testBackDequeOperationComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -191,6 +194,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("enqueue() and dequeue() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testQueueOperationComplexity() { Function<Integer, IQueue<Integer>> provide = (Integer numElements) -> { @@ -209,6 +213,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("push() and pop() take constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testStackOperationComplexity() { Function<Integer, IStack<Integer>> provide = (Integer numElements) -> { @@ -227,6 +232,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("peek() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testPeekComplexity() { Function<Integer, IStack<Integer>> provide = (Integer numElements) -> { @@ -243,6 +249,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("peekFront() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testPeekFrontComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -260,6 +267,7 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { @Order(complexityTestLevel) @DisplayName("peekBack() takes constant time") + @Timeout(value = 10, unit = SECONDS) @Test public void testPeekBackComplexity() { Function<Integer, IDeque<Integer>> provide = (Integer numElements) -> { @@ -276,8 +284,8 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { } @Order(dequeTestLevel) - @DisplayName("Cycle detection for addFront(...) and addBack(...)") - @ParameterizedTest(name = "Test cycles {1} random numbers with seed = {0}") + @DisplayName("Cycle detection for addFront(...), addBack(...), removeFront(...), and removeBack(...)") + @ParameterizedTest(name = "Test cycles - {1} random numbers with seed = {0}") @CsvSource({ "69, 2000", "20, 3000" }) public void checkForCycles(int seed, int size) { Random r = new Random(seed); @@ -311,8 +319,8 @@ public class LinkedDequeTests implements DequeTests, StackTests, QueueTests { } @Order(dequeTestLevel) - @DisplayName("Check reverses for addFront(...) and addBack(...)") - @ParameterizedTest(name = "Test reverse {1} random numbers with seed = {0}") + @DisplayName("Check reverses for addFront(...), addBack(...), removeFront(...), and removeBack(...)") + @ParameterizedTest(name = "Test reverse - {1} random numbers with seed = {0}") @CsvSource({ "31, 2000", "64, 3000" }) public void checkReverses(int seed, int size) { Random r = new Random(seed);