| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* test_trie_dict
|
| 4 |
* - tester for trie-tree based dict feature
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "log.h"
|
| 14 |
#include "trie_dict.h"
|
| 15 |
#include <errno.h>
|
| 16 |
#include <stdio.h>
|
| 17 |
#include <string.h>
|
| 18 |
#include <unistd.h>
|
| 19 |
|
| 20 |
static const int64_t TEST_VAL = ((int64_t)(0xcb429a63a017661f)); // int64_t
|
| 21 |
static const char TRIE_DICT_SHM_FILE[] = "~trie_dict_shm.dat";
|
| 22 |
|
| 23 |
const char *keys[] = {
|
| 24 |
"ABCDEFG",
|
| 25 |
"abcdefg",
|
| 26 |
"../$@^",
|
| 27 |
"1234567890",
|
| 28 |
"1234657890",
|
| 29 |
"HZ测试汉字HZ",
|
| 30 |
"P3p4P3z4P_",
|
| 31 |
"_bbBBz_Z_",
|
| 32 |
""};
|
| 33 |
|
| 34 |
int keys_cnt = 8; // + 1 to trigger error handling of blank key
|
| 35 |
|
| 36 |
void test_traverse_cb(const char *key, int64_t value)
|
| 37 |
{
|
| 38 |
printf("Traverse key: %s Len=%ld\n", key, strlen(key));
|
| 39 |
}
|
| 40 |
|
| 41 |
int main(int argc, char *argv[])
|
| 42 |
{
|
| 43 |
TRIE_NODE *p_dict;
|
| 44 |
int ret;
|
| 45 |
int64_t value;
|
| 46 |
FILE *fp;
|
| 47 |
|
| 48 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
|
| 49 |
{
|
| 50 |
printf("Open log error\n");
|
| 51 |
return -1;
|
| 52 |
}
|
| 53 |
|
| 54 |
log_common_redir(STDOUT_FILENO);
|
| 55 |
log_error_redir(STDERR_FILENO);
|
| 56 |
|
| 57 |
if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
|
| 58 |
{
|
| 59 |
log_error("fopen(%s) error", TRIE_DICT_SHM_FILE);
|
| 60 |
return -1;
|
| 61 |
}
|
| 62 |
fclose(fp);
|
| 63 |
|
| 64 |
for (int i = 0; i < keys_cnt; i++)
|
| 65 |
{
|
| 66 |
printf("Check key %d [%s] len=%ld\n", i, keys[i], strlen(keys[i]));
|
| 67 |
}
|
| 68 |
|
| 69 |
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
|
| 70 |
{
|
| 71 |
printf("trie_dict_init failed\n");
|
| 72 |
return -1;
|
| 73 |
}
|
| 74 |
|
| 75 |
printf("Testing #1 ...\n");
|
| 76 |
|
| 77 |
p_dict = trie_dict_create();
|
| 78 |
|
| 79 |
if (p_dict == NULL)
|
| 80 |
{
|
| 81 |
printf("OOM\n");
|
| 82 |
return -1;
|
| 83 |
}
|
| 84 |
|
| 85 |
ret = trie_dict_set(p_dict, NULL, TEST_VAL);
|
| 86 |
if (ret != -1)
|
| 87 |
{
|
| 88 |
printf("Set NULL key error (%d)\n", ret);
|
| 89 |
}
|
| 90 |
|
| 91 |
ret = trie_dict_del(p_dict, NULL);
|
| 92 |
if (ret != -1)
|
| 93 |
{
|
| 94 |
printf("Del NULL key error (%d)\n", ret);
|
| 95 |
}
|
| 96 |
|
| 97 |
for (int i = 0; i < keys_cnt; i++)
|
| 98 |
{
|
| 99 |
ret = trie_dict_set(p_dict, keys[i], TEST_VAL >> i);
|
| 100 |
if (ret != 1)
|
| 101 |
{
|
| 102 |
printf("Set non-existing [%s] error (%d)\n", keys[i], ret);
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
for (int i = 0; i < keys_cnt; i++)
|
| 107 |
{
|
| 108 |
ret = trie_dict_get(p_dict, keys[i], &value);
|
| 109 |
if (ret != 1)
|
| 110 |
{
|
| 111 |
printf("Get [%s] error (%d)\n", keys[i], ret);
|
| 112 |
}
|
| 113 |
else if (value != TEST_VAL >> i)
|
| 114 |
{
|
| 115 |
printf("Value of [%s] is incorrect (%ld != %ld)\n", keys[i], value, TEST_VAL >> i);
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
trie_dict_traverse(p_dict, test_traverse_cb);
|
| 120 |
|
| 121 |
for (int i = 0; i < keys_cnt; i++)
|
| 122 |
{
|
| 123 |
if (i % 2 == 0)
|
| 124 |
{
|
| 125 |
ret = trie_dict_del(p_dict, keys[i]);
|
| 126 |
if (ret != 1)
|
| 127 |
{
|
| 128 |
printf("Del existing [%s] error (%d)\n", keys[i], ret);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
else
|
| 132 |
{
|
| 133 |
ret = trie_dict_set(p_dict, keys[i], TEST_VAL >> i);
|
| 134 |
if (ret != 0)
|
| 135 |
{
|
| 136 |
printf("Set existing [%s] with the same value error (%d)\n", keys[i], ret);
|
| 137 |
}
|
| 138 |
}
|
| 139 |
}
|
| 140 |
|
| 141 |
for (int i = 0; i < keys_cnt; i++)
|
| 142 |
{
|
| 143 |
if (i % 2 == 0)
|
| 144 |
{
|
| 145 |
ret = trie_dict_get(p_dict, keys[i], &value);
|
| 146 |
if (ret != 0)
|
| 147 |
{
|
| 148 |
printf("Get non-existing [%s] error (%d)\n", keys[i], ret);
|
| 149 |
}
|
| 150 |
}
|
| 151 |
else
|
| 152 |
{
|
| 153 |
ret = trie_dict_get(p_dict, keys[i], &value);
|
| 154 |
if (ret != 1)
|
| 155 |
{
|
| 156 |
printf("Get [%s] error (%d)\n", keys[i], ret);
|
| 157 |
}
|
| 158 |
else if (value != TEST_VAL >> i)
|
| 159 |
{
|
| 160 |
printf("Value of [%s] is incorrect (%ld != %ld)\n", keys[i], value, TEST_VAL >> i);
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
for (int i = 0; i < keys_cnt; i++)
|
| 166 |
{
|
| 167 |
if (i % 2 == 0)
|
| 168 |
{
|
| 169 |
ret = trie_dict_del(p_dict, keys[i]);
|
| 170 |
if (ret != 0)
|
| 171 |
{
|
| 172 |
printf("Del non-existing [%s] error (%d)\n", keys[i], ret);
|
| 173 |
}
|
| 174 |
}
|
| 175 |
else
|
| 176 |
{
|
| 177 |
ret = trie_dict_set(p_dict, keys[i], TEST_VAL << i);
|
| 178 |
if (ret != 1)
|
| 179 |
{
|
| 180 |
printf("Set existing [%s] with different value error (%d)\n", keys[i], ret);
|
| 181 |
}
|
| 182 |
}
|
| 183 |
}
|
| 184 |
|
| 185 |
trie_dict_traverse(p_dict, test_traverse_cb);
|
| 186 |
|
| 187 |
for (int i = 0; i < keys_cnt; i++)
|
| 188 |
{
|
| 189 |
if (i % 2 == 0)
|
| 190 |
{
|
| 191 |
ret = trie_dict_get(p_dict, keys[i], &value);
|
| 192 |
if (ret != 0)
|
| 193 |
{
|
| 194 |
printf("Get non-existing [%s] error (%d)\n", keys[i], ret);
|
| 195 |
}
|
| 196 |
}
|
| 197 |
else
|
| 198 |
{
|
| 199 |
ret = trie_dict_get(p_dict, keys[i], &value);
|
| 200 |
if (ret != 1)
|
| 201 |
{
|
| 202 |
printf("Get [%s] error (%d)\n", keys[i], ret);
|
| 203 |
}
|
| 204 |
else if (value != TEST_VAL << i)
|
| 205 |
{
|
| 206 |
printf("Value of [%s] is incorrect (%ld != %ld)\n", keys[i], value, TEST_VAL << i);
|
| 207 |
}
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
printf("Total nodes used = %d\n", trie_dict_used_nodes());
|
| 212 |
|
| 213 |
trie_dict_destroy(p_dict);
|
| 214 |
p_dict = NULL;
|
| 215 |
|
| 216 |
printf("Total nodes used = %d\n", trie_dict_used_nodes());
|
| 217 |
|
| 218 |
printf("Testing #2 ...\n");
|
| 219 |
|
| 220 |
for (int i = 0; i < 100000; i++)
|
| 221 |
{
|
| 222 |
p_dict = trie_dict_create();
|
| 223 |
|
| 224 |
if (p_dict == NULL)
|
| 225 |
{
|
| 226 |
printf("OOM\n");
|
| 227 |
return -1;
|
| 228 |
}
|
| 229 |
|
| 230 |
trie_dict_destroy(p_dict);
|
| 231 |
p_dict = NULL;
|
| 232 |
}
|
| 233 |
|
| 234 |
printf("Testing #3 ...\n");
|
| 235 |
|
| 236 |
if (set_trie_dict_shm_readonly() < 0)
|
| 237 |
{
|
| 238 |
printf("load_trie_dict_shm_readonly() error\n");
|
| 239 |
}
|
| 240 |
|
| 241 |
printf("Total nodes used = %d, after set_shm_readonly\n", trie_dict_used_nodes());
|
| 242 |
|
| 243 |
trie_dict_cleanup();
|
| 244 |
|
| 245 |
printf("Total nodes used = %d, after cleanup\n", trie_dict_used_nodes());
|
| 246 |
|
| 247 |
printf("Done\n");
|
| 248 |
|
| 249 |
if (unlink(TRIE_DICT_SHM_FILE) < 0)
|
| 250 |
{
|
| 251 |
log_error("unlink(%s) error", TRIE_DICT_SHM_FILE);
|
| 252 |
return -1;
|
| 253 |
}
|
| 254 |
|
| 255 |
log_end();
|
| 256 |
|
| 257 |
return 0;
|
| 258 |
}
|