123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include "catch.hpp"
- #include "shm_manager.h"
- #include "shm.h"
- #include "shm_object.h"
- extern bg::detail::ShmContext g_shm_ctx;
- TEST_CASE("ShmManager, AllocateRawMemory")
- {
- bg::ShmOptions opts(false, "Test", nullptr);
- opts.fix_vptr_on_init = false;
- REQUIRE(bg::ShmInit(opts) == true);
- REQUIRE(g_shm_ctx.mgr);
- for(size_t i = 8; i < 0x100000000LL; i = i << 4)
- {
- size_t real_bytes = i - 1;
- void* memory = g_shm_ctx.mgr->AllocateRawMemory(&real_bytes, i);
- REQUIRE(memory);
- memset(memory, 0, real_bytes);
- REQUIRE(real_bytes == i);
- }
- 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);
- REQUIRE(g_shm_ctx.mgr);
- bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
- int a = 3;
- REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
- TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
- REQUIRE(temp->a == 1);
- REQUIRE(temp->b == 2);
- REQUIRE(temp->c == &a);
- bg::ShmDeleteSingleton<TestSigleton>();
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
- bg::ShmFini();
- }
- TEST_CASE("ShmManager, creat/resume")
- {
- char path[256];
- bg::ShmOptions opts(false, "Test", nullptr);
- opts.fix_vptr_on_init = true;
- REQUIRE(bg::ShmInit(opts) == true);
- int a = 3;
- REQUIRE(g_shm_ctx.mgr);
- bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
- REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == true);
- opts.resume = true;
- // 占住共享内存 windows 会自动释放
- size_t real_size = sizeof(bg::detail::ShmManager);
- snprintf(path, 256, "%s-mgr.mmap", opts.identifier);
- void* memory1 = bg::detail::ShmObjectAttach(path, 0, &real_size, &real_size);
- REQUIRE(memory1);
- bg::detail::ShmObjectDelete(g_shm_ctx.mgr, real_size, path);
- real_size = 0x100000000;
- void* memory2 = bg::detail::ShmObjectAttach("Test-000.mmap", 0, &real_size, &real_size);
- REQUIRE(memory2);
- bg::detail::ShmObjectDelete((void*)0x00006f0000000000, real_size, "Test-000.mmap");
- g_shm_ctx.mgr = nullptr;
- REQUIRE(bg::ShmInit(opts) == true);
- bg::detail::ShmObjectDelete(memory1, sizeof(bg::detail::ShmManager), path);
- bg::detail::ShmObjectDelete(memory2, real_size, "Test-000.mmap");
- TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
- REQUIRE(temp->a == 1);
- REQUIRE(temp->b == 2);
- REQUIRE(temp->c == &a);
- bg::ShmDeleteSingleton<TestSigleton>();
- REQUIRE(g_shm_ctx.mgr->HasSingleton(type) == false);
- bg::ShmFini();
- }
|