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