PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_radixtree.c File Reference
#include "postgres.h"
#include "common/int.h"
#include "common/pg_prng.h"
#include "fmgr.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "lib/radixtree.h"
Include dependency graph for test_radixtree.c:

Go to the source code of this file.

Data Structures

struct  rt_node_class_test_elem
 

Macros

#define EXPECT_TRUE(expr)
 
#define EXPECT_FALSE(expr)
 
#define EXPECT_EQ_U64(result_expr, expected_expr)
 
#define RT_PREFIX   rt
 
#define RT_SCOPE
 
#define RT_DECLARE
 
#define RT_DEFINE
 
#define RT_USE_DELETE
 
#define RT_VALUE_TYPE   TestValueType
 
#define RT_DEBUG
 

Typedefs

typedef uint64 TestValueType
 
typedef struct rt_node_class_test_elem rt_node_class_test_elem
 

Functions

static uint64 rt_num_entries (rt_radix_tree *tree)
 
 PG_FUNCTION_INFO_V1 (test_radixtree)
 
static void test_empty (void)
 
static void test_basic (rt_node_class_test_elem *test_info, int shift, bool asc)
 
static int key_cmp (const void *a, const void *b)
 
static void test_random (void)
 
Datum test_radixtree (PG_FUNCTION_ARGS)
 

Variables

static rt_node_class_test_elem rt_node_class_tests []
 
 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ EXPECT_EQ_U64

#define EXPECT_EQ_U64 (   result_expr,
  expected_expr 
)
Value:
do { \
"%s yielded %" PRIx64 ", expected %" PRIx64 " (%s) in file \"%s\" line %u", \
} while (0)
uint64_t uint64
Definition c.h:547
#define ERROR
Definition elog.h:39
static int fb(int x)

Definition at line 41 of file test_radixtree.c.

