test_object.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "catch.hpp"
  2. #include "shm_object.h"
  3. #include "shm.h"
  4. #include "shm_manager.h"
  5. #if defined(__GNUC__)
  6. #include <sys/mman.h>
  7. #endif
  8. TEST_CASE("ShmObjectCreate, creat/delete")
  9. {
  10. bg::ShmOptions opts(false, "Test", nullptr);
  11. char path[256];
  12. bg::detail::ShmManager::ShmBlock m_blocks[17]{};
  13. size_t count = opts.fixed_address_count;
  14. for(size_t i = 0; i < count; ++i)
  15. {
  16. size_t real_size = opts.shm_block_mmap_size / 0x10000;
  17. size_t mmap_size = opts.shm_block_mmap_size / 0x10000;
  18. snprintf(path, 0x100uLL, "%s-%03zu.mmap", opts.identifier, i);
  19. uintptr_t fixed_addr = 0ll;
  20. REQUIRE(opts.PopFixedAddress(&fixed_addr));
  21. void* memory = bg::detail::ShmObjectCreate(path, fixed_addr, &real_size, &mmap_size);
  22. REQUIRE(memory);
  23. memset(memory, 0, real_size);
  24. m_blocks[i].addr = memory;
  25. m_blocks[i].mmap_size = real_size;
  26. m_blocks[i].mmap_size = mmap_size;
  27. }
  28. for(size_t i = 0; i < count; ++i)
  29. {
  30. #if defined(__GNUC__)
  31. size_t real_size = opts.shm_block_mmap_size / 0x10000;
  32. size_t mmap_size = opts.shm_block_mmap_size / 0x10000;
  33. munmap(m_blocks[i].addr, m_blocks[i].real_size);
  34. snprintf(path, 0x100uLL, "%s-%03zu.mmap", opts.identifier, i);
  35. void* memory = bg::detail::ShmObjectAttach(path, (uintptr_t)m_blocks[i].addr, &real_size, &mmap_size);
  36. REQUIRE(memory);
  37. memset(memory, 0, real_size);
  38. #else
  39. size_t real_size = opts.shm_block_mmap_size / 0x10000;
  40. size_t mmap_size = opts.shm_block_mmap_size / 0x10000;
  41. snprintf(path, 0x100uLL, "%s-%03zu.mmap", opts.identifier, i);
  42. void* memory = bg::detail::ShmObjectAttach(path, 0, &real_size, &mmap_size);
  43. REQUIRE(memory);
  44. memset(memory, 0, real_size);
  45. bg::detail::ShmObjectDelete(memory, mmap_size, path);
  46. #endif
  47. }
  48. for(size_t i = 0; i < count; ++i)
  49. {
  50. snprintf(path, 0x100uLL, "%s-%03zu.mmap", opts.identifier, i);
  51. bg::detail::ShmObjectDelete(m_blocks[i].addr, m_blocks[i].mmap_size, path);
  52. }
  53. }