Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R script tests #5

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .githooks/pre-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

everything_staged=$(git diff --name-only)

if [ -z "$everything_staged" ]; then
if [ -n "$everything_staged" ]; then
# Don't commit because, after we reformat, the formatting will be unstaged, but it's too hard to determine (and maybe
# ambiguous) what are the formatting differences and what was originally unstaged.
mvn spotless:check || {
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.snapshots/**/*.fail*

### Build ###
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
Expand Down
2 changes: 1 addition & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified .idea/settings.zip
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/arithmetic.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/arithmetic.bc.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/calls.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/calls.bc.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/conditions.ast.rds
Binary file not shown.
Binary file added .snapshots/org/prlprg/bc/basics.R/conditions.bc.rds
Binary file not shown.
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
</dependency>
</dependencies>
<build>
<testResources>
<!-- Resources including R scripts which are treated as data -->
<testResource>
<directory>src/test/resources</directory>
</testResource>
<!-- R scripts which are treated as code -->
<testResource>
<directory>src/test/R</directory>
</testResource>
</testResources>
<plugins>
<!-- git-build-hook, install git hooks when you run any maven command so you don't forget -->
<plugin>
Expand Down Expand Up @@ -257,6 +267,11 @@
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<!-- Run tests in parallel. You can annotate tests with @NotThreadSafe if necessary -->
<configuration>
<parallel>all</parallel>
<threadCount>8</threadCount>
</configuration>
<!-- automatically runs on `test` (and `verify` because `verify` invokes `test`) -->
</plugin>
</plugins>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/prlprg/rds/RDSReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -34,6 +36,10 @@ public static SEXP readStream(InputStream input) throws IOException {
}
}

public static SEXP readFile(Path path) throws IOException {
return readStream(Files.newInputStream(path));
}

