001/* 002 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023/* 024 */ 025package com.oracle.graal.jtt; 026 027import java.io.*; 028import java.nio.charset.*; 029import java.nio.file.*; 030import java.util.*; 031 032/** 033 * Simple Utility to convert java tester tests from the proprietary test format into JUnit - tests. 034 */ 035public class ConvertJTT { 036 037 public static void main(String[] args) throws IOException { 038 String targetPath = "graalvm/graal/com.oracle.graal.tests/src/com/oracle/max/graal/jtt"; 039 String sourcePath = "maxine/com.oracle.max.vm/test/jtt"; 040 041 File target = new File(targetPath); 042 for (File dir : new File(sourcePath).listFiles()) { 043 if (dir.isDirectory()) { 044 String packageName = dir.getName(); 045 if (packageName.equals("exbytecode") || packageName.equals("max")) { 046 continue; 047 } 048 File targetDir = new File(target, packageName); 049 for (File file : dir.listFiles()) { 050 if (file.getName().endsWith(".java")) { 051 targetDir.mkdirs(); 052 try { 053 processFile(file.toPath(), new File(targetDir, file.getName()).toPath(), packageName); 054 } catch (RuntimeException e) { 055 throw new RuntimeException(String.format("Exception while processing file %s", file.getAbsolutePath()), e); 056 } 057 } 058 } 059 } 060 } 061 } 062 063 public static class Run { 064 065 public String input; 066 public String output; 067 068 public Run(String input, String output) { 069 this.input = input; 070 this.output = output; 071 } 072 073 @Override 074 public String toString() { 075 return String.format("%16s = %s", input, output); 076 } 077 } 078 079 private static void processFile(Path file, Path target, String packageName) throws IOException { 080 List<String> lines = Files.readAllLines(file, Charset.forName("UTF-8")); 081 Iterator<String> iter = lines.iterator(); 082 083 ArrayList<String> output = new ArrayList<>(); 084 ArrayList<Run> runs = new ArrayList<>(); 085 086 String line; 087 boolean javaHarness = false; 088 while (iter.hasNext()) { 089 line = iter.next(); 090 if (line.startsWith(" * Copyright (c) ")) { 091 output.add(" * Copyright (c) " + line.substring(17, 21) + ", 2012, Oracle and/or its affiliates. All rights reserved."); 092 } else if (line.contains("@Runs:")) { 093 line = line.substring(line.indexOf("@Runs:") + 6).trim(); 094 if (line.endsWith(";")) { 095 line = line.substring(0, line.length() - 1); 096 } 097 String[] runStrings; 098 if (charCount(line, ';') == charCount(line, '=') - 1) { 099 runStrings = line.split(";"); 100 } else if (charCount(line, ',') == charCount(line, '=') - 1) { 101 runStrings = line.split(","); 102 } else if (charCount(line, ',', ';') == charCount(line, '=') - 1) { 103 runStrings = line.split("[,;]"); 104 } else { 105 throw new RuntimeException("invalid run line: " + line); 106 } 107 for (String runString : runStrings) { 108 String[] split = runString.split("="); 109 if (split.length != 2) { 110 throw new RuntimeException("invalid run string: " + runString); 111 } 112 Run run = new Run(split[0].trim(), split[1].trim()); 113 runs.add(run); 114 } 115 } else if (line.contains("@Harness:")) { 116 if (line.contains("@Harness: java")) { 117 javaHarness = true; 118 } 119 } else if (line.startsWith("package jtt.")) { 120 output.add("package com.oracle.graal.jtt." + packageName + ";"); 121 output.add(""); 122 output.add("import org.junit.*;"); 123 } else if (line.contains("@NEVER_INLINE")) { 124 output.add("// " + line); 125 } else if (line.startsWith("import com.sun.max.annotate.")) { 126 // do nothing 127 } else if (line.equals("}")) { 128 if (runs != null) { 129 int n = 0; 130 for (Run run : runs) { 131 processRun(output, run, n++); 132 } 133 runs = null; 134 } 135 output.add(line); 136 } else { 137 // line = line.replace(oldClassName, newClassName); 138 line = line.replace(" jtt.", " com.oracle.graal.jtt."); 139 output.add(line); 140 } 141 } 142 if (!javaHarness) { 143 throw new RuntimeException("no java harness"); 144 } 145 if (runs != null) { 146 throw new RuntimeException("no ending brace found"); 147 } 148 149 Files.write(target, output, Charset.forName("UTF-8")); 150 } 151 152 private static void processRun(ArrayList<String> output, Run run, int n) { 153 if (run.output.startsWith("!")) { 154 output.add(" @Test(expected = " + run.output.substring(1).replace("jtt.", "com.oracle.graal.jtt.").replace('$', '.') + ".class)"); 155 output.add(" public void run" + n + "() throws Throwable {"); 156 output.add(" test(" + parameters(run.input) + ");"); 157 output.add(" }"); 158 output.add(""); 159 } else { 160 output.add(" @Test"); 161 output.add(" public void run" + n + "() throws Throwable {"); 162 String result = parameters(run.output); 163 if (result.endsWith("f") || result.endsWith("d") || result.endsWith("F") || result.endsWith("D")) { 164 output.add(" Assert.assertEquals(" + result + ", test(" + parameters(run.input) + "), 0);"); 165 } else { 166 output.add(" Assert.assertEquals(" + result + ", test(" + parameters(run.input) + "));"); 167 } 168 output.add(" }"); 169 output.add(""); 170 } 171 } 172 173 private static String parameters(String params) { 174 if (params.startsWith("(")) { 175 StringBuilder str = new StringBuilder(); 176 String[] split = params.substring(1, params.length() - 1).split(","); 177 for (int i = 0; i < split.length; i++) { 178 str.append(i == 0 ? "" : ", ").append(parameters(split[i].trim())); 179 } 180 return str.toString(); 181 } else if (params.startsWith("`")) { 182 return params.substring(1); 183 } else { 184 if (params.length() <= 1) { 185 return params; 186 } else { 187 if (params.endsWith("s")) { 188 return "((short) " + params.substring(0, params.length() - 1) + ")"; 189 } else if (params.endsWith("c")) { 190 return "((char) " + params.substring(0, params.length() - 1) + ")"; 191 } else if (params.endsWith("b")) { 192 return "((byte) " + params.substring(0, params.length() - 1) + ")"; 193 } 194 } 195 return params.replace("jtt.", "com.oracle.graal.jtt."); 196 } 197 } 198 199 private static int charCount(String str, char ch1) { 200 int count = 0; 201 for (int i = 0; i < str.length(); i++) { 202 if (str.charAt(i) == ch1) { 203 count++; 204 } 205 } 206 return count; 207 } 208 209 private static int charCount(String str, char ch1, char ch2) { 210 int count = 0; 211 for (int i = 0; i < str.length(); i++) { 212 if (str.charAt(i) == ch1 || str.charAt(i) == ch2) { 213 count++; 214 } 215 } 216 return count; 217 } 218 219}