Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Random/NextBytes.java
38812 views
/*1* Copyright (c) 2005, 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 426117026* @summary Tests for Random.nextBytes27* @author Martin Buchholz28*/2930import java.util.*;3132public class NextBytes {33private static void realMain(String[] args) throws Throwable {34byte[] expected = new byte[]35{27, -105, -24, 83, -77, -29, 119, -74, -106, 68, 54};36Random r = new java.util.Random(2398579034L);37for (int i = 0; i <= expected.length; i++) {38r.setSeed(2398579034L);39byte[] actual = new byte[i];40r.nextBytes(actual);41//System.out.println(Arrays.toString(actual));42check(Arrays.equals(actual, Arrays.copyOf(expected,i)));43}44}4546//--------------------- Infrastructure ---------------------------47static volatile int passed = 0, failed = 0;48static void pass() {passed++;}49static void fail() {failed++; Thread.dumpStack();}50static void fail(String msg) {System.out.println(msg); fail();}51static void unexpected(Throwable t) {failed++; t.printStackTrace();}52static void check(boolean cond) {if (cond) pass(); else fail();}53static void equal(Object x, Object y) {54if (x == null ? y == null : x.equals(y)) pass();55else fail(x + " not equal to " + y);}56public static void main(String[] args) throws Throwable {57try {realMain(args);} catch (Throwable t) {unexpected(t);}58System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);59if (failed > 0) throw new AssertionError("Some tests failed");}60}616263