From 2860e23ac636ea7acd779c1c98296dd7e54f3508 Mon Sep 17 00:00:00 2001 From: Ethan Ordentlich <eordentl@Caltech.edu> Date: Sun, 10 Jan 2021 13:23:05 -0800 Subject: [PATCH] Fix #4 Add test for array initial capacity --- .../cs2/datastructures/ArrayDequeTests.java | 38 ++++++++++++++++++- .../cs2/project03/Project03TestOrdering.java | 10 +++-- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java b/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java index ad87790..5ac09fb 100644 --- a/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java +++ b/tests/edu/caltech/cs2/datastructures/ArrayDequeTests.java @@ -17,7 +17,7 @@ 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; +import static org.junit.jupiter.api.Assertions.*; @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @Tag("C") @@ -126,6 +126,40 @@ public class ArrayDequeTests implements IDequeTests, IStackTests, IQueueTests { Inspection.assertConstructorHygiene(ARRAY_DEQUE_SOURCE); } + // ARRAYDEQUE TESTS --------------------------------------------------- + + @Order(implSpecificTestLevel) + @Test + public void testArrayDequeDefaultInitialCapacity() throws IllegalAccessException { + ArrayDeque<Integer> impl = new ArrayDeque<>(); + + // Reflect and get the backing array + Field arr = Reflection.getFieldByType(ArrayDeque.class, int[].class); + arr.setAccessible(true); + int[] backingArray = (int[]) arr.get(impl); + + assertEquals(10, backingArray.length, "Default initial capacity is not 10"); + } + + @Order(implSpecificTestLevel) + @DisplayName("enqueue should always succeed") + @Test + public void testThatArrayDequeEnqueueAlwaysSucceeds() { + ArrayDeque<Integer> impl = new ArrayDeque<>(); + for (int i = 0; i < 100; i ++) { + assertTrue(impl.enqueue(i), "enqueue should always succeed for ArrayDeque"); + } + } + + @Order(implSpecificTestLevel) + @DisplayName("push should always succeed") + @Test + public void testThatArrayDequePushAlwaysSucceeds() { + ArrayDeque<Integer> impl = new ArrayDeque<>(); + for (int i = 0; i < 100; i ++) { + assertTrue(impl.push(i), "push should always succeed for ArrayDeque"); + } + } // TOSTRING TESTS --------------------------------------------------- @@ -174,7 +208,7 @@ public class ArrayDequeTests implements IDequeTests, IStackTests, IQueueTests { } @Order(complexityTestLevel) - @DisplayName("addBack() and removeBack() take linear time") + @DisplayName("addBack() and removeBack() take constant time") @Timeout(value = 20, unit = SECONDS) @Test public void testBackDequeOperationComplexity() { diff --git a/tests/edu/caltech/cs2/project03/Project03TestOrdering.java b/tests/edu/caltech/cs2/project03/Project03TestOrdering.java index 1cbf631..86a4c34 100644 --- a/tests/edu/caltech/cs2/project03/Project03TestOrdering.java +++ b/tests/edu/caltech/cs2/project03/Project03TestOrdering.java @@ -5,6 +5,7 @@ public final class Project03TestOrdering { throw new InstantiationError("Class is only for storing constant variables"); } + // Tests related to class structure public static final int classSpecificTestLevel = 0; public static final int collectionTestLevel = 1; @@ -14,8 +15,11 @@ public final class Project03TestOrdering { public static final int fixedSizeQueueLevel = 3; - public static final int toStringTestLevel = 4; - public static final int complexityTestLevel = 5; + // Tests related to the particular implementation + public static final int implSpecificTestLevel = 4; - public static final int guitarStringTestLevel = 6; + public static final int toStringTestLevel = 5; + public static final int complexityTestLevel = 6; + + public static final int guitarStringTestLevel = 7; } -- GitLab