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