ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.4
Committed: Sat Sep 17 20:38:49 2016 UTC (7 years, 7 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +1 -2 lines
Log Message:
tidy imports

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