42 { \
46 elog(ERROR, \
47 "%s yielded %" PRIx64 ", expected %" PRIx64 " (%s) in file \"%s\" line %u", \
49 } while (0)

◆ EXPECT_FALSE

#define EXPECT_FALSE (   expr)
Value:
do { \
if (expr) \
"%s was unexpectedly true in file \"%s\" line %u", \
#expr, __FILE__, __LINE__); \
} while (0)

Definition at line 33 of file test_radixtree.c.

34 { \
35 if (expr) \
36 elog(ERROR, \
37 "%s was unexpectedly true in file \"%s\" line %u", \
38 #expr, __FILE__, __LINE__); \
39 } while (0)

◆ EXPECT_TRUE

#define EXPECT_TRUE (   expr)
Value:
do { \
if (!(expr)) \
"%s was unexpectedly false in file \"%s\" line %u", \
#expr, __FILE__, __LINE__); \
} while (0)

Definition at line 25 of file test_radixtree.c.

26 { \
27 if (!(expr)) \
28 elog(ERROR, \
29 "%s was unexpectedly false in file \"%s\" line %u", \
30 #expr, __FILE__, __LINE__); \
31 } while (0)

◆ RT_DEBUG

#define RT_DEBUG

Definition at line 103 of file test_radixtree.c.

◆ RT_DECLARE

#define RT_DECLARE

Definition at line 96 of file test_radixtree.c.

◆ RT_DEFINE

#define RT_DEFINE

Definition at line 97 of file test_radixtree.c.

◆ RT_PREFIX

#define RT_PREFIX   rt

Definition at line 94 of file test_radixtree.c.

◆ RT_SCOPE

#define RT_SCOPE

Definition at line 95 of file test_radixtree.c.

◆ RT_USE_DELETE

#define RT_USE_DELETE

Definition at line 98 of file test_radixtree.c.

◆ RT_VALUE_TYPE

#define RT_VALUE_TYPE   TestValueType

Definition at line 99 of file test_radixtree.c.

Typedef Documentation

◆ rt_node_class_test_elem

◆ TestValueType

Definition at line 56 of file test_radixtree.c.

Function Documentation

◆ key_cmp()

static int key_cmp ( const void a,
const void b 
)
static

Definition at line 287 of file test_radixtree.c.

288{
289 return pg_cmp_u64(*(const uint64 *) a, *(const uint64 *) b);
290}
static int pg_cmp_u64(uint64 a, uint64 b)
Definition int.h:731
int b
Definition isn.c:74
int a
Definition isn.c:73

References a, b, and pg_cmp_u64().

Referenced by test_random().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( test_radixtree  )

◆ rt_num_entries()

static uint64 rt_num_entries ( rt_radix_tree tree)
static

Definition at line 111 of file test_radixtree.c.

112{
113 return tree->ctl->num_keys;
114}
tree
Definition radixtree.h:1828

References tree.

Referenced by test_empty(), and test_random().

◆ test_basic()

static void test_basic ( rt_node_class_test_elem test_info,
int  shift,
bool  asc 
)
static

Definition at line 162 of file test_radixtree.c.

163{
165 rt_iter *iter;
166 uint64 *keys;
167 int children = test_info->nkeys;
168#ifdef TEST_SHARED_RT
169 int tranche_id = LWLockNewTrancheId("test_radix_tree");
170 dsa_area *dsa;
171
172 dsa = dsa_create(tranche_id);
173 radixtree = rt_create(dsa, tranche_id);
174#else
176
178 "test_radix_tree",
181#endif
182
183 elog(NOTICE, "testing node %s with shift %d and %s keys",
184 test_info->class_name, shift, asc ? "ascending" : "descending");
185
186 keys = palloc_array(uint64, children);
187 for (int i = 0; i < children; i++)
188 {
189 if (asc)
190 keys[i] = (uint64) i << shift;
191 else
192 keys[i] = (uint64) (children - 1 - i) << shift;
193 }
194
195 /*
196 * Insert keys. Since the tree was just created, rt_set should return
197 * false.
198 */
199 for (int i = 0; i < children; i++)
200 EXPECT_FALSE(rt_set(radixtree, keys[i], (TestValueType *) &keys[i]));
201
203
204 /* look up keys */
205 for (int i = 0; i < children; i++)
206 {
208
209 value = rt_find(radixtree, keys[i]);
210
211 /* Test rt_find returns the expected value */
214 }
215
216 /* update keys */
217 for (int i = 0; i < children; i++)
218 {
219 TestValueType update = keys[i] + 1;
220
221 /* rt_set should report the key found */
222 EXPECT_TRUE(rt_set(radixtree, keys[i], &update));
223 }
224
225 /* delete and re-insert keys */
226 for (int i = 0; i < children; i++)
227 {
229 EXPECT_FALSE(rt_set(radixtree, keys[i], (TestValueType *) &keys[i]));
230 }
231
232 /* look up keys after deleting and re-inserting */
233 for (int i = 0; i < children; i++)
234 {
236
237 value = rt_find(radixtree, keys[i]);
238
239 /* Test that rt_find returns the expected value */
242 }
243
244 /* test that iteration returns the expected keys and values */
246
247 for (int i = 0; i < children; i++)
248 {
252
253 /* iteration is ordered by key, so adjust expected value accordingly */
254 if (asc)
255 expected = keys[i];
256 else
257 expected = keys[children - 1 - i];
258
260
264 }
265
266 rt_end_iterate(iter);
267
268 /* delete all keys again */
269 for (int i = 0; i < children; i++)
271
272 /* test that all keys were deleted */
273 for (int i = 0; i < children; i++)
274 EXPECT_TRUE(rt_find(radixtree, keys[i]) == NULL);
275
277
278 pfree(keys);
280
281#ifdef TEST_SHARED_RT
282 dsa_detach(dsa);
283#endif
284}
void dsa_detach(dsa_area *area)
Definition dsa.c:2002
#define dsa_create(tranche_id)
Definition dsa.h:117
#define elog(elevel,...)
Definition elog.h:226
#define NOTICE
Definition elog.h:35
#define palloc_array(type, count)
Definition fe_memutils.h:76
static struct @172 value
int i
Definition isn.c:77
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:596
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_SMALL_SIZES
Definition memutils.h:170
#define EXPECT_TRUE(expr)
uint64 TestValueType
#define EXPECT_FALSE(expr)
#define EXPECT_EQ_U64(result_expr, expected_expr)

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, CurrentMemoryContext, dsa_create, dsa_detach(), elog, EXPECT_EQ_U64, EXPECT_FALSE, EXPECT_TRUE, fb(), i, LWLockNewTrancheId(), NOTICE, palloc_array, pfree(), and value.

Referenced by test_radixtree().

◆ test_empty()

static void test_empty ( void  )
static

Definition at line 121 of file test_radixtree.c.

122{
124 rt_iter *iter;
125 uint64 key;
126#ifdef TEST_SHARED_RT
127 int tranche_id = LWLockNewTrancheId("test_radix_tree");
128 dsa_area *dsa;
129
130 dsa = dsa_create(tranche_id);
131 radixtree = rt_create(dsa, tranche_id);
132#else
134
136 "test_radix_tree",
139#endif
140
141 /* Should not find anything in an empty tree */
147
148 /* Iterating on an empty tree should not return anything */
150 EXPECT_TRUE(rt_iterate_next(iter, &key) == NULL);
151 rt_end_iterate(iter);
152
154
155#ifdef TEST_SHARED_RT
156 dsa_detach(dsa);
157#endif
158}
#define PG_UINT64_MAX
Definition c.h:607
static uint64 rt_num_entries(rt_radix_tree *tree)

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, CurrentMemoryContext, dsa_create, dsa_detach(), EXPECT_FALSE, EXPECT_TRUE, fb(), LWLockNewTrancheId(), PG_UINT64_MAX, and rt_num_entries().

Referenced by test_radixtree().

◆ test_radixtree()

Datum test_radixtree ( PG_FUNCTION_ARGS  )

Definition at line 431 of file test_radixtree.c.

432{
433 /* borrowed from RT_MAX_SHIFT */
434 const int max_shift = (sizeof(uint64) - 1) * BITS_PER_BYTE;
435
436 test_empty();
437
438 for (int i = 0; i < lengthof(rt_node_class_tests); i++)
439 {
441
442 /* a tree with one level, i.e. a single node under the root node */
443 test_basic(test_info, 0, true);
444 test_basic(test_info, 0, false);
445
446 /* a tree with two levels */
447 test_basic(test_info, 8, true);
448 test_basic(test_info, 8, false);
449
450 /* a tree with the maximum number of levels */
453 }
454
455 test_random();
456
458}
#define lengthof(array)
Definition c.h:803
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define BITS_PER_BYTE
static void test_random(void)
static void test_empty(void)
static void test_basic(rt_node_class_test_elem *test_info, int shift, bool asc)
static rt_node_class_test_elem rt_node_class_tests[]

References BITS_PER_BYTE, fb(), i, lengthof, PG_RETURN_VOID, rt_node_class_tests, test_basic(), test_empty(), and test_random().

◆ test_random()

static void test_random ( void  )
static

Definition at line 293 of file test_radixtree.c.

294{
296 rt_iter *iter;
298
299 /* limit memory usage by limiting the key space */
300 uint64 filter = ((uint64) (0x07 << 24) | (0xFF << 16) | 0xFF);
302 int num_keys = 100000;
303 uint64 *keys;
304#ifdef TEST_SHARED_RT
305 int tranche_id = LWLockNewTrancheId("test_radix_tree");
306 dsa_area *dsa;
307
308 dsa = dsa_create(tranche_id);
309 radixtree = rt_create(dsa, tranche_id);
310#else
312
314 "test_radix_tree",
316 sizeof(TestValueType));
318#endif
319
320 /* add some random values */
321 pg_prng_seed(&state, seed);
322 keys = (TestValueType *) palloc(sizeof(uint64) * num_keys);
323 for (uint64 i = 0; i < num_keys; i++)
324 {
325 uint64 key = pg_prng_uint64(&state) & filter;
327
328 /* save in an array */
329 keys[i] = key;
330
331 rt_set(radixtree, key, &val);
332 }
333
335
336 for (uint64 i = 0; i < num_keys; i++)
337 {
339
340 value = rt_find(radixtree, keys[i]);
341
342 /* Test rt_find for values just inserted */
344 EXPECT_EQ_U64(*value, keys[i]);
345 }
346
347 /* sort keys for iteration and absence tests */
348 qsort(keys, num_keys, sizeof(uint64), key_cmp);
349
350 /* should not find numbers in between the keys */
351 for (uint64 i = 0; i < num_keys - 1; i++)
352 {
354
355 /* skip duplicate and adjacent keys */
356 if (keys[i + 1] == keys[i] || keys[i + 1] == keys[i] + 1)
357 continue;
358
359 /* should not find the number right after key */
360 value = rt_find(radixtree, keys[i] + 1);
362 }
363
364 /* should not find numbers lower than lowest key */
365 for (uint64 key = 0; key < keys[0]; key++)
366 {
368
369 /* arbitrary stopping point */
370 if (key > 10000)
371 break;
372
373 value = rt_find(radixtree, key);
375 }
376
377 /* should not find numbers higher than highest key */
378 for (uint64 i = 1; i < 10000; i++)
379 {
381
382 value = rt_find(radixtree, keys[num_keys - 1] + i);
384 }
385
386 /* test that iteration returns the expected keys and values */
388
389 for (int i = 0; i < num_keys; i++)
390 {
394
395 /* skip duplicate keys */
396 if (i < num_keys - 1 && keys[i + 1] == keys[i])
397 continue;
398
399 expected = keys[i];
401
405 }
406
407 rt_end_iterate(iter);
408
409 /* reset random number generator for deletion */
410 pg_prng_seed(&state, seed);
411
412 /* delete in original random order */
413 for (uint64 i = 0; i < num_keys; i++)
414 {
415 uint64 key = pg_prng_uint64(&state) & filter;
416
417 rt_delete(radixtree, key);
418 }
419
421
422 pfree(keys);
424
425#ifdef TEST_SHARED_RT
426 dsa_detach(dsa);
427#endif
428}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
long val
Definition informix.c:689
void * palloc(Size size)
Definition mcxt.c:1387
#define SLAB_DEFAULT_BLOCK_SIZE
Definition memutils.h:189
uint64 pg_prng_uint64(pg_prng_state *state)
Definition pg_prng.c:134
void pg_prng_seed(pg_prng_state *state, uint64 seed)
Definition pg_prng.c:89
#define qsort(a, b, c, d)
Definition port.h:495
MemoryContext SlabContextCreate(MemoryContext parent, const char *name, Size blockSize, Size chunkSize)
Definition slab.c:322
static int key_cmp(const void *a, const void *b)

References CurrentMemoryContext, dsa_create, dsa_detach(), EXPECT_EQ_U64, EXPECT_TRUE, fb(), GetCurrentTimestamp(), i, key_cmp(), LWLockNewTrancheId(), palloc(), pfree(), pg_prng_seed(), pg_prng_uint64(), qsort, rt_num_entries(), SLAB_DEFAULT_BLOCK_SIZE, SlabContextCreate(), val, and value.

Referenced by test_radixtree().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 116 of file test_radixtree.c.

◆ rt_node_class_tests

rt_node_class_test_elem rt_node_class_tests[]
static
Initial value:
=
{
{
.class_name = "node-4",
.nkeys = 2,
},
{
.class_name = "node-16-lo",
.nkeys = 15,
},
{
.class_name = "node-16-hi",
.nkeys = 30,
},
{
.class_name = "node-48",
.nkeys = 60,
},
{
.class_name = "node-256",
.nkeys = 256,
},
}

Definition at line 68 of file test_radixtree.c.

69{
70 {
71 .class_name = "node-4", /* RT_CLASS_4 */
72 .nkeys = 2,
73 },
74 {
75 .class_name = "node-16-lo", /* RT_CLASS_16_LO */
76 .nkeys = 15,
77 },
78 {
79 .class_name = "node-16-hi", /* RT_CLASS_16_HI */
80 .nkeys = 30,
81 },
82 {
83 .class_name = "node-48", /* RT_CLASS_48 */
84 .nkeys = 60,
85 },
86 {
87 .class_name = "node-256", /* RT_CLASS_256 */
88 .nkeys = 256,
89 },
90};

Referenced by test_radixtree().