ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.12
Committed: Wed Jan 17 03:53:47 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +1 -4 lines
Log Message:
testEmptyIterator: zero tolerance for iterator() returning null

File Contents

# User Rev Content
1 jsr166 1.1 /*
2 jsr166 1.6 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
3 jsr166 1.1 * 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     */
23    
24     /*
25     * @test
26     * @bug 4533691 7129185
27     * @summary Unit test for Collections.emptyNavigableSet
28     * @run testng EmptyNavigableSet
29     */
30 jsr166 1.11
31     import org.testng.Assert;
32     import org.testng.Assert.ThrowingRunnable;
33     import org.testng.annotations.DataProvider;
34     import org.testng.annotations.Test;
35    
36 jsr166 1.1 import java.math.BigInteger;
37     import java.util.Arrays;
38     import java.util.Collection;
39     import java.util.Collections;
40     import java.util.Comparator;
41     import java.util.Iterator;
42 jsr166 1.11 import java.util.NavigableSet;
43 jsr166 1.1 import java.util.NoSuchElementException;
44     import java.util.SortedSet;
45     import java.util.TreeSet;
46 jsr166 1.6
47 jsr166 1.1 import static org.testng.Assert.assertFalse;
48     import static org.testng.Assert.assertSame;
49 jsr166 1.4 import static org.testng.Assert.assertTrue;
50 jsr166 1.1
51     public class EmptyNavigableSet {
52    
53     public static <T> void assertInstance(T actual, Class<? extends T> expected) {
54 jsr166 1.7 assertInstance(actual, expected, null);
55 jsr166 1.1 }
56    
57     public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
58     assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
59     + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
60     }
61    
62     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) {
63     assertInstance(obj, NavigableSet.class);
64     assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));
65     }
66    
67     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {
68     assertInstance(obj, NavigableSet.class, message);
69     assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
70     ((null != message) ? message : "") + " Not empty. ");
71     }
72    
73 jsr166 1.6 private <T extends Throwable> void assertThrows(Class<T> throwableClass,
74     ThrowingRunnable runnable,
75     String message) {
76     try {
77     Assert.assertThrows(throwableClass, runnable);
78     } catch (AssertionError e) {
79     throw new AssertionError(String.format("%s%n%s",
80     ((null != message) ? message : ""), e.getMessage()), e);
81     }
82     }
83 jsr166 1.1
84 jsr166 1.6 private void assertThrowsCCE(ThrowingRunnable r, String s) {
85     assertThrows(ClassCastException.class, r, s);
86 jsr166 1.1 }
87    
88 jsr166 1.6 private void assertThrowsNPE(ThrowingRunnable r, String s) {
89     assertThrows(NullPointerException.class, r, s);
90 jsr166 1.1 }
91    
92 jsr166 1.6 private void assertThrowsIAE(ThrowingRunnable r, String s) {
93     assertThrows(IllegalArgumentException.class, r, s);
94     }
95 jsr166 1.1
96 jsr166 1.6 private void assertThrowsNSEE(ThrowingRunnable r, String s) {
97     assertThrows(NoSuchElementException.class, r, s);
98 jsr166 1.1 }
99    
100     public static final boolean isDescending(SortedSet<?> set) {
101     if (null == set.comparator()) {
102     // natural order
103     return false;
104     }
105    
106     if (Collections.reverseOrder() == set.comparator()) {
107     // reverse natural order.
108     return true;
109     }
110    
111     if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
112     // it's a Collections.reverseOrder(Comparator).
113     return true;
114     }
115    
116     throw new IllegalStateException("can't determine ordering for " + set);
117     }
118    
119     /**
120     * Tests that the comparator is {@code null}.
121     */
122     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
123     public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {
124     Comparator comparator = navigableSet.comparator();
125    
126     assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
127     }
128    
129     /**
130     * Tests that contains requires Comparable
131     */
132     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
133     public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
134 jsr166 1.9 assertThrowsCCE(
135     () -> navigableSet.contains(new Object()),
136 jsr166 1.10 description + ": Comparable should be required");
137 jsr166 1.1 }
138    
139     /**
140     * Tests that the contains method returns {@code false}.
141     */
142     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
143     public void testContains(String description, NavigableSet<?> navigableSet) {
144     assertFalse(navigableSet.contains(new Integer(1)),
145     description + ": Should not contain any elements.");
146     }
147    
148     /**
149     * Tests that the containsAll method returns {@code false}.
150     */
151     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
152     public void testContainsAll(String description, NavigableSet<?> navigableSet) {
153     TreeSet treeSet = new TreeSet();
154     treeSet.add("1");
155     treeSet.add("2");
156     treeSet.add("3");
157    
158     assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");
159     }
160    
161     /**
162     * Tests that the iterator is empty.
163     */
164     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
165     public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
166 jsr166 1.12 assertFalse(navigableSet.iterator().hasNext(), "The iterator is not empty.");
167 jsr166 1.1 }
168    
169     /**
170     * Tests that the set is empty.
171     */
172     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
173     public void testIsEmpty(String description, NavigableSet<?> navigableSet) {
174     assertTrue(navigableSet.isEmpty(), "The set is not empty.");
175     }
176    
177     /**
178     * Tests that the first() method throws NoSuchElementException
179     */
180     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
181     public void testFirst(String description, NavigableSet<?> navigableSet) {
182 jsr166 1.8 assertThrowsNSEE(navigableSet::first, description);
183 jsr166 1.1 }
184    
185     /**
186     * Tests the headSet() method.
187     */
188     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
189     public void testHeadSet(String description, NavigableSet navigableSet) {
190 jsr166 1.6 assertThrowsNPE(
191 jsr166 1.1 () -> { NavigableSet ns = navigableSet.headSet(null, false); },
192     description + ": Must throw NullPointerException for null element");
193    
194 jsr166 1.6 assertThrowsCCE(
195 jsr166 1.1 () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
196     description + ": Must throw ClassCastException for non-Comparable element");
197    
198     NavigableSet ns = navigableSet.headSet("1", false);
199    
200     assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");
201     }
202    
203     /**
204     * Tests that the last() method throws NoSuchElementException
205     */
206     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
207     public void testLast(String description, NavigableSet<?> navigableSet) {
208 jsr166 1.8 assertThrowsNSEE(navigableSet::last, description);
209 jsr166 1.1 }
210    
211     /**
212     * Tests that the size is 0.
213     */
214     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
215     public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {
216     assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");
217     }
218    
219     /**
220     * Tests the subSet() method.
221     */
222     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
223     public void testSubSet(String description, NavigableSet navigableSet) {
224 jsr166 1.6 assertThrowsNPE(
225 jsr166 1.1 () -> {
226     SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
227     },
228     description + ": Must throw NullPointerException for null element");
229    
230 jsr166 1.6 assertThrowsNPE(
231 jsr166 1.1 () -> {
232     SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
233     },
234     description + ": Must throw NullPointerException for null element");
235    
236 jsr166 1.6 assertThrowsNPE(
237 jsr166 1.1 () -> {
238     SortedSet ss = navigableSet.subSet(null, null);
239     },
240     description + ": Must throw NullPointerException for null element");
241    
242     Object obj1 = new Object();
243     Object obj2 = new Object();
244    
245 jsr166 1.6 assertThrowsCCE(
246 jsr166 1.1 () -> {
247     SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
248     },
249 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
250 jsr166 1.1
251 jsr166 1.6 assertThrowsCCE(
252 jsr166 1.1 () -> {
253     SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
254     },
255 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
256 jsr166 1.1
257 jsr166 1.6 assertThrowsCCE(
258 jsr166 1.1 () -> {
259     SortedSet ss = navigableSet.subSet(obj1, obj2);
260     },
261 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
262 jsr166 1.1
263     // minimal range
264     navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
265     navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
266     navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
267     navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
268    
269     Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
270     Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
271    
272 jsr166 1.6 assertThrowsIAE(
273 jsr166 1.9 () -> navigableSet.subSet(last, true, first, false),
274 jsr166 1.6 description
275 jsr166 1.3 + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
276 jsr166 1.1
277     navigableSet.subSet(first, true, last, false);
278     }
279    
280     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
281     public void testSubSetRanges(String description, NavigableSet navigableSet) {
282     Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
283     Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
284    
285     NavigableSet subSet = navigableSet.subSet(first, true, last, true);
286    
287     // same subset
288     subSet.subSet(first, true, last, true);
289    
290     // slightly smaller
291     NavigableSet ns = subSet.subSet(first, false, last, false);
292 jsr166 1.2 // slight expansion
293 jsr166 1.9 assertThrowsIAE(
294     () -> ns.subSet(first, true, last, true),
295 jsr166 1.1 description + ": Expansion should not be allowed");
296    
297     // much smaller
298     subSet.subSet(first, false, BigInteger.ONE, false);
299     }
300    
301     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
302     public void testheadSetRanges(String description, NavigableSet navigableSet) {
303     NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
304    
305     // same subset
306     subSet.headSet(BigInteger.ONE, true);
307    
308     // slightly smaller
309     NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
310    
311 jsr166 1.2 // slight expansion
312 jsr166 1.9 assertThrowsIAE(
313     () -> ns.headSet(BigInteger.ONE, true),
314 jsr166 1.1 description + ": Expansion should not be allowed");
315    
316     // much smaller
317     subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
318     }
319    
320     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
321     public void testTailSetRanges(String description, NavigableSet navigableSet) {
322     NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
323    
324     // same subset
325     subSet.tailSet(BigInteger.ONE, true);
326    
327     // slightly smaller
328     NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
329    
330 jsr166 1.2 // slight expansion
331 jsr166 1.9 assertThrowsIAE(
332     () -> ns.tailSet(BigInteger.ONE, true),
333 jsr166 1.1 description + ": Expansion should not be allowed");
334    
335     // much smaller
336     subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
337     }
338    
339     /**
340     * Tests the tailSet() method.
341     */
342     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
343     public void testTailSet(String description, NavigableSet navigableSet) {
344 jsr166 1.9 assertThrowsNPE(
345     () -> navigableSet.tailSet(null),
346 jsr166 1.1 description + ": Must throw NullPointerException for null element");
347    
348 jsr166 1.9 assertThrowsCCE(
349     () -> navigableSet.tailSet(new Object()),
350     description);
351 jsr166 1.1
352     NavigableSet ss = navigableSet.tailSet("1", true);
353    
354     assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
355     }
356    
357     /**
358     * Tests that the array has a size of 0.
359     */
360     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
361     public void testToArray(String description, NavigableSet<?> navigableSet) {
362     Object[] emptyNavigableSetArray = navigableSet.toArray();
363    
364     assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
365    
366     emptyNavigableSetArray = new Object[20];
367    
368     Object[] result = navigableSet.toArray(emptyNavigableSetArray);
369    
370     assertSame(emptyNavigableSetArray, result);
371    
372     assertTrue(result[0] == null);
373     }
374    
375     @DataProvider(name = "NavigableSet<?>", parallel = true)
376     public static Iterator<Object[]> navigableSetsProvider() {
377     return makeNavigableSets().iterator();
378     }
379    
380     public static Collection<Object[]> makeNavigableSets() {
381     return Arrays.asList(
382     new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
383     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
384     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
385     new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
386     new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
387     new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
388     );
389     }
390     }