allocatorstest.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. #include "rapidjson/allocators.h"
  16. using namespace rapidjson;
  17. template <typename Allocator>
  18. void TestAllocator(Allocator& a) {
  19. EXPECT_TRUE(a.Malloc(0) == 0);
  20. uint8_t* p = static_cast<uint8_t*>(a.Malloc(100));
  21. EXPECT_TRUE(p != 0);
  22. for (size_t i = 0; i < 100; i++)
  23. p[i] = static_cast<uint8_t>(i);
  24. // Expand
  25. uint8_t* q = static_cast<uint8_t*>(a.Realloc(p, 100, 200));
  26. EXPECT_TRUE(q != 0);
  27. for (size_t i = 0; i < 100; i++)
  28. EXPECT_EQ(i, q[i]);
  29. for (size_t i = 100; i < 200; i++)
  30. q[i] = static_cast<uint8_t>(i);
  31. // Shrink
  32. uint8_t *r = static_cast<uint8_t*>(a.Realloc(q, 200, 150));
  33. EXPECT_TRUE(r != 0);
  34. for (size_t i = 0; i < 150; i++)
  35. EXPECT_EQ(i, r[i]);
  36. Allocator::Free(r);
  37. // Realloc to zero size
  38. EXPECT_TRUE(a.Realloc(a.Malloc(1), 1, 0) == 0);
  39. }
  40. TEST(Allocator, CrtAllocator) {
  41. CrtAllocator a;
  42. TestAllocator(a);
  43. }
  44. TEST(Allocator, MemoryPoolAllocator) {
  45. MemoryPoolAllocator<> a;
  46. TestAllocator(a);
  47. for (size_t i = 1; i < 1000; i++) {
  48. EXPECT_TRUE(a.Malloc(i) != 0);
  49. EXPECT_LE(a.Size(), a.Capacity());
  50. }
  51. }
  52. TEST(Allocator, Alignment) {
  53. #if RAPIDJSON_64BIT == 1
  54. EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000000), RAPIDJSON_ALIGN(0));
  55. for (uint64_t i = 1; i < 8; i++) {
  56. EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008), RAPIDJSON_ALIGN(i));
  57. EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000010), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008) + i));
  58. EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000001, 0x00000000), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0xFFFFFFF8) + i));
  59. EXPECT_EQ(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF8), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0) + i));
  60. }
  61. #else
  62. EXPECT_EQ(0u, RAPIDJSON_ALIGN(0u));
  63. for (uint32_t i = 1; i < 4; i++) {
  64. EXPECT_EQ(4u, RAPIDJSON_ALIGN(i));
  65. EXPECT_EQ(8u, RAPIDJSON_ALIGN(4u + i));
  66. EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF4u + i));
  67. EXPECT_EQ(0xFFFFFFFCu, RAPIDJSON_ALIGN(0xFFFFFFF8u + i));
  68. }
  69. #endif
  70. }
  71. TEST(Allocator, Issue399) {
  72. MemoryPoolAllocator<> a;
  73. void* p = a.Malloc(100);
  74. void* q = a.Realloc(p, 100, 200);
  75. EXPECT_EQ(p, q);
  76. // exhuasive testing
  77. for (size_t j = 1; j < 32; j++) {
  78. a.Clear();
  79. a.Malloc(j); // some unaligned size
  80. p = a.Malloc(1);
  81. for (size_t i = 1; i < 1024; i++) {
  82. q = a.Realloc(p, i, i + 1);
  83. EXPECT_EQ(p, q);
  84. p = q;
  85. }
  86. }
  87. }