| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* test_hash_dict
|
| 4 |
* - tester for hash dict
|
| 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 "hash_dict.h"
|
| 14 |
#include "log.h"
|
| 15 |
#include <errno.h>
|
| 16 |
#include <stdio.h>
|
| 17 |
#include <unistd.h>
|
| 18 |
|
| 19 |
int main(int argc, char *argv[])
|
| 20 |
{
|
| 21 |
HASH_DICT *p_dict;
|
| 22 |
uint64_t key;
|
| 23 |
int64_t value;
|
| 24 |
unsigned int i;
|
| 25 |
int ret;
|
| 26 |
const int dict_item_count = 10000 * 10000;
|
| 27 |
|
| 28 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
|
| 29 |
{
|
| 30 |
printf("Open log error\n");
|
| 31 |
return -1;
|
| 32 |
}
|
| 33 |
|
| 34 |
log_common_redir(STDOUT_FILENO);
|
| 35 |
log_error_redir(STDERR_FILENO);
|
| 36 |
|
| 37 |
p_dict = hash_dict_create(dict_item_count);
|
| 38 |
if (p_dict == NULL)
|
| 39 |
{
|
| 40 |
printf("hash_dict_create() error\n");
|
| 41 |
return -2;
|
| 42 |
}
|
| 43 |
|
| 44 |
for (i = 0; i < dict_item_count; i++)
|
| 45 |
{
|
| 46 |
key = i * 37 + 13;
|
| 47 |
ret = hash_dict_get(p_dict, key, &value);
|
| 48 |
if (ret != 0)
|
| 49 |
{
|
| 50 |
printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
|
| 51 |
break;
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
for (i = 0; i < dict_item_count; i++)
|
| 56 |
{
|
| 57 |
key = i * 37 + 13;
|
| 58 |
if (i % 2 == 1)
|
| 59 |
{
|
| 60 |
if (hash_dict_inc(p_dict, key, i * 3 + 7) != 0)
|
| 61 |
{
|
| 62 |
printf("hash_dict_inc(%lu) error\n", key);
|
| 63 |
break;
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
if (hash_dict_set(p_dict, key, i * 3 + 7) != 0)
|
| 68 |
{
|
| 69 |
printf("hash_dict_set(%lu) error\n", key);
|
| 70 |
break;
|
| 71 |
}
|
| 72 |
}
|
| 73 |
|
| 74 |
ret = (int)hash_dict_item_count(p_dict);
|
| 75 |
if (ret != dict_item_count)
|
| 76 |
{
|
| 77 |
printf("hash_dict_item_count()=%d error\n", ret);
|
| 78 |
}
|
| 79 |
|
| 80 |
for (i = 0; i < dict_item_count; i++)
|
| 81 |
{
|
| 82 |
key = i * 37 + 13;
|
| 83 |
ret = hash_dict_get(p_dict, key, &value);
|
| 84 |
if (ret != 1)
|
| 85 |
{
|
| 86 |
printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
|
| 87 |
break;
|
| 88 |
}
|
| 89 |
if (value != i * 3 + 7)
|
| 90 |
{
|
| 91 |
printf("hash_dict_get(%lu) value=%ld error\n", key, value);
|
| 92 |
break;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
for (i = 0; i < dict_item_count; i++)
|
| 97 |
{
|
| 98 |
key = i * 37 + 13;
|
| 99 |
if (hash_dict_inc(p_dict, key, i * 5 + 17) != 1)
|
| 100 |
{
|
| 101 |
printf("hash_dict_inc(%lu) error\n", key);
|
| 102 |
break;
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
for (i = 0; i < dict_item_count; i++)
|
| 107 |
{
|
| 108 |
key = i * 37 + 13;
|
| 109 |
ret = hash_dict_get(p_dict, key, &value);
|
| 110 |
if (ret != 1)
|
| 111 |
{
|
| 112 |
printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
|
| 113 |
break;
|
| 114 |
}
|
| 115 |
if (value != i * 3 + 7 + i * 5 + 17)
|
| 116 |
{
|
| 117 |
printf("hash_dict_get(%lu) value=%ld error\n", key, value);
|
| 118 |
break;
|
| 119 |
}
|
| 120 |
}
|
| 121 |
|
| 122 |
ret = (int)hash_dict_item_count(p_dict);
|
| 123 |
if (ret != dict_item_count)
|
| 124 |
{
|
| 125 |
printf("hash_dict_item_count()=%d error\n", ret);
|
| 126 |
}
|
| 127 |
|
| 128 |
for (i = 0; i < dict_item_count; i++)
|
| 129 |
{
|
| 130 |
key = i * 37 + 13;
|
| 131 |
if (hash_dict_del(p_dict, key) != 1)
|
| 132 |
{
|
| 133 |
printf("hash_dict_del(%lu) error\n", key);
|
| 134 |
break;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
ret = (int)hash_dict_item_count(p_dict);
|
| 139 |
if (ret != 0)
|
| 140 |
{
|
| 141 |
printf("hash_dict_item_count()=%d error\n", ret);
|
| 142 |
}
|
| 143 |
|
| 144 |
for (i = 0; i < dict_item_count; i++)
|
| 145 |
{
|
| 146 |
key = i * 37 + 13;
|
| 147 |
ret = hash_dict_get(p_dict, key, &value);
|
| 148 |
if (ret != 0)
|
| 149 |
{
|
| 150 |
printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
|
| 151 |
break;
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
for (i = 0; i < dict_item_count; i++)
|
| 156 |
{
|
| 157 |
key = i * 37 + 13;
|
| 158 |
if (hash_dict_del(p_dict, key) != 0)
|
| 159 |
{
|
| 160 |
printf("hash_dict_del(%lu) error\n", key);
|
| 161 |
break;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
hash_dict_destroy(p_dict);
|
| 166 |
|
| 167 |
log_end();
|
| 168 |
|
| 169 |
return 0;
|
| 170 |
}
|