/[LeafOK_CVS]/lbbs/src/test_hash_dict.c
ViewVC logotype

Annotation of /lbbs/src/test_hash_dict.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Wed Dec 17 03:41:56 2025 UTC (2 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.2: +9 -11 lines
Content type: text/x-csrc
Refine hash_dict: hash_dict_inc() no longer add non-existing key. It returns 0 in that case.

1 sysadm 1.1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2     /*
3     * test_hash_dict
4     * - tester for hash dict
5     *
6     * Copyright (C) 2004-2025 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 sysadm 1.3 if (i % 2 == 1)
59 sysadm 1.1 {
60 sysadm 1.3 if (hash_dict_inc(p_dict, key, i * 3 + 7) != 0)
61 sysadm 1.2 {
62 sysadm 1.3 printf("hash_dict_inc(%lu) error\n", key);
63 sysadm 1.2 break;
64     }
65     }
66 sysadm 1.3
67     if (hash_dict_set(p_dict, key, i * 3 + 7) != 0)
68 sysadm 1.2 {
69 sysadm 1.3 printf("hash_dict_set(%lu) error\n", key);
70     break;
71 sysadm 1.1 }
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 sysadm 1.3 if (hash_dict_inc(p_dict, key, i * 5 + 17) != 1)
100 sysadm 1.1 {
101 sysadm 1.3 printf("hash_dict_inc(%lu) error\n", key);
102 sysadm 1.1 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 sysadm 1.2 if (value != i * 3 + 7 + i * 5 + 17)
116 sysadm 1.1 {
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     }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1