#include "shm.h" #include "catch.hpp" #include "./../testshmdll/testshmdll.h" #include "shm_helper.h" TEST_CASE("Shm, ShmMalloc shmCalloc ShmFree") { bg::ShmOptions opts(false, "Test", nullptr); opts.fix_vptr_on_init = false; REQUIRE(bg::ShmInit(opts) == true); for(size_t bytes = 8; bytes < (0x40000 << 4); bytes = bytes << 1) { void* memory = bg::ShmMalloc(bytes); REQUIRE(memory); memset(memory, 0, bytes); *(int*)memory = 1; *(int*)((uintptr_t)memory + bytes - 4) = 2; void* memory1 = bg::ShmRealloc(memory, bytes << 1); REQUIRE(memory1); REQUIRE(*(int*)memory1 == 1); REQUIRE(*(int*)((uintptr_t)memory1 + bytes - 4) == 2); bg::ShmFree(memory1); void* memory2 = bg::ShmCalloc(bytes / 4, 4); REQUIRE(memory2); for(size_t i = 0; i < bytes / 4; ++i) { REQUIRE(*(int*)memory2 == 0); } bg::ShmFree(memory2); } bg::ShmFini(); } struct TestSigleton { TestSigleton(int a, char b, void* c) { this->a = a; this->b = b; this->c = c; } int a{}; char b{}; void* c{}; }; TEST_CASE("ShmManager, Singleton") { bg::ShmOptions opts(false, "Test", nullptr); opts.fix_vptr_on_init = false; REQUIRE(bg::ShmInit(opts) == true); bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton)); REQUIRE(ShmHasSingleton(type) == false); int a = 3; REQUIRE(bg::ShmGetSingleton(1, 2, &a)); REQUIRE(ShmHasSingleton(type)); TestSigleton* temp = bg::ShmGetSingleton(1, 1, &a); REQUIRE(temp->a == 1); REQUIRE(temp->b == 2); REQUIRE(temp->c == &a); bg::ShmDeleteSingleton(); REQUIRE(ShmHasSingleton(type) == false); bg::ShmFini(); }