ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/EmptyNavigableSet.java
Revision: 1.10
Committed: Sun Jan 7 23:48:13 2018 UTC (6 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
typo

File Contents

# Content
1 /*
2 * Copyright (c) 2011, 2017, 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
41 import org.testng.Assert;
42 import org.testng.Assert.ThrowingRunnable;
43 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 import static org.testng.Assert.assertTrue;
49
50 public class EmptyNavigableSet {
51
52 public static <T> void assertInstance(T actual, Class<? extends T> expected) {
53 assertInstance(actual, expected, null);
54 }
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 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
83 private void assertThrowsCCE(ThrowingRunnable r, String s) {
84 assertThrows(ClassCastException.class, r, s);
85 }
86
87 private void assertThrowsNPE(ThrowingRunnable r, String s) {
88 assertThrows(NullPointerException.class, r, s);
89 }
90
91 private void assertThrowsIAE(ThrowingRunnable r, String s) {
92 assertThrows(IllegalArgumentException.class, r, s);
93 }
94
95 private void assertThrowsNSEE(ThrowingRunnable r, String s) {
96 assertThrows(NoSuchElementException.class, r, s);
97 }
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 assertThrowsCCE(
134 () -> navigableSet.contains(new Object()),
135 description + ": Comparable should be required");
136 }
137
138 /**
139 * Tests that the contains method returns {@code false}.
140 */
141 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
142 public void testContains(String description, NavigableSet<?> navigableSet) {
143 assertFalse(navigableSet.contains(new Integer(1)),
144 description + ": Should not contain any elements.");
145 }
146
147 /**
148 * Tests that the containsAll method returns {@code false}.
149 */
150 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
151 public void testContainsAll(String description, NavigableSet<?> navigableSet) {
152 TreeSet treeSet = new TreeSet();
153 treeSet.add("1");
154 treeSet.add("2");
155 treeSet.add("3");
156
157 assertFalse(navigableSet.containsAll(treeSet), "Should not contain any elements.");
158 }
159
160 /**
161 * Tests that the iterator is empty.
162 */
163 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
164 public void testEmptyIterator(String description, NavigableSet<?> navigableSet) {
165 Iterator emptyIterator = navigableSet.iterator();
166
167 assertFalse((emptyIterator != null) && (emptyIterator.hasNext()),
168 "The iterator is not empty.");
169 }
170
171 /**
172 * Tests that the set is empty.
173 */
174 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
175 public void testIsEmpty(String description, NavigableSet<?> navigableSet) {
176 assertTrue(navigableSet.isEmpty(), "The set is not empty.");
177 }
178
179 /**
180 * Tests that the first() method throws NoSuchElementException
181 */
182 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
183 public void testFirst(String description, NavigableSet<?> navigableSet) {
184 assertThrowsNSEE(navigableSet::first, description);
185 }
186
187 /**
188 * Tests the headSet() method.
189 */
190 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
191 public void testHeadSet(String description, NavigableSet navigableSet) {
192 assertThrowsNPE(
193 () -> { NavigableSet ns = navigableSet.headSet(null, false); },
194 description + ": Must throw NullPointerException for null element");
195
196 assertThrowsCCE(
197 () -> { NavigableSet ns = navigableSet.headSet(new Object(), true); },
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 assertThrowsNSEE(navigableSet::last, description);
211 }
212
213 /**
214 * Tests that the size is 0.
215 */
216 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
217 public void testSizeIsZero(String description, NavigableSet<?> navigableSet) {
218 assertTrue(0 == navigableSet.size(), "The size of the set is not 0.");
219 }
220
221 /**
222 * Tests the subSet() method.
223 */
224 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
225 public void testSubSet(String description, NavigableSet navigableSet) {
226 assertThrowsNPE(
227 () -> {
228 SortedSet ss = navigableSet.subSet(null, BigInteger.TEN);
229 },
230 description + ": Must throw NullPointerException for null element");
231
232 assertThrowsNPE(
233 () -> {
234 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, null);
235 },
236 description + ": Must throw NullPointerException for null element");
237
238 assertThrowsNPE(
239 () -> {
240 SortedSet ss = navigableSet.subSet(null, null);
241 },
242 description + ": Must throw NullPointerException for null element");
243
244 Object obj1 = new Object();
245 Object obj2 = new Object();
246
247 assertThrowsCCE(
248 () -> {
249 SortedSet ss = navigableSet.subSet(obj1, BigInteger.TEN);
250 },
251 description + ": Must throw ClassCastException for parameter which is not Comparable.");
252
253 assertThrowsCCE(
254 () -> {
255 SortedSet ss = navigableSet.subSet(BigInteger.ZERO, obj2);
256 },
257 description + ": Must throw ClassCastException for parameter which is not Comparable.");
258
259 assertThrowsCCE(
260 () -> {
261 SortedSet ss = navigableSet.subSet(obj1, obj2);
262 },
263 description + ": Must throw ClassCastException for parameter which is not Comparable.");
264
265 // minimal range
266 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, false);
267 navigableSet.subSet(BigInteger.ZERO, false, BigInteger.ZERO, true);
268 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, false);
269 navigableSet.subSet(BigInteger.ZERO, true, BigInteger.ZERO, true);
270
271 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
272 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
273
274 assertThrowsIAE(
275 () -> navigableSet.subSet(last, true, first, false),
276 description
277 + ": Must throw IllegalArgumentException when fromElement is not less than toElement.");
278
279 navigableSet.subSet(first, true, last, false);
280 }
281
282 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
283 public void testSubSetRanges(String description, NavigableSet navigableSet) {
284 Object first = isDescending(navigableSet) ? BigInteger.TEN : BigInteger.ZERO;
285 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;
286
287 NavigableSet subSet = navigableSet.subSet(first, true, last, true);
288
289 // same subset
290 subSet.subSet(first, true, last, true);
291
292 // slightly smaller
293 NavigableSet ns = subSet.subSet(first, false, last, false);
294 // slight expansion
295 assertThrowsIAE(
296 () -> ns.subSet(first, true, last, true),
297 description + ": Expansion should not be allowed");
298
299 // much smaller
300 subSet.subSet(first, false, BigInteger.ONE, false);
301 }
302
303 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
304 public void testheadSetRanges(String description, NavigableSet navigableSet) {
305 NavigableSet subSet = navigableSet.headSet(BigInteger.ONE, true);
306
307 // same subset
308 subSet.headSet(BigInteger.ONE, true);
309
310 // slightly smaller
311 NavigableSet ns = subSet.headSet(BigInteger.ONE, false);
312
313 // slight expansion
314 assertThrowsIAE(
315 () -> ns.headSet(BigInteger.ONE, true),
316 description + ": Expansion should not be allowed");
317
318 // much smaller
319 subSet.headSet(isDescending(subSet) ? BigInteger.TEN : BigInteger.ZERO, true);
320 }
321
322 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
323 public void testTailSetRanges(String description, NavigableSet navigableSet) {
324 NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
325
326 // same subset
327 subSet.tailSet(BigInteger.ONE, true);
328
329 // slightly smaller
330 NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
331
332 // slight expansion
333 assertThrowsIAE(
334 () -> ns.tailSet(BigInteger.ONE, true),
335 description + ": Expansion should not be allowed");
336
337 // much smaller
338 subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
339 }
340
341 /**
342 * Tests the tailSet() method.
343 */
344 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
345 public void testTailSet(String description, NavigableSet navigableSet) {
346 assertThrowsNPE(
347 () -> navigableSet.tailSet(null),
348 description + ": Must throw NullPointerException for null element");
349
350 assertThrowsCCE(
351 () -> navigableSet.tailSet(new Object()),
352 description);
353
354 NavigableSet ss = navigableSet.tailSet("1", true);
355
356 assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
357 }
358
359 /**
360 * Tests that the array has a size of 0.
361 */
362 @Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
363 public void testToArray(String description, NavigableSet<?> navigableSet) {
364 Object[] emptyNavigableSetArray = navigableSet.toArray();
365
366 assertTrue(emptyNavigableSetArray.length == 0, "Returned non-empty Array.");
367
368 emptyNavigableSetArray = new Object[20];
369
370 Object[] result = navigableSet.toArray(emptyNavigableSetArray);
371
372 assertSame(emptyNavigableSetArray, result);
373
374 assertTrue(result[0] == null);
375 }
376
377 @DataProvider(name = "NavigableSet<?>", parallel = true)
378 public static Iterator<Object[]> navigableSetsProvider() {
379 return makeNavigableSets().iterator();
380 }
381
382 public static Collection<Object[]> makeNavigableSets() {
383 return Arrays.asList(
384 new Object[]{"UnmodifiableNavigableSet(TreeSet)", Collections.unmodifiableNavigableSet(new TreeSet())},
385 new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet())},
386 new Object[]{"UnmodifiableNavigableSet(TreeSet.descendingSet().descendingSet()", Collections.unmodifiableNavigableSet(new TreeSet().descendingSet().descendingSet())},
387 new Object[]{"emptyNavigableSet()", Collections.emptyNavigableSet()},
388 new Object[]{"emptyNavigableSet().descendingSet()", Collections.emptyNavigableSet().descendingSet()},
389 new Object[]{"emptyNavigableSet().descendingSet().descendingSet()", Collections.emptyNavigableSet().descendingSet().descendingSet()}
390 );
391 }
392 }