package com.oracle.truffle.tools.profiler.test;
import java.io.ByteArrayOutputStream;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.instrumentation.StandardTags;
import com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage;
public abstract class AbstractProfilerTest {
protected Context context;
protected final ByteArrayOutputStream out = new ByteArrayOutputStream();
protected final ByteArrayOutputStream err = new ByteArrayOutputStream();
protected final Source defaultSource = makeSource(
"ROOT(" +
"DEFINE(foo,ROOT(STATEMENT))," +
"DEFINE(bar,ROOT(BLOCK(STATEMENT,LOOP(10, CALL(foo)))))," +
"DEFINE(baz,ROOT(BLOCK(STATEMENT,LOOP(10, CALL(bar)))))," +
"CALL(baz),CALL(bar)" +
")");
protected final Source defaultRecursiveSource = makeSource(
"ROOT(" +
"DEFINE(foo,ROOT(BLOCK(STATEMENT,RECURSIVE_CALL(foo, 10))))," +
"DEFINE(bar,ROOT(BLOCK(STATEMENT,LOOP(10, CALL(foo)))))," +
"CALL(bar)" +
")");
protected static void expectProfilerException(Runnable configProfiler, Runnable runProfiler) {
try {
configProfiler.run();
runProfiler.run();
Assert.fail("Exception expected.");
} catch (Exception e) {
Assert.assertEquals("class com.oracle.truffle.tools.profiler.ProfilerException", e.getClass().toString());
}
}
protected Source makeSource(String s) {
return Source.newBuilder(InstrumentationTestLanguage.ID, s, "test").buildLiteral();
}
protected static final SourceSectionFilter NO_INTERNAL_ROOT_TAG_FILTER = SourceSectionFilter.
newBuilder().sourceIs(s -> !s.isInternal()).
tagIs(StandardTags.RootTag.class).build();
protected static final SourceSectionFilter NO_INTERNAL_CALL_TAG_FILTER = SourceSectionFilter.
newBuilder().sourceIs(s -> !s.isInternal()).
tagIs(StandardTags.CallTag.class).build();
protected static final SourceSectionFilter NO_INTERNAL_STATEMENT_TAG_FILTER = SourceSectionFilter.
newBuilder().sourceIs(s -> !s.isInternal()).
tagIs(StandardTags.StatementTag.class).build();
@Before
public void setup() {
context = Context.newBuilder().in(System.in).out(out).err(err).build();
}
protected void eval(Source source) {
context.eval(source);
}
@After
public void after() {
context.close();
}
}