~ubuntu-branches/ubuntu/quantal/pgbouncer/quantal-security

« back to all changes in this revision

Viewing changes to lib/test/test_cbtree.c

  • Committer: Package Import Robot
  • Author(s): Christoph Berg, Peter Eisentraut, Christoph Berg
  • Date: 2012-01-27 17:40:22 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120127174022-zrp5nr3h6lwi5l1e
Tags: 1.5-1
[ Peter Eisentraut ]
* Update watch file to allow .tar.gz in addition to .tgz
* Remove obsolete README.source and repack support in watch file

[ Christoph Berg ]
* New upstream release.
* Use start-stop-daemon for stopping the daemon.  Closes: #641568.
* Use pgbouncer -R to restart in place, thanks Cody Cutrer for the patch.
  Closes: #657204.
* Update URL in README.Debian.  Closes: #655283.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <usual/cbtree.h>
 
3
 
 
4
#include <usual/string.h>
 
5
#include "test_common.h"
 
6
 
 
7
static char *OK = "OK";
 
8
 
 
9
struct MyNode {
 
10
        char str[64];
 
11
        int len;
 
12
};
 
13
 
 
14
static unsigned int my_getkey(void *ctx, void *obj, const void **dst_p)
 
15
{
 
16
        struct MyNode *node = obj;
 
17
        *dst_p = node->str;
 
18
        return node->len;
 
19
}
 
20
 
 
21
static struct MyNode *make_node(int value)
 
22
{
 
23
        struct MyNode *node = malloc(sizeof(*node));
 
24
        memset(node, 0, sizeof(*node));
 
25
        snprintf(node->str, sizeof(node->str), "%d", value);
 
26
        node->len = strlen(node->str);
 
27
        return node;
 
28
}
 
29
 
 
30
static bool my_node_free(void *ctx, void *obj)
 
31
{
 
32
        free(obj);
 
33
        return true;
 
34
}
 
35
 
 
36
/*
 
37
 * Test tree sanity
 
38
 */
 
39
 
 
40
/*
 
41
 * checking operations
 
42
 */
 
43
 
 
44
static const char *my_search(struct CBTree *tree, int value)
 
45
{
 
46
        struct AANode *res;
 
47
        char buf[64];
 
48
        snprintf(buf, sizeof(buf), "%d", value);
 
49
        res = cbtree_lookup(tree, buf, strlen(buf));
 
50
        return res ? OK : "not found";
 
51
}
 
52
 
 
53
static const char *my_insert(struct CBTree *tree, int value)
 
54
{
 
55
        struct MyNode *my = make_node(value);
 
56
        if (!cbtree_insert(tree, my))
 
57
                return "insert failed";
 
58
        return my_search(tree, value);
 
59
}
 
60
 
 
61
static const char *my_remove(struct CBTree *tree, int value)
 
62
{
 
63
        struct MyNode *my;
 
64
        char buf[64];
 
65
        snprintf(buf, sizeof(buf), "%d", value);
 
66
 
 
67
        my = cbtree_lookup(tree, buf, strlen(buf));
 
68
        if (!my)
 
69
                return "nonexsist element";
 
70
        cbtree_delete(tree, buf, strlen(buf));
 
71
        if (cbtree_lookup(tree, buf, strlen(buf)) != NULL)
 
72
                return "still found";
 
73
        return OK;
 
74
}
 
75
 
 
76
/*
 
77
 * Simple opeartions.
 
78
 */
 
79
 
 
80
static void test_cbtree_basic(void *p)
 
81
{
 
82
        struct CBTree *tree;
 
83
        int i;
 
84
 
 
85
        tree = cbtree_create(my_getkey, my_node_free, NULL, USUAL_ALLOC);
 
86
 
 
87
        str_check(my_search(tree, 1), "not found");
 
88
 
 
89
        for (i = 0; i < 15; i++) {
 
90
                str_check(my_insert(tree, i), "OK");
 
91
        }
 
92
        for (i = -1; i > -15; i--) {
 
93
                str_check(my_insert(tree, i), "OK");
 
94
        }
 
95
        for (i = 30; i < 45; i++) {
 
96
                str_check(my_insert(tree, i), "OK");
 
97
        }
 
98
        for (i = 15; i < 30; i++) {
 
99
                str_check(my_insert(tree, i), "OK");
 
100
        }
 
101
 
 
102
        for (i = -14; i < 45; i++) {
 
103
                str_check(my_remove(tree, i), "OK");
 
104
        }
 
105
end:
 
106
        cbtree_destroy(tree);
 
107
}
 
108
 
 
109
/*
 
110
 * randomized test
 
111
 */
 
112
 
 
113
#define RSIZE 3000
 
114
 
 
115
static int get_next(bool with_stat, bool added[])
 
116
{
 
117
        int r = random() % RSIZE;
 
118
        int i = r;
 
119
        while (1) {
 
120
                if (added[i] == with_stat)
 
121
                        return i;
 
122
                if (++i >= RSIZE)
 
123
                        i = 0;
 
124
                if (i == r)
 
125
                        return -1;
 
126
        }
 
127
}
 
128
 
 
129
static void test_cbtree_random(void *p)
 
130
{
 
131
        bool is_added[RSIZE];
 
132
        int prefer_remove = 0; // 0 - insert, 1 - delete
 
133
        int n;
 
134
        int op; // 0 - insert, 1 - delete
 
135
        struct CBTree *tree;
 
136
        unsigned long long total = 0;
 
137
 
 
138
        //printf("\n\n*** rand test ***\n\n");
 
139
        srandom(123123);
 
140
        memset(is_added, 0, sizeof(is_added));
 
141
 
 
142
        tree = cbtree_create(my_getkey, my_node_free, NULL, USUAL_ALLOC);
 
143
 
 
144
        while (total < 20000) {
 
145
                int r = random() & 15;
 
146
                if (prefer_remove)
 
147
                        op = r > 5;
 
148
                else
 
149
                        op = r > 10;
 
150
                //op = 0;
 
151
 
 
152
                n = get_next(op, is_added);
 
153
                if (n < 0) {
 
154
                        //break;
 
155
                        if (prefer_remove == op) {
 
156
                                prefer_remove = !prefer_remove;
 
157
                                //printf("** toggling remove to %d\n", prefer_remove);
 
158
                        }
 
159
                        continue;
 
160
                }
 
161
 
 
162
                if (op == 0) {
 
163
                        str_check(my_insert(tree, n), "OK");
 
164
                        is_added[n] = 1;
 
165
                } else {
 
166
                        str_check(my_remove(tree, n), "OK");
 
167
                        is_added[n] = 0;
 
168
                }
 
169
                total++;
 
170
        }
 
171
end:
 
172
        cbtree_destroy(tree);
 
173
}
 
174
 
 
175
struct testcase_t cbtree_tests[] = {
 
176
        { "basic", test_cbtree_basic },
 
177
        { "random", test_cbtree_random },
 
178
        END_OF_TESTCASES
 
179
};
 
180