< prev index next >

test/jdk/java/util/Collections/BigBinarySearch.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
--- 1,7 ---
  /*
!  * Copyright (c) 2006, 2026, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 24,22 ***
--- 24,31 ---
  /*
   * @test
   * @bug 5045582
   * @summary binarySearch of Collections larger than 1<<30
   * @author Martin Buchholz
+  * @library /test/lib
   */
  
+ import jdk.test.lib.valueclass.VClass;
  import java.util.AbstractList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.RandomAccess;
  
  public class BigBinarySearch {
  
+     static class SparseTupleList extends AbstractList<VClass> implements RandomAccess {
+         final Map<Integer, VClass> m = new HashMap<>();
+         public VClass get(int i) { return m.getOrDefault(i, new VClass(0, new int[] { 0 })); }
+         public int size() { return Collections.max(m.keySet()) + 1; }
+         public VClass set(int i, VClass v) { return m.put(i, v); }
+     }
+ 
      // Allows creation of very "big" collections without using too
      // many real resources
      static class SparseIntegerList
          extends AbstractList<Integer>
          implements RandomAccess

*** 100,10 ***
--- 109,18 ---
          }
          for (int i : ints)
              big.set(i, - big.get(i));
          for (int i : ints)
              checkBinarySearch(big, i, reverse);
+ 
+         System.out.println("binarySearch(SparseTupleList, Tuple)");
+         SparseTupleList vl = new SparseTupleList();
+         vl.set(0, new VClass(0, new int[] { 0 }));
+         vl.set(1, new VClass(1, new int[] { 1 }));
+         vl.set(n - 2, new VClass(n - 2, new int[] { n - 2 }));
+         vl.set(n - 1, new VClass(n - 1, new int[] { n - 1 }));
+         equal(n - 1, Collections.binarySearch(vl, new VClass(n - 1, new int[] { n - 1 })));
      }
  
      //--------------------- Infrastructure ---------------------------
      static volatile int passed = 0, failed = 0;
      static void pass() {passed++;}
< prev index next >