package com.oracle.truffle.llvm.tests.bitcodeformat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.oracle.truffle.llvm.tests.Platform;
import com.oracle.truffle.llvm.tests.pipe.CaptureNativeOutput;
import com.oracle.truffle.llvm.tests.pipe.CaptureOutput;
import com.oracle.truffle.llvm.tests.util.ProcessUtil;
import org.graalvm.polyglot.Context;
import com.oracle.truffle.llvm.tests.options.TestOptions;
import com.oracle.truffle.tck.TruffleRunner;
import org.junit.Assume;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runners.Parameterized;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(Parameterized.class)
public class BitcodeFormatTest {
@ClassRule public static TruffleRunner.RunWithPolyglotRule runWithPolyglot = new TruffleRunner.RunWithPolyglotRule();
private static final Path testBase = Paths.get(TestOptions.TEST_SUITE_PATH, "bitcodeformat");
protected Map<String, String> getContextOptions() {
return Collections.emptyMap();
}
protected Function<Context.Builder, CaptureOutput> getCaptureOutput() {
return c -> new CaptureNativeOutput();
}
private void runCandidate(Path candidateBinary) throws IOException {
assertTrue("File " + candidateBinary.toAbsolutePath().toFile() + " does not exist.",
candidateBinary.toAbsolutePath().toFile().exists());
ProcessUtil.ProcessResult result;
result = ProcessUtil.executeSulongTestMain(candidateBinary.toAbsolutePath().toFile(), new String[]{},
getContextOptions(), getCaptureOutput());
int sulongRet = result.getReturnValue();
assertEquals(0, sulongRet);
assertEquals("Hello, World!\n", result.getStdOutput());
}
@Parameters(name = "{0}")
public static Collection<Object[]> data() throws IOException {
Set<String> blacklist = getBlacklist();
Collection<Object[]> testlist = Files.list(testBase).map(f -> new Object[]{f.getFileName()}).collect(Collectors.toList());
testlist.removeIf(t -> blacklist.contains(t[0].toString()));
return testlist;
}
protected static Set<String> getBlacklist() {
Set<String> blacklist = new HashSet<>();
if (Platform.isAArch64()) {
blacklist.addAll(Arrays.asList(
"hello-linux-link-fembed-bitcode", "hello-linux-link-fembed-bitcode.so"));
}
return blacklist;
}
@Parameter(0) public Path value;
@Before
public void checkOS() {
Assume.assumeTrue("Linux only test", !Platform.isDarwin() || !value.toString().contains("linux-link"));
Assume.assumeTrue("Darwin only test", Platform.isDarwin() || !value.toString().contains("darwin-link"));
}
@Test
public void checkNumbers() throws IOException {
runCandidate(testBase.resolve(value));
}
}