/[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.2 - (hide annotations)
Sun Nov 16 04:22:30 2025 UTC (4 months ago) by sysadm
Branch: MAIN
Changes since 1.1: +16 -5 lines
Content type: text/x-csrc
Add hash_dict_inc()

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.2 if (i % 2 == 0)
59 sysadm 1.1 {
60 sysadm 1.2 if (hash_dict_set(p_dict, key, i * 3 + 7) != 0)
61     {
62     printf("hash_dict_set(%lu) error\n", key);
63     break;
64     }
65     }
66     else
67     {
68     if (hash_dict_inc(p_dict, key, i * 3 + 7) != 0)
69     {
70     printf("hash_dict_inc(%lu) error\n", key);
71     break;
72     }
73 sysadm 1.1 }
74     }
75    
76     ret = (int)hash_dict_item_count(p_dict);
77     if (ret != dict_item_count)
78     {
79     printf("hash_dict_item_count()=%d error\n", ret);
80     }
81    
82     for (i = 0; i < dict_item_count; i++)
83     {
84     key = i * 37 + 13;
85     ret = hash_dict_get(p_dict, key, &value);
86     if (ret != 1)
87     {
88     printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
89     break;
90     }
91     if (value != i * 3 + 7)
92     {
93     printf("hash_dict_get(%lu) value=%ld error\n", key, value);
94     break;
95     }
96     }
97    
98     for (i = 0; i < dict_item_count; i++)
99     {
100     key = i * 37 + 13;
101 sysadm 1.2 if (hash_dict_inc(p_dict, key, i * 5 + 17) != 0)
102 sysadm 1.1 {
103     printf("hash_dict_set(%lu) error\n", key);
104     break;
105     }
106     }
107    
108     for (i = 0; i < dict_item_count; i++)
109     {
110     key = i * 37 + 13;
111     ret = hash_dict_get(p_dict, key, &value);
112     if (ret != 1)
113     {
114     printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
115     break;
116     }
117 sysadm 1.2 if (value != i * 3 + 7 + i * 5 + 17)
118 sysadm 1.1 {
119     printf("hash_dict_get(%lu) value=%ld error\n", key, value);
120     break;
121     }
122     }
123    
124     ret = (int)hash_dict_item_count(p_dict);
125     if (ret != dict_item_count)
126     {
127     printf("hash_dict_item_count()=%d error\n", ret);
128     }
129    
130     for (i = 0; i < dict_item_count; i++)
131     {
132     key = i * 37 + 13;
133     if (hash_dict_del(p_dict, key) != 1)
134     {
135     printf("hash_dict_del(%lu) error\n", key);
136     break;
137     }
138     }
139    
140     ret = (int)hash_dict_item_count(p_dict);
141     if (ret != 0)
142     {
143     printf("hash_dict_item_count()=%d error\n", ret);
144     }
145    
146     for (i = 0; i < dict_item_count; i++)
147     {
148     key = i * 37 + 13;
149     ret = hash_dict_get(p_dict, key, &value);
150     if (ret != 0)
151     {
152     printf("hash_dict_get(%lu) ret=%d error\n", key, ret);
153     break;
154     }
155     }
156    
157     for (i = 0; i < dict_item_count; i++)
158     {
159     key = i * 37 + 13;
160     if (hash_dict_del(p_dict, key) != 0)
161     {
162     printf("hash_dict_del(%lu) error\n", key);
163     break;
164     }
165     }
166    
167     hash_dict_destroy(p_dict);
168    
169     log_end();
170    
171     return 0;
172     }

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