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

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 877
351027202f60
child 982
671bb63f3ed5
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

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

mercurial