private void readHeader() throws IOException {
var type = in.readByte();
if (type != 'X') {
Expand Down
13 changes: 13 additions & 0 deletions src/test/R/org/prlprg/bc/serialize-closures.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.args <- commandArgs(trailingOnly = TRUE)
.source <- .args[1]
.target <- .args[2]

source(.source, chdir = TRUE)
dir.create(.target)
for (.name in ls()) {
if (is.function(get(.name))) {
.func <- get(.name)
saveRDS(.func, compress = FALSE, file = file.path(.target, paste0(.name, ".ast.rds")))
saveRDS(compiler::cmpfun(.func), compress = FALSE, file = file.path(.target, paste0(.name, ".bc.rds")))
}
}
55 changes: 54 additions & 1 deletion src/test/java/org/prlprg/bc/CompilerTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
package org.prlprg.bc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.prlprg.util.Assertions.assertSnapshot;
import static org.prlprg.util.StructuralUtils.printStructurally;

import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.prlprg.compile.Compiler;
import org.prlprg.rds.RDSReader;
import org.prlprg.sexp.BCodeSXP;
import org.prlprg.sexp.CloSXP;
import org.prlprg.util.DirectorySource;
import org.prlprg.util.Files;
import org.prlprg.util.SubTest;
import org.prlprg.util.Tests;

public class CompilerTest implements Tests {
@Test
public void testBasic() throws Exception {
public void testSomeOutput() throws IOException {
var source = (CloSXP) RDSReader.readStream(getResourceAsStream("f1.rds"));
var bc = Compiler.compileFun(source);
System.out.println(bc);
}

@Disabled
@ParameterizedTest(name = "Commutative read/compile {0}")
@DirectorySource(glob = "*.R", relativize = true, exclude = "serialize-closures.R")
void testCommutativeReadAndCompile(Path sourceName) {
var sourcePath = getResourcePath(sourceName);
var compiledRoot = getSnapshotPath(sourceName);

// Generate `compiledRoot` from `sourcePath` using GNU-R if necessary
if (Files.exists(compiledRoot) && Files.isOlder(compiledRoot, sourcePath)) {
Files.deleteRecursively(compiledRoot);
}
if (!Files.exists(compiledRoot)) {
// Generate `compiledRoot` from `sourcePath` using GNU-R
cmd(
"R",
"-s",
"-f",
getResourcePath("serialize-closures.R"),
"--args",
sourcePath,
compiledRoot);
}

// Test each closure in the file
for (var astPath : Files.listDir(compiledRoot, "*.ast.rds", 1, false, false)) {
var name = astPath.getFileName().toString().split("\\.")[0];
var bcPath = compiledRoot.resolve(name + ".bc.rds");
var bcOutPath = compiledRoot.resolve(name + ".bc.out");
SubTest.run(
name,
() -> {
var astClos = (CloSXP) RDSReader.readFile(astPath);
var bcClos = (CloSXP) RDSReader.readFile(bcPath);
var ourBc = printStructurally(Compiler.compileFun(astClos));
var rBc = printStructurally(((BCodeSXP) bcClos.body()).bc());
assertEquals(ourBc, rBc, "`compile(read(ast)) == read(R.compile(ast))`");
assertSnapshot(bcOutPath, bcClos::toString, "`print(bc)`");
});
}
}
}
54 changes: 54 additions & 0 deletions src/test/java/org/prlprg/util/Assertions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.prlprg.util;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Path;
import java.util.function.Supplier;
import org.opentest4j.AssertionFailedError;

public class Assertions {
/**
* First it asserts that producing the string twice results in the same string.
*
* <p>Then, if {@code snapshotPath} exists, it asserts that the produced string matches its
* content. If {@code snapshotPath} doesn't exist, it instead writes the produced string to it and
* logs that it was created.
*
* <p>Additionally, test failures are written to a file next to {@code snapshotPath} which gets
* automatically deleted after the test passes; this file can be diffed with the snapshot to see
* the regression, and if the snapshot was supposed to change it can be moved to {@code
* snapshotPath} to update it.
*
* @param snapshotPath The absolute path to the snapshot file.
* @param snapshotName A human-readable name for the snapshot, used in the assertion messages.
*/
public static void assertSnapshot(
Path snapshotPath, Supplier<String> actual, String snapshotName) {
if (!snapshotPath.isAbsolute()) {
throw new IllegalArgumentException(
"Snapshot path must be absolute: "
+ snapshotPath
+ "\nUse Tests.getSnapshot to get an absolute path from a relative one.");
}
var firstActual = actual.get();
var secondActual = actual.get();
assertEquals(firstActual, secondActual, "Non-determinism in " + snapshotName);

var failingSnapshotPath =
Paths.withExtension(snapshotPath, ".fail" + Paths.getExtension(snapshotPath));
Files.deleteIfExists(failingSnapshotPath);

if (!Files.exists(snapshotPath)) {
Files.writeString(snapshotPath, secondActual);
System.err.println("Created snapshot: " + snapshotPath);
} else {
var expected = Files.readString(snapshotPath);
if (!expected.equals(secondActual)) {
Files.writeString(failingSnapshotPath, secondActual);
throw new AssertionFailedError("Regression in " + snapshotName, expected, secondActual);
}
}
}

private Assertions() {}
}
72 changes: 72 additions & 0 deletions src/test/java/org/prlprg/util/DirectorySource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.prlprg.util;

import com.google.common.collect.ImmutableSet;
import java.lang.annotation.*;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.support.AnnotationConsumer;

/** List all files in a directory and provide each one's path as an argument. */
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(DirectoryArgumentsProvider.class)
public @interface DirectorySource {
/** Filter files by glob applied to the filename. Default is to not filter. */
@Nullable String glob();

/** Whether to include directories. Default is to not. */
boolean includeDirs() default false;

/** Whether to relativize the paths to {@link #root}. Default is to not. */
boolean relativize() default false;

/**
* Specify a number > 1 to include files in subdirectories. Specify INT_MAX to recurse infinitely.
* Defaults to 1.
*/
int depth() default 1;

/**
* Directory to list files from, defaults to corresponding resources directory of the test
* directory. Paths will be relative to this default unless they start with {@code /}
*/
String root() default "";

/** Paths to exclude. */
String[] exclude() default {};
}

class DirectoryArgumentsProvider implements ArgumentsProvider, AnnotationConsumer<DirectorySource> {
private boolean accepted = false;
private @Nullable String glob;
private boolean includeDirs;
private boolean relativize;
private int depth;
private String root = "";
private ImmutableSet<String> exclude = ImmutableSet.of();

@Override
public void accept(DirectorySource directorySource) {
accepted = true;
glob = directorySource.glob();
includeDirs = directorySource.includeDirs();
relativize = directorySource.relativize();
root = directorySource.root();
depth = directorySource.depth();
exclude = ImmutableSet.copyOf(directorySource.exclude());
}

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
assert accepted;
var path = Tests.getResourcePath(context.getRequiredTestClass(), root);
return Files.listDir(path, glob, depth, includeDirs, relativize).stream()
.filter(p -> !exclude.contains(p.toString()))
.map(Arguments::of);
}
}
Loading
Loading