1 /*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
40 * need to be performed on many implementations, but the onus on
41 * writing the tests falls on the engineer introducing the new
42 * implementation.
43 *
44 * The idea of this mega-test is that:
45 *
46 * An engineer adding a new collection implementation could simply add
47 * their new implementation to a list of implementations in this
48 * test's main method. Any general purpose Collection<Integer> or
49 * Map<Integer,Integer> class is appropriate.
50 *
51 * An engineer fixing a regression could add their regression test here and
52 * simultaneously test all other implementations.
53 */
54
55 import java.io.*;
56 import java.util.*;
57 import java.util.concurrent.*;
58 import static java.util.Collections.*;
59 import java.lang.reflect.*;
60
61 public class MOAT {
62 // Collections under test must not be initialized to contain this value,
63 // and maps under test must not contain this value as a key.
64 // It's used as a sentinel for absent-element testing.
65 static final int ABSENT_VALUE = 778347983;
66
67 static final Integer[] integerArray;
68 static {
69 Integer[] ia = new Integer[20];
70 // fill with 1..20 inclusive
71 for (int i = 0; i < ia.length; i++) {
72 ia[i] = i + 1;
73 }
74 integerArray = ia;
75 }
76
77 public static void realMain(String[] args) {
78
79 testCollection(new NewAbstractCollection<Integer>());
213 testListMutatorsAlwaysThrow(List.of());
214 testEmptyListMutatorsAlwaysThrow(List.of());
215 for (List<Integer> list : Arrays.asList(
216 List.<Integer>of(),
217 List.of(1),
218 List.of(1, 2),
219 List.of(1, 2, 3),
220 List.of(1, 2, 3, 4),
221 List.of(1, 2, 3, 4, 5),
222 List.of(1, 2, 3, 4, 5, 6),
223 List.of(1, 2, 3, 4, 5, 6, 7),
224 List.of(1, 2, 3, 4, 5, 6, 7, 8),
225 List.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
226 List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
227 List.of(integerArray))) {
228 testCollection(list);
229 testImmutableList(list);
230 testListMutatorsAlwaysThrow(list);
231 }
232
233 // Immutable Set
234 testEmptySet(Set.of());
235 testCollMutatorsAlwaysThrow(Set.of());
236 testEmptyCollMutatorsAlwaysThrow(Set.of());
237 for (Set<Integer> set : Arrays.asList(
238 Set.<Integer>of(),
239 Set.of(1),
240 Set.of(1, 2),
241 Set.of(1, 2, 3),
242 Set.of(1, 2, 3, 4),
243 Set.of(1, 2, 3, 4, 5),
244 Set.of(1, 2, 3, 4, 5, 6),
245 Set.of(1, 2, 3, 4, 5, 6, 7),
246 Set.of(1, 2, 3, 4, 5, 6, 7, 8),
247 Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
248 Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
249 Set.of(integerArray))) {
250 testCollection(set);
251 testImmutableSet(set);
252 testCollMutatorsAlwaysThrow(set);
253 }
254
255 // Immutable Map
256
257 @SuppressWarnings("unchecked")
258 Map.Entry<Integer,Integer>[] ea = (Map.Entry<Integer,Integer>[])new Map.Entry<?,?>[20];
259 for (int i = 0; i < ea.length; i++) {
260 ea[i] = Map.entry(i+1, i+101);
261 }
262
263 testEmptyMap(Map.of());
264 testMapMutatorsAlwaysThrow(Map.of());
265 testEmptyMapMutatorsAlwaysThrow(Map.of());
266 for (Map<Integer,Integer> map : Arrays.asList(
267 Map.<Integer,Integer>of(),
268 Map.of(1, 101),
269 Map.of(1, 101, 2, 202),
270 Map.of(1, 101, 2, 202, 3, 303),
271 Map.of(1, 101, 2, 202, 3, 303, 4, 404),
272 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505),
273 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606),
274 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707),
275 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808),
276 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909),
277 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909, 10, 1010),
278 Map.ofEntries(ea))) {
279 testMap(map);
280 testImmutableMap(map);
281 testMapMutatorsAlwaysThrow(map);
282 }
283 }
284
285 private static void checkContainsSelf(Collection<Integer> c) {
286 check(c.containsAll(c));
287 check(c.containsAll(Arrays.asList(c.toArray())));
288 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
289 }
290
291 private static void checkContainsEmpty(Collection<Integer> c) {
292 check(c.containsAll(new ArrayList<Integer>()));
293 }
294
295 private static void checkUnique(Set<Integer> s) {
296 for (Integer i : s) {
297 int count = 0;
298 for (Integer j : s) {
299 if (Objects.equals(i,j))
300 ++count;
301 }
302 check(count == 1);
|
1 /*
2 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
40 * need to be performed on many implementations, but the onus on
41 * writing the tests falls on the engineer introducing the new
42 * implementation.
43 *
44 * The idea of this mega-test is that:
45 *
46 * An engineer adding a new collection implementation could simply add
47 * their new implementation to a list of implementations in this
48 * test's main method. Any general purpose Collection<Integer> or
49 * Map<Integer,Integer> class is appropriate.
50 *
51 * An engineer fixing a regression could add their regression test here and
52 * simultaneously test all other implementations.
53 */
54
55 import java.io.*;
56 import java.util.*;
57 import java.util.concurrent.*;
58 import static java.util.Collections.*;
59 import java.lang.reflect.*;
60 import java.util.stream.Collectors;
61 import java.util.stream.Stream;
62
63 public class MOAT {
64 // Collections under test must not be initialized to contain this value,
65 // and maps under test must not contain this value as a key.
66 // It's used as a sentinel for absent-element testing.
67 static final int ABSENT_VALUE = 778347983;
68
69 static final Integer[] integerArray;
70 static {
71 Integer[] ia = new Integer[20];
72 // fill with 1..20 inclusive
73 for (int i = 0; i < ia.length; i++) {
74 ia[i] = i + 1;
75 }
76 integerArray = ia;
77 }
78
79 public static void realMain(String[] args) {
80
81 testCollection(new NewAbstractCollection<Integer>());
215 testListMutatorsAlwaysThrow(List.of());
216 testEmptyListMutatorsAlwaysThrow(List.of());
217 for (List<Integer> list : Arrays.asList(
218 List.<Integer>of(),
219 List.of(1),
220 List.of(1, 2),
221 List.of(1, 2, 3),
222 List.of(1, 2, 3, 4),
223 List.of(1, 2, 3, 4, 5),
224 List.of(1, 2, 3, 4, 5, 6),
225 List.of(1, 2, 3, 4, 5, 6, 7),
226 List.of(1, 2, 3, 4, 5, 6, 7, 8),
227 List.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
228 List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
229 List.of(integerArray))) {
230 testCollection(list);
231 testImmutableList(list);
232 testListMutatorsAlwaysThrow(list);
233 }
234
235 List<Integer> listCopy = List.copyOf(Arrays.asList(1, 2, 3));
236 testCollection(listCopy);
237 testImmutableList(listCopy);
238 testListMutatorsAlwaysThrow(listCopy);
239
240 List<Integer> listCollected = Stream.of(1, 2, 3).collect(Collectors.toUnmodifiableList());
241 equal(listCollected, List.of(1, 2, 3));
242 testCollection(listCollected);
243 testImmutableList(listCollected);
244 testListMutatorsAlwaysThrow(listCollected);
245
246 // Immutable Set
247 testEmptySet(Set.of());
248 testCollMutatorsAlwaysThrow(Set.of());
249 testEmptyCollMutatorsAlwaysThrow(Set.of());
250 for (Set<Integer> set : Arrays.asList(
251 Set.<Integer>of(),
252 Set.of(1),
253 Set.of(1, 2),
254 Set.of(1, 2, 3),
255 Set.of(1, 2, 3, 4),
256 Set.of(1, 2, 3, 4, 5),
257 Set.of(1, 2, 3, 4, 5, 6),
258 Set.of(1, 2, 3, 4, 5, 6, 7),
259 Set.of(1, 2, 3, 4, 5, 6, 7, 8),
260 Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9),
261 Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
262 Set.of(integerArray))) {
263 testCollection(set);
264 testImmutableSet(set);
265 testCollMutatorsAlwaysThrow(set);
266 }
267
268 Set<Integer> setCopy = Set.copyOf(Arrays.asList(1, 2, 3));
269 testCollection(setCopy);
270 testImmutableSet(setCopy);
271 testCollMutatorsAlwaysThrow(setCopy);
272
273 Set<Integer> setCollected = Stream.of(1, 1, 2, 3, 2, 3)
274 .collect(Collectors.toUnmodifiableSet());
275 equal(setCollected, Set.of(1, 2, 3));
276 testCollection(setCollected);
277 testImmutableSet(setCollected);
278 testCollMutatorsAlwaysThrow(setCollected);
279
280 // Immutable Map
281
282 @SuppressWarnings("unchecked")
283 Map.Entry<Integer,Integer>[] ea = (Map.Entry<Integer,Integer>[])new Map.Entry<?,?>[20];
284 for (int i = 0; i < ea.length; i++) {
285 ea[i] = Map.entry(i+1, i+101);
286 }
287
288 testEmptyMap(Map.of());
289 testMapMutatorsAlwaysThrow(Map.of());
290 testEmptyMapMutatorsAlwaysThrow(Map.of());
291 for (Map<Integer,Integer> map : Arrays.asList(
292 Map.<Integer,Integer>of(),
293 Map.of(1, 101),
294 Map.of(1, 101, 2, 202),
295 Map.of(1, 101, 2, 202, 3, 303),
296 Map.of(1, 101, 2, 202, 3, 303, 4, 404),
297 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505),
298 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606),
299 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707),
300 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808),
301 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909),
302 Map.of(1, 101, 2, 202, 3, 303, 4, 404, 5, 505, 6, 606, 7, 707, 8, 808, 9, 909, 10, 1010),
303 Map.ofEntries(ea))) {
304 testMap(map);
305 testImmutableMap(map);
306 testMapMutatorsAlwaysThrow(map);
307 }
308
309 Map<Integer,Integer> mapCopy = Map.copyOf(new HashMap<>(Map.of(1, 101, 2, 202, 3, 303)));
310 testMap(mapCopy);
311 testImmutableMap(mapCopy);
312 testMapMutatorsAlwaysThrow(mapCopy);
313
314 Map<Integer,Integer> mapCollected1 =
315 Stream.of(1, 2, 3)
316 .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
317 equal(mapCollected1, Map.of(1, 101, 2, 202, 3, 303));
318 testMap(mapCollected1);
319 testImmutableMap(mapCollected1);
320 testMapMutatorsAlwaysThrow(mapCollected1);
321
322 try {
323 Stream.of(1, 1, 2, 3, 2, 3)
324 .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i));
325 fail("duplicates should have thrown an exception");
326 } catch (IllegalStateException ise) {
327 pass();
328 }
329
330 Map<Integer,Integer> mapCollected2 =
331 Stream.of(1, 1, 2, 3, 2, 3)
332 .collect(Collectors.toUnmodifiableMap(i -> i, i -> 101 * i, Integer::sum));
333 equal(mapCollected2, Map.of(1, 202, 2, 404, 3, 606));
334 testMap(mapCollected2);
335 testImmutableMap(mapCollected2);
336 testMapMutatorsAlwaysThrow(mapCollected2);
337 }
338
339 private static void checkContainsSelf(Collection<Integer> c) {
340 check(c.containsAll(c));
341 check(c.containsAll(Arrays.asList(c.toArray())));
342 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
343 }
344
345 private static void checkContainsEmpty(Collection<Integer> c) {
346 check(c.containsAll(new ArrayList<Integer>()));
347 }
348
349 private static void checkUnique(Set<Integer> s) {
350 for (Integer i : s) {
351 int count = 0;
352 for (Integer j : s) {
353 if (Objects.equals(i,j))
354 ++count;
355 }
356 check(count == 1);
|