Path: blob/master/test/jdk/java/util/Collections/BigBinarySearch.java
41149 views
/*1* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 504558226* @summary binarySearch of Collections larger than 1<<3027* @author Martin Buchholz28*/2930import java.util.AbstractList;31import java.util.Collections;32import java.util.Comparator;33import java.util.HashMap;34import java.util.List;35import java.util.Map;36import java.util.RandomAccess;3738public class BigBinarySearch {3940// Allows creation of very "big" collections without using too41// many real resources42static class SparseIntegerList43extends AbstractList<Integer>44implements RandomAccess45{46private Map<Integer,Integer> m = new HashMap<>();4748public Integer get(int i) {49if (i < 0) throw new IndexOutOfBoundsException(""+i);50Integer v = m.get(i);51return (v == null) ? Integer.valueOf(0) : v;52}5354public int size() {55return Collections.max(m.keySet()) + 1;56}5758public Integer set(int i, Integer v) {59if (i < 0) throw new IndexOutOfBoundsException(""+i);60Integer ret = get(i);61if (v == 0)62m.remove(i);63else64m.put(i, v);65return ret;66}67}6869/** Checks that binarySearch finds an element where we got it. */70private static void checkBinarySearch(List<Integer> l, int i) {71try { equal(i, Collections.binarySearch(l, l.get(i))); }72catch (Throwable t) { unexpected(t); }73}7475/** Checks that binarySearch finds an element where we got it. */76private static void checkBinarySearch(List<Integer> l, int i,77Comparator<Integer> comparator) {78try { equal(i, Collections.binarySearch(l, l.get(i), comparator)); }79catch (Throwable t) { unexpected(t); }80}8182private static void realMain(String[] args) throws Throwable {83final int n = (1<<30) + 47;8485System.out.println("binarySearch(List<Integer>, Integer)");86List<Integer> big = new SparseIntegerList();87big.set( 0, -44);88big.set( 1, -43);89big.set(n-2, 43);90big.set(n-1, 44);91int[] ints = { 0, 1, n-2, n-1 };92Comparator<Integer> reverse = Collections.reverseOrder();93Comparator<Integer> natural = Collections.reverseOrder(reverse);9495for (int i : ints) {96checkBinarySearch(big, i);97checkBinarySearch(big, i, null);98checkBinarySearch(big, i, natural);99}100for (int i : ints)101big.set(i, - big.get(i));102for (int i : ints)103checkBinarySearch(big, i, reverse);104}105106//--------------------- Infrastructure ---------------------------107static volatile int passed = 0, failed = 0;108static void pass() {passed++;}109static void fail() {failed++; Thread.dumpStack();}110static void fail(String msg) {System.out.println(msg); fail();}111static void unexpected(Throwable t) {failed++; t.printStackTrace();}112static void check(boolean cond) {if (cond) pass(); else fail();}113static void equal(Object x, Object y) {114if (x == null ? y == null : x.equals(y)) pass();115else fail(x + " not equal to " + y);}116public static void main(String[] args) throws Throwable {117try {realMain(args);} catch (Throwable t) {unexpected(t);}118System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);119if (failed > 0) throw new AssertionError("Some tests failed");}120}121122123