6 |
|
*/ |
7 |
|
|
8 |
|
import java.util.Collection; |
9 |
+ |
import java.util.Deque; |
10 |
+ |
import java.util.NoSuchElementException; |
11 |
+ |
import java.util.Queue; |
12 |
+ |
import java.util.function.Consumer; |
13 |
|
|
14 |
|
import junit.framework.Test; |
15 |
|
|
35 |
|
impl)); |
36 |
|
} |
37 |
|
|
38 |
< |
/** A test of the CollectionImplementation implementation ! */ |
38 |
> |
/** Checks properties of empty collections. */ |
39 |
|
public void testEmptyMeansEmpty() { |
40 |
< |
assertTrue(impl.emptyCollection().isEmpty()); |
41 |
< |
assertEquals(0, impl.emptyCollection().size()); |
40 |
> |
Collection c = impl.emptyCollection(); |
41 |
> |
assertTrue(c.isEmpty()); |
42 |
> |
assertEquals(0, c.size()); |
43 |
> |
assertEquals("[]", c.toString()); |
44 |
> |
assertEquals(0, c.toArray().length); |
45 |
> |
{ |
46 |
> |
Object[] a = new Object[0]; |
47 |
> |
assertSame(a, c.toArray(a)); |
48 |
> |
} |
49 |
> |
assertIteratorExhausted(c.iterator()); |
50 |
> |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
51 |
> |
c.forEach(alwaysThrows); |
52 |
> |
c.iterator().forEachRemaining(alwaysThrows); |
53 |
> |
c.spliterator().forEachRemaining(alwaysThrows); |
54 |
> |
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
55 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
56 |
> |
Queue q = (Queue) c; |
57 |
> |
assertNull(q.peek()); |
58 |
> |
assertNull(q.poll()); |
59 |
> |
} |
60 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
61 |
> |
Deque d = (Deque) c; |
62 |
> |
assertNull(d.peekFirst()); |
63 |
> |
assertNull(d.peekLast()); |
64 |
> |
assertNull(d.pollFirst()); |
65 |
> |
assertNull(d.pollLast()); |
66 |
> |
assertIteratorExhausted(d.descendingIterator()); |
67 |
> |
} |
68 |
|
} |
69 |
|
|
70 |
< |
// public void testCollectionDebugFail() { fail(); } |
70 |
> |
public void testNullPointerExceptions() { |
71 |
> |
Collection c = impl.emptyCollection(); |
72 |
> |
assertThrows( |
73 |
> |
NullPointerException.class, |
74 |
> |
() -> c.addAll(null), |
75 |
> |
() -> c.containsAll(null), |
76 |
> |
() -> c.retainAll(null), |
77 |
> |
() -> c.removeAll(null), |
78 |
> |
() -> c.removeIf(null)); |
79 |
> |
|
80 |
> |
if (!impl.permitsNulls()) { |
81 |
> |
assertThrows( |
82 |
> |
NullPointerException.class, |
83 |
> |
() -> c.add(null)); |
84 |
> |
} |
85 |
> |
if (!impl.permitsNulls() |
86 |
> |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
87 |
> |
Queue q = (Queue) c; |
88 |
> |
assertThrows( |
89 |
> |
NullPointerException.class, |
90 |
> |
() -> q.offer(null)); |
91 |
> |
} |
92 |
> |
if (!impl.permitsNulls() |
93 |
> |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
94 |
> |
Deque d = (Deque) c; |
95 |
> |
assertThrows( |
96 |
> |
NullPointerException.class, |
97 |
> |
() -> d.addFirst(null), |
98 |
> |
() -> d.addLast(null), |
99 |
> |
() -> d.offerFirst(null), |
100 |
> |
() -> d.offerLast(null), |
101 |
> |
() -> d.push(null)); |
102 |
> |
} |
103 |
> |
} |
104 |
> |
|
105 |
> |
public void testNoSuchElementExceptions() { |
106 |
> |
Collection c = impl.emptyCollection(); |
107 |
> |
assertThrows( |
108 |
> |
NoSuchElementException.class, |
109 |
> |
() -> c.iterator().next()); |
110 |
> |
|
111 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
112 |
> |
Queue q = (Queue) c; |
113 |
> |
assertThrows( |
114 |
> |
NoSuchElementException.class, |
115 |
> |
() -> q.element(), |
116 |
> |
() -> q.remove()); |
117 |
> |
} |
118 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
119 |
> |
Deque d = (Deque) c; |
120 |
> |
assertThrows( |
121 |
> |
NoSuchElementException.class, |
122 |
> |
() -> d.getFirst(), |
123 |
> |
() -> d.getLast(), |
124 |
> |
() -> d.removeFirst(), |
125 |
> |
() -> d.removeLast(), |
126 |
> |
() -> d.pop(), |
127 |
> |
() -> d.descendingIterator().next()); |
128 |
> |
} |
129 |
> |
} |
130 |
> |
|
131 |
> |
// public void testCollectionDebugFail() { |
132 |
> |
// fail(impl.klazz().getSimpleName()); |
133 |
> |
// } |
134 |
|
} |