1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test TestG1ConcMarkStepDurationMillis
26 * @key gc
27 * @requires vm.gc.G1
28 * @summary Tests argument processing for double type flag, G1ConcMarkStepDurationMillis
29 * @library /test/lib
30 * @modules java.base/jdk.internal.misc
31 *          java.management
32 */
33
34import jdk.test.lib.process.ProcessTools;
35import jdk.test.lib.process.OutputAnalyzer;
36import java.util.*;
37import java.util.regex.*;
38
39public class TestG1ConcMarkStepDurationMillis {
40
41  static final int PASS                = 0;
42  static final int FAIL_IMPROPER_VALUE = 1;
43  static final int FAIL_OUT_RANGE      = 2;
44
45  static final String DOUBLE_1       = "1.0";
46  static final String DOUBLE_MAX     = "1.79e+308";
47
48  static final String DOUBLE_NEG_EXP = "1.0e-30";
49  static final String NEG_DOUBLE_1   = "-1.0";
50
51  static final String DOUBLE_INF     = "1.79e+309";
52  static final String NEG_DOUBLE_INF = "-1.79e+309";
53  static final String DOUBLE_NAN     = "abe+309";
54  static final String WRONG_DOUBLE_1 = "1.79e+308e";
55  static final String WRONG_DOUBLE_2 = "1.79ee+308";
56
57  public static void main(String args[]) throws Exception {
58    // Pass cases
59    runG1ConcMarkStepDurationMillisTest(DOUBLE_1,       PASS);
60    runG1ConcMarkStepDurationMillisTest(DOUBLE_MAX,     PASS);
61
62    // Fail cases: out of range
63    runG1ConcMarkStepDurationMillisTest(DOUBLE_NEG_EXP, FAIL_OUT_RANGE);
64    runG1ConcMarkStepDurationMillisTest(NEG_DOUBLE_1,   FAIL_OUT_RANGE);
65
66    // Fail cases: not double
67    runG1ConcMarkStepDurationMillisTest(DOUBLE_INF,     FAIL_IMPROPER_VALUE);
68    runG1ConcMarkStepDurationMillisTest(NEG_DOUBLE_INF, FAIL_IMPROPER_VALUE);
69    runG1ConcMarkStepDurationMillisTest(DOUBLE_NAN,     FAIL_IMPROPER_VALUE);
70    runG1ConcMarkStepDurationMillisTest(WRONG_DOUBLE_1, FAIL_IMPROPER_VALUE);
71    runG1ConcMarkStepDurationMillisTest(WRONG_DOUBLE_2, FAIL_IMPROPER_VALUE);
72  }
73
74  private static void runG1ConcMarkStepDurationMillisTest(String expectedValue, int expectedResult) throws Exception {
75    List<String> vmOpts = new ArrayList<>();
76
77    Collections.addAll(vmOpts, "-XX:+UseG1GC", "-XX:G1ConcMarkStepDurationMillis="+expectedValue, "-XX:+PrintFlagsFinal", "-version");
78
79    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(vmOpts.toArray(new String[vmOpts.size()]));
80    OutputAnalyzer output = new OutputAnalyzer(pb.start());
81
82    output.shouldHaveExitValue(expectedResult == PASS ? 0 : 1);
83    String stdout = output.getStdout();
84    if (expectedResult == PASS) {
85      checkG1ConcMarkStepDurationMillisConsistency(stdout, expectedValue);
86    } else if (expectedResult == FAIL_IMPROPER_VALUE) {
87      output.shouldContain("Improperly specified VM option");
88    } else if (expectedResult == FAIL_OUT_RANGE) {
89      output.shouldContain("outside the allowed range");
90    }
91  }
92
93  private static void checkG1ConcMarkStepDurationMillisConsistency(String output, String expectedValue) {
94    double actualValue = getDoubleValue("G1ConcMarkStepDurationMillis", output);
95
96    if (Double.parseDouble(expectedValue) != actualValue) {
97      throw new RuntimeException(
98            "Actual G1ConcMarkStepDurationMillis(" + Double.toString(actualValue)
99            + ") is not equal to expected value(" + expectedValue + ")");
100    }
101  }
102
103  public static double getDoubleValue(String flag, String where) {
104    Matcher m = Pattern.compile(flag + "\\s+:?=\\s+\\d+").matcher(where);
105    if (!m.find()) {
106      throw new RuntimeException("Could not find value for flag " + flag + " in output string");
107    }
108    String match = m.group();
109    return Double.parseDouble(match.substring(match.lastIndexOf(" ") + 1, match.length()));
110  }
111}
112