hotspot/test/testlibrary_tests/RandomGeneratorTest.java
changeset 31232 741963a4e59e
parent 30604 b8d532cb6420
child 36851 03e2f4d0a421
equal deleted inserted replaced
31231:068d1f94b3bf 31232:741963a4e59e
    30  * @run driver RandomGeneratorTest SAME_SEED
    30  * @run driver RandomGeneratorTest SAME_SEED
    31  * @run driver RandomGeneratorTest NO_SEED
    31  * @run driver RandomGeneratorTest NO_SEED
    32  * @run driver RandomGeneratorTest DIFFERENT_SEED
    32  * @run driver RandomGeneratorTest DIFFERENT_SEED
    33  */
    33  */
    34 
    34 
    35 import jdk.test.lib.ProcessTools;
    35 import java.io.FileWriter;
    36 import jdk.test.lib.Utils;
    36 import java.io.IOException;
       
    37 import java.io.PrintWriter;
    37 import java.util.ArrayList;
    38 import java.util.ArrayList;
    38 import java.util.List;
    39 import java.util.List;
    39 import java.util.Random;
    40 import java.util.Random;
       
    41 import jdk.test.lib.OutputAnalyzer;
       
    42 import jdk.test.lib.ProcessTools;
       
    43 import jdk.test.lib.Utils;
    40 
    44 
    41 /**
    45 /**
    42  * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
    46  * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
    43  * Test works in three modes: same seed provided, no seed provided and
    47  * Test works in three modes: same seed provided, no seed provided and
    44  * different seed provided. In the first case the test expects that all random numbers
    48  * different seed provided. In the first case the test expects that all random numbers
    57         String optStr = seedOpt.getSeedOption();
    61         String optStr = seedOpt.getSeedOption();
    58         if (optStr != null) {
    62         if (optStr != null) {
    59             jvmArgs.add(optStr);
    63             jvmArgs.add(optStr);
    60         }
    64         }
    61         jvmArgs.add(RandomRunner.class.getName());
    65         jvmArgs.add(RandomRunner.class.getName());
       
    66         String origFileName = seedOpt.name() + "_orig";
       
    67         jvmArgs.add(origFileName);
       
    68         int fileNameIndex = jvmArgs.size() - 1;
    62         String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
    69         String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
    63         String etalon = ProcessTools.executeTestJvm(cmdLineArgs).getStdout().trim();
    70         ProcessTools.executeTestJvm(cmdLineArgs).shouldHaveExitValue(0);
       
    71         String etalon = Utils.fileAsString(origFileName).trim();
       
    72         cmdLineArgs[fileNameIndex] = seedOpt.name();
    64         seedOpt.verify(etalon, cmdLineArgs);
    73         seedOpt.verify(etalon, cmdLineArgs);
    65     }
    74     }
    66 
    75 
    67     /**
    76     /**
    68      * The utility enum helps to generate an appropriate string that should be passed
    77      * The utility enum helps to generate an appropriate string that should be passed
   119          * @param orig original output
   128          * @param orig original output
   120          * @param cmdLine command line arguments
   129          * @param cmdLine command line arguments
   121          * @throws Throwable - Throws an exception in case test failure.
   130          * @throws Throwable - Throws an exception in case test failure.
   122          */
   131          */
   123         public void verify(String orig, String[] cmdLine) {
   132         public void verify(String orig, String[] cmdLine) {
   124             String lastLineOrig = getLastLine(orig);
   133             String output;
   125             String lastLine;
   134             OutputAnalyzer oa;
   126             try {
   135             try {
   127                 lastLine = getLastLine(ProcessTools.executeTestJvm(cmdLine).getStdout().trim());
   136                 oa = ProcessTools.executeTestJvm(cmdLine);
   128             } catch (Throwable t) {
   137             } catch (Throwable t) {
   129                 throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
   138                 throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
   130             }
   139             }
   131             if (!isOutputExpected(lastLineOrig, lastLine)) {
   140             oa.shouldHaveExitValue(0);
   132                     throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
   141             try {
       
   142                 output = Utils.fileAsString(name()).trim();
       
   143             } catch (IOException ioe) {
       
   144                 throw new Error("TESTBUG: Problem during IO operation with file: " + name(), ioe);
   133             }
   145             }
   134         }
   146             if (!isOutputExpected(orig, output)) {
   135 
   147                 System.err.println("Initial output: " + orig);
   136         private static String getLastLine(String output) {
   148                 System.err.println("Second run output: " + output);
   137             return output.substring(output.lastIndexOf(Utils.NEW_LINE)).trim();
   149                 throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
       
   150             }
   138         }
   151         }
   139     }
   152     }
   140 
   153 
   141     /**
   154     /**
   142      * The helper class generates several random numbers
   155      * The helper class generates several random numbers
   143      * and prints them out.
   156      * and put results to a file. The file name came as first
       
   157      * command line argument.
   144      */
   158      */
   145     public static class RandomRunner {
   159     public static class RandomRunner {
   146         private static final int COUNT = 10;
   160         private static final int COUNT = 10;
   147         public static void main(String[] args) {
   161         public static void main(String[] args) {
   148             StringBuilder sb = new StringBuilder();
   162             StringBuilder sb = new StringBuilder();
   149             Random rng = Utils.getRandomInstance();
   163             Random rng = Utils.getRandomInstance();
   150             for (int i = 0; i < COUNT; i++) {
   164             for (int i = 0; i < COUNT; i++) {
   151                 sb.append(rng.nextLong()).append(' ');
   165                 sb.append(rng.nextLong()).append(' ');
   152             }
   166             }
   153             System.out.println(sb.toString());
   167             try (PrintWriter pw = new PrintWriter(new FileWriter(args[0]))) {
       
   168                 pw.write(sb.toString());
       
   169             } catch (IOException ioe) {
       
   170                 throw new Error("TESTBUG: Problem during IO operation with file: " + args[0], ioe);
       
   171             }
   154         }
   172         }
   155     }
   173     }
   156 }
   174 }