@@ -42,16 +42,14 @@ struct PoolLargeEntry : Pooled<PoolLargeEntry>
42
42
43
43
using PoolLarge = Pool<PoolLargeEntry>;
44
44
45
- template <bool order>
46
- struct PoolSortEntry : Pooled<PoolSortEntry<order>>
45
+ struct PoolSortEntry : Pooled<PoolSortEntry>
47
46
{
48
47
int field;
49
48
50
49
PoolSortEntry () : field(1 ){};
51
50
};
52
51
53
- template <bool order>
54
- using PoolSort = Pool<PoolSortEntry<order>>;
52
+ using PoolSort = Pool<PoolSortEntry>;
55
53
56
54
void test_alloc ()
57
55
{
@@ -159,50 +157,86 @@ void test_large()
159
157
* This test confirms that the pool is sorted consistently with
160
158
* respect to the iterator after a call to sort.
161
159
*/
162
- template <bool order>
163
160
void test_sort ()
164
161
{
165
- auto position = [](PoolSortEntry<order> * ptr) {
162
+ auto position = [](PoolSortEntry* ptr) {
166
163
size_t i = 0 ;
167
- auto curr = PoolSort<order> ::iterate();
164
+ auto curr = PoolSort::iterate ();
168
165
while (ptr != curr)
169
166
{
170
- curr = PoolSort<order> ::iterate(curr);
167
+ curr = PoolSort::iterate (curr);
171
168
++i;
172
169
}
173
170
return i;
174
171
};
175
172
176
173
// This test checks that `sort` puts the elements in the right order,
177
174
// so it is the same as if they had been allocated in that order.
178
- auto a1 = PoolSort<order>::acquire ();
179
- auto a2 = PoolSort<order>::acquire ();
175
+ auto a1 = PoolSort::acquire ();
176
+ auto a2 = PoolSort::acquire ();
177
+ auto a3 = PoolSort::acquire ();
180
178
181
179
auto position1 = position (a1);
182
180
auto position2 = position (a2);
181
+ auto position3 = position (a3);
183
182
184
- // Release in either order.
185
- if (order)
186
- {
187
- PoolSort<order>:: release (a1 );
188
- PoolSort<order>:: release (a2);
189
- }
190
- else
183
+ PoolSort::release (a1);
184
+ PoolSort::release (a2);
185
+ PoolSort::release (a3);
186
+ PoolSort::sort ( );
187
+
188
+ // Repeat the test to ensure it re-establishes the order.
189
+ for ( size_t i = 0 ; i < 12 ; i++)
191
190
{
192
- PoolSort<order>:: release (a2 );
193
- PoolSort<order>:: release (a1 );
194
- }
191
+ auto b1 = PoolSort::acquire ( );
192
+ auto b2 = PoolSort::acquire ( );
193
+ auto b3 = PoolSort::acquire ();
195
194
196
- PoolSort<order>::sort ();
195
+ auto new_position1 = position (b1);
196
+ auto new_position2 = position (b2);
197
+ auto new_position3 = position (b3);
197
198
198
- auto b1 = PoolSort<order>::acquire ();
199
- auto b2 = PoolSort<order>::acquire ();
199
+ SNMALLOC_CHECK (new_position1 == position1);
200
+ SNMALLOC_CHECK (new_position2 == position2);
201
+ SNMALLOC_CHECK (new_position3 == position3);
200
202
201
- SNMALLOC_CHECK (position1 == position (b1));
202
- SNMALLOC_CHECK (position2 == position (b2));
203
+ // Release in either order.
204
+ switch (i % 6 )
205
+ {
206
+ case 0 :
207
+ PoolSort::release (b1);
208
+ PoolSort::release (b2);
209
+ PoolSort::release (b3);
210
+ break ;
211
+ case 1 :
212
+ PoolSort::release (b1);
213
+ PoolSort::release (b3);
214
+ PoolSort::release (b2);
215
+ break ;
216
+ case 2 :
217
+ PoolSort::release (b2);
218
+ PoolSort::release (b1);
219
+ PoolSort::release (b3);
220
+ break ;
221
+ case 3 :
222
+ PoolSort::release (b2);
223
+ PoolSort::release (b3);
224
+ PoolSort::release (b1);
225
+ break ;
226
+ case 4 :
227
+ PoolSort::release (b3);
228
+ PoolSort::release (b1);
229
+ PoolSort::release (b2);
230
+ break ;
231
+ case 5 :
232
+ PoolSort::release (b3);
233
+ PoolSort::release (b2);
234
+ PoolSort::release (b1);
235
+ break ;
236
+ }
203
237
204
- PoolSort<order>:: release (b1 );
205
- PoolSort<order>:: release (b2);
238
+ PoolSort::sort ( );
239
+ }
206
240
}
207
241
208
242
int main (int argc, char ** argv)
@@ -230,9 +264,7 @@ int main(int argc, char** argv)
230
264
std::cout << " test_iterator passed" << std::endl;
231
265
test_large ();
232
266
std::cout << " test_large passed" << std::endl;
233
- test_sort<false >();
234
- std::cout << " test_sort<false> passed" << std::endl;
235
- test_sort<true >();
236
- std::cout << " test_sort<true> passed" << std::endl;
267
+ test_sort ();
268
+ std::cout << " test_sort passed" << std::endl;
237
269
return 0 ;
238
270
}
0 commit comments