| 1 |
module external_allocator_test; |
| 2 |
|
| 3 |
import containers.cyclicbuffer; |
| 4 |
import containers.dynamicarray; |
| 5 |
import containers.hashmap; |
| 6 |
import containers.hashset; |
| 7 |
import containers.immutablehashset; |
| 8 |
import containers.openhashset; |
| 9 |
import containers.simdset; |
| 10 |
import containers.slist; |
| 11 |
import containers.treemap; |
| 12 |
import containers.ttree; |
| 13 |
import containers.unrolledlist; |
| 14 |
import std.meta : AliasSeq; |
| 15 |
import std.range.primitives : walkLength; |
| 16 |
import std.stdio : stdout; |
| 17 |
import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; |
| 18 |
import std.experimental.allocator.building_blocks.free_list : FreeList; |
| 19 |
import std.experimental.allocator.building_blocks.region : Region; |
| 20 |
import std.experimental.allocator.building_blocks.stats_collector : StatsCollector; |
| 21 |
import std.experimental.allocator.mallocator : Mallocator; |
| 22 |
|
| 23 |
// Chosen for a very important and completely undocumented reason |
| 24 |
private enum VERY_SPECIFIC_NUMBER = 371; |
| 25 |
|
| 26 |
private void testSingle(alias Container, Allocator)(Allocator allocator) |
| 27 |
{ |
| 28 |
auto intMap = Container!(int, Allocator)(allocator); |
| 29 |
foreach (i; 0 .. VERY_SPECIFIC_NUMBER) |
| 30 |
intMap.insert(i); |
| 31 |
assert(intMap.length == VERY_SPECIFIC_NUMBER); |
| 32 |
} |
| 33 |
|
| 34 |
private void testDouble(alias Container, Allocator)(Allocator allocator) |
| 35 |
{ |
| 36 |
auto intMap = Container!(int, int, Allocator)(allocator); |
| 37 |
foreach (i; 0 .. VERY_SPECIFIC_NUMBER) |
| 38 |
intMap[i] = VERY_SPECIFIC_NUMBER - i; |
| 39 |
assert(intMap.length == VERY_SPECIFIC_NUMBER); |
| 40 |
} |
| 41 |
|
| 42 |
version (D_InlineAsm_X86_64) |
| 43 |
{ |
| 44 |
alias SingleContainers = AliasSeq!(CyclicBuffer, DynamicArray, HashSet, /+ImmutableHashSet,+/ |
| 45 |
OpenHashSet, SimdSet, SList, TTree, UnrolledList); |
| 46 |
} |
| 47 |
else |
| 48 |
{ |
| 49 |
alias SingleContainers = AliasSeq!(CyclicBuffer, DynamicArray, HashSet, /+ImmutableHashSet,+/ |
| 50 |
OpenHashSet, /+SimdSet,+/ SList, TTree, UnrolledList); |
| 51 |
} |
| 52 |
|
| 53 |
alias DoubleContainers = AliasSeq!(HashMap, TreeMap); |
| 54 |
|
| 55 |
alias AllocatorType = StatsCollector!( |
| 56 |
FreeList!(AllocatorList!(a => Region!(Mallocator)(1024 * 1024), Mallocator), 64)); |
| 57 |
|
| 58 |
unittest |
| 59 |
{ |
| 60 |
foreach (C; SingleContainers) |
| 61 |
{ |
| 62 |
AllocatorType allocator; |
| 63 |
testSingle!(C, AllocatorType*)(&allocator); |
| 64 |
assert(allocator.numAllocate > 0 || allocator.numReallocate > 0, |
| 65 |
"No allocations happened for " ~ C.stringof); |
| 66 |
assert(allocator.numAllocate == allocator.numDeallocate || allocator.numReallocate > 0); |
| 67 |
assert(allocator.bytesUsed == 0); |
| 68 |
} |
| 69 |
foreach (C; DoubleContainers) |
| 70 |
{ |
| 71 |
AllocatorType allocator; |
| 72 |
testDouble!(C, AllocatorType*)(&allocator); |
| 73 |
assert(allocator.numAllocate > 0 || allocator.numReallocate > 0, |
| 74 |
"No allocations happened for " ~ C.stringof); |
| 75 |
assert(allocator.numAllocate == allocator.numDeallocate || allocator.numReallocate > 0); |
| 76 |
assert(allocator.bytesUsed == 0); |
| 77 |
} |
| 78 |
} |