Testing And Benchmarking in Karel

Here’s the translation of the Go testing and benchmarking example to Java, formatted for Hugo:

Unit testing is an important part of writing principled Java programs. The `JUnit` framework provides the tools we need to write unit tests, and various build tools like Maven or Gradle can run these tests.

For the sake of demonstration, this code is in a single file, but it could be split into separate files. Testing code typically lives in a separate directory from the code it tests, often named `test`.

```java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;

public class IntUtilsTest {

    // We'll be testing this simple implementation of an
    // integer minimum. Typically, the code we're testing
    // would be in a separate source file named something like
    // IntUtils.java, and the test file for it would then
    // be named IntUtilsTest.java.
    public static int intMin(int a, int b) {
        return Math.min(a, b);
    }

    // A test is created by writing a method with a @Test annotation.
    @Test
    public void testIntMinBasic() {
        int ans = intMin(2, -2);
        // Assert methods will report test failures.
        assertEquals(-2, ans, "IntMin(2, -2) should be -2");
    }

    // Writing tests can be repetitive, so it's idiomatic to
    // use a parameterized test, where test inputs and
    // expected outputs are provided as parameters.
    @ParameterizedTest
    @CsvSource({
        "0, 1, 0",
        "1, 0, 0",
        "2, -2, -2",
        "0, -1, -1",
        "-1, 0, -1"
    })
    public void testIntMinTableDriven(int a, int b, int expected) {
        int ans = intMin(a, b);
        assertEquals(expected, ans, 
            String.format("IntMin(%d, %d) should be %d", a, b, expected));
    }
}

Benchmark tests in Java are typically done using frameworks like JMH (Java Microbenchmark Harness). Here’s a simple example of how you might set up a benchmark:

import org.openjdk.jmh.annotations.*;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class IntUtilsBenchmark {

    @Benchmark
    public void benchmarkIntMin() {
        IntUtilsTest.intMin(1, 2);
    }

    // To run the benchmark, you would typically use a separate main method
    // or let your build tool handle it.
}

To run the tests, you would typically use a build tool like Maven or Gradle, or you can run JUnit directly from the command line:

$ java -jar junit-platform-console-standalone.jar -cp . --select-class IntUtilsTest

For benchmarks, you would use the JMH tool:

$ java -jar jmh.jar IntUtilsBenchmark

The output would show the benchmark results, including operations per second and average time per operation.

Note that Java’s testing and benchmarking ecosystem is quite different from that of other languages. JUnit is the standard for unit testing, while JMH is commonly used for microbenchmarking. The exact commands and setup may vary depending on your project structure and build tools.