5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
+ |
import java.util.ArrayList; |
9 |
|
import java.util.Collection; |
10 |
+ |
import java.util.Deque; |
11 |
+ |
import java.util.NoSuchElementException; |
12 |
+ |
import java.util.Queue; |
13 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
14 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
15 |
+ |
import java.util.function.Consumer; |
16 |
+ |
import java.util.function.Predicate; |
17 |
|
|
18 |
|
import junit.framework.Test; |
19 |
|
|
39 |
|
impl)); |
40 |
|
} |
41 |
|
|
42 |
< |
/** A test of the CollectionImplementation implementation ! */ |
42 |
> |
/** Checks properties of empty collections. */ |
43 |
|
public void testEmptyMeansEmpty() { |
44 |
< |
assertTrue(impl.emptyCollection().isEmpty()); |
45 |
< |
assertEquals(0, impl.emptyCollection().size()); |
44 |
> |
Collection c = impl.emptyCollection(); |
45 |
> |
assertTrue(c.isEmpty()); |
46 |
> |
assertEquals(0, c.size()); |
47 |
> |
assertEquals("[]", c.toString()); |
48 |
> |
assertEquals(0, c.toArray().length); |
49 |
> |
{ |
50 |
> |
Object[] a = new Object[0]; |
51 |
> |
assertSame(a, c.toArray(a)); |
52 |
> |
} |
53 |
> |
assertIteratorExhausted(c.iterator()); |
54 |
> |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
55 |
> |
c.forEach(alwaysThrows); |
56 |
> |
c.iterator().forEachRemaining(alwaysThrows); |
57 |
> |
c.spliterator().forEachRemaining(alwaysThrows); |
58 |
> |
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
59 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
60 |
> |
Queue q = (Queue) c; |
61 |
> |
assertNull(q.peek()); |
62 |
> |
assertNull(q.poll()); |
63 |
> |
} |
64 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
65 |
> |
Deque d = (Deque) c; |
66 |
> |
assertNull(d.peekFirst()); |
67 |
> |
assertNull(d.peekLast()); |
68 |
> |
assertNull(d.pollFirst()); |
69 |
> |
assertNull(d.pollLast()); |
70 |
> |
assertIteratorExhausted(d.descendingIterator()); |
71 |
> |
} |
72 |
|
} |
73 |
|
|
74 |
< |
// public void testCollectionDebugFail() { fail(); } |
74 |
> |
public void testNullPointerExceptions() { |
75 |
> |
Collection c = impl.emptyCollection(); |
76 |
> |
assertThrows( |
77 |
> |
NullPointerException.class, |
78 |
> |
() -> c.addAll(null), |
79 |
> |
() -> c.containsAll(null), |
80 |
> |
() -> c.retainAll(null), |
81 |
> |
() -> c.removeAll(null), |
82 |
> |
() -> c.removeIf(null)); |
83 |
> |
|
84 |
> |
if (!impl.permitsNulls()) { |
85 |
> |
assertThrows( |
86 |
> |
NullPointerException.class, |
87 |
> |
() -> c.add(null)); |
88 |
> |
} |
89 |
> |
if (!impl.permitsNulls() |
90 |
> |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
91 |
> |
Queue q = (Queue) c; |
92 |
> |
assertThrows( |
93 |
> |
NullPointerException.class, |
94 |
> |
() -> q.offer(null)); |
95 |
> |
} |
96 |
> |
if (!impl.permitsNulls() |
97 |
> |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
98 |
> |
Deque d = (Deque) c; |
99 |
> |
assertThrows( |
100 |
> |
NullPointerException.class, |
101 |
> |
() -> d.addFirst(null), |
102 |
> |
() -> d.addLast(null), |
103 |
> |
() -> d.offerFirst(null), |
104 |
> |
() -> d.offerLast(null), |
105 |
> |
() -> d.push(null)); |
106 |
> |
} |
107 |
> |
} |
108 |
> |
|
109 |
> |
public void testNoSuchElementExceptions() { |
110 |
> |
Collection c = impl.emptyCollection(); |
111 |
> |
assertThrows( |
112 |
> |
NoSuchElementException.class, |
113 |
> |
() -> c.iterator().next()); |
114 |
> |
|
115 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
116 |
> |
Queue q = (Queue) c; |
117 |
> |
assertThrows( |
118 |
> |
NoSuchElementException.class, |
119 |
> |
() -> q.element(), |
120 |
> |
() -> q.remove()); |
121 |
> |
} |
122 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
123 |
> |
Deque d = (Deque) c; |
124 |
> |
assertThrows( |
125 |
> |
NoSuchElementException.class, |
126 |
> |
() -> d.getFirst(), |
127 |
> |
() -> d.getLast(), |
128 |
> |
() -> d.removeFirst(), |
129 |
> |
() -> d.removeLast(), |
130 |
> |
() -> d.pop(), |
131 |
> |
() -> d.descendingIterator().next()); |
132 |
> |
} |
133 |
> |
} |
134 |
> |
|
135 |
> |
public void testRemoveIf() { |
136 |
> |
Collection c = impl.emptyCollection(); |
137 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
138 |
> |
int n = rnd.nextInt(6); |
139 |
> |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
140 |
> |
AtomicReference threwAt = new AtomicReference(null); |
141 |
> |
ArrayList survivors = new ArrayList(c); |
142 |
> |
ArrayList accepts = new ArrayList(); |
143 |
> |
ArrayList rejects = new ArrayList(); |
144 |
> |
Predicate randomPredicate = (e) -> { |
145 |
> |
assertNull(threwAt.get()); |
146 |
> |
switch (rnd.nextInt(3)) { |
147 |
> |
case 0: accepts.add(e); return true; |
148 |
> |
case 1: rejects.add(e); return false; |
149 |
> |
case 2: threwAt.set(e); throw new ArithmeticException(); |
150 |
> |
default: throw new AssertionError(); |
151 |
> |
} |
152 |
> |
}; |
153 |
> |
try { |
154 |
> |
boolean modified = c.removeIf(randomPredicate); |
155 |
> |
if (!modified) { |
156 |
> |
assertNull(threwAt.get()); |
157 |
> |
assertEquals(n, rejects.size()); |
158 |
> |
assertEquals(0, accepts.size()); |
159 |
> |
} |
160 |
> |
} catch (ArithmeticException ok) {} |
161 |
> |
survivors.removeAll(accepts); |
162 |
> |
assertEquals(n - accepts.size(), c.size()); |
163 |
> |
assertTrue(c.containsAll(survivors)); |
164 |
> |
assertTrue(survivors.containsAll(rejects)); |
165 |
> |
for (Object x : accepts) assertFalse(c.contains(x)); |
166 |
> |
if (threwAt.get() == null) |
167 |
> |
assertEquals(accepts.size() + rejects.size(), n); |
168 |
> |
} |
169 |
> |
|
170 |
> |
// public void testCollectionDebugFail() { |
171 |
> |
// fail(impl.klazz().getSimpleName()); |
172 |
> |
// } |
173 |
|
} |