ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.3
Committed: Mon Feb 8 22:13:07 2016 UTC (8 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
import 8138838: docs cleanup for java.desktop

File Contents

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