Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
18
19using namespace rapidjson;
20
21template <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
54
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
65TEST(Allocator, Alignment) {
66 if (sizeof(size_t) >= 8) {
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 }
75
77 for (uint32_t i = 1; i < 8; i++) {
79 EXPECT_EQ(0xFFFFFFF8u, RAPIDJSON_ALIGN(0xFFFFFFF0u + i));
80 }
81}
82
83TEST(Allocator, Issue399) {
85 void* p = a.Malloc(100);
86 void* q = a.Realloc(p, 100, 200);
87 EXPECT_EQ(p, q);
88
89 // exhuasive testing
90 for (size_t j = 1; j < 32; j++) {
91 a.Clear();
92 a.Malloc(j); // some unaligned size
93 p = a.Malloc(1);
94 for (size_t i = 1; i < 1024; i++) {
95 q = a.Realloc(p, i, i + 1);
96 EXPECT_EQ(p, q);
97 p = q;
98 }
99 }
100}
void TestAllocator(Allocator &a)
const mie::Vuint & p
Definition bn.cpp:27
const mie::Vuint & r
Definition bn.cpp:28
C-runtime library allocator.
Definition allocators.h:75
Default memory allocator used by the parser and DOM.
Definition allocators.h:115
Concept for allocating, resizing and freeing memory block.
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition rapidjson.h:276
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define EXPECT_LE(val1, val2)
Definition gtest.h:1960
main RapidJSON namespace
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition rapidjson.h:289
unsigned int uint32_t
Definition stdint.h:126
unsigned char uint8_t
Definition stdint.h:124
unsigned __int64 uint64_t
Definition stdint.h:136
uint16_t j