Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/rbtree.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 // Iterate key, make sure they're sorted.
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 // Search.
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 // Erase node.
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 /* INCLUDE_RBTREE_TEST */
153
154