1 /*
   2  * Copyright (c) 2006, 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 5045582
  27  * @summary arrays larger than 1<<30
  28  * @author Martin Buchholz
  29  */
  30 
  31 // A proper regression test for 5045582 requires too much memory.
  32 // If you have a really big machine, run like this:
  33 // java -d64 -Xms25g -Xmx25g Big 30
  34 
  35 import java.util.*;
  36 
  37 public class Big {
  38 
  39     private static void realMain(String[] args) throws Throwable {
  40         final int shift = intArg(args, 0, 10); // "30" is real test
  41         final int tasks = intArg(args, 1, ~0); // all tasks
  42         final int n = (1<<shift) + 47;
  43 
  44         // To test byte arrays larger than 1<<30, you need 1600MB. Run like:
  45         // java -Xms1600m -Xmx1600m Big 30 1
  46         if ((tasks & 0x1) != 0) {
  47             System.out.println("byte[]");
  48             System.gc();
  49             byte[] a = new byte[n];
  50             a[0]   = (byte) -44;
  51             a[1]   = (byte) -43;
  52             a[n-2] = (byte) +43;
  53             a[n-1] = (byte) +44;
  54             for (int i : new int[] { 0, 1, n-2, n-1 })
  55                 try { equal(i, Arrays.binarySearch(a, a[i])); }
  56                 catch (Throwable t) { unexpected(t); }
  57             for (int i : new int[] { n-2, n-1 })
  58                 try { equal(i, Arrays.binarySearch(a, n-5, n, a[i])); }
  59                 catch (Throwable t) { unexpected(t); }
  60 
  61             a[n-19] = (byte) 45;
  62             try { Arrays.sort(a, n-29, n); }
  63             catch (Throwable t) { unexpected(t); }
  64             equal(a[n-1], (byte) 45);
  65             equal(a[n-2], (byte) 44);
  66             equal(a[n-3], (byte) 43);
  67             equal(a[n-4], (byte)  0);
  68         }
  69 
  70         // To test Object arrays larger than 1<<30, you need 13GB. Run like:
  71         // java -d64 -Xms13g -Xmx13g Big 30 2
  72         if ((tasks & 0x2) != 0) {
  73             System.out.println("Integer[]");
  74             System.gc();
  75             Integer[] a = new Integer[n];
  76             Integer ZERO = 0;
  77             Arrays.fill(a, ZERO);
  78             a[0]   =  -44;
  79             a[1]   =  -43;
  80             a[n-2] =  +43;
  81             a[n-1] =  +44;
  82             for (int i : new int[] { 0, 1, n-2, n-1 })
  83                 try { equal(i, Arrays.binarySearch(a, a[i])); }
  84                 catch (Throwable t) { unexpected(t); }
  85             for (int i : new int[] { n-2, n-1 })
  86                 try { equal(i, Arrays.binarySearch(a, n-5, n, a[i])); }
  87                 catch (Throwable t) { unexpected(t); }
  88 
  89             a[n-19] = 45;
  90             try { Arrays.sort(a, n-29, n); }
  91             catch (Throwable t) { unexpected(t); }
  92             equal(a[n-1],  45);
  93             equal(a[n-2],  44);
  94             equal(a[n-3],  43);
  95             equal(a[n-4],   0);
  96         }
  97     }
  98 
  99     //--------------------- Infrastructure ---------------------------
 100     static volatile int passed = 0, failed = 0;
 101     static void pass() {passed++;}
 102     static void fail() {failed++; Thread.dumpStack();}
 103     static void fail(String msg) {System.out.println(msg); fail();}
 104     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
 105     static void check(boolean cond) {if (cond) pass(); else fail();}
 106     static void equal(Object x, Object y) {
 107         if (x == null ? y == null : x.equals(y)) pass();
 108         else fail(x + " not equal to " + y);}
 109     public static void main(String[] args) throws Throwable {
 110         try {realMain(args);} catch (Throwable t) {unexpected(t);}
 111         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
 112         if (failed > 0) throw new AssertionError("Some tests failed");}
 113     static int intArg(String[] args, int i, int defaultValue) {
 114         return args.length > i ? Integer.parseInt(args[i]) : defaultValue;}
 115 }