ICollectionTests.java 5.14 KB
Newer Older
1
package edu.caltech.cs2.interfaces;
Adam Blank's avatar
Adam Blank committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

import edu.caltech.cs2.interfaces.ICollection;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collections;
import java.util.List;
import java.util.Random;

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

20
public interface ICollectionTests {
Adam Blank's avatar
Adam Blank committed
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    ICollection<Object> newCollection();

    @Order(collectionTestLevel)
    @DisplayName("Simple tests of various ICollection functions")
    @ParameterizedTest(name = "Test add(), size(), isEmpty(), contains(), and clear() on [{arguments}]")
    @ValueSource(strings = {
            "",
            "1",
            "0, 1, 2, 3",
            "5, 4, 3, 2, 1",
            "8, 3, 5, 7, 4, 3, 12, 12, 1"
    })
    default void testCollectionFunctions(String inputs) {
        ICollection<Object> impl = newCollection();
        List<Object> reference = new java.util.ArrayList<>();
        // Check that collection is empty
        assertTrue(impl.isEmpty(), "collection should be empty");

        // Check that values are not in collection
        for (Object value : inputs.trim().split(", ")) {
            assertFalse(impl.contains(value), "value should not be contained");
        }

        // Add all values to collection
        for (Object value : inputs.trim().split(", ")) {
            impl.add(value);
            reference.add(value);
        }

        // Check that size() and isEmpty() is correct
        assertEquals(reference.size(), impl.size(), "sizes should be equal");
        assertFalse(impl.isEmpty(), "collection should not be empty");

        // Check that values are in collection
        for (Object value : inputs.trim().split(", ")) {
            assertTrue(impl.contains(value), "value should be contained");
        }

        // Clear and make sure size() and isEmpty() match
        impl.clear();
        assertEquals(0, impl.size(), "size should be 0");
        assertTrue(impl.isEmpty(), "collection should be empty");

        // Check that values are not in collection
        for (Object value : inputs.trim().split(", ")) {
            assertFalse(impl.contains(value), "value should not be contained");
        }
    }

    @Order(collectionTestLevel)
    @Test
    @DisplayName("Test repeated emptying and filling of ICollection with single element")
    default void testFillEmptyCollection() {
        ICollection<Object> impl = newCollection();
        for (int i = 0; i < 10; i ++) {
            impl.add("a");
77
            assertEquals(1, impl.size(), "collection should have 1 element");
Adam Blank's avatar
Adam Blank committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
            impl.clear();
            assertTrue(impl.isEmpty());
        }
    }

    @Order(collectionTestLevel)
    @DisplayName("Stress test for add(...)")
    @ParameterizedTest(name = "Test add()ing {1} random numbers with seed = {0}")
    @CsvSource({
            "100, 3000", "42, 1000"
    })
    default void stressTestAdd(int seed, int size) {
        Random r = new Random(seed);
        List<Integer> reference = new java.util.ArrayList<>();
        ICollection<Object> impl = newCollection();
        // Test adding values updates size and displays contained correctly
        for (int i = 0; i < size; i++) {
            int num = r.nextInt();
            reference.add(num);
            impl.add(num);
            assertEquals(reference.size(), impl.size(), "size()s are not equal");
            assertEquals(reference.contains(num), impl.contains(num), "value should be contained");
        }
        // Test that values not in collection are not contained
        for (int i = 0; i < size; i++) {
            int num = r.nextInt();
            assertEquals(reference.contains(num), impl.contains(num), "contained values do not match");
        }
    }

    @Order(collectionTestLevel)
    @DisplayName("Stress test for contains(...)")
    @ParameterizedTest(name = "Test contains() with {1} random numbers and seed = {0}")
    @CsvSource({
            "100, 3000", "42, 1000"
    })
    default void stressTestContains(int seed, int size) {
        Random r = new Random(seed);
        List<Integer> nums = new java.util.ArrayList<>();
        ICollection<Object> impl = newCollection();
        // Add values to both the list of nums and test collection
        for (int i = 0; i < size; i++) {
            int num = r.nextInt();
            nums.add(num);
            impl.add(num);
        }
        // Shuffle order of nums and check that all are contained in the collection
        Collections.shuffle(nums);
        for (int num : nums) {
127
            assertTrue(impl.contains(num), "value should be contained");
Adam Blank's avatar
Adam Blank committed
128
129
130
131
132
133
134
135
136
        }

        // Test that values not in collection are not contained
        for (int i = 0; i < size; i++) {
            int num = r.nextInt();
            assertEquals(nums.contains(num), impl.contains(num), "contained values do not match");
        }
    }
}