Testing And Benchmarking in UnrealScript

Here’s an idiomatic UnrealScript example demonstrating testing and benchmarking concepts:

class IntMinTest extends Object;

// Function to test
static function int IntMin(int a, int b)
{
    return (a < b) ? a : b;
}

// Basic test function
static function bool TestIntMinBasic()
{
    local int result;
    result = IntMin(2, -2);
    return result == -2;
}

// Table-driven test
struct TestCase
{
    var int a, b, expected;
};

static function bool TestIntMinTableDriven()
{
    local array<TestCase> TestCases;
    local TestCase TestCase;
    local bool bAllPassed;
    local int result;

    // Define test cases
    TestCases.Add((a:0, b:1, expected:0));
    TestCases.Add((a:1, b:0, expected:0));
    TestCases.Add((a:2, b:-2, expected:-2));
    TestCases.Add((a:0, b:-1, expected:-1));
    TestCases.Add((a:-1, b:0, expected:-1));

    bAllPassed = true;

    // Run all test cases
    foreach TestCases(TestCase)
    {
        result = IntMin(TestCase.a, TestCase.b);
        if (result != TestCase.expected)
        {
            `log("Test case failed: IntMin("$TestCase.a$", "$TestCase.b$") = "$result$"; expected "$TestCase.expected);
            bAllPassed = false;
        }
    }

    return bAllPassed;
}

// Benchmark function
static function BenchmarkIntMin()
{
    local int i;
    local float StartTime, EndTime, ElapsedTime;
    local int NumIterations;

    NumIterations = 1000000;
    StartTime = `WORLDINFO.TimeSeconds;

    for (i = 0; i < NumIterations; i++)
    {
        IntMin(1, 2);
    }

    EndTime = `WORLDINFO.TimeSeconds;
    ElapsedTime = EndTime - StartTime;

    `log("BenchmarkIntMin: "$NumIterations$" iterations in "$ElapsedTime$" seconds");
    `log("Average time per operation: "$(ElapsedTime / NumIterations)$" seconds");
}

// Run all tests and benchmarks
static function RunTests()
{
    `log("Running tests for IntMin");

    if (TestIntMinBasic())
    {
        `log("TestIntMinBasic: PASSED");
    }
    else
    {
        `log("TestIntMinBasic: FAILED");
    }

    if (TestIntMinTableDriven())
    {
        `log("TestIntMinTableDriven: PASSED");
    }
    else
    {
        `log("TestIntMinTableDriven: FAILED");
    }

    `log("Running benchmark for IntMin");
    BenchmarkIntMin();
}

defaultproperties
{
}

This UnrealScript example demonstrates testing and benchmarking concepts similar to the Go example. Here’s a breakdown of the code:

  1. We define a simple IntMin function to test.

  2. TestIntMinBasic is a basic test function that checks a single case.

  3. TestIntMinTableDriven implements a table-driven test approach using an array of TestCase structs.

  4. BenchmarkIntMin is a simple benchmarking function that runs the IntMin function multiple times and measures the elapsed time.

  5. The RunTests function executes all tests and the benchmark.

To use this in an Unreal Engine project:

  1. Create a new UnrealScript file named IntMinTest.uc in your project’s Classes folder.
  2. Paste the code into this file.
  3. Compile your project.
  4. You can call IntMinTest.RunTests() from another script or console command to execute the tests and benchmark.

Note that UnrealScript doesn’t have built-in testing frameworks like Go’s testing package. This example simulates similar functionality using UnrealScript’s features. The log function is used to output results, which will appear in the Unreal Engine log.

Also, UnrealScript doesn’t have a direct equivalent to Go’s benchmarking tools. The benchmark function provided here is a simple timing mechanism and may not be as precise as dedicated benchmarking tools.

Remember that in a real Unreal Engine project, you might want to integrate this testing logic with the engine’s gameplay framework or create a custom testing system that fits your project’s needs.