test/tools/javac/scope/7017664/CompoundScopeTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7017664 7036906
aoqi@0 27 * @summary Basher for CompoundScopes
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.util.Random;
aoqi@0 31 import java.util.Map;
aoqi@0 32 import java.util.HashMap;
aoqi@0 33 import com.sun.tools.javac.util.*;
aoqi@0 34 import com.sun.tools.javac.code.*;
aoqi@0 35 import com.sun.tools.javac.code.Scope.*;
aoqi@0 36 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 37 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 38
aoqi@0 39 public class CompoundScopeTest {
aoqi@0 40 public static void main(String... args) throws Exception {
aoqi@0 41 new CompoundScopeTest().run(args);
aoqi@0 42 }
aoqi@0 43
aoqi@0 44 static final int MAX_SYMBOLS_COUNT = 20;
aoqi@0 45 static final int PASSES = 10;
aoqi@0 46
aoqi@0 47 void run(String... args) throws Exception {
aoqi@0 48 int count = PASSES;
aoqi@0 49
aoqi@0 50 for (int i = 0; i < args.length; i++) {
aoqi@0 51 String arg = args[i];
aoqi@0 52 if (arg.equals("-seed") && (i + 1 < args.length))
aoqi@0 53 seed = Long.parseLong(args[++i]);
aoqi@0 54 else if(arg.equals("-tests") && (i + 1 < args.length))
aoqi@0 55 count = Integer.parseInt(args[++i]);
aoqi@0 56 else
aoqi@0 57 throw new Exception("unknown arg: " + arg);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 rgen = new Random(seed);
aoqi@0 61
aoqi@0 62 for (int i = 0; i < count; i++) {
aoqi@0 63 Test t = new Test();
aoqi@0 64 t.run();
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 if (errors > 0)
aoqi@0 68 throw new Exception(errors + " errors found");
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * Write a message to stderr.
aoqi@0 73 */
aoqi@0 74 void log(String msg) {
aoqi@0 75 System.err.println(msg);
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * Write an error message to stderr.
aoqi@0 80 */
aoqi@0 81 void error(String msg) {
aoqi@0 82 System.err.println("Error: " + msg);
aoqi@0 83 errors++;
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 Random rgen;
aoqi@0 87 long seed = 0;
aoqi@0 88
aoqi@0 89 int errors;
aoqi@0 90
aoqi@0 91 /** Class to encapsulate a test run. */
aoqi@0 92 class Test {
aoqi@0 93
aoqi@0 94 List<Symbol> elems = List.nil();
aoqi@0 95 Map<Name, List<Symbol>> shadowedMap = new HashMap<Name, List<Symbol>>();
aoqi@0 96
aoqi@0 97 /** Run the test. */
aoqi@0 98 void run() throws Exception {
aoqi@0 99 log ("starting test");
aoqi@0 100 setup();
aoqi@0 101 Scope[] scopes = { createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)),
aoqi@0 102 createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)),
aoqi@0 103 createScope(rgen.nextInt(MAX_SYMBOLS_COUNT)) };
aoqi@0 104 boolean[][] scopeNesting = { {false, true, false, true},
aoqi@0 105 {false, true, true, true},
aoqi@0 106 {false, false, true, true} };
aoqi@0 107 /**
aoqi@0 108 * We want to generate (and check) the following compound scopes:
aoqi@0 109 * C1 = C(S1, S2, S3)
aoqi@0 110 * C2 = C((S1, S2), S3)
aoqi@0 111 * C3 = C(S1, (S2, S3))
aoqi@0 112 * C3 = C(C(S1, S2, S3))
aoqi@0 113 */
aoqi@0 114 for (int i = 0 ; i < 4 ; i ++) {
aoqi@0 115 CompoundScope root = new CompoundScope(symtab.noSymbol);
aoqi@0 116 CompoundScope sub = new CompoundScope(symtab.noSymbol);
aoqi@0 117 boolean subAdded = false;
aoqi@0 118 for (int sc = 0 ; sc < 3 ; sc ++) {
aoqi@0 119 if (scopeNesting[sc][i]) {
aoqi@0 120 sub.addSubScope(scopes[sc]);
aoqi@0 121 if (!subAdded) {
aoqi@0 122 root.addSubScope(sub);
aoqi@0 123 subAdded = true;
aoqi@0 124 }
aoqi@0 125 } else {
aoqi@0 126 root.addSubScope(scopes[sc]);
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129 log("testing scope: " + root);
aoqi@0 130 checkElems(root, null);
aoqi@0 131 checkElems(root, new OddFilter());
aoqi@0 132 checkShadowed(root, null);
aoqi@0 133 checkShadowed(root, new OddFilter());
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 class OddFilter implements Filter<Symbol> {
aoqi@0 138 public boolean accepts(Symbol s) {
aoqi@0 139 Name numPart = s.name.subName(1, s.name.length());
aoqi@0 140 return Integer.parseInt(numPart.toString()) % 2 != 0;
aoqi@0 141 }
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 /**
aoqi@0 145 * Create a scope containing a given number of synthetic symbols
aoqi@0 146 */
aoqi@0 147 Scope createScope(int nelems) {
aoqi@0 148 Scope s = new Scope(symtab.noSymbol);
aoqi@0 149 for (int i = 0 ; i < nelems ; i++) {
aoqi@0 150 Symbol sym = new TypeVariableSymbol(0, names.fromString("s" + i), null, null);
aoqi@0 151 s.enter(sym);
aoqi@0 152 elems = elems.prepend(sym);
aoqi@0 153 List<Symbol> shadowed = shadowedMap.get(sym.name);
aoqi@0 154 if (shadowed == null) {
aoqi@0 155 shadowed = List.nil();
aoqi@0 156 }
aoqi@0 157 shadowedMap.put(sym.name, shadowed.prepend(sym));
aoqi@0 158 }
aoqi@0 159 return s;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 /**
aoqi@0 163 * Setup compiler context
aoqi@0 164 */
aoqi@0 165 void setup() {
aoqi@0 166 log ("setup");
aoqi@0 167 context = new Context();
aoqi@0 168 JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
aoqi@0 169 names = Names.instance(context); // Name.Table impls tied to an instance of Names
aoqi@0 170 symtab = Symtab.instance(context);
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 /**
aoqi@0 174 * Check that CompoundScope.getElements() correctly visits all symbols
aoqi@0 175 * in all subscopes (in the correct order)
aoqi@0 176 */
aoqi@0 177 void checkElems(CompoundScope cs, Filter<Symbol> sf) {
aoqi@0 178 int count = 0;
aoqi@0 179 ListBuffer<Symbol> found = new ListBuffer<>();
aoqi@0 180 List<Symbol> allSymbols = sf == null ?
aoqi@0 181 elems :
aoqi@0 182 filter(elems, sf);
aoqi@0 183 int expectedCount = allSymbols.length();
aoqi@0 184 for (Symbol s : sf == null ? cs.getElements() : cs.getElements(sf)) {
aoqi@0 185 checkSameSymbols(s, allSymbols.head);
aoqi@0 186 allSymbols = allSymbols.tail;
aoqi@0 187 found.append(s);
aoqi@0 188 count++;
aoqi@0 189 }
aoqi@0 190 if (count != expectedCount) {
aoqi@0 191 error("CompoundScope.getElements() did not returned enough symbols");
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Check that CompoundScope.getElements() correctly visits all symbols
aoqi@0 197 * with a given name in all subscopes (in the correct order)
aoqi@0 198 */
aoqi@0 199 void checkShadowed(CompoundScope cs, Filter<Symbol> sf) {
aoqi@0 200 for (Map.Entry<Name, List<Symbol>> shadowedEntry : shadowedMap.entrySet()) {
aoqi@0 201 int count = 0;
aoqi@0 202 List<Symbol> shadowed = sf == null ?
aoqi@0 203 shadowedEntry.getValue() :
aoqi@0 204 filter(shadowedEntry.getValue(), sf);
aoqi@0 205 int expectedCount = shadowed.length();
aoqi@0 206 Name name = shadowedEntry.getKey();
aoqi@0 207 for (Symbol s : sf == null ? cs.getElementsByName(name) : cs.getElementsByName(name, sf)) {
aoqi@0 208 checkSameSymbols(s, shadowed.head);
aoqi@0 209 shadowed = shadowed.tail;
aoqi@0 210 count++;
aoqi@0 211 }
aoqi@0 212 if (count != expectedCount) {
aoqi@0 213 error("CompoundScope.lookup() did not returned enough symbols for name " + name);
aoqi@0 214 }
aoqi@0 215 }
aoqi@0 216 }
aoqi@0 217
aoqi@0 218 List<Symbol> filter(List<Symbol> elems, Filter<Symbol> sf) {
aoqi@0 219 ListBuffer<Symbol> res = new ListBuffer<>();
aoqi@0 220 for (Symbol s : elems) {
aoqi@0 221 if (sf.accepts(s)) {
aoqi@0 222 res.append(s);
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225 return res.toList();
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 void checkSameSymbols(Symbol found, Symbol req) {
aoqi@0 229 if (found != req) {
aoqi@0 230 error("Symbol mismatch - found : " + found + ":" + found.hashCode() + "\n" +
aoqi@0 231 " required : " + req + ":" + req.hashCode());
aoqi@0 232 }
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 Context context;
aoqi@0 236 Symtab symtab;
aoqi@0 237 Names names;
aoqi@0 238 }
aoqi@0 239 }

mercurial