File: CollectionSupplier.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (250 lines) | stat: -rw-r--r-- 9,508 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.lang.Exception;
import java.lang.Integer;
import java.lang.Iterable;
import java.lang.Override;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.testng.TestException;

import static org.testng.Assert.assertTrue;

import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @library
 * @summary A Supplier of test cases for Collection tests
 */
public final class CollectionSupplier<C extends Collection<Integer>> implements Supplier<Iterable<CollectionSupplier.TestCase<C>>> {

    private final List<Function<Collection<Integer>, C>> suppliers;
    private final int size;

    /**
     * A Collection test case.
     */
    public static final class TestCase<C extends Collection<Integer>> {
        /**
         * The name of the test case.
         */
        public final String name;

        /**
         * The supplier of a collection
         */
        public Function<Collection<Integer>, C> supplier;

        /**
         * Unmodifiable reference collection, useful for comparisons.
         */
        public final List<Integer> expected;

        /**
         * A modifiable test collection.
         */
        public final C collection;

        /**
         * Create a Collection test case.
         *
         * @param name name of the test case
         * @param collection the modifiable test collection
         */
        public TestCase(String name, Function<Collection<Integer>, C> supplier, C collection) {
            this.name = name;
            this.supplier = supplier;
            this.expected = Collections.unmodifiableList(
                Arrays.asList(collection.toArray(new Integer[0])));
            this.collection = collection;
        }

        @Override
        public String toString() {
            return name + " " + collection.getClass().toString();
        }
    }

    /**
     * Shuffle a list using a PRNG with known seed for repeatability
     *
     * @param list the list to be shuffled
     */
    public static <E> void shuffle(final List<E> list) {
        // PRNG with known seed for repeatable tests
        final Random prng = new Random(13);
        final int size = list.size();
        for (int i = 0; i < size; i++) {
            // random index in interval [i, size)
            final int j = i + prng.nextInt(size - i);
            // swap elements at indices i & j
            final E e = list.get(i);
            list.set(i, list.get(j));
            list.set(j, e);
        }
    }

    /**
     * Create a {@code CollectionSupplier} that creates instances of specified
     * collection suppliers of the specified size.
     *
     * @param suppliers the suppliers names that supply {@code Collection}
     *        instances
     * @param size the desired size of each collection
     */
    public CollectionSupplier(List<Function<Collection<Integer>, C>> suppliers, int size) {
        this.suppliers = suppliers;
        this.size = size;
    }

    @Override
    public Iterable<TestCase<C>> get() {
        final Collection<TestCase<C>> cases = new LinkedList<>();
        for (final Function<Collection<Integer>, C> supplier : suppliers)
            try {
                cases.add(new TestCase<>("empty", supplier, supplier.apply(Collections.emptyList())));

                cases.add(new TestCase<>("single", supplier, supplier.apply(Arrays.asList(42))));

                final Collection<Integer> regular = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    regular.add(i);
                }
                cases.add(new TestCase<>("regular", supplier, supplier.apply(regular)));

                final Collection<Integer> reverse = new ArrayList<>();
                for (int i = size; i >= 0; i--) {
                    reverse.add(i);
                }
                cases.add(new TestCase<>("reverse", supplier, supplier.apply(reverse)));

                final Collection<Integer> odds = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    odds.add((i * 2) + 1);
                }
                cases.add(new TestCase<>("odds", supplier, supplier.apply(odds)));

                final Collection<Integer> evens = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    evens.add(i * 2);
                }
                cases.add(new TestCase<>("evens", supplier, supplier.apply(evens)));

                final Collection<Integer> fibonacci = new ArrayList<>();
                int prev2 = 0;
                int prev1 = 1;
                for (int i = 0; i < size; i++) {
                    final int n = prev1 + prev2;
                    if (n < 0) { // stop on overflow
                        break;
                    }
                    fibonacci.add(n);
                    prev2 = prev1;
                    prev1 = n;
                }
                cases.add(new TestCase<>("fibonacci", supplier, supplier.apply(fibonacci)));


                boolean isStructurallyModifiable = false;
                try {
                    C t = supplier.apply(Collections.emptyList());
                    t.add(1);
                    isStructurallyModifiable = true;
                } catch (UnsupportedOperationException e) { }

                if (!isStructurallyModifiable)
                    continue;


                // variants where the size of the backing storage != reported size
                // created by removing half of the elements
                final C emptyWithSlack = supplier.apply(Collections.emptyList());
                emptyWithSlack.add(42);
                assertTrue(emptyWithSlack.remove(42));
                cases.add(new TestCase<>("emptyWithSlack", supplier, emptyWithSlack));

                final C singleWithSlack = supplier.apply(Collections.emptyList());
                singleWithSlack.add(42);
                singleWithSlack.add(43);
                assertTrue(singleWithSlack.remove(43));
                cases.add(new TestCase<>("singleWithSlack", supplier, singleWithSlack));

                final C regularWithSlack = supplier.apply(Collections.emptyList());
                for (int i = 0; i < (2 * size); i++) {
                    regularWithSlack.add(i);
                }
                assertTrue(regularWithSlack.removeIf(x -> x < size));
                cases.add(new TestCase<>("regularWithSlack", supplier, regularWithSlack));

                final C reverseWithSlack = supplier.apply(Collections.emptyList());
                for (int i = 2 * size; i >= 0; i--) {
                    reverseWithSlack.add(i);
                }
                assertTrue(reverseWithSlack.removeIf(x -> x < size));
                cases.add(new TestCase<>("reverseWithSlack", supplier, reverseWithSlack));

                final C oddsWithSlack = supplier.apply(Collections.emptyList());
                for (int i = 0; i < 2 * size; i++) {
                    oddsWithSlack.add((i * 2) + 1);
                }
                assertTrue(oddsWithSlack.removeIf(x -> x >= size));
                cases.add(new TestCase<>("oddsWithSlack", supplier, oddsWithSlack));

                final C evensWithSlack = supplier.apply(Collections.emptyList());
                for (int i = 0; i < 2 * size; i++) {
                    evensWithSlack.add(i * 2);
                }
                assertTrue(evensWithSlack.removeIf(x -> x >= size));
                cases.add(new TestCase<>("evensWithSlack", supplier, evensWithSlack));

                final C fibonacciWithSlack = supplier.apply(Collections.emptyList());
                prev2 = 0;
                prev1 = 1;
                for (int i = 0; i < size; i++) {
                    final int n = prev1 + prev2;
                    if (n < 0) { // stop on overflow
                        break;
                    }
                    fibonacciWithSlack.add(n);
                    prev2 = prev1;
                    prev1 = n;
                }
                assertTrue(fibonacciWithSlack.removeIf(x -> x < 20));
                cases.add(new TestCase<>("fibonacciWithSlack", supplier, fibonacciWithSlack));
            }
            catch (Exception failed) {
                throw new TestException(failed);
            }

        return cases;
    }

}