test/zserio/PolymorphicAllocatorTest.cpp
Line | Count | Source |
1 | | #include "gtest/gtest.h" |
2 | | |
3 | | #include "zserio/pmr/PolymorphicAllocator.h" |
4 | | #include "zserio/pmr/NewDeleteResource.h" |
5 | | |
6 | | namespace zserio |
7 | | { |
8 | | |
9 | | namespace |
10 | | { |
11 | | |
12 | | class TestResource : public zserio::pmr::MemoryResource |
13 | | { |
14 | | public: |
15 | | void* doAllocate(size_t bytes, size_t) override |
16 | 7 | { |
17 | 7 | ++m_numAllocations; |
18 | 7 | return ::operator new(bytes); |
19 | 7 | } |
20 | | |
21 | | void doDeallocate(void* p, size_t, size_t) override |
22 | 7 | { |
23 | 7 | --m_numAllocations; |
24 | 7 | ::operator delete(p); |
25 | 7 | } |
26 | | |
27 | | bool doIsEqual(const MemoryResource& other) const noexcept override |
28 | 4 | { |
29 | 4 | return this == &other; |
30 | 4 | } |
31 | | |
32 | | size_t getNumAllocations() const |
33 | 10 | { |
34 | 10 | return m_numAllocations; |
35 | 10 | } |
36 | | |
37 | | private: |
38 | | size_t m_numAllocations = 0; |
39 | | }; |
40 | | |
41 | | } // namespace |
42 | | |
43 | | TEST(PolymorphicAllocatorTest, constructorAndResource) |
44 | 1 | { |
45 | 1 | pmr::PolymorphicAllocator<> allocator; |
46 | 1 | ASSERT_EQ(pmr::getDefaultResource(), allocator.resource()); |
47 | | |
48 | 1 | pmr::PolymorphicAllocator<> allocatorNull(nullptr); |
49 | 1 | ASSERT_EQ(pmr::getDefaultResource(), allocatorNull.resource()); |
50 | | |
51 | 1 | TestResource resource; |
52 | | |
53 | 1 | pmr::PolymorphicAllocator<> allocator2(&resource); |
54 | 1 | ASSERT_EQ(&resource, allocator2.resource()); |
55 | | |
56 | 1 | pmr::PolymorphicAllocator<> allocator2Copy(allocator2); |
57 | 1 | ASSERT_EQ(&resource, allocator2Copy.resource()); |
58 | | |
59 | 1 | pmr::PolymorphicAllocator<> allocator2Moved(std::move(allocator2)); |
60 | 1 | ASSERT_EQ(&resource, allocator2Moved.resource()); |
61 | 1 | } |
62 | | |
63 | | TEST(PropagatingPolymorphicAllocatorTest, constructorAndResource) |
64 | 1 | { |
65 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator; |
66 | 1 | ASSERT_EQ(pmr::getDefaultResource(), allocator.resource()); |
67 | | |
68 | 1 | pmr::PropagatingPolymorphicAllocator<> allocatorNull(nullptr); |
69 | 1 | ASSERT_EQ(pmr::getDefaultResource(), allocatorNull.resource()); |
70 | | |
71 | 1 | TestResource resource; |
72 | | |
73 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator2(&resource); |
74 | 1 | ASSERT_EQ(&resource, allocator2.resource()); |
75 | | |
76 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator2Copy(allocator2); |
77 | 1 | ASSERT_EQ(&resource, allocator2Copy.resource()); |
78 | | |
79 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator2Moved(std::move(allocator2)); |
80 | 1 | ASSERT_EQ(&resource, allocator2Moved.resource()); |
81 | 1 | } |
82 | | |
83 | | TEST(PolymorphicAllocatorTest, constructorRebind) |
84 | 1 | { |
85 | 1 | TestResource resource; |
86 | 1 | pmr::PolymorphicAllocator<> allocator(&resource); |
87 | 1 | pmr::PolymorphicAllocator<int> allocatorRebind(allocator); |
88 | 1 | ASSERT_EQ(&resource, allocatorRebind.resource()); |
89 | 1 | } |
90 | | |
91 | | TEST(PropagatingPolymorphicAllocatorTest, constructorRebind) |
92 | 1 | { |
93 | 1 | TestResource resource; |
94 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator(&resource); |
95 | 1 | pmr::PolymorphicAllocator<int> allocatorRebind(allocator); |
96 | 1 | ASSERT_EQ(&resource, allocatorRebind.resource()); |
97 | 1 | } |
98 | | |
99 | | TEST(PolymorphicAllocatorTest, assignment) |
100 | 1 | { |
101 | 1 | TestResource resource; |
102 | 1 | pmr::PolymorphicAllocator<> allocator(&resource); |
103 | 1 | pmr::PolymorphicAllocator<int> allocatorRebind; |
104 | 1 | allocatorRebind = allocator; |
105 | 1 | ASSERT_EQ(&resource, allocatorRebind.resource()); |
106 | 1 | } |
107 | | |
108 | | TEST(PropagatingPolymorphicAllocatorTest, assignment) |
109 | 1 | { |
110 | 1 | TestResource resource; |
111 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator(&resource); |
112 | 1 | pmr::PropagatingPolymorphicAllocator<int> allocatorRebind; |
113 | 1 | allocatorRebind = allocator; |
114 | 1 | ASSERT_EQ(&resource, allocatorRebind.resource()); |
115 | 1 | } |
116 | | |
117 | | TEST(PolymorphicAllocatorTest, allocations) |
118 | 1 | { |
119 | 1 | TestResource resource; |
120 | 1 | pmr::PolymorphicAllocator<> allocator(&resource); |
121 | 1 | ASSERT_EQ(0, resource.getNumAllocations()); |
122 | | |
123 | 1 | const auto ptr = allocator.allocate(1); |
124 | 1 | ASSERT_EQ(1, resource.getNumAllocations()); |
125 | | |
126 | 1 | const auto ptr2 = allocator.allocate(10); |
127 | 1 | ASSERT_EQ(2, resource.getNumAllocations()); |
128 | | |
129 | 1 | allocator.deallocate(ptr, 1); |
130 | 1 | ASSERT_EQ(1, resource.getNumAllocations()); |
131 | | |
132 | 1 | allocator.deallocate(ptr2, 10); |
133 | 1 | ASSERT_EQ(0, resource.getNumAllocations()); |
134 | 1 | } |
135 | | |
136 | | TEST(PropagatingPolymorphicAllocatorTest, allocations) |
137 | 1 | { |
138 | 1 | TestResource resource; |
139 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator(&resource); |
140 | 1 | ASSERT_EQ(0, resource.getNumAllocations()); |
141 | | |
142 | 1 | const auto ptr = allocator.allocate(1); |
143 | 1 | ASSERT_EQ(1, resource.getNumAllocations()); |
144 | | |
145 | 1 | const auto ptr2 = allocator.allocate(10); |
146 | 1 | ASSERT_EQ(2, resource.getNumAllocations()); |
147 | | |
148 | 1 | allocator.deallocate(ptr, 1); |
149 | 1 | ASSERT_EQ(1, resource.getNumAllocations()); |
150 | | |
151 | 1 | allocator.deallocate(ptr2, 10); |
152 | 1 | ASSERT_EQ(0, resource.getNumAllocations()); |
153 | 1 | } |
154 | | |
155 | | TEST(PolymorphicAllocatorTest, select_on_container_copy_construction) |
156 | 1 | { |
157 | 1 | TestResource resource; |
158 | 1 | pmr::PolymorphicAllocator<> allocator(&resource); |
159 | | |
160 | 1 | std::vector<int, pmr::PolymorphicAllocator<int>> vector(allocator); |
161 | 1 | vector.assign({ 0, 13, 42 }); |
162 | 1 | std::vector<int, pmr::PolymorphicAllocator<int>> vectorCopy(vector); |
163 | 1 | ASSERT_NE(&resource, vectorCopy.get_allocator().resource()); |
164 | 1 | } |
165 | | |
166 | | TEST(PropagatingPolymorphicAllocatorTest, select_on_container_copy_construction) |
167 | 1 | { |
168 | 1 | TestResource resource; |
169 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator(&resource); |
170 | | |
171 | 1 | std::vector<int, pmr::PropagatingPolymorphicAllocator<int>> vector(allocator); |
172 | 1 | vector.assign({ 0, 13, 42 }); |
173 | 1 | std::vector<int, pmr::PropagatingPolymorphicAllocator<int>> vectorCopy(vector); |
174 | 1 | ASSERT_EQ(&resource, vectorCopy.get_allocator().resource()); |
175 | 1 | } |
176 | | |
177 | | TEST(PolymorphicAllocatorTest, compare) |
178 | 1 | { |
179 | 1 | pmr::PolymorphicAllocator<> allocator; |
180 | 1 | pmr::PolymorphicAllocator<> allocator2; |
181 | 1 | ASSERT_TRUE(allocator == allocator2); |
182 | 1 | ASSERT_FALSE(allocator != allocator2); |
183 | | |
184 | 1 | TestResource resource; |
185 | | |
186 | 1 | pmr::PolymorphicAllocator<> allocatorRes(&resource); |
187 | 1 | pmr::PolymorphicAllocator<> allocatorRes2(&resource); |
188 | 1 | ASSERT_TRUE(allocatorRes == allocatorRes2); |
189 | 1 | ASSERT_FALSE(allocatorRes != allocatorRes2); |
190 | 1 | ASSERT_FALSE(allocatorRes == allocator); |
191 | 1 | ASSERT_TRUE(allocatorRes != allocator); |
192 | 1 | } |
193 | | |
194 | | TEST(PropagatingPolymorphicAllocatorTest, compare) |
195 | 1 | { |
196 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator; |
197 | 1 | pmr::PropagatingPolymorphicAllocator<> allocator2; |
198 | 1 | ASSERT_TRUE(allocator == allocator2); |
199 | 1 | ASSERT_FALSE(allocator != allocator2); |
200 | | |
201 | 1 | TestResource resource; |
202 | | |
203 | 1 | pmr::PropagatingPolymorphicAllocator<> allocatorRes(&resource); |
204 | 1 | pmr::PropagatingPolymorphicAllocator<> allocatorRes2(&resource); |
205 | 1 | ASSERT_TRUE(allocatorRes == allocatorRes2); |
206 | 1 | ASSERT_FALSE(allocatorRes != allocatorRes2); |
207 | 1 | ASSERT_FALSE(allocatorRes == allocator); |
208 | 1 | ASSERT_TRUE(allocatorRes != allocator); |
209 | 1 | } |
210 | | |
211 | | } // namespace zserio |