| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
#if INCLUDE_RBTREE_TEST |
| 22 |
|
| 23 |
#include <pjlib.h> |
| 24 |
|
| 25 |
#define LOOP 32 |
| 26 |
#define MIN_COUNT 64 |
| 27 |
#define MAX_COUNT (LOOP * MIN_COUNT) |
| 28 |
#define STRSIZE 16 |
| 29 |
#define THIS_FILE "rbtree_test" |
| 30 |
|
| 31 |
typedef struct node_key |
| 32 |
{ |
| 33 |
pj_uint32_t hash; |
| 34 |
char str[STRSIZE]; |
| 35 |
} node_key; |
| 36 |
|
| 37 |
static int compare_node(const node_key *k1, const node_key *k2) |
| 38 |
{ |
| 39 |
if (k1->hash == k2->hash) { |
| 40 |
return strcmp(k1->str, k2->str); |
| 41 |
} else { |
| 42 |
return k1->hash < k2->hash ? -1 : 1; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
static int test(void) |
| 47 |
{ |
| 48 |
pj_rbtree rb; |
| 49 |
node_key *key; |
| 50 |
pj_rbtree_node *node; |
| 51 |
pj_pool_t *pool; |
| 52 |
int rc=0; |
| 53 |
int count = MIN_COUNT; |
| 54 |
int i; |
| 55 |
unsigned size; |
| 56 |
|
| 57 |
pj_rbtree_init(&rb, (pj_rbtree_comp*)&compare_node); |
| 58 |
size = MAX_COUNT*(sizeof(*key)+PJ_RBTREE_NODE_SIZE) + |
| 59 |
PJ_RBTREE_SIZE + PJ_POOL_SIZE; |
| 60 |
pool = pj_pool_create( mem, "pool", size, 0, NULL); |
| 61 |
PJ_TEST_NOT_NULL(pool, NULL, return -10); |
| 62 |
|
| 63 |
key = (node_key *)pj_pool_alloc(pool, MAX_COUNT*sizeof(*key)); |
| 64 |
PJ_TEST_NOT_NULL(key, NULL, { rc=-20; goto on_error; }); |
| 65 |
|
| 66 |
node = (pj_rbtree_node*)pj_pool_alloc(pool, MAX_COUNT*sizeof(*node)); |
| 67 |
PJ_TEST_NOT_NULL(node, NULL, { rc=-30; goto on_error; }); |
| 68 |
|
| 69 |
for (i=0; i<LOOP; ++i) { |
| 70 |
int j; |
| 71 |
pj_rbtree_node *prev, *it; |
| 72 |
pj_timestamp t1, t2, t_setup, t_insert, t_search, t_erase; |
| 73 |
|
| 74 |
pj_assert(rb.size == 0); |
| 75 |
|
| 76 |
t_setup.u32.lo = t_insert.u32.lo = t_search.u32.lo = t_erase.u32.lo = 0; |
| 77 |
|
| 78 |
for (j=0; j<count; j++) { |
| 79 |
pj_create_random_string(key[j].str, STRSIZE); |
| 80 |
|
| 81 |
pj_get_timestamp(&t1); |
| 82 |
node[j].key = &key[j]; |
| 83 |
node[j].user_data = key[j].str; |
| 84 |
key[j].hash = pj_hash_calc(0, key[j].str, PJ_HASH_KEY_STRING); |
| 85 |
pj_get_timestamp(&t2); |
| 86 |
t_setup.u32.lo += (t2.u32.lo - t1.u32.lo); |
| 87 |
|
| 88 |
pj_get_timestamp(&t1); |
| 89 |
PJ_TEST_EQ(pj_rbtree_insert(&rb, &node[j]), 0, NULL, |
| 90 |
{ rc=-35; goto on_error; }); |
| 91 |
pj_get_timestamp(&t2); |
| 92 |
t_insert.u32.lo += (t2.u32.lo - t1.u32.lo); |
| 93 |
} |
| 94 |
|
| 95 |
PJ_TEST_EQ(rb.size, (unsigned)count, NULL, { rc=-40; goto on_error; }); |
| 96 |
|
| 97 |
|
| 98 |
prev = NULL; |
| 99 |
it = pj_rbtree_first(&rb); |
| 100 |
while (it) { |
| 101 |
if (prev) { |
| 102 |
if (compare_node((node_key*)prev->key,(node_key*)it->key)>=0) { |
| 103 |
PJ_LOG(3, (THIS_FILE, "Error: %s >= %s", |
| 104 |
(char*)prev->user_data, (char*)it->user_data)); |
| 105 |
rc=-45; |
| 106 |
goto on_error; |
| 107 |
} |
| 108 |
} |
| 109 |
prev = it; |
| 110 |
it = pj_rbtree_next(&rb, it); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
for (j=0; j<count; j++) { |
| 115 |
pj_get_timestamp(&t1); |
| 116 |
PJ_TEST_NOT_NULL( pj_rbtree_find(&rb, &key[j]), NULL, |
| 117 |
{rc=-50; goto on_error;} ); |
| 118 |
pj_get_timestamp(&t2); |
| 119 |
t_search.u32.lo += (t2.u32.lo - t1.u32.lo); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
for (j=0; j<count; j++) { |
| 124 |
pj_get_timestamp(&t1); |
| 125 |
it = pj_rbtree_erase(&rb, &node[j]); |
| 126 |
pj_get_timestamp(&t2); |
| 127 |
t_erase.u32.lo += (t2.u32.lo - t1.u32.lo); |
| 128 |
} |
| 129 |
|
| 130 |
PJ_LOG(4, (THIS_FILE, |
| 131 |
"...count:%d, setup:%d, insert:%d, search:%d, erase:%d", |
| 132 |
count, |
| 133 |
t_setup.u32.lo / count, t_insert.u32.lo / count, |
| 134 |
t_search.u32.lo / count, t_erase.u32.lo / count)); |
| 135 |
|
| 136 |
count = 2 * count; |
| 137 |
if (count > MAX_COUNT) |
| 138 |
break; |
| 139 |
} |
| 140 |
|
| 141 |
on_error: |
| 142 |
pj_pool_release(pool); |
| 143 |
return rc; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
int rbtree_test() |
| 148 |
{ |
| 149 |
return test(); |
| 150 |
} |
| 151 |
|
| 152 |
#endif |
| 153 |
|
| 154 |
|