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