156 157 // Singleton collections 158 Set<Integer> singletonSet = singleton(1); 159 equal(singletonSet.size(), 1); 160 testCollection(singletonSet); 161 testImmutableSet(singletonSet); 162 163 List<Integer> singletonList = singletonList(1); 164 equal(singletonList.size(), 1); 165 testCollection(singletonList); 166 testImmutableList(singletonList); 167 testImmutableList(singletonList.subList(0,1)); 168 testImmutableList(singletonList.subList(0,1).subList(0,1)); 169 testEmptyList(singletonList.subList(0,0)); 170 testEmptyList(singletonList.subList(0,0).subList(0,0)); 171 172 Map<Integer,Integer> singletonMap = singletonMap(1,2); 173 equal(singletonMap.size(), 1); 174 testMap(singletonMap); 175 testImmutableMap(singletonMap); 176 } 177 178 private static void checkContainsSelf(Collection<Integer> c) { 179 check(c.containsAll(c)); 180 check(c.containsAll(Arrays.asList(c.toArray()))); 181 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0])))); 182 } 183 184 private static void checkContainsEmpty(Collection<Integer> c) { 185 check(c.containsAll(new ArrayList<Integer>())); 186 } 187 188 private static <T> void testEmptyCollection(Collection<T> c) { 189 check(c.isEmpty()); 190 equal(c.size(), 0); 191 equal(c.toString(),"[]"); 192 equal(c.toArray().length, 0); 193 equal(c.toArray(new Object[0]).length, 0); 194 check(c.toArray(new Object[]{42})[0] == null); 195 1376 } 1377 public int size() { 1378 return list.size(); 1379 } 1380 } 1381 private static class NewAbstractSet<E> extends AbstractSet<E> { 1382 HashSet<E> set = new HashSet<>(); 1383 public boolean remove(Object obj) { 1384 return set.remove(obj); 1385 } 1386 public boolean add(E e) { 1387 return set.add(e); 1388 } 1389 public Iterator<E> iterator() { 1390 return set.iterator(); 1391 } 1392 public int size() { 1393 return set.size(); 1394 } 1395 } 1396 1397 } | 156 157 // Singleton collections 158 Set<Integer> singletonSet = singleton(1); 159 equal(singletonSet.size(), 1); 160 testCollection(singletonSet); 161 testImmutableSet(singletonSet); 162 163 List<Integer> singletonList = singletonList(1); 164 equal(singletonList.size(), 1); 165 testCollection(singletonList); 166 testImmutableList(singletonList); 167 testImmutableList(singletonList.subList(0,1)); 168 testImmutableList(singletonList.subList(0,1).subList(0,1)); 169 testEmptyList(singletonList.subList(0,0)); 170 testEmptyList(singletonList.subList(0,0).subList(0,0)); 171 172 Map<Integer,Integer> singletonMap = singletonMap(1,2); 173 equal(singletonMap.size(), 1); 174 testMap(singletonMap); 175 testImmutableMap(singletonMap); 176 177 // Protected ArrayList#removeRange 178 NewArrayList.testRemoveRange(); 179 } 180 181 private static void checkContainsSelf(Collection<Integer> c) { 182 check(c.containsAll(c)); 183 check(c.containsAll(Arrays.asList(c.toArray()))); 184 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0])))); 185 } 186 187 private static void checkContainsEmpty(Collection<Integer> c) { 188 check(c.containsAll(new ArrayList<Integer>())); 189 } 190 191 private static <T> void testEmptyCollection(Collection<T> c) { 192 check(c.isEmpty()); 193 equal(c.size(), 0); 194 equal(c.toString(),"[]"); 195 equal(c.toArray().length, 0); 196 equal(c.toArray(new Object[0]).length, 0); 197 check(c.toArray(new Object[]{42})[0] == null); 198 1379 } 1380 public int size() { 1381 return list.size(); 1382 } 1383 } 1384 private static class NewAbstractSet<E> extends AbstractSet<E> { 1385 HashSet<E> set = new HashSet<>(); 1386 public boolean remove(Object obj) { 1387 return set.remove(obj); 1388 } 1389 public boolean add(E e) { 1390 return set.add(e); 1391 } 1392 public Iterator<E> iterator() { 1393 return set.iterator(); 1394 } 1395 public int size() { 1396 return set.size(); 1397 } 1398 } 1399 private static class NewArrayList extends ArrayList<Integer> { 1400 // ArrayList#removeRange is protected, so is tested 1401 // from within a derived class 1402 public static void testRemoveRange() { 1403 final int MAX_ITEM = 8; 1404 final int START_INDEX = -2; 1405 final int END_INDEX = MAX_ITEM + 2; 1406 1407 final NewArrayList list = new NewArrayList(); 1408 try { list.removeRange(list.size(), list.size()); } 1409 catch (Throwable t) { unexpected(t); } 1410 for (int i = 0; i <= MAX_ITEM; ++i) { 1411 list.add(i); 1412 } 1413 1414 for (int fromIndex = START_INDEX; fromIndex <= END_INDEX; ++fromIndex) { 1415 for (int toIndex = START_INDEX; toIndex <= END_INDEX; ++toIndex) { 1416 if (fromIndex < 0 1417 || toIndex > list.size() 1418 || toIndex < fromIndex) { 1419 final int FROM_INDEX = fromIndex; 1420 final int TO_INDEX = toIndex; 1421 THROWS(IndexOutOfBoundsException.class, 1422 new Fun() { void f() { list.removeRange(FROM_INDEX, TO_INDEX); }}); 1423 } else { 1424 try { 1425 NewArrayList list1 = (NewArrayList)list.clone(); 1426 list1.removeRange(fromIndex, toIndex); 1427 for (int i = 0, j = 0; i < list1.size(); ++i, ++j) { 1428 if (j == fromIndex) 1429 j = toIndex; 1430 check(list1.get(i) == j); 1431 } 1432 } catch (Throwable t) { unexpected(t); } 1433 } 1434 } 1435 } 1436 1437 } 1438 } 1439 } |