Electroneum
allocatorstest.cpp File Reference
#include "unittest.h"
#include "rapidjson/allocators.h"
Include dependency graph for allocatorstest.cpp:

Go to the source code of this file.

Functions

template<typename Allocator >
void TestAllocator (Allocator &a)
 
 TEST (Allocator, CrtAllocator)
 
 TEST (Allocator, MemoryPoolAllocator)
 
 TEST (Allocator, Alignment)
 
 TEST (Allocator, Issue399)
 

Function Documentation

◆ TEST() [1/4]

TEST ( Allocator  ,
CrtAllocator   
)

Definition at line 50 of file allocatorstest.cpp.

50  {
53 }
C-runtime library allocator.
Definition: allocators.h:75
void TestAllocator(Allocator &a)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
Here is the call graph for this function:

◆ TEST() [2/4]

TEST ( Allocator  ,
MemoryPoolAllocator   
)

Definition at line 55 of file allocatorstest.cpp.

55  {
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 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
void TestAllocator(Allocator &a)
#define EXPECT_LE(val1, val2)
Definition: gtest.h:1928
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
Here is the call graph for this function:

◆ TEST() [3/4]

TEST ( Allocator  ,
Alignment   
)

Definition at line 65 of file allocatorstest.cpp.

65  {
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 }
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:280
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922

◆ TEST() [4/4]

TEST ( Allocator  ,
Issue399   
)

Definition at line 85 of file allocatorstest.cpp.

85  {
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 }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922

◆ TestAllocator()

template<typename Allocator >
void TestAllocator ( Allocator a)

Definition at line 22 of file allocatorstest.cpp.

22  {
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 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
unsigned char uint8_t
Definition: stdint.h:124
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Here is the caller graph for this function: