comparison truffle/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/instrument/SLInstrumentTestRunner.java @ 22219:1c0f490984d5

Merge with f47b601edbc626dcfe8b3636933b4834c89f7779
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Wed, 16 Sep 2015 15:36:22 -0700
parents dc83cc1f94f2 3aad794eec0e
children a0fa69e3e60e
comparison
equal deleted inserted replaced
22160:0599e2df6a9f 22219:1c0f490984d5
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 * SOFTWARE. 39 * SOFTWARE.
40 */ 40 */
41 package com.oracle.truffle.sl.test.instrument; 41 package com.oracle.truffle.sl.test.instrument;
42 42
43 import com.oracle.truffle.api.instrument.ASTProber;
44 import com.oracle.truffle.api.instrument.Instrument;
45 import com.oracle.truffle.api.instrument.Probe;
46 import com.oracle.truffle.api.instrument.StandardSyntaxTag;
47 import com.oracle.truffle.api.instrument.impl.DefaultSimpleInstrumentListener;
48 import com.oracle.truffle.api.source.Source;
49 import com.oracle.truffle.api.vm.TruffleVM;
50 import com.oracle.truffle.sl.nodes.instrument.SLStandardASTProber;
51 import com.oracle.truffle.sl.nodes.local.SLWriteLocalVariableNode;
52 import com.oracle.truffle.sl.test.SLTestRunner;
53 import com.oracle.truffle.sl.test.instrument.SLInstrumentTestRunner.InstrumentTestCase;
54 import java.io.BufferedReader; 43 import java.io.BufferedReader;
55 import java.io.ByteArrayOutputStream; 44 import java.io.ByteArrayOutputStream;
56 import java.io.FileNotFoundException; 45 import java.io.FileNotFoundException;
57 import java.io.IOException; 46 import java.io.IOException;
58 import java.io.PrintWriter; 47 import java.io.PrintWriter;
59 import java.io.StringReader; 48 import java.io.StringReader;
49 import java.lang.reflect.Field;
60 import java.nio.charset.Charset; 50 import java.nio.charset.Charset;
61 import java.nio.file.FileSystems; 51 import java.nio.file.FileSystems;
62 import java.nio.file.FileVisitResult; 52 import java.nio.file.FileVisitResult;
63 import java.nio.file.Files; 53 import java.nio.file.Files;
64 import java.nio.file.Path; 54 import java.nio.file.Path;
65 import java.nio.file.SimpleFileVisitor; 55 import java.nio.file.SimpleFileVisitor;
66 import java.nio.file.attribute.BasicFileAttributes; 56 import java.nio.file.attribute.BasicFileAttributes;
67 import java.util.ArrayList; 57 import java.util.ArrayList;
68 import java.util.List; 58 import java.util.List;
59
69 import org.junit.Assert; 60 import org.junit.Assert;
70 import org.junit.internal.TextListener; 61 import org.junit.internal.TextListener;
71 import org.junit.runner.Description; 62 import org.junit.runner.Description;
72 import org.junit.runner.JUnitCore; 63 import org.junit.runner.JUnitCore;
73 import org.junit.runner.Result; 64 import org.junit.runner.Result;
76 import org.junit.runner.notification.Failure; 67 import org.junit.runner.notification.Failure;
77 import org.junit.runner.notification.RunNotifier; 68 import org.junit.runner.notification.RunNotifier;
78 import org.junit.runners.ParentRunner; 69 import org.junit.runners.ParentRunner;
79 import org.junit.runners.model.InitializationError; 70 import org.junit.runners.model.InitializationError;
80 71
72 import com.oracle.truffle.api.instrument.ASTProber;
73 import com.oracle.truffle.api.instrument.Instrument;
74 import com.oracle.truffle.api.instrument.Instrumenter;
75 import com.oracle.truffle.api.instrument.Probe;
76 import com.oracle.truffle.api.instrument.StandardSyntaxTag;
77 import com.oracle.truffle.api.instrument.impl.DefaultSimpleInstrumentListener;
78 import com.oracle.truffle.api.source.Source;
79 import com.oracle.truffle.api.vm.TruffleVM;
80 import com.oracle.truffle.sl.nodes.instrument.SLStandardASTProber;
81 import com.oracle.truffle.sl.nodes.local.SLWriteLocalVariableNode;
82 import com.oracle.truffle.sl.test.SLTestRunner;
83 import com.oracle.truffle.sl.test.instrument.SLInstrumentTestRunner.InstrumentTestCase;
84
81 /** 85 /**
82 * This class builds and executes the tests for instrumenting SL. Although much of this class is 86 * This class builds and executes the tests for instrumenting SL. Although much of this class is
83 * written with future automation in mind, at the moment the tests that are created are hard-coded 87 * written with future automation in mind, at the moment the tests that are created are hard-coded
84 * according to the file name of the test. To be automated, an automatic way of generating both the 88 * according to the file name of the test. To be automated, an automatic way of generating both the
85 * node visitor and the node prober is necessary. 89 * node visitor and the node prober is necessary.
93 private static final String OUTPUT_SUFFIX = ".output"; 97 private static final String OUTPUT_SUFFIX = ".output";
94 private static final String ASSIGNMENT_VALUE_SUFFIX = "_assnCount"; 98 private static final String ASSIGNMENT_VALUE_SUFFIX = "_assnCount";
95 99
96 private static final String LF = System.getProperty("line.separator"); 100 private static final String LF = System.getProperty("line.separator");
97 101
98 static class InstrumentTestCase { 102 static final class InstrumentTestCase {
99 protected final Description name; 103 protected final Description name;
100 protected final Path path; 104 protected final Path path;
101 protected final String baseName; 105 protected final String baseName;
102 protected final String sourceName; 106 protected final String sourceName;
103 protected final String testInput; 107 protected final String testInput;
114 } 118 }
115 } 119 }
116 120
117 private final List<InstrumentTestCase> testCases; 121 private final List<InstrumentTestCase> testCases;
118 122
119 public SLInstrumentTestRunner(Class<?> testClass) throws InitializationError { 123 public SLInstrumentTestRunner(Class<?> testClass) throws InitializationError, SecurityException, IllegalArgumentException {
120 super(testClass); 124 super(testClass);
121 final SLStandardASTProber prober = new SLStandardASTProber();
122 Probe.registerASTProber(prober);
123 try { 125 try {
124 testCases = createTests(testClass); 126 testCases = createTests(testClass);
125 } catch (IOException e) { 127 } catch (IOException e) {
126 throw new InitializationError(e); 128 throw new InitializationError(e);
127 } finally {
128 Probe.unregisterASTProber(prober);
129 } 129 }
130 } 130 }
131 131
132 @Override 132 @Override
133 protected List<InstrumentTestCase> getChildren() { 133 protected List<InstrumentTestCase> getChildren() {
235 notifier.fireTestStarted(testCase.name); 235 notifier.fireTestStarted(testCase.name);
236 236
237 ByteArrayOutputStream out = new ByteArrayOutputStream(); 237 ByteArrayOutputStream out = new ByteArrayOutputStream();
238 PrintWriter printer = new PrintWriter(out); 238 PrintWriter printer = new PrintWriter(out);
239 final ASTProber prober = new SLStandardASTProber(); 239 final ASTProber prober = new SLStandardASTProber();
240 Probe.registerASTProber(prober); 240
241 try { 241 try {
242 // We use the name of the file to determine what visitor to attach to it. 242 // We use the name of the file to determine what visitor to attach to it.
243 if (testCase.baseName.endsWith(ASSIGNMENT_VALUE_SUFFIX)) { 243 if (testCase.baseName.endsWith(ASSIGNMENT_VALUE_SUFFIX)) {
244 // Set up the execution context for Simple and register our two listeners 244 // Set up the execution context for Simple and register our two listeners
245 TruffleVM vm = TruffleVM.newVM().stdIn(new BufferedReader(new StringReader(testCase.testInput))).stdOut(printer).build(); 245 TruffleVM vm = TruffleVM.newVM().stdIn(new BufferedReader(new StringReader(testCase.testInput))).stdOut(printer).build();
246 246
247 final Field field = TruffleVM.class.getDeclaredField("instrumenter");
248 field.setAccessible(true);
249 final Instrumenter instrumenter = (Instrumenter) field.get(vm);
250 instrumenter.registerASTProber(prober);
251
247 final String src = readAllLines(testCase.path); 252 final String src = readAllLines(testCase.path);
248 vm.eval(Source.fromText(src, testCase.path.toString()).withMimeType("application/x-sl")); 253 vm.eval(Source.fromText(src, testCase.path.toString()).withMimeType("application/x-sl"));
249 254
250 // Attach an instrument to every probe tagged as an assignment 255 // Attach an instrument to every probe tagged as an assignment
251 for (Probe probe : Probe.findProbesTaggedAs(StandardSyntaxTag.ASSIGNMENT)) { 256 for (Probe probe : instrumenter.findProbesTaggedAs(StandardSyntaxTag.ASSIGNMENT)) {
252 SLPrintAssigmentValueListener slPrintAssigmentValueListener = new SLPrintAssigmentValueListener(printer); 257 SLPrintAssigmentValueListener slPrintAssigmentValueListener = new SLPrintAssigmentValueListener(printer);
253 probe.attach(Instrument.create(slPrintAssigmentValueListener, "SL print assignment value")); 258 probe.attach(Instrument.create(slPrintAssigmentValueListener, "SL print assignment value"));
254 } 259 }
255 260
256 TruffleVM.Symbol main = vm.findGlobalSymbol("main"); 261 TruffleVM.Symbol main = vm.findGlobalSymbol("main");
263 String actualOutput = new String(out.toByteArray()); 268 String actualOutput = new String(out.toByteArray());
264 Assert.assertEquals(testCase.expectedOutput, actualOutput); 269 Assert.assertEquals(testCase.expectedOutput, actualOutput);
265 } catch (Throwable ex) { 270 } catch (Throwable ex) {
266 notifier.fireTestFailure(new Failure(testCase.name, ex)); 271 notifier.fireTestFailure(new Failure(testCase.name, ex));
267 } finally { 272 } finally {
268 Probe.unregisterASTProber(prober);
269 notifier.fireTestFinished(testCase.name); 273 notifier.fireTestFinished(testCase.name);
270 } 274 }
271 275
272 } 276 }
273 277
274 public static void runInMain(Class<?> testClass, String[] args) throws InitializationError, NoTestsRemainException { 278 public static void runInMain(Class<?> testClass, String[] args) throws InitializationError, NoTestsRemainException, SecurityException, IllegalArgumentException {
275 JUnitCore core = new JUnitCore(); 279 JUnitCore core = new JUnitCore();
276 core.addListener(new TextListener(System.out)); 280 core.addListener(new TextListener(System.out));
277 SLInstrumentTestRunner suite = new SLInstrumentTestRunner(testClass); 281 SLInstrumentTestRunner suite = new SLInstrumentTestRunner(testClass);
278 if (args.length > 0) { 282 if (args.length > 0) {
279 suite.filter(new NameFilter(args[0])); 283 suite.filter(new NameFilter(args[0]));