ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.8
Committed: Sun Jan 7 23:26:04 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +2 -6 lines
Log Message:
use method references

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     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 jsr166 1.6
41     import org.testng.Assert;
42     import org.testng.Assert.ThrowingRunnable;
43 jsr166 1.1 import org.testng.annotations.Test;
44     import org.testng.annotations.DataProvider;
45    
46     import static org.testng.Assert.assertFalse;
47     import static org.testng.Assert.assertSame;
48 jsr166 1.4 import static org.testng.Assert.assertTrue;
49 jsr166 1.1
50     public class EmptyNavigableSet {
51    
52     public static <T> void assertInstance(T actual, Class<? extends T> expected) {
53 jsr166 1.7 assertInstance(actual, expected, null);
54 jsr166 1.1 }
55    
56     public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) {
57     assertTrue(expected.isInstance(actual), ((null != message) ? message : "")
58     + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". ");
59     }
60    
61     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj) {
62     assertInstance(obj, NavigableSet.class);
63     assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0));
64     }
65    
66     public static <T extends Throwable> void assertEmptyNavigableSet(Object obj, String message) {
67     assertInstance(obj, NavigableSet.class, message);
68     assertTrue(((NavigableSet)obj).isEmpty() && (((NavigableSet)obj).size() == 0),
69     ((null != message) ? message : "") + " Not empty. ");
70     }
71    
72 jsr166 1.6 private <T extends Throwable> void assertThrows(Class<T> throwableClass,
73     ThrowingRunnable runnable,
74     String message) {
75     try {
76     Assert.assertThrows(throwableClass, runnable);
77     } catch (AssertionError e) {
78     throw new AssertionError(String.format("%s%n%s",
79     ((null != message) ? message : ""), e.getMessage()), e);
80     }
81     }
82 jsr166 1.1
83 jsr166 1.6 private void assertThrowsCCE(ThrowingRunnable r, String s) {
84     assertThrows(ClassCastException.class, r, s);
85 jsr166 1.1 }
86    
87 jsr166 1.6 private void assertThrowsNPE(ThrowingRunnable r, String s) {
88     assertThrows(NullPointerException.class, r, s);
89 jsr166 1.1 }
90    
91 jsr166 1.6 private void assertThrowsIAE(ThrowingRunnable r, String s) {
92     assertThrows(IllegalArgumentException.class, r, s);
93     }
94 jsr166 1.1
95 jsr166 1.6 private void assertThrowsNSEE(ThrowingRunnable r, String s) {
96     assertThrows(NoSuchElementException.class, r, s);
97 jsr166 1.1 }
98    
99     public static final boolean isDescending(SortedSet<?> set) {
100     if (null == set.comparator()) {
101     // natural order
102     return false;
103     }
104    
105     if (Collections.reverseOrder() == set.comparator()) {
106     // reverse natural order.
107     return true;
108     }
109    
110     if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) {
111     // it's a Collections.reverseOrder(Comparator).
112     return true;
113     }
114    
115     throw new IllegalStateException("can't determine ordering for " + set);
116     }
117    
118     /**
119     * Tests that the comparator is {@code null}.
120     */
121     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
122     public void testComparatorIsNull(String description, NavigableSet<?> navigableSet) {
123     Comparator comparator = navigableSet.comparator();
124    
125     assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null.");
126     }
127    
128     /**
129     * Tests that contains requires Comparable
130     */
131     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
132     public void testContainsRequiresComparable(String description, NavigableSet<?> navigableSet) {
133 jsr166 1.6 assertThrowsCCE(() -> {
134 jsr166 1.1 navigableSet.contains(new Object());
135     },
136 jsr166 1.6 description + ": Compareable 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.1 () -> {
277     navigableSet.subSet(last, true, first, false);
278     },
279 jsr166 1.6 description
280 jsr166 1.3 + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
281 jsr166 1.1
282     navigableSet.subSet(first, true, last, false);
283     }
284    
285     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
286     public void testSubSetRanges(String description, NavigableSet navigableSet) {
287     Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
288     Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
289    
290     NavigableSet subSet = navigableSet.subSet(first, true, last, true);
291    
292     // same subset
293     subSet.subSet(first, true, last, true);
294    
295     // slightly smaller
296     NavigableSet ns = subSet.subSet(first, false, last, false);
297 jsr166 1.2 // slight expansion
298 jsr166 1.6 assertThrowsIAE(() -> {
299 jsr166 1.1 ns.subSet(first, true, last, true);
300     },
301     description + ": Expansion should not be allowed");
302    
303     // much smaller
304     subSet.subSet(first, false, BigInteger.ONE, false);
305     }
306    
307     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
308     public void testheadSetRanges(String description, NavigableSet navigableSet) {
309     NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
310    
311     // same subset
312     subSet.headSet(BigInteger.ONE, true);
313    
314     // slightly smaller
315     NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
316    
317 jsr166 1.2 // slight expansion
318 jsr166 1.6 assertThrowsIAE(() -> {
319 jsr166 1.1 ns.headSet(BigInteger.ONE, true);
320     },
321     description + ": Expansion should not be allowed");
322    
323     // much smaller
324     subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
325     }
326    
327     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
328     public void testTailSetRanges(String description, NavigableSet navigableSet) {
329     NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
330    
331     // same subset
332     subSet.tailSet(BigInteger.ONE, true);
333    
334     // slightly smaller
335     NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
336    
337 jsr166 1.2 // slight expansion
338 jsr166 1.6 assertThrowsIAE(() -> {
339 jsr166 1.1 ns.tailSet(BigInteger.ONE, true);
340     },
341     description + ": Expansion should not be allowed");
342    
343     // much smaller
344     subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
345     }
346    
347     /**
348     * Tests the tailSet() method.
349     */
350     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
351     public void testTailSet(String description, NavigableSet navigableSet) {
352 jsr166 1.6 assertThrowsNPE(() -> {
353 jsr166 1.1 navigableSet.tailSet(null);
354     },
355     description + ": Must throw NullPointerException for null element");
356    
357 jsr166 1.6 assertThrowsCCE(() -> {
358 jsr166 1.1 navigableSet.tailSet(new Object());
359 jsr166 1.6 }, description);
360 jsr166 1.1
361     NavigableSet ss = navigableSet.tailSet("1", true);
362    
363     assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
364     }
365    
366     /**
367     * Tests that the array has a size of 0.
368     */
369     @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
370     public void testToArray(String description, NavigableSet<?> navigableSet) {
371     Object[] emptyNavigableSetArray = navigableSet.toArray();
372    
373     assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
374    
375     emptyNavigableSetArray = new Object[20];
376    
377     Object[] result = navigableSet.toArray(emptyNavigableSetArray);
378    
379     assertSame(emptyNavigableSetArray, result);
380    
381     assertTrue(result[0] == null);
382     }
383    
384     @DataProvider(name = "NavigableSet<?>", parallel = true)
385     public static Iterator<Object[]> navigableSetsProvider() {
386     return makeNavigableSets().iterator();
387     }
388    
389     public static Collection<Object[]> makeNavigableSets() {
390     return Arrays.asList(
391     new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
392     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
393     new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
394     new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
395     new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
396     new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
397     );
398     }
399     }