test/tools/javac/lambdaShapes/org/openjdk/tests/vm/FDSeparateCompilationTest.java

changeset 1422
d898d9ee352f
child 1448
7d34e91f66bb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambdaShapes/org/openjdk/tests/vm/FDSeparateCompilationTest.java	Tue Nov 20 09:58:55 2012 -0800
     1.3 @@ -0,0 +1,197 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package org.openjdk.tests.vm;
    1.30 +
    1.31 +import java.util.*;
    1.32 +
    1.33 +import org.testng.ITestResult;
    1.34 +import org.testng.annotations.Test;
    1.35 +import org.testng.annotations.DataProvider;
    1.36 +import org.testng.annotations.AfterMethod;
    1.37 +import org.testng.annotations.AfterSuite;
    1.38 +
    1.39 +import org.openjdk.tests.separate.*;
    1.40 +import org.openjdk.tests.separate.Compiler;
    1.41 +
    1.42 +import org.openjdk.tests.shapegen.Hierarchy;
    1.43 +import org.openjdk.tests.shapegen.HierarchyGenerator;
    1.44 +import org.openjdk.tests.shapegen.ClassCase;
    1.45 +
    1.46 +import static org.testng.Assert.*;
    1.47 +import static org.openjdk.tests.separate.SourceModel.*;
    1.48 +import static org.openjdk.tests.separate.SourceModel.Class;
    1.49 +import static org.openjdk.tests.separate.SourceModel.Method;
    1.50 +import static org.openjdk.tests.separate.SourceModel.Type;
    1.51 +
    1.52 +public class FDSeparateCompilationTest extends TestHarness {
    1.53 +
    1.54 +    private static String EMPTY = "\"\"";
    1.55 +
    1.56 +    public FDSeparateCompilationTest() {
    1.57 +        super(false, true);
    1.58 +    }
    1.59 +
    1.60 +    @DataProvider(name = "allShapes", parallel = true)
    1.61 +    public Object[][] hierarchyGenerator() {
    1.62 +        ArrayList<Object[]> allCases = new ArrayList<>();
    1.63 +
    1.64 +        HierarchyGenerator hg = new HierarchyGenerator();
    1.65 +        for (Object x : hg.getOK()) {
    1.66 +            allCases.add(new Object[]{x});
    1.67 +        }
    1.68 +        for (Object x : hg.getErr()) {
    1.69 +            allCases.add(new Object[]{x});
    1.70 +        }
    1.71 +        return allCases.toArray(new Object[0][]);
    1.72 +    }
    1.73 +
    1.74 +    // The expected value obtained when invoking the method from the specified
    1.75 +    // class.  If returns null, then an AbstractMethodError is expected.
    1.76 +    private static String getExpectedResult(ClassCase cc) {
    1.77 +        Set<ClassCase> provs = cc.get_mprov();
    1.78 +        if (cc.get_mres() != null) {
    1.79 +            return cc.get_mres().getName();
    1.80 +        } else if (provs != null && provs.size() == 1) {
    1.81 +            ClassCase cand = provs.iterator().next();
    1.82 +            switch (cand.kind) {
    1.83 +                case CCONCRETE:
    1.84 +                case IDEFAULT:
    1.85 +                    return cand.getName();
    1.86 +                case CNONE:
    1.87 +                case IVAC:
    1.88 +                    return getExpectedResult(cand);
    1.89 +            }
    1.90 +        }
    1.91 +        return null;
    1.92 +    }
    1.93 +
    1.94 +    private static final ConcreteMethod canonicalMethod = new ConcreteMethod(
    1.95 +            "String", "m", "returns " + EMPTY + ";", AccessFlag.PUBLIC);
    1.96 +
    1.97 +    @Test(groups = "vm", dataProvider = "allShapes")
    1.98 +    public void separateCompilationTest(Hierarchy hs) {
    1.99 +        ClassCase cc = hs.root;
   1.100 +        Type type = sourceTypeFrom(hs.root);
   1.101 +
   1.102 +        Class specimen = null;
   1.103 +        if (type instanceof Class) {
   1.104 +            Class ctype = (Class)type;
   1.105 +            if (ctype.isAbstract()) {
   1.106 +                specimen = new Class("Test" + ctype.getName(), ctype);
   1.107 +            } else {
   1.108 +                specimen = ctype;
   1.109 +            }
   1.110 +        } else {
   1.111 +            specimen = new Class("Test" + type.getName(), (Interface)type);
   1.112 +        }
   1.113 +
   1.114 +        String value = getExpectedResult(cc);
   1.115 +        if (value != null) {
   1.116 +            assertInvokeVirtualEquals(value, specimen, canonicalMethod, EMPTY);
   1.117 +        } else {
   1.118 +            assertThrows(AbstractMethodError.class, specimen,
   1.119 +                canonicalMethod, EMPTY);
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    @AfterMethod
   1.124 +    public void printCaseError(ITestResult result) {
   1.125 +        if (result.getStatus() == ITestResult.FAILURE) {
   1.126 +            Hierarchy hs = (Hierarchy)result.getParameters()[0];
   1.127 +            System.out.println("Separate compilation case " + hs);
   1.128 +            printCaseDetails(hs);
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    @AfterSuite
   1.133 +    public void cleanupCompilerCache() {
   1.134 +        Compiler.purgeCache();
   1.135 +    }
   1.136 +
   1.137 +    private void printCaseDetails(Hierarchy hs) {
   1.138 +        String exp = getExpectedResult(hs.root);
   1.139 +        for (String s : hs.getDescription()) {
   1.140 +             System.out.println("    " + s);
   1.141 +        }
   1.142 +        if (exp != null) {
   1.143 +            System.out.println("    Expected \"" + exp + "\"");
   1.144 +        } else {
   1.145 +            System.out.println("    Expected AbstractMethodError");
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +    private Type sourceTypeFrom(ClassCase cc) {
   1.150 +        Type type = null;
   1.151 +
   1.152 +        if (cc.isInterface()) {
   1.153 +            Interface iface = new Interface(cc.getName());
   1.154 +            for (ClassCase scc : cc.getInterfaces()) {
   1.155 +                Interface supertype = (Interface)sourceTypeFrom(scc);
   1.156 +                iface.addSuperType(supertype);
   1.157 +            }
   1.158 +            type = iface;
   1.159 +        } else {
   1.160 +            Class cls = new Class(cc.getName());
   1.161 +            if (cc.hasSuperclass()) {
   1.162 +                Class superc = (Class)sourceTypeFrom(cc.getSuperclass());
   1.163 +                cls.setSuperClass(superc);
   1.164 +            }
   1.165 +            for (ClassCase scc : cc.getInterfaces()) {
   1.166 +                Interface supertype = (Interface)sourceTypeFrom(scc);
   1.167 +                cls.addSuperType(supertype);
   1.168 +            }
   1.169 +            if (cc.isAbstract()) {
   1.170 +                cls.getAccessFlags().add(AccessFlag.ABSTRACT);
   1.171 +            }
   1.172 +            type = cls;
   1.173 +        }
   1.174 +        Method method = methodFrom(cc);
   1.175 +        if (method != null) {
   1.176 +            type.addMethod(method);
   1.177 +        }
   1.178 +        return type;
   1.179 +    }
   1.180 +
   1.181 +    private Method methodFrom(ClassCase cc) {
   1.182 +        switch (cc.kind) {
   1.183 +            case IVAC:
   1.184 +            case CNONE: return null;
   1.185 +            case IPRESENT:
   1.186 +            case CABSTRACT:
   1.187 +                return new AbstractMethod("String", "m", AccessFlag.PUBLIC);
   1.188 +            case IDEFAULT:
   1.189 +                return new DefaultMethod(
   1.190 +                    "String", "m", "return \"" + cc.getName() + "\";");
   1.191 +            case CCONCRETE:
   1.192 +                return new ConcreteMethod(
   1.193 +                    "String", "m", "return \"" + cc.getName() + "\";",
   1.194 +                    AccessFlag.PUBLIC);
   1.195 +            default:
   1.196 +                fail("Unknown method type in class");
   1.197 +                return null;
   1.198 +        }
   1.199 +    }
   1.200 +}

mercurial