Testing And Benchmarking in AngelScript

Here’s the translation of the Go testing and benchmarking example to AngelScript, formatted in Markdown suitable for Hugo:

Unit testing is an important part of writing principled AngelScript programs. While AngelScript doesn’t have built-in testing features, we can create a simple testing framework to achieve similar functionality.

For the sake of demonstration, we’ll create a test file alongside our main script. In practice, you might want to organize your tests in a separate directory.

First, let’s implement the function we want to test:

// intutils.as
int IntMin(int a, int b) {
    if (a < b) {
        return a;
    }
    return b;
}

Now, let’s create a simple testing framework and write our tests:

// test_intutils.as
#include "intutils.as"

void assert(bool condition, string message) {
    if (!condition) {
        print("Test failed: " + message);
    } else {
        print("Test passed: " + message);
    }
}

void TestIntMinBasic() {
    int ans = IntMin(2, -2);
    assert(ans == -2, "IntMin(2, -2) should return -2");
}

void TestIntMinTableDriven() {
    array<array<int>> tests = {
        {0, 1, 0},
        {1, 0, 0},
        {2, -2, -2},
        {0, -1, -1},
        {-1, 0, -1}
    };

    for (uint i = 0; i < tests.length(); i++) {
        int a = tests[i][0];
        int b = tests[i][1];
        int expected = tests[i][2];
        int result = IntMin(a, b);
        assert(result == expected, "IntMin(" + a + ", " + b + ") should return " + expected);
    }
}

void RunTests() {
    TestIntMinBasic();
    TestIntMinTableDriven();
}

RunTests();

To run the tests, you would execute the test script:

$ angelscript test_intutils.as
Test passed: IntMin(2, -2) should return -2
Test passed: IntMin(0, 1) should return 0
Test passed: IntMin(1, 0) should return 0
Test passed: IntMin(2, -2) should return -2
Test passed: IntMin(0, -1) should return -1
Test passed: IntMin(-1, 0) should return -1

Benchmarking in AngelScript is not as straightforward as in Go, as the language doesn’t have built-in benchmarking tools. However, we can create a simple timing mechanism to measure performance:

// benchmark_intutils.as
#include "intutils.as"

uint64 GetTimeNanos() {
    return uint64(get_time_us() * 1000);
}

void BenchmarkIntMin(int iterations) {
    uint64 startTime = GetTimeNanos();
    
    for (int i = 0; i < iterations; i++) {
        IntMin(1, 2);
    }
    
    uint64 endTime = GetTimeNanos();
    uint64 duration = endTime - startTime;
    
    print("BenchmarkIntMin: " + iterations + " iterations in " + (duration / 1000000) + " ms");
    print("Average time per operation: " + (duration / iterations) + " ns");
}

BenchmarkIntMin(1000000);

To run the benchmark:

$ angelscript benchmark_intutils.as
BenchmarkIntMin: 1000000 iterations in 15 ms
Average time per operation: 15 ns

Note that AngelScript doesn’t have the same level of built-in testing and benchmarking support as Go. The examples provided here are simplified versions that demonstrate the concepts. In a real-world scenario, you might want to use or create a more robust testing framework for AngelScript.

Also, the performance measurements may not be as precise as Go’s benchmarking tool, and the results can vary significantly depending on the system and runtime environment.