summaryrefslogtreecommitdiff
path: root/test/pkmem.c
blob: ad95899109c391ba03d0e0b737c67fb92336ea71 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#define PK_IMPL_MEM

#include "../pkmem.h"

#define PK_IMPL_FUNCINSTR
#include "../pkfuncinstr.h"

#include <stdio.h>
#include <string.h>

struct mem_test {
	struct pk_membucket *bkt1;
	struct pk_membucket *bkt2;
} mt;

void spinup() {
	mt.bkt1 = pk_mem_bucket_create("bkt1", 1024, false);
	mt.bkt2 = pk_mem_bucket_create("bkt2", 1024, false);
}

void spinup_w_instr() {
	pk_funcinstr_init();
	spinup();
}

void teardown() {
	pk_mem_bucket_destroy(mt.bkt1);
	pk_mem_bucket_destroy(mt.bkt2);
	pk_funcinstr_teardown();
}

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

	// pk_new_base
	{
		spinup_w_instr();
		pk_mem_bucket_set_client_mem_bucket(mt.bkt1);
		char *some_dang_string = (char*)pk_new_base(64, alignof(char*));
		fprintf(stdout, "some_dang_string: %s: %p\n", some_dang_string, (void *)some_dang_string);
		pk_delete_base(some_dang_string, 64);
		teardown();
	}

	/* template
	{
		spinup();
		teardown();
	}
	*/

	// zero-length alloc
	{
		spinup_w_instr();
		char *zero_length_string;
		char *three_length_string;

		zero_length_string = pk_new_bkt(0, 0, mt.bkt1);
		three_length_string = pk_new_bkt(3, 0, mt.bkt1);
		three_length_string[0] = '1';
		three_length_string[1] = '2';

		if (zero_length_string != NULL) exit(1);
		if (strlen(three_length_string) != 2) exit(1);
		if (mt.bkt1->alloc_count != 1) exit(1);
		if (mt.bkt1->head != 3) exit(1);
		if (mt.bkt1->block_head_r != 0) exit(1);

		PK_LOGV_INF("%s: %s\n", __FILE__, "handles zero-length alloc");
		teardown();
	}

	// expand buckets
	{
		spinup_w_instr();

		struct pk_memblock *blk;

		for (size_t i = 0; i < 16; ++i) {
			pk_new_bkt(7, 8, mt.bkt1);
		}

		blk = mt.bkt1->blocks;

		if (mt.bkt1->alloc_count != 16) exit(1);
		if (mt.bkt1->head != (8*16)-1) exit(1);
		if (mt.bkt1->block_head_r != 15) exit(1);
		if (mt.bkt1->block_capacity != 16) exit(1);
		if (mt.bkt1->blocks != blk) exit(1);

		pk_new_bkt(7, 8, mt.bkt1);

		if (mt.bkt1->alloc_count != 17) exit(1);
		if (mt.bkt1->head != (8*17)-1) exit(1);
		if (mt.bkt1->block_head_r != 16) exit(1);
		if (mt.bkt1->block_capacity != 17) exit(1);
		if (mt.bkt1->blocks == blk) exit(1);

		blk = mt.bkt1->blocks;

		pk_new_bkt(7, 8, mt.bkt1);

		if (mt.bkt1->alloc_count != 18) exit(1);
		if (mt.bkt1->head != (8*18)-1) exit(1);
		if (mt.bkt1->block_head_r != 17) exit(1);
		if (mt.bkt1->block_capacity != 18) exit(1);
		if (mt.bkt1->blocks == blk) exit(1);

		PK_LOGV_INF("%s: %s\n", __FILE__, "handles block growth");
		teardown();
	}

	return 0;
}