1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
jsr166 |
1.6 |
import java.util.ArrayList; |
9 |
jsr166 |
1.1 |
import java.util.Collection; |
10 |
jsr166 |
1.2 |
import java.util.Deque; |
11 |
|
|
import java.util.NoSuchElementException; |
12 |
|
|
import java.util.Queue; |
13 |
jsr166 |
1.6 |
import java.util.concurrent.ThreadLocalRandom; |
14 |
|
|
import java.util.concurrent.atomic.AtomicReference; |
15 |
jsr166 |
1.3 |
import java.util.function.Consumer; |
16 |
jsr166 |
1.6 |
import java.util.function.Predicate; |
17 |
jsr166 |
1.1 |
|
18 |
|
|
import junit.framework.Test; |
19 |
|
|
|
20 |
|
|
/** |
21 |
|
|
* Contains tests applicable to all Collection implementations. |
22 |
|
|
*/ |
23 |
|
|
public class CollectionTest extends JSR166TestCase { |
24 |
|
|
final CollectionImplementation impl; |
25 |
|
|
|
26 |
|
|
/** Tests are parameterized by a Collection implementation. */ |
27 |
|
|
CollectionTest(CollectionImplementation impl, String methodName) { |
28 |
|
|
super(methodName); |
29 |
|
|
this.impl = impl; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
public static Test testSuite(CollectionImplementation impl) { |
33 |
|
|
return newTestSuite |
34 |
|
|
(parameterizedTestSuite(CollectionTest.class, |
35 |
|
|
CollectionImplementation.class, |
36 |
|
|
impl), |
37 |
|
|
jdk8ParameterizedTestSuite(CollectionTest.class, |
38 |
|
|
CollectionImplementation.class, |
39 |
|
|
impl)); |
40 |
|
|
} |
41 |
|
|
|
42 |
jsr166 |
1.2 |
/** Checks properties of empty collections. */ |
43 |
jsr166 |
1.1 |
public void testEmptyMeansEmpty() { |
44 |
jsr166 |
1.2 |
Collection c = impl.emptyCollection(); |
45 |
|
|
assertTrue(c.isEmpty()); |
46 |
|
|
assertEquals(0, c.size()); |
47 |
|
|
assertEquals("[]", c.toString()); |
48 |
jsr166 |
1.8 |
{ |
49 |
|
|
Object[] a = c.toArray(); |
50 |
|
|
assertEquals(0, a.length); |
51 |
|
|
assertSame(Object[].class, a.getClass()); |
52 |
|
|
} |
53 |
jsr166 |
1.2 |
{ |
54 |
|
|
Object[] a = new Object[0]; |
55 |
|
|
assertSame(a, c.toArray(a)); |
56 |
|
|
} |
57 |
jsr166 |
1.8 |
{ |
58 |
|
|
Integer[] a = new Integer[0]; |
59 |
|
|
assertSame(a, c.toArray(a)); |
60 |
|
|
} |
61 |
|
|
{ |
62 |
|
|
Integer[] a = { 1, 2, 3}; |
63 |
|
|
assertSame(a, c.toArray(a)); |
64 |
|
|
assertNull(a[0]); |
65 |
|
|
assertSame(2, a[1]); |
66 |
|
|
assertSame(3, a[2]); |
67 |
|
|
} |
68 |
jsr166 |
1.5 |
assertIteratorExhausted(c.iterator()); |
69 |
jsr166 |
1.3 |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
70 |
|
|
c.forEach(alwaysThrows); |
71 |
|
|
c.iterator().forEachRemaining(alwaysThrows); |
72 |
|
|
c.spliterator().forEachRemaining(alwaysThrows); |
73 |
|
|
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
74 |
jsr166 |
1.2 |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
75 |
|
|
Queue q = (Queue) c; |
76 |
|
|
assertNull(q.peek()); |
77 |
|
|
assertNull(q.poll()); |
78 |
|
|
} |
79 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
80 |
|
|
Deque d = (Deque) c; |
81 |
|
|
assertNull(d.peekFirst()); |
82 |
|
|
assertNull(d.peekLast()); |
83 |
|
|
assertNull(d.pollFirst()); |
84 |
|
|
assertNull(d.pollLast()); |
85 |
jsr166 |
1.5 |
assertIteratorExhausted(d.descendingIterator()); |
86 |
jsr166 |
1.2 |
} |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
public void testNullPointerExceptions() { |
90 |
|
|
Collection c = impl.emptyCollection(); |
91 |
|
|
assertThrows( |
92 |
|
|
NullPointerException.class, |
93 |
|
|
() -> c.addAll(null), |
94 |
|
|
() -> c.containsAll(null), |
95 |
|
|
() -> c.retainAll(null), |
96 |
|
|
() -> c.removeAll(null), |
97 |
jsr166 |
1.8 |
() -> c.removeIf(null), |
98 |
|
|
() -> c.toArray(null)); |
99 |
jsr166 |
1.2 |
|
100 |
|
|
if (!impl.permitsNulls()) { |
101 |
|
|
assertThrows( |
102 |
|
|
NullPointerException.class, |
103 |
|
|
() -> c.add(null)); |
104 |
|
|
} |
105 |
|
|
if (!impl.permitsNulls() |
106 |
|
|
&& Queue.class.isAssignableFrom(impl.klazz())) { |
107 |
|
|
Queue q = (Queue) c; |
108 |
|
|
assertThrows( |
109 |
|
|
NullPointerException.class, |
110 |
|
|
() -> q.offer(null)); |
111 |
|
|
} |
112 |
|
|
if (!impl.permitsNulls() |
113 |
|
|
&& Deque.class.isAssignableFrom(impl.klazz())) { |
114 |
|
|
Deque d = (Deque) c; |
115 |
|
|
assertThrows( |
116 |
|
|
NullPointerException.class, |
117 |
|
|
() -> d.addFirst(null), |
118 |
|
|
() -> d.addLast(null), |
119 |
|
|
() -> d.offerFirst(null), |
120 |
|
|
() -> d.offerLast(null), |
121 |
|
|
() -> d.push(null)); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
public void testNoSuchElementExceptions() { |
126 |
|
|
Collection c = impl.emptyCollection(); |
127 |
jsr166 |
1.3 |
assertThrows( |
128 |
|
|
NoSuchElementException.class, |
129 |
|
|
() -> c.iterator().next()); |
130 |
jsr166 |
1.2 |
|
131 |
|
|
if (Queue.class.isAssignableFrom(impl.klazz())) { |
132 |
|
|
Queue q = (Queue) c; |
133 |
|
|
assertThrows( |
134 |
|
|
NoSuchElementException.class, |
135 |
|
|
() -> q.element(), |
136 |
|
|
() -> q.remove()); |
137 |
|
|
} |
138 |
|
|
if (Deque.class.isAssignableFrom(impl.klazz())) { |
139 |
|
|
Deque d = (Deque) c; |
140 |
|
|
assertThrows( |
141 |
|
|
NoSuchElementException.class, |
142 |
|
|
() -> d.getFirst(), |
143 |
|
|
() -> d.getLast(), |
144 |
|
|
() -> d.removeFirst(), |
145 |
|
|
() -> d.removeLast(), |
146 |
jsr166 |
1.5 |
() -> d.pop(), |
147 |
|
|
() -> d.descendingIterator().next()); |
148 |
jsr166 |
1.2 |
} |
149 |
jsr166 |
1.1 |
} |
150 |
|
|
|
151 |
jsr166 |
1.6 |
public void testRemoveIf() { |
152 |
|
|
Collection c = impl.emptyCollection(); |
153 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
154 |
|
|
int n = rnd.nextInt(6); |
155 |
|
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
156 |
|
|
AtomicReference threwAt = new AtomicReference(null); |
157 |
|
|
ArrayList survivors = new ArrayList(c); |
158 |
|
|
ArrayList accepts = new ArrayList(); |
159 |
|
|
ArrayList rejects = new ArrayList(); |
160 |
|
|
Predicate randomPredicate = (e) -> { |
161 |
|
|
assertNull(threwAt.get()); |
162 |
|
|
switch (rnd.nextInt(3)) { |
163 |
|
|
case 0: accepts.add(e); return true; |
164 |
|
|
case 1: rejects.add(e); return false; |
165 |
|
|
case 2: threwAt.set(e); throw new ArithmeticException(); |
166 |
|
|
default: throw new AssertionError(); |
167 |
|
|
} |
168 |
|
|
}; |
169 |
|
|
try { |
170 |
|
|
boolean modified = c.removeIf(randomPredicate); |
171 |
|
|
if (!modified) { |
172 |
|
|
assertNull(threwAt.get()); |
173 |
|
|
assertEquals(n, rejects.size()); |
174 |
|
|
assertEquals(0, accepts.size()); |
175 |
|
|
} |
176 |
|
|
} catch (ArithmeticException ok) {} |
177 |
|
|
survivors.removeAll(accepts); |
178 |
jsr166 |
1.7 |
if (n - accepts.size() != c.size()) { |
179 |
|
|
System.err.println(impl.klazz()); |
180 |
|
|
System.err.println(c); |
181 |
|
|
System.err.println(accepts); |
182 |
|
|
System.err.println(rejects); |
183 |
|
|
System.err.println(survivors); |
184 |
|
|
System.err.println(threwAt.get()); |
185 |
|
|
} |
186 |
jsr166 |
1.6 |
assertEquals(n - accepts.size(), c.size()); |
187 |
|
|
assertTrue(c.containsAll(survivors)); |
188 |
|
|
assertTrue(survivors.containsAll(rejects)); |
189 |
|
|
for (Object x : accepts) assertFalse(c.contains(x)); |
190 |
|
|
if (threwAt.get() == null) |
191 |
|
|
assertEquals(accepts.size() + rejects.size(), n); |
192 |
|
|
} |
193 |
|
|
|
194 |
jsr166 |
1.4 |
// public void testCollectionDebugFail() { |
195 |
|
|
// fail(impl.klazz().getSimpleName()); |
196 |
|
|
// } |
197 |
jsr166 |
1.1 |
} |