Files
simian/tests/math_bindings_tests.cpp
T
nick 3e23f7da8e
CI / build-and-test (push) Failing after 10m16s
CI / build-and-test (pull_request) Successful in 3m2s
feat: added physFS
2026-03-11 16:43:36 +13:00

70 lines
2.4 KiB
C++

#define TEST_NO_MAIN
#include "acutest.h"
#include <filesystem>
#include <fstream>
#include <cmath>
#include "../include/scripting/ScriptEngine.h"
#include "physfs_test_utils.h"
void test_math_bindings(void) {
std::filesystem::path tmpDir = std::filesystem::current_path() / "tmp_math";
std::filesystem::create_directories(tmpDir);
std::filesystem::path scriptPath = tmpDir / "math_test.as";
{
std::ofstream script(scriptPath);
script << "float testSin = Math::Sin(1.57079632679f);\n";
script << "float testCos = Math::Cos(0.0f);\n";
script << "float testTan = Math::Tan(0.78539816339f);\n";
script << "float testSqrt = Math::Sqrt(25.0f);\n";
script << "float testPow = Math::Pow(2.0f, 8.0f);\n";
script << "float testAbs = Math::Abs(-42.5f);\n";
}
TEST_CHECK(InitPhysFSTest(tmpDir) == true);
TEST_CHECK(MountPhysFSTest(tmpDir, "") == true);
ScriptEngine se;
TEST_CHECK(se.Initialize() == true);
bool compiled = se.CompileScript("math_test.as");
TEST_CHECK(compiled == true);
asIScriptEngine* engine = se.GetEngine();
TEST_CHECK(engine != nullptr);
asIScriptModule* module = engine ? engine->GetModule("main") : nullptr;
TEST_CHECK(module != nullptr);
auto fetchValue = [module](const char* name) -> float {
int idx = module->GetGlobalVarIndexByName(name);
TEST_CHECK_(idx >= 0, "Global '%s' not found", name);
if (idx < 0)
return 0.0f;
void* address = module->GetAddressOfGlobalVar(idx);
TEST_CHECK_(address != nullptr, "Global '%s' address null", name);
if (!address)
return 0.0f;
return *static_cast<float*>(address);
};
const float sinVal = fetchValue("testSin");
const float cosVal = fetchValue("testCos");
const float tanVal = fetchValue("testTan");
const float sqrtVal = fetchValue("testSqrt");
const float powVal = fetchValue("testPow");
const float absVal = fetchValue("testAbs");
TEST_CHECK(std::fabs(sinVal - 1.0f) < 1e-4f);
TEST_CHECK(std::fabs(cosVal - 1.0f) < 1e-4f);
TEST_CHECK(std::fabs(tanVal - 1.0f) < 1e-3f);
TEST_CHECK(std::fabs(sqrtVal - 5.0f) < 1e-4f);
TEST_CHECK(std::fabs(powVal - 256.0f) < 1e-3f);
TEST_CHECK(std::fabs(absVal - 42.5f) < 1e-4f);
se.Shutdown();
ShutdownPhysFSTest();
std::filesystem::remove(scriptPath);
std::filesystem::remove(tmpDir);
}