allocatorstest.cpp
Go to the documentation of this file.
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 
15 #include "unittest.h"
16 
17 #include "rapidjson/allocators.h"
18 
19 using namespace rapidjson;
20 
21 template <typename Allocator>
23  EXPECT_TRUE(a.Malloc(0) == 0);
24 
25  uint8_t* p = static_cast<uint8_t*>(a.Malloc(100));
26  EXPECT_TRUE(p != 0);
27  for (size_t i = 0; i < 100; i++)
28  p[i] = static_cast<uint8_t>(i);
29 
30  // Expand
31  uint8_t* q = static_cast<uint8_t*>(a.Realloc(p, 100, 200));
32  EXPECT_TRUE(q != 0);
33  for (size_t i = 0; i < 100; i++)
34  EXPECT_EQ(i, q[i]);
35  for (size_t i = 100; i < 200; i++)
36  q[i] = static_cast<uint8_t>(i);
37 
38  // Shrink
39  uint8_t *r = static_cast<uint8_t*>(a.Realloc(q, 200, 150));
40  EXPECT_TRUE(r != 0);
41  for (size_t i = 0; i < 150; i++)
42  EXPECT_EQ(i, r[i]);
43 
44  Allocator::Free(r);
45 
46  // Realloc to zero size
47  EXPECT_TRUE(a.Realloc(a.Malloc(1), 1, 0) == 0);
48 }
49 
52  TestAllocator(a);
53 }
54 
57  TestAllocator(a);
58 
59  for (size_t i = 1; i < 1000; i++) {
60  EXPECT_TRUE(a.Malloc(i) != 0);
61  EXPECT_LE(a.Size(), a.Capacity());
62  }
63 }
64 
65 TEST(Allocator, Alignment) {
66 #if RAPIDJSON_64BIT == 1
67  EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000000), RAPIDJSON_ALIGN(0));
68  for (uint64_t i = 1; i < 8; i++) {
69  EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008), RAPIDJSON_ALIGN(i));
70  EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000010), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008) + i));
71  EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000001, 0x00000000), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0xFFFFFFF8) + i));
72  EXPECT_EQ(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF8), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0) + i));
73  }
74 #else
75  EXPECT_EQ(0u, RAPIDJSON_ALIGN(0u));
76  for (uint32_t i = 1; i < 4; i++) {
77  EXPECT_EQ(4u, RAPIDJSON_ALIGN(i));
78  EXPECT_EQ(8u, RAPIDJSON_ALIGN(4u + i));
79  EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF4u + i));
80  EXPECT_EQ(0xFFFFFFFCu, RAPIDJSON_ALIGN(0xFFFFFFF8u + i));
81  }
82 #endif
83 }
84 
85 TEST(Allocator, Issue399) {
87  void* p = a.Malloc(100);
88  void* q = a.Realloc(p, 100, 200);
89  EXPECT_EQ(p, q);
90 
91  // exhuasive testing
92  for (size_t j = 1; j < 32; j++) {
93  a.Clear();
94  a.Malloc(j); // some unaligned size
95  p = a.Malloc(1);
96  for (size_t i = 1; i < 1024; i++) {
97  q = a.Realloc(p, i, i + 1);
98  EXPECT_EQ(p, q);
99  p = q;
100  }
101  }
102 }
TEST(Allocator, CrtAllocator)
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:158
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:176
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:168
void TestAllocator(Allocator &a)
unsigned char uint8_t
Definition: stdint.h:124
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:145
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
Concept for allocating, resizing and freeing memory block.
main RapidJSON namespace
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1121
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:191
C-runtime library allocator.
Definition: allocators.h:62
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:280
Default memory allocator used by the parser and DOM.
Definition: allocators.h:102


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:08