Debian Codegraph @

LspCpp/third_party/rapidjson/test/unittest/allocatorstest.cpp

raw
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.
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 #include <map>
20 #include <string>
21 #include <utility>
22 #include <functional>
23
24 using namespace rapidjson;
25
26 template <typename Allocator>
27 void TestAllocator(Allocator& a) {
28 EXPECT_TRUE(a.Malloc(0) == 0);
29
30 uint8_t* p = static_cast<uint8_t*>(a.Malloc(100));
31 EXPECT_TRUE(p != 0);
32 for (size_t i = 0; i < 100; i++)
33 p[i] = static_cast<uint8_t>(i);
34
35 // Expand
36 uint8_t* q = static_cast<uint8_t*>(a.Realloc(p, 100, 200));
37 EXPECT_TRUE(q != 0);
38 for (size_t i = 0; i < 100; i++)
39 EXPECT_EQ(i, q[i]);
40 for (size_t i = 100; i < 200; i++)
41 q[i] = static_cast<uint8_t>(i);
42
43 // Shrink
44 uint8_t *r = static_cast<uint8_t*>(a.Realloc(q, 200, 150));
45 EXPECT_TRUE(r != 0);
46 for (size_t i = 0; i < 150; i++)
47 EXPECT_EQ(i, r[i]);
48
49 Allocator::Free(r);
50
51 // Realloc to zero size
52 EXPECT_TRUE(a.Realloc(a.Malloc(1), 1, 0) == 0);
53 }
54
55 struct TestStdAllocatorData {
56 TestStdAllocatorData(int &constructions, int &destructions) :
57 constructions_(&constructions),
58 destructions_(&destructions)
59 {
60 ++*constructions_;
61 }
62 TestStdAllocatorData(const TestStdAllocatorData& rhs) :
63 constructions_(rhs.constructions_),
64 destructions_(rhs.destructions_)
65 {
66 ++*constructions_;
67 }
68 TestStdAllocatorData& operator=(const TestStdAllocatorData& rhs)
69 {
70 this->~TestStdAllocatorData();
71 constructions_ = rhs.constructions_;
72 destructions_ = rhs.destructions_;
73 ++*constructions_;
74 return *this;
75 }
76 ~TestStdAllocatorData()
77 {
78 ++*destructions_;
79 }
80 private:
81 TestStdAllocatorData();
82 int *constructions_,
83 *destructions_;
84 };
85
86 template <typename Allocator>
87 void TestStdAllocator(const Allocator& a) {
88 #if RAPIDJSON_HAS_CXX17
89 typedef StdAllocator<bool, Allocator> BoolAllocator;
90 #else
91 typedef StdAllocator<void, Allocator> VoidAllocator;
92 typedef typename VoidAllocator::template rebind<bool>::other BoolAllocator;
93 #endif
94 BoolAllocator ba(a), ba2(a);
95 EXPECT_TRUE(ba == ba2);
96 EXPECT_FALSE(ba!= ba2);
97 ba.deallocate(ba.allocate());
98 EXPECT_TRUE(ba == ba2);
99 EXPECT_FALSE(ba != ba2);
100
101 unsigned long long ll = 0, *llp = &ll;
102 const unsigned long long cll = 0, *cllp = &cll;
103 StdAllocator<unsigned long long, Allocator> lla(a);
104 EXPECT_EQ(lla.address(ll), llp);
105 EXPECT_EQ(lla.address(cll), cllp);
106 EXPECT_TRUE(lla.max_size() > 0 && lla.max_size() <= SIZE_MAX / sizeof(unsigned long long));
107
108 int *arr;
109 StdAllocator<int, Allocator> ia(a);
110 arr = ia.allocate(10 * sizeof(int));
111 EXPECT_TRUE(arr != 0);
112 for (int i = 0; i < 10; ++i) {
113 arr[i] = 0x0f0f0f0f;
114 }
115 ia.deallocate(arr, 10);
116 arr = Malloc<int>(ia, 10);
117 EXPECT_TRUE(arr != 0);
118 for (int i = 0; i < 10; ++i) {
119 arr[i] = 0x0f0f0f0f;
120 }
121 arr = Realloc<int>(ia, arr, 10, 20);
122 EXPECT_TRUE(arr != 0);
123 for (int i = 0; i < 10; ++i) {
124 EXPECT_EQ(arr[i], 0x0f0f0f0f);
125 }
126 for (int i = 10; i < 20; i++) {
127 arr[i] = 0x0f0f0f0f;
128 }
129 Free<int>(ia, arr, 20);
130
131 int cons = 0, dest = 0;
132 StdAllocator<TestStdAllocatorData, Allocator> da(a);
133 for (int i = 1; i < 10; i++) {
134 TestStdAllocatorData *d = da.allocate();
135 EXPECT_TRUE(d != 0);
136
137 da.destroy(new(d) TestStdAllocatorData(cons, dest));
138 EXPECT_EQ(cons, i);
139 EXPECT_EQ(dest, i);
140
141 da.deallocate(d);
142 }
143
144 typedef StdAllocator<char, Allocator> CharAllocator;
145 typedef std::basic_string<char, std::char_traits<char>, CharAllocator> String;
146 #if RAPIDJSON_HAS_CXX11
147 String s(CharAllocator{a});
148 #else
149 CharAllocator ca(a);
150 String s(ca);
151 #endif
152 for (int i = 0; i < 26; i++) {
153 s.push_back(static_cast<char>('A' + i));
154 }
155 EXPECT_TRUE(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
156
157 typedef StdAllocator<std::pair<const int, bool>, Allocator> MapAllocator;
158 typedef std::map<int, bool, std::less<int>, MapAllocator> Map;
159 #if RAPIDJSON_HAS_CXX11
160 Map map(std::less<int>(), MapAllocator{a});
161 #else
162 MapAllocator ma(a);
163 Map map(std::less<int>(), ma);
164 #endif
165 for (int i = 0; i < 10; i++) {
166 map.insert(std::make_pair(i, (i % 2) == 0));
167 }
168 EXPECT_TRUE(map.size() == 10);
169 for (int i = 0; i < 10; i++) {
170 typename Map::iterator it = map.find(i);
171 EXPECT_TRUE(it != map.end());
172 EXPECT_TRUE(it->second == ((i % 2) == 0));
173 }
174 }
175
176 TEST(Allocator, CrtAllocator) {
177 CrtAllocator a;
178
179 TestAllocator(a);
180 TestStdAllocator(a);
181
182 CrtAllocator a2;
183 EXPECT_TRUE(a == a2);
184 EXPECT_FALSE(a != a2);
185 a2.Free(a2.Malloc(1));
186 EXPECT_TRUE(a == a2);
187 EXPECT_FALSE(a != a2);
188 }
189
190 TEST(Allocator, MemoryPoolAllocator) {
191 const size_t capacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY;
192 MemoryPoolAllocator<> a(capacity);
193
194 a.Clear(); // noop
195 EXPECT_EQ(a.Size(), 0u);
196 EXPECT_EQ(a.Capacity(), 0u);
197 EXPECT_EQ(a.Shared(), false);
198 {
199 MemoryPoolAllocator<> a2(a);
200 EXPECT_EQ(a2.Shared(), true);
201 EXPECT_EQ(a.Shared(), true);
202 EXPECT_TRUE(a == a2);
203 EXPECT_FALSE(a != a2);
204 a2.Free(a2.Malloc(1));
205 EXPECT_TRUE(a == a2);
206 EXPECT_FALSE(a != a2);
207 }
208 EXPECT_EQ(a.Shared(), false);
209 EXPECT_EQ(a.Capacity(), capacity);
210 EXPECT_EQ(a.Size(), 8u); // aligned
211 a.Clear();
212 EXPECT_EQ(a.Capacity(), 0u);
213 EXPECT_EQ(a.Size(), 0u);
214
215 TestAllocator(a);
216 TestStdAllocator(a);
217
218 for (size_t i = 1; i < 1000; i++) {
219 EXPECT_TRUE(a.Malloc(i) != 0);
220 EXPECT_LE(a.Size(), a.Capacity());
221 }
222
223 CrtAllocator baseAllocator;
224 a = MemoryPoolAllocator<>(capacity, &baseAllocator);
225 EXPECT_EQ(a.Capacity(), 0u);
226 EXPECT_EQ(a.Size(), 0u);
227 a.Free(a.Malloc(1));
228 EXPECT_EQ(a.Capacity(), capacity);
229 EXPECT_EQ(a.Size(), 8u); // aligned
230
231 {
232 a.Clear();
233 const size_t bufSize = 1024;
234 char *buffer = (char *)a.Malloc(bufSize);
235 MemoryPoolAllocator<> aligned_a(buffer, bufSize);
236 EXPECT_TRUE(aligned_a.Capacity() > 0 && aligned_a.Capacity() <= bufSize);
237 EXPECT_EQ(aligned_a.Size(), 0u);
238 aligned_a.Free(aligned_a.Malloc(1));
239 EXPECT_TRUE(aligned_a.Capacity() > 0 && aligned_a.Capacity() <= bufSize);
240 EXPECT_EQ(aligned_a.Size(), 8u); // aligned
241 }
242
243 {
244 a.Clear();
245 const size_t bufSize = 1024;
246 char *buffer = (char *)a.Malloc(bufSize);
247 RAPIDJSON_ASSERT(bufSize % sizeof(void*) == 0);
248 MemoryPoolAllocator<> unaligned_a(buffer + 1, bufSize - 1);
249 EXPECT_TRUE(unaligned_a.Capacity() > 0 && unaligned_a.Capacity() <= bufSize - sizeof(void*));
250 EXPECT_EQ(unaligned_a.Size(), 0u);
251 unaligned_a.Free(unaligned_a.Malloc(1));
252 EXPECT_TRUE(unaligned_a.Capacity() > 0 && unaligned_a.Capacity() <= bufSize - sizeof(void*));
253 EXPECT_EQ(unaligned_a.Size(), 8u); // aligned
254 }
255 }
256
257 TEST(Allocator, Alignment) {
258 if (sizeof(size_t) >= 8) {
259 EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000000), RAPIDJSON_ALIGN(0));
260 for (uint64_t i = 1; i < 8; i++) {
261 EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008), RAPIDJSON_ALIGN(i));
262 EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000000, 0x00000010), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0x00000008) + i));
263 EXPECT_EQ(RAPIDJSON_UINT64_C2(0x00000001, 0x00000000), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0x00000000, 0xFFFFFFF8) + i));
264 EXPECT_EQ(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF8), RAPIDJSON_ALIGN(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFF0) + i));
265 }
266 }
267
268 EXPECT_EQ(0u, RAPIDJSON_ALIGN(0u));
269 for (uint32_t i = 1; i < 8; i++) {
270 EXPECT_EQ(8u, RAPIDJSON_ALIGN(i));
271 EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF0u + i));
272 }
273 }
274
275 TEST(Allocator, Issue399) {
276 MemoryPoolAllocator<> a;
277 void* p = a.Malloc(100);
278 void* q = a.Realloc(p, 100, 200);
279 EXPECT_EQ(p, q);
280
281 // exhuasive testing
282 for (size_t j = 1; j < 32; j++) {
283 a.Clear();
284 a.Malloc(j); // some unaligned size
285 p = a.Malloc(1);
286 for (size_t i = 1; i < 1024; i++) {
287 q = a.Realloc(p, i, i + 1);
288 EXPECT_EQ(p, q);
289 p = q;
290 }
291 }
292 }