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 |
> |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
50 |
> |
c.forEach(alwaysThrows); |
51 |
> |
c.iterator().forEachRemaining(alwaysThrows); |
52 |
> |
c.spliterator().forEachRemaining(alwaysThrows); |
53 |
> |
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
54 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
55 |
> |
Queue q = (Queue) c; |
56 |
> |
assertNull(q.peek()); |
57 |
> |
assertNull(q.poll()); |
58 |
> |
} |
59 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
60 |
> |
Deque d = (Deque) c; |
61 |
> |
assertNull(d.peekFirst()); |
62 |
> |
assertNull(d.peekLast()); |
63 |
> |
assertNull(d.pollFirst()); |
64 |
> |
assertNull(d.pollLast()); |
65 |
> |
} |
66 |
> |
} |
67 |
> |
|
68 |
> |
public void testNullPointerExceptions() { |
69 |
> |
Collection c = impl.emptyCollection(); |
70 |
> |
assertThrows( |
71 |
> |
NullPointerException.class, |
72 |
> |
() -> c.addAll(null), |
73 |
> |
() -> c.containsAll(null), |
74 |
> |
() -> c.retainAll(null), |
75 |
> |
() -> c.removeAll(null), |
76 |
> |
() -> c.removeIf(null)); |
77 |
> |
|
78 |
> |
if (!impl.permitsNulls()) { |
79 |
> |
assertThrows( |
80 |
> |
NullPointerException.class, |
81 |
> |
() -> c.add(null)); |
82 |
> |
} |
83 |
> |
if (!impl.permitsNulls() |
84 |
> |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
85 |
> |
Queue q = (Queue) c; |
86 |
> |
assertThrows( |
87 |
> |
NullPointerException.class, |
88 |
> |
() -> q.offer(null)); |
89 |
> |
} |
90 |
> |
if (!impl.permitsNulls() |
91 |
> |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
92 |
> |
Deque d = (Deque) c; |
93 |
> |
assertThrows( |
94 |
> |
NullPointerException.class, |
95 |
> |
() -> d.addFirst(null), |
96 |
> |
() -> d.addLast(null), |
97 |
> |
() -> d.offerFirst(null), |
98 |
> |
() -> d.offerLast(null), |
99 |
> |
() -> d.push(null)); |
100 |
> |
} |
101 |
> |
} |
102 |
> |
|
103 |
> |
public void testNoSuchElementExceptions() { |
104 |
> |
Collection c = impl.emptyCollection(); |
105 |
> |
assertThrows( |
106 |
> |
NoSuchElementException.class, |
107 |
> |
() -> c.iterator().next()); |
108 |
> |
|
109 |
> |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
110 |
> |
Queue q = (Queue) c; |
111 |
> |
assertThrows( |
112 |
> |
NoSuchElementException.class, |
113 |
> |
() -> q.element(), |
114 |
> |
() -> q.remove()); |
115 |
> |
} |
116 |
> |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
117 |
> |
Deque d = (Deque) c; |
118 |
> |
assertThrows( |
119 |
> |
NoSuchElementException.class, |
120 |
> |
() -> d.getFirst(), |
121 |
> |
() -> d.getLast(), |
122 |
> |
() -> d.removeFirst(), |
123 |
> |
() -> d.removeLast(), |
124 |
> |
() -> d.pop()); |
125 |
> |
} |
126 |
|
} |
127 |
|
|
128 |
|
// public void testCollectionDebugFail() { fail(); } |