ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.11
Committed: Mon Jan 8 03:12:03 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.10: +7 -6 lines
Log Message:
organize imports

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     Iterator emptyIterator = navigableSet.iterator();
167    
168     assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
169     "The iterator is not empty.");
170     }
171    
172     /**
173     * Tests that the set is empty.
174     */
175     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
176     public void testIsEmpty(String description, NavigableSet<?> navigableSet) {
177     assertTrue(navigableSet.isEmpty(), "The set is not empty.");
178     }
179    
180     /**
181     * Tests that the first() method throws NoSuchElementException
182     */
183     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
184     public void testFirst(String description, NavigableSet<?> navigableSet) {
185 jsr166 1.8 assertThrowsNSEE(navigableSet::first, description);
186 jsr166 1.1 }
187    
188     /**
189     * Tests the headSet() method.
190     */
191     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
192     public void testHeadSet(String description, NavigableSet navigableSet) {
193 jsr166 1.6 assertThrowsNPE(
194 jsr166 1.1 () -> { NavigableSet ns = navigableSet.headSet(null, false); },
195     description + ": Must throw NullPointerException for null element");
196    
197 jsr166 1.6 assertThrowsCCE(
198 jsr166 1.1 () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
199     description + ": Must throw ClassCastException for non-Comparable element");
200    
201     NavigableSet ns = navigableSet.headSet("1", false);
202    
203     assertEmptyNavigableSet(ns, description + ": Returned value is not empty navigable set.");
204     }
205    
206     /**
207     * Tests that the last() method throws NoSuchElementException
208     */
209     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
210     public void testLast(String description, NavigableSet<?> navigableSet) {
211 jsr166 1.8 assertThrowsNSEE(navigableSet::last, description);
212 jsr166 1.1 }
213    
214     /**
215     * Tests that the size is 0.
216     */
217     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
218     public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {
219     assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");
220     }
221    
222     /**
223     * Tests the subSet() method.
224     */
225     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
226     public void testSubSet(String description, NavigableSet navigableSet) {
227 jsr166 1.6 assertThrowsNPE(
228 jsr166 1.1 () -> {
229     SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
230     },
231     description + ": Must throw NullPointerException for null element");
232    
233 jsr166 1.6 assertThrowsNPE(
234 jsr166 1.1 () -> {
235     SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
236     },
237     description + ": Must throw NullPointerException for null element");
238    
239 jsr166 1.6 assertThrowsNPE(
240 jsr166 1.1 () -> {
241     SortedSet ss = navigableSet.subSet(null, null);
242     },
243     description + ": Must throw NullPointerException for null element");
244    
245     Object obj1 = new Object();
246     Object obj2 = new Object();
247    
248 jsr166 1.6 assertThrowsCCE(
249 jsr166 1.1 () -> {
250     SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
251     },
252 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
253 jsr166 1.1
254 jsr166 1.6 assertThrowsCCE(
255 jsr166 1.1 () -> {
256     SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
257     },
258 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
259 jsr166 1.1
260 jsr166 1.6 assertThrowsCCE(
261 jsr166 1.1 () -> {
262     SortedSet ss = navigableSet.subSet(obj1, obj2);
263     },
264 jsr166 1.6 description + ": Must throw ClassCastException for parameter which is not Comparable.");
265 jsr166 1.1
266     // minimal range
267     navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
268     navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
269     navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
270     navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
271    
272     Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
273     Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
274    
275 jsr166 1.6 assertThrowsIAE(
276 jsr166 1.9 () -> navigableSet.subSet(last, true, first, false),
277 jsr166 1.6 description
278 jsr166 1.3 + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
279 jsr166 1.1
280     navigableSet.subSet(first, true, last, false);
281     }
282    
283     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
284     public void testSubSetRanges(String description, NavigableSet navigableSet) {
285     Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
286     Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
287    
288     NavigableSet subSet = navigableSet.subSet(first, true, last, true);
289    
290     // same subset
291     subSet.subSet(first, true, last, true);
292    
293     // slightly smaller
294     NavigableSet ns = subSet.subSet(first, false, last, false);
295 jsr166 1.2 // slight expansion
296 jsr166 1.9 assertThrowsIAE(
297     () -> ns.subSet(first, true, last, true),
298 jsr166 1.1 description + ": Expansion should not be allowed");
299    
300     // much smaller
301     subSet.subSet(first, false, BigInteger.ONE, false);
302     }
303    
304     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
305     public void testheadSetRanges(String description, NavigableSet navigableSet) {
306     NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
307    
308     // same subset
309     subSet.headSet(BigInteger.ONE, true);
310    
311     // slightly smaller
312     NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
313    
314 jsr166 1.2 // slight expansion
315 jsr166 1.9 assertThrowsIAE(
316     () -> ns.headSet(BigInteger.ONE, true),
317 jsr166 1.1 description + ": Expansion should not be allowed");
318    
319     // much smaller
320     subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
321     }
322    
323     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
324     public void testTailSetRanges(String description, NavigableSet navigableSet) {
325     NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
326    
327     // same subset
328     subSet.tailSet(BigInteger.ONE, true);
329    
330     // slightly smaller
331     NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
332    
333 jsr166 1.2 // slight expansion
334 jsr166 1.9 assertThrowsIAE(
335     () -> ns.tailSet(BigInteger.ONE, true),
336 jsr166 1.1 description + ": Expansion should not be allowed");
337    
338     // much smaller
339     subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
340     }
341    
342     /**
343     * Tests the tailSet() method.
344     */
345     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
346     public void testTailSet(String description, NavigableSet navigableSet) {
347 jsr166 1.9 assertThrowsNPE(
348     () -> navigableSet.tailSet(null),
349 jsr166 1.1 description + ": Must throw NullPointerException for null element");
350    
351 jsr166 1.9 assertThrowsCCE(
352     () -> navigableSet.tailSet(new Object()),
353     description);
354 jsr166 1.1
355     NavigableSet ss = navigableSet.tailSet("1", true);
356    
357     assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
358     }
359    
360     /**
361     * Tests that the array has a size of 0.
362     */
363     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
364     public void testToArray(String description, NavigableSet<?> navigableSet) {
365     Object[] emptyNavigableSetArray = navigableSet.toArray();
366    
367     assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
368    
369     emptyNavigableSetArray = new Object[20];
370    
371     Object[] result = navigableSet.toArray(emptyNavigableSetArray);
372    
373     assertSame(emptyNavigableSetArray, result);
374    
375     assertTrue(result[0] == null);
376     }
377    
378     @DataProvider(name = "NavigableSet<?>", parallel = true)
379     public static Iterator<Object[]> navigableSetsProvider() {
380     return makeNavigableSets().iterator();
381     }
382    
383     public static Collection<Object[]> makeNavigableSets() {
384     return Arrays.asList(
385     new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
386     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
387     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
388     new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
389     new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
390     new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
391     );
392     }
393     }