summaryrefslogtreecommitdiff
path: root/memory/jemalloc/src/test/unit/rtree.c
blob: b54b3e86f574b3a3fc3bf42b8697b8fd38bdb1d1 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "test/jemalloc_test.h"

static rtree_node_elm_t *
node_alloc(size_t nelms)
{

	return ((rtree_node_elm_t *)calloc(nelms, sizeof(rtree_node_elm_t)));
}

static void
node_dalloc(rtree_node_elm_t *node)
{

	free(node);
}

TEST_BEGIN(test_rtree_get_empty)
{
	unsigned i;

	for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
		rtree_t rtree;
		assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
		    "Unexpected rtree_new() failure");
		assert_ptr_null(rtree_get(&rtree, 0, false),
		    "rtree_get() should return NULL for empty tree");
		rtree_delete(&rtree);
	}
}
TEST_END

TEST_BEGIN(test_rtree_extrema)
{
	unsigned i;
	extent_node_t node_a, node_b;

	for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
		rtree_t rtree;
		assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
		    "Unexpected rtree_new() failure");

		assert_false(rtree_set(&rtree, 0, &node_a),
		    "Unexpected rtree_set() failure");
		assert_ptr_eq(rtree_get(&rtree, 0, true), &node_a,
		    "rtree_get() should return previously set value");

		assert_false(rtree_set(&rtree, ~((uintptr_t)0), &node_b),
		    "Unexpected rtree_set() failure");
		assert_ptr_eq(rtree_get(&rtree, ~((uintptr_t)0), true), &node_b,
		    "rtree_get() should return previously set value");

		rtree_delete(&rtree);
	}
}
TEST_END

TEST_BEGIN(test_rtree_bits)
{
	unsigned i, j, k;

	for (i = 1; i < (sizeof(uintptr_t) << 3); i++) {
		uintptr_t keys[] = {0, 1,
		    (((uintptr_t)1) << (sizeof(uintptr_t)*8-i)) - 1};
		extent_node_t node;
		rtree_t rtree;

		assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
		    "Unexpected rtree_new() failure");

		for (j = 0; j < sizeof(keys)/sizeof(uintptr_t); j++) {
			assert_false(rtree_set(&rtree, keys[j], &node),
			    "Unexpected rtree_set() failure");
			for (k = 0; k < sizeof(keys)/sizeof(uintptr_t); k++) {
				assert_ptr_eq(rtree_get(&rtree, keys[k], true),
				    &node, "rtree_get() should return "
				    "previously set value and ignore "
				    "insignificant key bits; i=%u, j=%u, k=%u, "
				    "set key=%#"FMTxPTR", get key=%#"FMTxPTR, i,
				    j, k, keys[j], keys[k]);
			}
			assert_ptr_null(rtree_get(&rtree,
			    (((uintptr_t)1) << (sizeof(uintptr_t)*8-i)), false),
			    "Only leftmost rtree leaf should be set; "
			    "i=%u, j=%u", i, j);
			assert_false(rtree_set(&rtree, keys[j], NULL),
			    "Unexpected rtree_set() failure");
		}

		rtree_delete(&rtree);
	}
}
TEST_END

TEST_BEGIN(test_rtree_random)
{
	unsigned i;
	sfmt_t *sfmt;
#define	NSET 16
#define	SEED 42

	sfmt = init_gen_rand(SEED);
	for (i = 1; i <= (sizeof(uintptr_t) << 3); i++) {
		uintptr_t keys[NSET];
		extent_node_t node;
		unsigned j;
		rtree_t rtree;

		assert_false(rtree_new(&rtree, i, node_alloc, node_dalloc),
		    "Unexpected rtree_new() failure");

		for (j = 0; j < NSET; j++) {
			keys[j] = (uintptr_t)gen_rand64(sfmt);
			assert_false(rtree_set(&rtree, keys[j], &node),
			    "Unexpected rtree_set() failure");
			assert_ptr_eq(rtree_get(&rtree, keys[j], true), &node,
			    "rtree_get() should return previously set value");
		}
		for (j = 0; j < NSET; j++) {
			assert_ptr_eq(rtree_get(&rtree, keys[j], true), &node,
			    "rtree_get() should return previously set value");
		}

		for (j = 0; j < NSET; j++) {
			assert_false(rtree_set(&rtree, keys[j], NULL),
			    "Unexpected rtree_set() failure");
			assert_ptr_null(rtree_get(&rtree, keys[j], true),
			    "rtree_get() should return previously set value");
		}
		for (j = 0; j < NSET; j++) {
			assert_ptr_null(rtree_get(&rtree, keys[j], true),
			    "rtree_get() should return previously set value");
		}

		rtree_delete(&rtree);
	}
	fini_gen_rand(sfmt);
#undef NSET
#undef SEED
}
TEST_END

int
main(void)
{

	return (test(
	    test_rtree_get_empty,
	    test_rtree_extrema,
	    test_rtree_bits,
	    test_rtree_random));
}