test_shm.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "shm.h"
  2. #include "catch.hpp"
  3. #include "./../testshmdll/testshmdll.h"
  4. #include "shm_helper.h"
  5. TEST_CASE("Shm, ShmMalloc shmCalloc ShmFree")
  6. {
  7. bg::ShmOptions opts(false, "Test", nullptr);
  8. opts.fix_vptr_on_init = false;
  9. REQUIRE(bg::ShmInit(opts) == true);
  10. for(size_t bytes = 8; bytes < (0x40000 << 4); bytes = bytes << 1)
  11. {
  12. void* memory = bg::ShmMalloc(bytes);
  13. REQUIRE(memory);
  14. memset(memory, 0, bytes);
  15. *(int*)memory = 1;
  16. *(int*)((uintptr_t)memory + bytes - 4) = 2;
  17. void* memory1 = bg::ShmRealloc(memory, bytes << 1);
  18. REQUIRE(memory1);
  19. REQUIRE(*(int*)memory1 == 1);
  20. REQUIRE(*(int*)((uintptr_t)memory1 + bytes - 4) == 2);
  21. bg::ShmFree(memory1);
  22. void* memory2 = bg::ShmCalloc(bytes / 4, 4);
  23. REQUIRE(memory2);
  24. for(size_t i = 0; i < bytes / 4; ++i)
  25. {
  26. REQUIRE(*(int*)memory2 == 0);
  27. }
  28. bg::ShmFree(memory2);
  29. }
  30. bg::ShmFini();
  31. }
  32. struct TestSigleton
  33. {
  34. TestSigleton(int a, char b, void* c)
  35. {
  36. this->a = a;
  37. this->b = b;
  38. this->c = c;
  39. }
  40. int a{};
  41. char b{};
  42. void* c{};
  43. };
  44. TEST_CASE("ShmManager, Singleton")
  45. {
  46. bg::ShmOptions opts(false, "Test", nullptr);
  47. opts.fix_vptr_on_init = false;
  48. REQUIRE(bg::ShmInit(opts) == true);
  49. bg::detail::TypeName type(typeid(TestSigleton).name(), sizeof(TestSigleton));
  50. REQUIRE(ShmHasSingleton(type) == false);
  51. int a = 3;
  52. REQUIRE(bg::ShmGetSingleton<TestSigleton>(1, 2, &a));
  53. REQUIRE(ShmHasSingleton(type));
  54. TestSigleton* temp = bg::ShmGetSingleton<TestSigleton>(1, 1, &a);
  55. REQUIRE(temp->a == 1);
  56. REQUIRE(temp->b == 2);
  57. REQUIRE(temp->c == &a);
  58. bg::ShmDeleteSingleton<TestSigleton>();
  59. REQUIRE(ShmHasSingleton(type) == false);
  60. bg::ShmFini();
  61. }