1 /*
   2  * Copyright (c) 2013, 2014, 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 package intrinsics;
  24 
  25 import java.io.BufferedReader;
  26 import java.io.FileReader;
  27 import java.util.Properties;
  28 
  29 public class Verifier {
  30     enum VerificationStrategy {
  31         VERIFY_STRONG_EQUALITY {
  32             @Override
  33             void verify(Properties expectedProperties, int fullMatchCnt,
  34                         int suspectCnt) {
  35                 int expectedCount = Integer.parseInt(
  36                         expectedProperties.getProperty(
  37                                 Verifier.INTRINSIC_EXPECTED_COUNT_PROPERTY));
  38                 String intrinsicID = expectedProperties.getProperty(
  39                         Verifier.INTRINSIC_NAME_PROPERTY);
  40 
  41                 System.out.println("Intrinsic " + intrinsicID
  42                         + " verification, expected: " + expectedCount
  43                         + ", matched: " + fullMatchCnt
  44                         + ", suspected: " + suspectCnt);
  45                 if (expectedCount != fullMatchCnt) {
  46                     throw new RuntimeException(
  47                             "Unexpected count of intrinsic  "
  48                                     + intrinsicID
  49                                     + " expected:" + expectedCount
  50                                     + ", matched: " + fullMatchCnt
  51                                     + ", suspected: " + suspectCnt);
  52                 }
  53             }
  54         },
  55 
  56         VERIFY_INTRINSIC_USAGE {
  57             @Override
  58             void verify(Properties expectedProperties, int fullMatchCnt,
  59                         int suspectCnt) {
  60                 boolean isExpected = Boolean.parseBoolean(
  61                         expectedProperties.getProperty(
  62                                 Verifier.INTRINSIC_IS_EXPECTED_PROPERTY));
  63                 String intrinsicID = expectedProperties.getProperty(
  64                         Verifier.INTRINSIC_NAME_PROPERTY);
  65 
  66                 System.out.println("Intrinsic " + intrinsicID
  67                         + " verification, is expected: " + isExpected
  68                         + ", matched: " + fullMatchCnt
  69                         + ", suspected: " + suspectCnt);
  70                 if ((fullMatchCnt == 0 && isExpected)
  71                         || (fullMatchCnt > 0 && !isExpected)) {
  72                     throw new RuntimeException(
  73                             "Unexpected count of intrinsic  "
  74                                     + intrinsicID
  75                                     + " is expected:" + isExpected
  76                                     + ", matched: " + fullMatchCnt
  77                                     + ", suspected: " + suspectCnt);
  78                 }
  79             }
  80         };
  81 
  82         void verify(Properties expectedProperties, int fullMathCnt,
  83                     int suspectCnt) {
  84             throw new RuntimeException("Default strategy is not implemented.");
  85         }
  86     }
  87 
  88     public static final String PROPERTY_FILE_SUFFIX = ".verify.properties";
  89     public static final String INTRINSIC_NAME_PROPERTY = "intrinsic.name";
  90     public static final String INTRINSIC_IS_EXPECTED_PROPERTY
  91             = "intrinsic.expected";
  92     public static final String INTRINSIC_EXPECTED_COUNT_PROPERTY
  93             = "intrinsic.expectedCount";
  94     private static final String DEFAULT_STRATEGY
  95             = VerificationStrategy.VERIFY_STRONG_EQUALITY.name();
  96 
  97     public static void main(String[] args) throws Exception {
  98         if (args.length == 0) {
  99             throw new RuntimeException("Test bug, nothing to verify");
 100         }
 101         for (String hsLogFile : args) {
 102             verify(hsLogFile);
 103         }
 104     }
 105 
 106     private static void verify(String hsLogFile) throws Exception {
 107         System.out.println("Verifying " + hsLogFile);
 108 
 109         Properties expectedProperties = new Properties();
 110         FileReader reader = new FileReader(hsLogFile
 111                 + Verifier.PROPERTY_FILE_SUFFIX);
 112         expectedProperties.load(reader);
 113         reader.close();
 114 
 115         int fullMatchCnt = 0;
 116         int suspectCnt = 0;
 117         String intrinsicId = expectedProperties.getProperty(
 118                 Verifier.INTRINSIC_NAME_PROPERTY);
 119         String prefix = "<intrinsic id='";
 120         String prefixWithId = prefix + intrinsicId + "'";
 121 
 122         try (BufferedReader compLogReader
 123                      = new BufferedReader(new FileReader(hsLogFile))) {
 124             String logLine;
 125             while ((logLine = compLogReader.readLine()) != null) {
 126                 if (logLine.startsWith(prefix)) {
 127                     if (logLine.startsWith(prefixWithId)) {
 128                         fullMatchCnt++;
 129                     } else {
 130                         suspectCnt++;
 131                         System.err.println(
 132                                 "WARNING: Other intrinsic detected " + logLine);
 133                     }
 134                 }
 135             }
 136         }
 137 
 138         VerificationStrategy strategy = VerificationStrategy.valueOf(
 139                 System.getProperty("verificationStrategy",
 140                         Verifier.DEFAULT_STRATEGY));
 141         strategy.verify(expectedProperties, fullMatchCnt, suspectCnt);
 142     }
 143 }