Revert "LATER... ei_kerberos_kdc_session_key ..."
[wireshark-sm.git] / epan / fifo_string_cache_test.c
blobfd967eee0b508a5b1962c2d286131e74dc0d3317
1 /* fifo_string_cache_test.c
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
10 #include "config.h"
11 #undef G_DISABLE_ASSERT
13 #include <stdio.h>
14 #include <string.h>
15 #include <glib.h>
17 #include "fifo_string_cache.h"
20 // Simple test of insertion and checking its true/false values
21 static void
22 test_fifo_string_cache_01(void)
24 fifo_string_cache_t fcache;
25 bool has;
27 fifo_string_cache_init(&fcache, 10, NULL);
29 has = fifo_string_cache_insert(&fcache, "alpha");
30 g_assert_false(has);
32 has = fifo_string_cache_insert(&fcache, "alpha");
33 g_assert_true(has);
35 has = fifo_string_cache_insert(&fcache, "beta");
36 g_assert_false(has);
38 has = fifo_string_cache_insert(&fcache, "beta");
39 g_assert_true(has);
41 has = fifo_string_cache_insert(&fcache, "alpha");
42 g_assert_true(has);
44 fifo_string_cache_free(&fcache);
47 // Is the max_entries honored?
48 static void
49 test_fifo_string_cache_02(void)
51 fifo_string_cache_t fcache;
52 bool has;
53 fifo_string_cache_init(&fcache, 4, NULL);
55 // Insert 4 items
56 has = fifo_string_cache_insert(&fcache, "alpha");
57 g_assert_false(has);
58 has = fifo_string_cache_insert(&fcache, "beta");
59 g_assert_false(has);
60 has = fifo_string_cache_insert(&fcache, "gamma");
61 g_assert_false(has);
62 has = fifo_string_cache_insert(&fcache, "delta");
63 g_assert_false(has);
65 // They should all be there
66 has = fifo_string_cache_contains(&fcache, "alpha");
67 g_assert_true(has);
68 has = fifo_string_cache_contains(&fcache, "beta");
69 g_assert_true(has);
70 has = fifo_string_cache_contains(&fcache, "gamma");
71 g_assert_true(has);
72 has = fifo_string_cache_contains(&fcache, "delta");
73 g_assert_true(has);
75 // Add a 5th item
76 has = fifo_string_cache_insert(&fcache, "epsilon");
77 g_assert_false(has);
79 // The first one should no longer be there
80 has = fifo_string_cache_contains(&fcache, "alpha");
81 g_assert_false(has); // false
82 has = fifo_string_cache_contains(&fcache, "beta");
83 g_assert_true(has);
84 has = fifo_string_cache_contains(&fcache, "gamma");
85 g_assert_true(has);
86 has = fifo_string_cache_contains(&fcache, "delta");
87 g_assert_true(has);
88 has = fifo_string_cache_contains(&fcache, "epsilon");
89 g_assert_true(has);
91 // Add a 6th item
92 has = fifo_string_cache_insert(&fcache, "zeta");
93 g_assert_false(has);
95 // The first two should no longer be there
96 has = fifo_string_cache_contains(&fcache, "alpha");
97 g_assert_false(has); // false
98 has = fifo_string_cache_contains(&fcache, "beta");
99 g_assert_false(has); // false
100 has = fifo_string_cache_contains(&fcache, "gamma");
101 g_assert_true(has);
102 has = fifo_string_cache_contains(&fcache, "delta");
103 g_assert_true(has);
104 has = fifo_string_cache_contains(&fcache, "epsilon");
105 g_assert_true(has);
106 has = fifo_string_cache_contains(&fcache, "zeta");
107 g_assert_true(has);
109 fifo_string_cache_free(&fcache);
112 // Check a max_entries == 1, to ensure we don't have any mistakes
113 // at that end of the range
114 static void
115 test_fifo_string_cache_03(void)
117 fifo_string_cache_t fcache;
118 bool has;
119 fifo_string_cache_init(&fcache, 1, NULL);
121 // Insert
122 has = fifo_string_cache_insert(&fcache, "alpha");
123 g_assert_false(has);
125 // Check
126 has = fifo_string_cache_contains(&fcache, "alpha");
127 g_assert_true(has);
129 // Insert
130 has = fifo_string_cache_insert(&fcache, "beta");
131 g_assert_false(has);
133 // Check
134 has = fifo_string_cache_contains(&fcache, "alpha");
135 g_assert_false(has);
136 has = fifo_string_cache_contains(&fcache, "beta");
137 g_assert_true(has);
139 // Insert
140 has = fifo_string_cache_insert(&fcache, "gamma");
141 g_assert_false(has);
143 // Check
144 has = fifo_string_cache_contains(&fcache, "alpha");
145 g_assert_false(has);
146 has = fifo_string_cache_contains(&fcache, "beta");
147 g_assert_false(has);
148 has = fifo_string_cache_contains(&fcache, "gamma");
149 g_assert_true(has);
151 fifo_string_cache_free(&fcache);
154 // Test an unbounded maximum (max_entries == 0)
155 static void
156 test_fifo_string_cache_04(void)
158 fifo_string_cache_t fcache;
159 bool has;
160 fifo_string_cache_init(&fcache, 0, g_free);
162 // Insert; we call g_strdup because in this test, the cache owns the string
163 has = fifo_string_cache_insert(&fcache, g_strdup("alpha"));
164 g_assert_false(has);
166 // Check
167 has = fifo_string_cache_contains(&fcache, "alpha");
168 g_assert_true(has);
170 // Insert; we call g_strdup because in this test, the cache owns the string
171 has = fifo_string_cache_insert(&fcache, g_strdup("beta"));
172 g_assert_false(has);
174 // Check
175 has = fifo_string_cache_contains(&fcache, "alpha");
176 g_assert_true(has);
177 has = fifo_string_cache_contains(&fcache, "beta");
178 g_assert_true(has);
180 // Insert many
181 int i;
182 char *s;
183 for (i = 0; i < 1000 ; i++) {
184 s = g_strdup_printf("%d", i);
185 has = fifo_string_cache_insert(&fcache, s);
186 g_assert_false(has);
189 // Check everything
190 has = fifo_string_cache_contains(&fcache, "alpha");
191 g_assert_true(has);
192 has = fifo_string_cache_contains(&fcache, "beta");
193 g_assert_true(has);
194 for (i = 0; i < 1000 ; i++) {
195 s = g_strdup_printf("%d", i);
196 has = fifo_string_cache_contains(&fcache, s);
197 g_assert_true(has);
199 fifo_string_cache_free(&fcache);
203 main(int argc, char **argv)
205 int result;
207 g_test_init(&argc, &argv, NULL);
209 g_test_add_func("/fifo_string_cache/01", test_fifo_string_cache_01);
210 g_test_add_func("/fifo_string_cache/02", test_fifo_string_cache_02);
211 g_test_add_func("/fifo_string_cache/03", test_fifo_string_cache_03);
212 g_test_add_func("/fifo_string_cache/04", test_fifo_string_cache_04);
214 result = g_test_run();
216 return result;
220 * Editor modelines - https://www.wireshark.org/tools/modelines.html
222 * Local variables:
223 * c-basic-offset: 4
224 * tab-width: 8
225 * indent-tabs-mode: nil
226 * End:
228 * vi: set shiftwidth=4 tabstop=8 expandtab:
229 * :indentSize=4:tabSize=8:noTabs=true: