/* * Copyright (c) 2014, 2020, 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. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @key randomness * @summary Verify correctnes of the random generator from Utils.java * @library /test/lib * @modules java.base/jdk.internal.misc * java.management * @run driver RandomGeneratorTest SAME_SEED * @run driver RandomGeneratorTest NO_SEED * @run driver RandomGeneratorTest DIFFERENT_SEED
*/
/** * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}. * Test works in three modes: same seed provided, no seed provided and * different seed provided. * In the first case, the test expects that all random numbers will be repeated in all next iterations. * In the second case, the numbers are expected to be the same for promotable builds and different for other builds. * In the last case, the test expects the randomly generated numbers differ from original.
*/ publicclass RandomGeneratorTest { privatestaticfinal String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
/** * The utility enum helps to generate an appropriate string that should be passed * to the command line depends on the testing mode. It is also responsible for the result * validation.
*/ privateenum SeedOption {
SAME_SEED {
@Override public String getSeedOption() { return SEED_VM_OPTION + Utils.SEED;
}
/** * Generates a string to be added as a command line argument. * It contains "-D" prefix, system property name, '=' sign * and seed value. * @return command line argument
*/ publicabstract String getSeedOption();
/** * Verifies that the original output meets expectations * depending on the test mode. It compares the output of second execution * to original one. * @param orig original output * @param cmdLine command line arguments * @throws Throwable - Throws an exception in case test failure.
*/ publicvoid verify(String orig, String[] cmdLine) {
String output;
OutputAnalyzer oa; try {
oa = ProcessTools.executeTestJvm(cmdLine);
} catch (Throwable t) { thrownew Error("TESTBUG: Unexpedted exception during jvm execution.", t);
}
oa.shouldHaveExitValue(0); try {
output = Utils.fileAsString(name()).trim();
} catch (IOException ioe) { thrownew Error("TESTBUG: Problem during IO operation with file: " + name(), ioe);
} if (!isOutputExpected(orig, output)) {
System.err.println("Initial output: " + orig);
System.err.println("Second run output: " + output); thrownew AssertionError("Unexpected random number sequence for mode: " + this.name());
}
}
}
/** * The helper class generates several random numbers * and put results to a file. The file name came as first * command line argument.
*/ publicstaticclass RandomRunner { privatestaticfinalint COUNT = 10; publicstaticvoid main(String[] args) {
StringBuilder sb = new StringBuilder();
Random rng = Utils.getRandomInstance(); for (int i = 0; i < COUNT; i++) {
sb.append(rng.nextLong()).append(' ');
} try (PrintWriter pw = new PrintWriter(new FileWriter(args[0]))) {
pw.write(sb.toString());
} catch (IOException ioe) { thrownew Error("TESTBUG: Problem during IO operation with file: " + args[0], ioe);
}
}
}
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.