#define TEST_NO_MAIN #include "acutest.h" #include #include #include #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(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); }