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.emptyNavigableMap 28 * @run testng EmptyNavigableMap 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.NavigableMap; 37 import java.util.SortedMap; 38 import java.util.TreeMap; 39 import org.testng.annotations.Test; 40 import org.testng.annotations.DataProvider; 41 42 import static org.testng.Assert.fail; 43 import static org.testng.Assert.assertTrue; 44 import static org.testng.Assert.assertFalse; 45 46 public class EmptyNavigableMap { 47 48 public static <T> void assertInstance(T actual, Class<? extends T> expected) { 49 assertInstance(expected.isInstance(actual), null); 50 } 51 52 public static <T> void assertInstance(T actual, Class<? extends T> expected, String message) { 53 assertTrue(expected.isInstance(actual), ((null != message) ? message : "") 54 + " " + (actual == null ? "<null>" : actual.getClass().getSimpleName()) + " != " + expected.getSimpleName() + ". "); 55 } 56 57 public static <T extends Throwable> void assertEmptyNavigableMap(Object obj) { 58 assertInstance(obj, NavigableMap.class); 59 assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0)); 60 } 61 62 public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) { 63 assertInstance(obj, NavigableMap.class, message); 64 assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0), 65 ((null != message) ? message : "") + " Not empty. "); 66 } 67 68 public interface Thrower<T extends Throwable> { 69 70 public void run() throws T; 71 } 72 73 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { 74 assertThrows(thrower, throwable, null); 75 } 76 77 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { 78 Throwable result; 79 try { 80 thrower.run(); 81 fail(((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); 82 return; 83 } catch (Throwable caught) { 84 result = caught; 85 } 86 87 assertInstance(result, throwable, ((null != message) ? message : "") + "Failed to throw " + throwable.getCanonicalName() + ". "); 88 } 89 90 public static final boolean isDescending(SortedMap<?,?> set) { 91 if (null == set.comparator()) { 92 // natural order 93 return false; 94 } 95 96 if (Collections.reverseOrder() == set.comparator()) { 97 // reverse natural order. 98 return true; 99 } 100 101 if (set.comparator().equals(Collections.reverseOrder(Collections.reverseOrder(set.comparator())))) { 102 // it's a Collections.reverseOrder(Comparator). 103 return true; 104 } 105 106 throw new IllegalStateException("can't determine ordering for " + set); 107 } 108 109 /** 110 * Tests that the comparator is {@code null}. 111 */ 112 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 113 public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) { 114 Comparator comparator = navigableMap.comparator(); 115 116 assertTrue(comparator == null || comparator == Collections.reverseOrder(), description + ": Comparator (" + comparator + ") is not null."); 117 } 118 119 /** 120 * Tests that contains requires Comparable 121 */ 122 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 123 public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) { 124 assertThrows(() -> { 125 navigableMap.containsKey(new Object()); 126 }, 127 ClassCastException.class, 128 description + ": Compareable should be required"); 129 } 130 131 /** 132 * Tests that the contains method returns {@code false}. 133 */ 134 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 135 public void testContains(String description, NavigableMap<?,?> navigableMap) { 136 assertFalse(navigableMap.containsKey(new Integer(1)), 137 description + ": Should not contain any elements."); 138 assertFalse(navigableMap.containsValue(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 = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 146 public void testContainsAll(String description, NavigableMap<?,?> navigableMap) { 147 TreeMap treeMap = new TreeMap(); 158 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 159 public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) { 160 assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty."); 161 assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty."); 162 assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty."); 163 } 164 165 /** 166 * Tests that the set is empty. 167 */ 168 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 169 public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) { 170 assertTrue(navigableMap.isEmpty(), "The set is not empty."); 171 } 172 173 /** 174 * Tests the headMap() method. 175 */ 176 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 177 public void testHeadMap(String description, NavigableMap navigableMap) { 178 assertThrows( 179 () -> { NavigableMap ss = navigableMap.headMap(null, false); }, 180 NullPointerException.class, 181 description + ": Must throw NullPointerException for null element"); 182 183 assertThrows( 184 () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, 185 ClassCastException.class, 186 description + ": Must throw ClassCastException for non-Comparable element"); 187 188 NavigableMap ss = navigableMap.headMap("1", false); 189 190 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); 191 } 192 193 /** 194 * Tests that the size is 0. 195 */ 196 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 197 public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) { 198 assertTrue(0 == navigableMap.size(), "The size of the set is not 0."); 199 } 200 201 /** 202 * Tests the subMap() method. 203 */ 204 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 205 public void testSubMap(String description, NavigableMap navigableMap) { 206 assertThrows( 207 () -> { 208 SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); 209 }, 210 NullPointerException.class, 211 description + ": Must throw NullPointerException for null element"); 212 213 assertThrows( 214 () -> { 215 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); 216 }, 217 NullPointerException.class, 218 description + ": Must throw NullPointerException for null element"); 219 220 assertThrows( 221 () -> { 222 SortedMap ss = navigableMap.subMap(null, null); 223 }, 224 NullPointerException.class, 225 description + ": Must throw NullPointerException for null element"); 226 227 Object obj1 = new Object(); 228 Object obj2 = new Object(); 229 230 assertThrows( 231 () -> { 232 SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); 233 }, 234 ClassCastException.class, description 235 + ": Must throw ClassCastException for parameter which is not Comparable."); 236 237 assertThrows( 238 () -> { 239 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); 240 }, 241 ClassCastException.class, description 242 + ": Must throw ClassCastException for parameter which is not Comparable."); 243 244 assertThrows( 245 () -> { 246 SortedMap ss = navigableMap.subMap(obj1, obj2); 247 }, 248 ClassCastException.class, description 249 + ": Must throw ClassCastException for parameter which is not Comparable."); 250 251 // minimal range 252 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); 253 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true); 254 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false); 255 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true); 256 257 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; 258 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 259 260 assertThrows( 261 () -> { 262 navigableMap.subMap(last, true, first, false); 263 }, 264 IllegalArgumentException.class, description 265 + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); 266 267 navigableMap.subMap(first, true, last, false); 268 } 269 270 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 271 public void testSubMapRanges(String description, NavigableMap navigableMap) { 272 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; 273 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 274 275 NavigableMap subMap = navigableMap.subMap(first, true, last, true); 276 277 // same subset 278 subMap.subMap(first, true, last, true); 279 280 // slightly smaller 281 NavigableMap ns = subMap.subMap(first, false, last, false); 282 // slight expansion 283 assertThrows(() -> { 284 ns.subMap(first, true, last, true); 285 }, 286 IllegalArgumentException.class, 287 description + ": Expansion should not be allowed"); 288 289 // much smaller 290 subMap.subMap(first, false, BigInteger.ONE, false); 291 } 292 293 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 294 public void testheadMapRanges(String description, NavigableMap navigableMap) { 295 NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true); 296 297 // same subset 298 subMap.headMap(BigInteger.ONE, true); 299 300 // slightly smaller 301 NavigableMap ns = subMap.headMap(BigInteger.ONE, false); 302 303 // slight expansion 304 assertThrows(() -> { 305 ns.headMap(BigInteger.ONE, true); 306 }, 307 IllegalArgumentException.class, 308 description + ": Expansion should not be allowed"); 309 310 // much smaller 311 subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true); 312 } 313 314 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 315 public void testTailMapRanges(String description, NavigableMap navigableMap) { 316 NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true); 317 318 // same subset 319 subMap.tailMap(BigInteger.ONE, true); 320 321 // slightly smaller 322 NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); 323 324 // slight expansion 325 assertThrows(() -> { 326 ns.tailMap(BigInteger.ONE, true); 327 }, 328 IllegalArgumentException.class, 329 description + ": Expansion should not be allowed"); 330 331 // much smaller 332 subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false); 333 } 334 335 /** 336 * Tests the tailMap() method. 337 */ 338 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 339 public void testTailMap(String description, NavigableMap navigableMap) { 340 assertThrows(() -> { 341 navigableMap.tailMap(null); 342 }, 343 NullPointerException.class, 344 description + ": Must throw NullPointerException for null element"); 345 346 assertThrows(() -> { 347 navigableMap.tailMap(new Object()); 348 }, ClassCastException.class); 349 350 NavigableMap ss = navigableMap.tailMap("1", true); 351 352 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); 353 } 354 355 @DataProvider(name = "NavigableMap<?,?>", parallel = true) 356 public static Iterator<Object[]> navigableMapsProvider() { 357 return makeNavigableMaps().iterator(); 358 } 359 360 public static Collection<Object[]> makeNavigableMaps() { 361 return Arrays.asList( 362 new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())}, 363 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())}, 364 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())}, 365 new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()}, 366 new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()}, 367 new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()} 368 ); | 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.emptyNavigableMap 28 * @run testng EmptyNavigableMap 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.NavigableMap; 37 import java.util.SortedMap; 38 import java.util.TreeMap; 39 40 import org.testng.Assert; 41 import org.testng.Assert.ThrowingRunnable; 42 import org.testng.annotations.Test; 43 import org.testng.annotations.DataProvider; 44 45 import static org.testng.Assert.assertTrue; 46 import static org.testng.Assert.assertFalse; 47 48 public class EmptyNavigableMap { 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 assertEmptyNavigableMap(Object obj) { 60 assertInstance(obj, NavigableMap.class); 61 assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0)); 62 } 63 64 public static <T extends Throwable> void assertEmptyNavigableMap(Object obj, String message) { 65 assertInstance(obj, NavigableMap.class, message); 66 assertTrue(((NavigableMap)obj).isEmpty() && (((NavigableMap)obj).size() == 0), 67 ((null != message) ? message : "") + " Not empty. "); 68 } 69 70 private <T extends Throwable> void assertThrows(Class<T> throwableClass, 71 ThrowingRunnable runnable, 72 String message) { 73 try { 74 Assert.assertThrows(throwableClass, runnable); 75 } catch (AssertionError e) { 76 throw new AssertionError(String.format("%s%n%s", 77 ((null != message) ? message : ""), e.getMessage()), e); 78 } 79 } 80 81 private void assertThrowsCCE(ThrowingRunnable r, String s) { 82 assertThrows(ClassCastException.class, r, s); 83 } 84 85 private void assertThrowsNPE(ThrowingRunnable r, String s) { 86 assertThrows(NullPointerException.class, r, s); 87 } 88 89 private void assertThrowsIAE(ThrowingRunnable r, String s) { 90 assertThrows(IllegalArgumentException.class, r, s); 91 } 92 93 public static final boolean isDescending(SortedMap<?,?> 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 = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 116 public void testComparatorIsNull(String description, NavigableMap<?,?> navigableMap) { 117 Comparator comparator = navigableMap.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 = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 126 public void testContainsRequiresComparable(String description, NavigableMap<?,?> navigableMap) { 127 assertThrowsCCE(() -> { 128 navigableMap.containsKey(new Object()); 129 }, 130 description + ": Compareable should be required"); 131 } 132 133 /** 134 * Tests that the contains method returns {@code false}. 135 */ 136 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 137 public void testContains(String description, NavigableMap<?,?> navigableMap) { 138 assertFalse(navigableMap.containsKey(new Integer(1)), 139 description + ": Should not contain any elements."); 140 assertFalse(navigableMap.containsValue(new Integer(1)), 141 description + ": Should not contain any elements."); 142 } 143 144 /** 145 * Tests that the containsAll method returns {@code false}. 146 */ 147 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 148 public void testContainsAll(String description, NavigableMap<?,?> navigableMap) { 149 TreeMap treeMap = new TreeMap(); 160 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 161 public void testEmptyIterator(String description, NavigableMap<?,?> navigableMap) { 162 assertFalse(navigableMap.keySet().iterator().hasNext(), "The iterator is not empty."); 163 assertFalse(navigableMap.values().iterator().hasNext(), "The iterator is not empty."); 164 assertFalse(navigableMap.entrySet().iterator().hasNext(), "The iterator is not empty."); 165 } 166 167 /** 168 * Tests that the set is empty. 169 */ 170 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 171 public void testIsEmpty(String description, NavigableMap<?,?> navigableMap) { 172 assertTrue(navigableMap.isEmpty(), "The set is not empty."); 173 } 174 175 /** 176 * Tests the headMap() method. 177 */ 178 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 179 public void testHeadMap(String description, NavigableMap navigableMap) { 180 assertThrowsNPE( 181 () -> { NavigableMap ss = navigableMap.headMap(null, false); }, 182 description + ": Must throw NullPointerException for null element"); 183 184 assertThrowsCCE( 185 () -> { NavigableMap ss = navigableMap.headMap(new Object(), true); }, 186 description + ": Must throw ClassCastException for non-Comparable element"); 187 188 NavigableMap ss = navigableMap.headMap("1", false); 189 190 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); 191 } 192 193 /** 194 * Tests that the size is 0. 195 */ 196 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 197 public void testSizeIsZero(String description, NavigableMap<?,?> navigableMap) { 198 assertTrue(0 == navigableMap.size(), "The size of the set is not 0."); 199 } 200 201 /** 202 * Tests the subMap() method. 203 */ 204 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 205 public void testSubMap(String description, NavigableMap navigableMap) { 206 assertThrowsNPE( 207 () -> { 208 SortedMap ss = navigableMap.subMap(null, BigInteger.TEN); 209 }, 210 description + ": Must throw NullPointerException for null element"); 211 212 assertThrowsNPE( 213 () -> { 214 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, null); 215 }, 216 description + ": Must throw NullPointerException for null element"); 217 218 assertThrowsNPE( 219 () -> { 220 SortedMap ss = navigableMap.subMap(null, null); 221 }, 222 description + ": Must throw NullPointerException for null element"); 223 224 Object obj1 = new Object(); 225 Object obj2 = new Object(); 226 227 assertThrowsCCE( 228 () -> { 229 SortedMap ss = navigableMap.subMap(obj1, BigInteger.TEN); 230 }, 231 description + ": Must throw ClassCastException for parameter which is not Comparable."); 232 233 assertThrowsCCE( 234 () -> { 235 SortedMap ss = navigableMap.subMap(BigInteger.ZERO, obj2); 236 }, 237 description + ": Must throw ClassCastException for parameter which is not Comparable."); 238 239 assertThrowsCCE( 240 () -> { 241 SortedMap ss = navigableMap.subMap(obj1, obj2); 242 }, 243 description + ": Must throw ClassCastException for parameter which is not Comparable."); 244 245 // minimal range 246 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, false); 247 navigableMap.subMap(BigInteger.ZERO, false, BigInteger.ZERO, true); 248 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, false); 249 navigableMap.subMap(BigInteger.ZERO, true, BigInteger.ZERO, true); 250 251 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; 252 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 253 254 assertThrowsIAE( 255 () -> { 256 navigableMap.subMap(last, true, first, false); 257 }, 258 description + ": Must throw IllegalArgumentException when fromElement is not less than toElement."); 259 260 navigableMap.subMap(first, true, last, false); 261 } 262 263 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 264 public void testSubMapRanges(String description, NavigableMap navigableMap) { 265 Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO; 266 Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO; 267 268 NavigableMap subMap = navigableMap.subMap(first, true, last, true); 269 270 // same subset 271 subMap.subMap(first, true, last, true); 272 273 // slightly smaller 274 NavigableMap ns = subMap.subMap(first, false, last, false); 275 // slight expansion 276 assertThrowsIAE(() -> { 277 ns.subMap(first, true, last, true); 278 }, 279 description + ": Expansion should not be allowed"); 280 281 // much smaller 282 subMap.subMap(first, false, BigInteger.ONE, false); 283 } 284 285 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 286 public void testheadMapRanges(String description, NavigableMap navigableMap) { 287 NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true); 288 289 // same subset 290 subMap.headMap(BigInteger.ONE, true); 291 292 // slightly smaller 293 NavigableMap ns = subMap.headMap(BigInteger.ONE, false); 294 295 // slight expansion 296 assertThrowsIAE(() -> { 297 ns.headMap(BigInteger.ONE, true); 298 }, 299 description + ": Expansion should not be allowed"); 300 301 // much smaller 302 subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true); 303 } 304 305 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 306 public void testTailMapRanges(String description, NavigableMap navigableMap) { 307 NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true); 308 309 // same subset 310 subMap.tailMap(BigInteger.ONE, true); 311 312 // slightly smaller 313 NavigableMap ns = subMap.tailMap(BigInteger.ONE, false); 314 315 // slight expansion 316 assertThrowsIAE(() -> { 317 ns.tailMap(BigInteger.ONE, true); 318 }, 319 description + ": Expansion should not be allowed"); 320 321 // much smaller 322 subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false); 323 } 324 325 /** 326 * Tests the tailMap() method. 327 */ 328 @Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class) 329 public void testTailMap(String description, NavigableMap navigableMap) { 330 assertThrowsNPE(() -> { 331 navigableMap.tailMap(null); 332 }, 333 description + ": Must throw NullPointerException for null element"); 334 335 assertThrowsCCE(() -> { 336 navigableMap.tailMap(new Object()); 337 }, 338 description); 339 340 NavigableMap ss = navigableMap.tailMap("1", true); 341 342 assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set."); 343 } 344 345 @DataProvider(name = "NavigableMap<?,?>", parallel = true) 346 public static Iterator<Object[]> navigableMapsProvider() { 347 return makeNavigableMaps().iterator(); 348 } 349 350 public static Collection<Object[]> makeNavigableMaps() { 351 return Arrays.asList( 352 new Object[]{"UnmodifiableNavigableMap(TreeMap)", Collections.unmodifiableNavigableMap(new TreeMap())}, 353 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap())}, 354 new Object[]{"UnmodifiableNavigableMap(TreeMap.descendingMap().descendingMap()", Collections.unmodifiableNavigableMap(new TreeMap().descendingMap().descendingMap())}, 355 new Object[]{"emptyNavigableMap()", Collections.emptyNavigableMap()}, 356 new Object[]{"emptyNavigableMap().descendingMap()", Collections.emptyNavigableMap().descendingMap()}, 357 new Object[]{"emptyNavigableMap().descendingMap().descendingMap()", Collections.emptyNavigableMap().descendingMap().descendingMap()} 358 ); |