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
11 #undef G_DISABLE_ASSERT
17 #include "fifo_string_cache.h"
20 // Simple test of insertion and checking its true/false values
22 test_fifo_string_cache_01(void)
24 fifo_string_cache_t fcache
;
27 fifo_string_cache_init(&fcache
, 10, NULL
);
29 has
= fifo_string_cache_insert(&fcache
, "alpha");
32 has
= fifo_string_cache_insert(&fcache
, "alpha");
35 has
= fifo_string_cache_insert(&fcache
, "beta");
38 has
= fifo_string_cache_insert(&fcache
, "beta");
41 has
= fifo_string_cache_insert(&fcache
, "alpha");
44 fifo_string_cache_free(&fcache
);
47 // Is the max_entries honored?
49 test_fifo_string_cache_02(void)
51 fifo_string_cache_t fcache
;
53 fifo_string_cache_init(&fcache
, 4, NULL
);
56 has
= fifo_string_cache_insert(&fcache
, "alpha");
58 has
= fifo_string_cache_insert(&fcache
, "beta");
60 has
= fifo_string_cache_insert(&fcache
, "gamma");
62 has
= fifo_string_cache_insert(&fcache
, "delta");
65 // They should all be there
66 has
= fifo_string_cache_contains(&fcache
, "alpha");
68 has
= fifo_string_cache_contains(&fcache
, "beta");
70 has
= fifo_string_cache_contains(&fcache
, "gamma");
72 has
= fifo_string_cache_contains(&fcache
, "delta");
76 has
= fifo_string_cache_insert(&fcache
, "epsilon");
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");
84 has
= fifo_string_cache_contains(&fcache
, "gamma");
86 has
= fifo_string_cache_contains(&fcache
, "delta");
88 has
= fifo_string_cache_contains(&fcache
, "epsilon");
92 has
= fifo_string_cache_insert(&fcache
, "zeta");
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");
102 has
= fifo_string_cache_contains(&fcache
, "delta");
104 has
= fifo_string_cache_contains(&fcache
, "epsilon");
106 has
= fifo_string_cache_contains(&fcache
, "zeta");
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
115 test_fifo_string_cache_03(void)
117 fifo_string_cache_t fcache
;
119 fifo_string_cache_init(&fcache
, 1, NULL
);
122 has
= fifo_string_cache_insert(&fcache
, "alpha");
126 has
= fifo_string_cache_contains(&fcache
, "alpha");
130 has
= fifo_string_cache_insert(&fcache
, "beta");
134 has
= fifo_string_cache_contains(&fcache
, "alpha");
136 has
= fifo_string_cache_contains(&fcache
, "beta");
140 has
= fifo_string_cache_insert(&fcache
, "gamma");
144 has
= fifo_string_cache_contains(&fcache
, "alpha");
146 has
= fifo_string_cache_contains(&fcache
, "beta");
148 has
= fifo_string_cache_contains(&fcache
, "gamma");
151 fifo_string_cache_free(&fcache
);
154 // Test an unbounded maximum (max_entries == 0)
156 test_fifo_string_cache_04(void)
158 fifo_string_cache_t fcache
;
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"));
167 has
= fifo_string_cache_contains(&fcache
, "alpha");
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"));
175 has
= fifo_string_cache_contains(&fcache
, "alpha");
177 has
= fifo_string_cache_contains(&fcache
, "beta");
183 for (i
= 0; i
< 1000 ; i
++) {
184 s
= g_strdup_printf("%d", i
);
185 has
= fifo_string_cache_insert(&fcache
, s
);
190 has
= fifo_string_cache_contains(&fcache
, "alpha");
192 has
= fifo_string_cache_contains(&fcache
, "beta");
194 for (i
= 0; i
< 1000 ; i
++) {
195 s
= g_strdup_printf("%d", i
);
196 has
= fifo_string_cache_contains(&fcache
, s
);
199 fifo_string_cache_free(&fcache
);
203 main(int argc
, char **argv
)
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();
220 * Editor modelines - https://www.wireshark.org/tools/modelines.html
225 * indent-tabs-mode: nil
228 * vi: set shiftwidth=4 tabstop=8 expandtab:
229 * :indentSize=4:tabSize=8:noTabs=true: