IFixedSizeQueueTests.java 2.63 KB
Newer Older
1
package edu.caltech.cs2.interfaces;
Adam Blank's avatar
Adam Blank committed
2

3
import edu.caltech.cs2.datastructures.CircularArrayFixedSizeQueue;
Adam Blank's avatar
Adam Blank committed
4
5
6
7
8
9
10
11
12
13
14
15
16
import edu.caltech.cs2.helpers.Reflection;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.lang.reflect.Constructor;
import java.util.Queue;
import java.util.Random;

import static edu.caltech.cs2.project03.Project03TestOrdering.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

17
public interface IFixedSizeQueueTests extends IQueueTests {
Adam Blank's avatar
Adam Blank committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    IFixedSizeQueue<Object> newFixedSizeQueue(int capacity);

    @Order(fixedSizeQueueLevel)
    @DisplayName("Overflow test for enqueue(...)")
    @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"
    })
    default void overflowTestEnqueue(int seed, int numVals, int queueSize) {
        Random r = new Random(seed);
        Constructor c = Reflection.getConstructor(CircularArrayFixedSizeQueue.class, int.class);
        IFixedSizeQueue<Object> me = newFixedSizeQueue(queueSize);
        Queue<Object> reference = new java.util.ArrayDeque<>();
        assertEquals(queueSize, me.capacity(), "capacity does not match expected value");
        int count = 0;
        for (int i = 0; i < numVals; i++) {
            int num = r.nextInt();
            // Check that we get the expected value from enqueue when it has a risk of overflowing
            if (count < queueSize) {
                assertEquals(false, me.isFull(), "queue should not be full");
                assertEquals(true, me.enqueue(num), "enqueue should be successful");
                reference.add(num);
                count++;
            }
            else {
                assertEquals(true, me.isFull(), "queue should be full");
                assertEquals(false, me.enqueue(num), "enqueue should have failed");
            }

            // Standard checks to make sure peeks() and dequeues() match up
            assertEquals(reference.peek(), me.peek(),"return values of peek()s are not equal");
            if (r.nextBoolean()) {
                assertEquals(reference.remove(), me.dequeue(),"return values of dequeue()s are not equal");
                assertEquals(reference.peek(), me.peek(),"return values of peek()s are not equal");
                count--;
            }
            assertEquals(reference.size(), me.size(), "size()s are not equal");
            assertEquals(queueSize, me.capacity(), "capacity does not match expected value");
        }
    }
}