summaryrefslogtreecommitdiff
path: root/test/pkbktarr.cpp
blob: 8d691eb9462bcfb576ab1e1650b5486ddcad2a60 (plain)
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

#include "../pkmem.h"
#define PK_IMPL_MEM
#define PK_IMPL_BKTARR
#include "../pkbktarr.h"

void test_spinup(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
	*bkt_buckets = pk_bucket_create("buckets", 1 << 16, false);
	*bkt_data = pk_bucket_create("data", 1 << 16, false);
}

void test_teardown(struct pk_membucket **bkt_buckets, struct pk_membucket **bkt_data)
{
	pk_bucket_destroy(*bkt_buckets);
	pk_bucket_destroy(*bkt_data);
	*bkt_buckets = nullptr;
	*bkt_data = nullptr;
}

int main(int argc, char *argv[])
{
	(void)argc;
	(void)argv;

	struct pk_membucket *bkt_buckets = nullptr;
	struct pk_membucket *bkt_data = nullptr;

	// test it works
	test_spinup(&bkt_buckets, &bkt_data);
	{
		assert(bkt_buckets != nullptr);
		assert(bkt_data != nullptr);
		struct pk_bkt_arr_handle limits;
		limits.b = PK_BKT_ARR_HANDLE_B_MAX;
		limits.i = PK_BKT_ARR_HANDLE_I_MAX;
		struct pk_bkt_arr_t<int> arr(limits, bkt_buckets, bkt_data);

		struct pk_bkt_arr_handle h = pk_bkt_arr_new_handle(&arr);
		arr[h] = 128;
		if (h.b != 0) exit(1);
		if (h.i != 0) exit(1);
		if (arr.head_l.b != 0) exit(1);
		if (arr.head_l.i != 1) exit(1);
		if (arr.head_r.b != 0) exit(1);
		if (arr.head_r.i != 1) exit(1);
		if (arr.idx_unused[0] != 0xFFFFFFFFFFFFFFFE) exit(1);
		if (bkt_buckets->allocs != 2) exit(1);
		if (bkt_data->allocs != 1) exit(1);
		if (arr[h] != 128) exit(1);

		pk_bkt_arr_free_handle(&arr, h);
		if (arr.head_l.b != 0) exit(1);
		if (arr.head_l.i != 0) exit(1);
		if (arr.head_r.b != 0) exit(1);
		if (arr.head_r.i != 1) exit(1);
		if (arr.idx_unused[0] != 0xFFFFFFFFFFFFFFFF) exit(1);
		if (bkt_buckets->allocs != 2) exit(1);
		if (bkt_data->allocs != 1) exit(1);

		arr.~pk_bkt_arr_t<int>();
		if (bkt_buckets->allocs != 0) exit(1);
		if (bkt_data->allocs != 0) exit(1);
	}
	test_teardown(&bkt_buckets, &bkt_data);
}