64 lines
2.2 KiB
C++
64 lines
2.2 KiB
C++
#define TEST_NO_MAIN
|
|
#include "acutest.h"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <cmath>
|
|
#include "../include/scripting/ScriptEngine.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";
|
|
}
|
|
|
|
ScriptEngine se;
|
|
TEST_CHECK(se.Initialize() == true);
|
|
bool compiled = se.CompileScript(scriptPath.string());
|
|
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();
|
|
|
|
std::filesystem::remove(scriptPath);
|
|
std::filesystem::remove(tmpDir);
|
|
}
|