File: external_allocator_test.d

package info (click to toggle)
dcontainers 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 344 kB
  • sloc: makefile: 74
file content (78 lines) | stat: -rw-r--r-- 2,672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module external_allocator_test;

import containers.cyclicbuffer;
import containers.dynamicarray;
import containers.hashmap;
import containers.hashset;
import containers.immutablehashset;
import containers.openhashset;
import containers.simdset;
import containers.slist;
import containers.treemap;
import containers.ttree;
import containers.unrolledlist;
import std.meta : AliasSeq;
import std.range.primitives : walkLength;
import std.stdio : stdout;
import std.experimental.allocator.building_blocks.allocator_list : AllocatorList;
import std.experimental.allocator.building_blocks.free_list : FreeList;
import std.experimental.allocator.building_blocks.region : Region;
import std.experimental.allocator.building_blocks.stats_collector : StatsCollector;
import std.experimental.allocator.mallocator : Mallocator;

// Chosen for a very important and completely undocumented reason
private enum VERY_SPECIFIC_NUMBER = 371;

private void testSingle(alias Container, Allocator)(Allocator allocator)
{
	auto intMap = Container!(int, Allocator)(allocator);
	foreach (i; 0 .. VERY_SPECIFIC_NUMBER)
		intMap.insert(i);
	assert(intMap.length == VERY_SPECIFIC_NUMBER);
}

private void testDouble(alias Container, Allocator)(Allocator allocator)
{
	auto intMap = Container!(int, int, Allocator)(allocator);
	foreach (i; 0 .. VERY_SPECIFIC_NUMBER)
		intMap[i] = VERY_SPECIFIC_NUMBER - i;
	assert(intMap.length == VERY_SPECIFIC_NUMBER);
}

version (D_InlineAsm_X86_64)
{
	alias SingleContainers = AliasSeq!(CyclicBuffer, DynamicArray,  HashSet,  /+ImmutableHashSet,+/
	OpenHashSet, SimdSet, SList, TTree, UnrolledList);
}
else
{
	alias SingleContainers = AliasSeq!(CyclicBuffer, DynamicArray,  HashSet,  /+ImmutableHashSet,+/
	OpenHashSet, /+SimdSet,+/ SList, TTree, UnrolledList);
}

alias DoubleContainers = AliasSeq!(HashMap, TreeMap);

alias AllocatorType = StatsCollector!(
	FreeList!(AllocatorList!(a => Region!(Mallocator)(1024 * 1024), Mallocator), 64));

unittest
{
	foreach (C; SingleContainers)
	{
		AllocatorType allocator;
		testSingle!(C, AllocatorType*)(&allocator);
		assert(allocator.numAllocate > 0 || allocator.numReallocate > 0,
			"No allocations happened for " ~ C.stringof);
		assert(allocator.numAllocate == allocator.numDeallocate || allocator.numReallocate > 0);
		assert(allocator.bytesUsed == 0);
	}
	foreach (C; DoubleContainers)
	{
		AllocatorType allocator;
		testDouble!(C, AllocatorType*)(&allocator);
		assert(allocator.numAllocate > 0 || allocator.numReallocate > 0,
			"No allocations happened for " ~ C.stringof);
		assert(allocator.numAllocate == allocator.numDeallocate || allocator.numReallocate > 0);
		assert(allocator.bytesUsed == 0);
	}
}