test/tools/javac/lambda/mostSpecific/StructuralMostSpecificTest.java

Tue, 08 Jan 2013 13:47:57 +0000

author
vromero
date
Tue, 08 Jan 2013 13:47:57 +0000
changeset 1482
954541f13717
parent 1415
01c9d4161882
child 1520
5c956be64b9e
permissions
-rw-r--r--

8005167: execution time of combo tests in javac should be improved
Reviewed-by: jjg, jjh

mcimadamore@1415 1 /*
vromero@1482 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1415 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1415 4 *
mcimadamore@1415 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1415 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1415 7 * published by the Free Software Foundation.
mcimadamore@1415 8 *
mcimadamore@1415 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1415 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1415 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1415 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1415 13 * accompanied this code).
mcimadamore@1415 14 *
mcimadamore@1415 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1415 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1415 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1415 18 *
mcimadamore@1415 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1415 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1415 21 * questions.
mcimadamore@1415 22 */
mcimadamore@1415 23
mcimadamore@1415 24 /*
mcimadamore@1415 25 * @test
mcimadamore@1415 26 * @bug 8003280
mcimadamore@1415 27 * @summary Add lambda tests
mcimadamore@1415 28 * Automatic test for checking correctness of structural most specific test routine
vromero@1482 29 * @library ../../lib
vromero@1482 30 * @build JavacTestingAbstractThreadedTest
vromero@1482 31 * @run main/timeout=600 StructuralMostSpecificTest
mcimadamore@1415 32 */
mcimadamore@1415 33
vromero@1482 34 import java.net.URI;
vromero@1482 35 import java.util.Arrays;
vromero@1482 36 import javax.tools.Diagnostic;
vromero@1482 37 import javax.tools.JavaFileObject;
vromero@1482 38 import javax.tools.SimpleJavaFileObject;
mcimadamore@1415 39 import com.sun.source.util.JavacTask;
mcimadamore@1415 40 import com.sun.tools.javac.api.ClientCodeWrapper;
mcimadamore@1415 41 import com.sun.tools.javac.util.JCDiagnostic;
mcimadamore@1415 42
vromero@1482 43 public class StructuralMostSpecificTest
vromero@1482 44 extends JavacTestingAbstractThreadedTest
vromero@1482 45 implements Runnable {
mcimadamore@1415 46
mcimadamore@1415 47 enum RetTypeKind {
mcimadamore@1415 48 SHORT("short"),
mcimadamore@1415 49 INT("int"),
mcimadamore@1415 50 OBJECT("Object"),
mcimadamore@1415 51 INTEGER("Integer"),
mcimadamore@1415 52 VOID("void"),
mcimadamore@1415 53 J_L_VOID("Void");
mcimadamore@1415 54
mcimadamore@1415 55 String retTypeStr;
mcimadamore@1415 56
mcimadamore@1415 57 RetTypeKind(String retTypeStr) {
mcimadamore@1415 58 this.retTypeStr = retTypeStr;
mcimadamore@1415 59 }
mcimadamore@1415 60
mcimadamore@1415 61 boolean moreSpecificThan(RetTypeKind rk) {
mcimadamore@1415 62 return moreSpecificThan[this.ordinal()][rk.ordinal()];
mcimadamore@1415 63 }
mcimadamore@1415 64
mcimadamore@1415 65 static boolean[][] moreSpecificThan = {
mcimadamore@1415 66 // SHORT | INT | OBJECT | INTEGER | VOID | J_L_VOID
mcimadamore@1415 67 /* SHORT */ { true , true , true , false , false , false },
mcimadamore@1415 68 /* INT */ { false , true , true , true , false , false },
mcimadamore@1415 69 /* OBJECT */ { false , false , true , false , false , false },
mcimadamore@1415 70 /* INTEGER */ { false , false , true , true , false , false },
mcimadamore@1415 71 /* VOID */ { false , false , false , false , true , true },
mcimadamore@1415 72 /* J_L_VOID */{ false , false , true , false , false , true } };
mcimadamore@1415 73 }
mcimadamore@1415 74
mcimadamore@1415 75 enum ArgTypeKind {
mcimadamore@1415 76 SHORT("short"),
mcimadamore@1415 77 INT("int"),
mcimadamore@1415 78 BOOLEAN("boolean"),
mcimadamore@1415 79 OBJECT("Object"),
mcimadamore@1415 80 INTEGER("Integer"),
mcimadamore@1415 81 DOUBLE("Double");
mcimadamore@1415 82
mcimadamore@1415 83 String argTypeStr;
mcimadamore@1415 84
mcimadamore@1415 85 ArgTypeKind(String typeStr) {
mcimadamore@1415 86 this.argTypeStr = typeStr;
mcimadamore@1415 87 }
mcimadamore@1415 88 }
mcimadamore@1415 89
mcimadamore@1415 90 enum ExceptionKind {
mcimadamore@1415 91 NONE(""),
mcimadamore@1415 92 EXCEPTION("throws Exception"),
mcimadamore@1415 93 SQL_EXCEPTION("throws java.sql.SQLException"),
mcimadamore@1415 94 IO_EXCEPTION("throws java.io.IOException");
mcimadamore@1415 95
mcimadamore@1415 96 String exceptionStr;
mcimadamore@1415 97
mcimadamore@1415 98 ExceptionKind(String exceptionStr) {
mcimadamore@1415 99 this.exceptionStr = exceptionStr;
mcimadamore@1415 100 }
mcimadamore@1415 101 }
mcimadamore@1415 102
mcimadamore@1415 103 enum LambdaReturnKind {
mcimadamore@1415 104 VOID("return;"),
mcimadamore@1415 105 SHORT("return (short)0;"),
mcimadamore@1415 106 INT("return 0;"),
vromero@1482 107 INTEGER("return (Integer)null;"),
mcimadamore@1415 108 NULL("return null;");
mcimadamore@1415 109
mcimadamore@1415 110 String retStr;
mcimadamore@1415 111
mcimadamore@1415 112 LambdaReturnKind(String retStr) {
mcimadamore@1415 113 this.retStr = retStr;
mcimadamore@1415 114 }
mcimadamore@1415 115
mcimadamore@1415 116 boolean compatibleWith(RetTypeKind rk) {
mcimadamore@1415 117 return compatibleWith[rk.ordinal()][ordinal()];
mcimadamore@1415 118 }
mcimadamore@1415 119
mcimadamore@1415 120 static boolean[][] compatibleWith = {
mcimadamore@1415 121 // VOID | SHORT | INT | INTEGER | NULL
mcimadamore@1415 122 /* SHORT */ { false , true , false , false , false },
mcimadamore@1415 123 /* INT */ { false , true , true , true , false },
mcimadamore@1415 124 /* OBJECT */ { false , true , true , true , true },
mcimadamore@1415 125 /* INTEGER */ { false , false , true , true , true },
mcimadamore@1415 126 /* VOID */ { true , false , false , false , false },
mcimadamore@1415 127 /* J_L_VOID */{ false , false , false , false , true } };
mcimadamore@1415 128
mcimadamore@1415 129 boolean needsConversion(RetTypeKind rk) {
mcimadamore@1415 130 return needsConversion[rk.ordinal()][ordinal()];
mcimadamore@1415 131 }
mcimadamore@1415 132
mcimadamore@1415 133 static boolean[][] needsConversion = {
mcimadamore@1415 134 // VOID | SHORT | INT | INTEGER | NULL
mcimadamore@1415 135 /* SHORT */ { false , false , false , false , false },
mcimadamore@1415 136 /* INT */ { false , false , false , true , false },
mcimadamore@1415 137 /* OBJECT */ { false , true , true , false , false },
mcimadamore@1415 138 /* INTEGER */ { false , false , true , false , false },
mcimadamore@1415 139 /* VOID */ { false , false , false , false , false },
mcimadamore@1415 140 /* J_L_VOID */{ true , false , false , false , false } };
mcimadamore@1415 141 }
mcimadamore@1415 142
mcimadamore@1415 143 public static void main(String... args) throws Exception {
mcimadamore@1415 144 for (LambdaReturnKind lrk : LambdaReturnKind.values()) {
mcimadamore@1415 145 for (RetTypeKind rk1 : RetTypeKind.values()) {
mcimadamore@1415 146 for (RetTypeKind rk2 : RetTypeKind.values()) {
mcimadamore@1415 147 for (ExceptionKind ek1 : ExceptionKind.values()) {
mcimadamore@1415 148 for (ExceptionKind ek2 : ExceptionKind.values()) {
mcimadamore@1415 149 for (ArgTypeKind ak11 : ArgTypeKind.values()) {
mcimadamore@1415 150 for (ArgTypeKind ak12 : ArgTypeKind.values()) {
vromero@1482 151 pool.execute(
vromero@1482 152 new StructuralMostSpecificTest(lrk, rk1,
vromero@1482 153 rk2, ek1, ek2, ak11, ak12));
mcimadamore@1415 154 }
mcimadamore@1415 155 }
mcimadamore@1415 156 }
mcimadamore@1415 157 }
mcimadamore@1415 158 }
mcimadamore@1415 159 }
mcimadamore@1415 160 }
vromero@1482 161
vromero@1482 162 checkAfterExec();
mcimadamore@1415 163 }
mcimadamore@1415 164
mcimadamore@1415 165 LambdaReturnKind lrk;
mcimadamore@1415 166 RetTypeKind rt1, rt2;
mcimadamore@1415 167 ArgTypeKind ak1, ak2;
mcimadamore@1415 168 ExceptionKind ek1, ek2;
mcimadamore@1415 169 JavaSource source;
mcimadamore@1415 170 DiagnosticChecker diagChecker;
mcimadamore@1415 171
mcimadamore@1415 172 StructuralMostSpecificTest(LambdaReturnKind lrk, RetTypeKind rt1, RetTypeKind rt2,
mcimadamore@1415 173 ExceptionKind ek1, ExceptionKind ek2, ArgTypeKind ak1, ArgTypeKind ak2) {
mcimadamore@1415 174 this.lrk = lrk;
mcimadamore@1415 175 this.rt1 = rt1;
mcimadamore@1415 176 this.rt2 = rt2;
mcimadamore@1415 177 this.ek1 = ek1;
mcimadamore@1415 178 this.ek2 = ek2;
mcimadamore@1415 179 this.ak1 = ak1;
mcimadamore@1415 180 this.ak2 = ak2;
mcimadamore@1415 181 this.source = new JavaSource();
mcimadamore@1415 182 this.diagChecker = new DiagnosticChecker();
mcimadamore@1415 183 }
mcimadamore@1415 184
mcimadamore@1415 185 class JavaSource extends SimpleJavaFileObject {
mcimadamore@1415 186
mcimadamore@1415 187 String template = "interface SAM1 {\n" +
mcimadamore@1415 188 " #R1 m(#A1 a1) #E1;\n" +
mcimadamore@1415 189 "}\n" +
mcimadamore@1415 190 "interface SAM2 {\n" +
mcimadamore@1415 191 " #R2 m(#A2 a1) #E2;\n" +
mcimadamore@1415 192 "}\n" +
mcimadamore@1415 193 "class Test {\n" +
mcimadamore@1415 194 " void m(SAM1 s) { }\n" +
mcimadamore@1415 195 " void m(SAM2 s) { }\n" +
mcimadamore@1415 196 " { m(x->{ #LR }); }\n" +
mcimadamore@1415 197 "}\n";
mcimadamore@1415 198
mcimadamore@1415 199 String source;
mcimadamore@1415 200
mcimadamore@1415 201 public JavaSource() {
mcimadamore@1415 202 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
mcimadamore@1415 203 source = template.replaceAll("#LR", lrk.retStr)
mcimadamore@1415 204 .replaceAll("#R1", rt1.retTypeStr)
mcimadamore@1415 205 .replaceAll("#R2", rt2.retTypeStr)
mcimadamore@1415 206 .replaceAll("#A1", ak1.argTypeStr)
mcimadamore@1415 207 .replaceAll("#A2", ak2.argTypeStr)
mcimadamore@1415 208 .replaceAll("#E1", ek1.exceptionStr)
mcimadamore@1415 209 .replaceAll("#E2", ek2.exceptionStr);
mcimadamore@1415 210 }
mcimadamore@1415 211
mcimadamore@1415 212 @Override
mcimadamore@1415 213 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
mcimadamore@1415 214 return source;
mcimadamore@1415 215 }
mcimadamore@1415 216 }
mcimadamore@1415 217
vromero@1482 218 public void run() {
vromero@1482 219 JavacTask ct = (JavacTask)comp.getTask(null, fm.get(), diagChecker,
mcimadamore@1415 220 Arrays.asList("-XDverboseResolution=all,-predef,-internal,-object-init"),
mcimadamore@1415 221 null, Arrays.asList(source));
mcimadamore@1415 222 try {
mcimadamore@1415 223 ct.analyze();
mcimadamore@1415 224 } catch (Throwable ex) {
vromero@1482 225 throw new
vromero@1482 226 AssertionError("Error thron when analyzing the following source:\n" +
vromero@1482 227 source.getCharContent(true));
mcimadamore@1415 228 }
mcimadamore@1415 229 check();
mcimadamore@1415 230 }
mcimadamore@1415 231
mcimadamore@1415 232 void check() {
vromero@1482 233 checkCount.incrementAndGet();
mcimadamore@1415 234
mcimadamore@1415 235 if (!lrk.compatibleWith(rt1) || !lrk.compatibleWith(rt2))
mcimadamore@1415 236 return;
mcimadamore@1415 237
mcimadamore@1415 238 if (lrk.needsConversion(rt1) != lrk.needsConversion(rt2))
mcimadamore@1415 239 return;
mcimadamore@1415 240
mcimadamore@1415 241 boolean m1MoreSpecific = moreSpecific(rt1, rt2, ek1, ek2, ak1, ak2);
mcimadamore@1415 242 boolean m2MoreSpecific = moreSpecific(rt2, rt1, ek2, ek1, ak2, ak1);
mcimadamore@1415 243
mcimadamore@1415 244 boolean ambiguous = (m1MoreSpecific == m2MoreSpecific);
mcimadamore@1415 245
mcimadamore@1415 246 if (ambiguous != diagChecker.ambiguityFound) {
mcimadamore@1415 247 throw new Error("invalid diagnostics for source:\n" +
mcimadamore@1415 248 source.getCharContent(true) +
mcimadamore@1415 249 "\nAmbiguity found: " + diagChecker.ambiguityFound +
mcimadamore@1415 250 "\nm1 more specific: " + m1MoreSpecific +
mcimadamore@1415 251 "\nm2 more specific: " + m2MoreSpecific +
mcimadamore@1415 252 "\nexpected ambiguity: " + ambiguous);
mcimadamore@1415 253 }
mcimadamore@1415 254
mcimadamore@1415 255 if (!ambiguous) {
mcimadamore@1415 256 String sigToCheck = m1MoreSpecific ? "m(SAM1)" : "m(SAM2)";
mcimadamore@1415 257 if (!sigToCheck.equals(diagChecker.mostSpecificSig)) {
mcimadamore@1415 258 throw new Error("invalid most specific method selected:\n" +
mcimadamore@1415 259 source.getCharContent(true) +
mcimadamore@1415 260 "\nMost specific found: " + diagChecker.mostSpecificSig +
mcimadamore@1415 261 "\nm1 more specific: " + m1MoreSpecific +
mcimadamore@1415 262 "\nm2 more specific: " + m2MoreSpecific);
mcimadamore@1415 263 }
mcimadamore@1415 264 }
mcimadamore@1415 265 }
mcimadamore@1415 266
vromero@1482 267 boolean moreSpecific(RetTypeKind rk1, RetTypeKind rk2, ExceptionKind ek1,
vromero@1482 268 ExceptionKind ek2, ArgTypeKind ak1, ArgTypeKind ak2) {
mcimadamore@1415 269 if (!rk1.moreSpecificThan(rk2))
mcimadamore@1415 270 return false;
mcimadamore@1415 271
mcimadamore@1415 272 if (ak1 != ak2)
mcimadamore@1415 273 return false;
mcimadamore@1415 274
mcimadamore@1415 275 return true;
mcimadamore@1415 276 }
mcimadamore@1415 277
vromero@1482 278 static class DiagnosticChecker
vromero@1482 279 implements javax.tools.DiagnosticListener<JavaFileObject> {
mcimadamore@1415 280
mcimadamore@1415 281 boolean ambiguityFound;
mcimadamore@1415 282 String mostSpecificSig;
mcimadamore@1415 283
mcimadamore@1415 284 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
mcimadamore@1415 285 try {
mcimadamore@1415 286 if (diagnostic.getKind() == Diagnostic.Kind.ERROR &&
mcimadamore@1415 287 diagnostic.getCode().equals("compiler.err.ref.ambiguous")) {
mcimadamore@1415 288 ambiguityFound = true;
mcimadamore@1415 289 } else if (diagnostic.getKind() == Diagnostic.Kind.NOTE &&
vromero@1482 290 diagnostic.getCode()
vromero@1482 291 .equals("compiler.note.verbose.resolve.multi")) {
mcimadamore@1415 292 ClientCodeWrapper.DiagnosticSourceUnwrapper dsu =
vromero@1482 293 (ClientCodeWrapper.DiagnosticSourceUnwrapper)diagnostic;
vromero@1482 294 JCDiagnostic.MultilineDiagnostic mdiag =
vromero@1482 295 (JCDiagnostic.MultilineDiagnostic)dsu.d;
mcimadamore@1415 296 int mostSpecificIndex = (Integer)mdiag.getArgs()[2];
vromero@1482 297 mostSpecificSig =
vromero@1482 298 ((JCDiagnostic)mdiag.getSubdiagnostics()
vromero@1482 299 .get(mostSpecificIndex)).getArgs()[1].toString();
mcimadamore@1415 300 }
mcimadamore@1415 301 } catch (RuntimeException t) {
mcimadamore@1415 302 t.printStackTrace();
mcimadamore@1415 303 throw t;
mcimadamore@1415 304 }
mcimadamore@1415 305 }
mcimadamore@1415 306 }
vromero@1482 307
mcimadamore@1415 308 }

mercurial