zone.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <msgpack.hpp>
  2. #include <gtest/gtest.h>
  3. TEST(zone, allocate_align)
  4. {
  5. msgpack::zone z;
  6. char* start = (char*)z.allocate_align(1);
  7. EXPECT_EQ(0ul, reinterpret_cast<std::size_t>(start) % sizeof(int));
  8. for (std::size_t s = 1; s < sizeof(int); ++s) {
  9. z.allocate_no_align(s);
  10. char* buf_a = (char*)z.allocate_align(1);
  11. EXPECT_EQ(0ul, reinterpret_cast<std::size_t>(buf_a) % sizeof(int));
  12. }
  13. }
  14. TEST(zone, allocate_align_custom)
  15. {
  16. msgpack::zone z;
  17. for (std::size_t align = 1; align < 64; ++align) {
  18. char* start = (char*)z.allocate_align(1, align);
  19. EXPECT_EQ(0ul, reinterpret_cast<std::size_t>(start) % align);
  20. for (std::size_t s = 1; s < align; ++s) {
  21. z.allocate_no_align(s);
  22. char* buf_a = (char*)z.allocate_align(1, align);
  23. EXPECT_EQ(0ul, reinterpret_cast<std::size_t>(buf_a) % align);
  24. }
  25. }
  26. }
  27. class myclass {
  28. public:
  29. myclass() : num(0), str("default") { }
  30. myclass(int num, const std::string& str) :
  31. num(num), str(str) { }
  32. ~myclass() { }
  33. int num;
  34. std::string str;
  35. private:
  36. myclass(const myclass&);
  37. };
  38. TEST(zone, allocate)
  39. {
  40. msgpack::zone z;
  41. myclass* m = z.allocate<myclass>();
  42. EXPECT_EQ(m->num, 0);
  43. EXPECT_EQ(m->str, "default");
  44. }
  45. TEST(zone, allocate_constructor)
  46. {
  47. msgpack::zone z;
  48. myclass* m = z.allocate<myclass>(7, "msgpack");
  49. EXPECT_EQ(m->num, 7);
  50. EXPECT_EQ(m->str, "msgpack");
  51. }
  52. static void custom_finalizer_func(void* user)
  53. {
  54. myclass* m = (myclass*)user;
  55. delete m;
  56. }
  57. TEST(zone, push_finalizer)
  58. {
  59. msgpack::zone z;
  60. myclass* m = new myclass();
  61. z.push_finalizer(custom_finalizer_func, (void*)m);
  62. }
  63. TEST(zone, push_finalizer_unique_ptr)
  64. {
  65. msgpack::zone z;
  66. msgpack::unique_ptr<myclass> am(new myclass());
  67. z.push_finalizer(msgpack::move(am));
  68. }
  69. TEST(zone, allocate_no_align)
  70. {
  71. msgpack::zone z;
  72. char* buf1 = (char*)z.allocate_no_align(4);
  73. char* buf2 = (char*)z.allocate_no_align(4);
  74. EXPECT_EQ(buf1+4, buf2);
  75. }