summaryrefslogtreecommitdiffstats
path: root/epan/fifo_string_cache_test.c
blob: 78a0a3dc2b4dcb2890b974ece94b249ee82a4794 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* fifo_string_cache_test.c
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"
#undef G_DISABLE_ASSERT

#include <stdio.h>
#include <string.h>
#include <glib.h>

#include "fifo_string_cache.h"


// Simple test of insertion and checking its true/false values
static void
test_fifo_string_cache_01(void)
{
    fifo_string_cache_t fcache;
    gboolean has;

    fifo_string_cache_init(&fcache, 10, NULL);

    has = fifo_string_cache_insert(&fcache, "alpha");
    g_assert_false(has);

    has = fifo_string_cache_insert(&fcache, "alpha");
    g_assert_true(has);

    has = fifo_string_cache_insert(&fcache, "beta");
    g_assert_false(has);

    has = fifo_string_cache_insert(&fcache, "beta");
    g_assert_true(has);

    has = fifo_string_cache_insert(&fcache, "alpha");
    g_assert_true(has);

    fifo_string_cache_free(&fcache);
}

// Is the max_entries honored?
static void
test_fifo_string_cache_02(void)
{
    fifo_string_cache_t fcache;
    gboolean has;
    fifo_string_cache_init(&fcache, 4, NULL);

    // Insert 4 items
    has = fifo_string_cache_insert(&fcache, "alpha");
    g_assert_false(has);
    has = fifo_string_cache_insert(&fcache, "beta");
    g_assert_false(has);
    has = fifo_string_cache_insert(&fcache, "gamma");
    g_assert_false(has);
    has = fifo_string_cache_insert(&fcache, "delta");
    g_assert_false(has);

    // They should all be there
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "gamma");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "delta");
    g_assert_true(has);

    // Add a 5th item
    has = fifo_string_cache_insert(&fcache, "epsilon");
    g_assert_false(has);

    // The first one should no longer be there
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_false(has); // FALSE
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "gamma");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "delta");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "epsilon");
    g_assert_true(has);

    // Add a 6th item
    has = fifo_string_cache_insert(&fcache, "zeta");
    g_assert_false(has);

    // The first two should no longer be there
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_false(has); // FALSE
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_false(has); // FALSE
    has = fifo_string_cache_contains(&fcache, "gamma");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "delta");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "epsilon");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "zeta");
    g_assert_true(has);

    fifo_string_cache_free(&fcache);
}

// Check a max_entries == 1, to ensure we don't have any mistakes
// at that end of the range
static void
test_fifo_string_cache_03(void)
{
    fifo_string_cache_t fcache;
    gboolean has;
    fifo_string_cache_init(&fcache, 1, NULL);

    // Insert
    has = fifo_string_cache_insert(&fcache, "alpha");
    g_assert_false(has);

    // Check
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_true(has);

    // Insert
    has = fifo_string_cache_insert(&fcache, "beta");
    g_assert_false(has);

    // Check
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_false(has);
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_true(has);

    // Insert
    has = fifo_string_cache_insert(&fcache, "gamma");
    g_assert_false(has);

    // Check
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_false(has);
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_false(has);
    has = fifo_string_cache_contains(&fcache, "gamma");
    g_assert_true(has);

    fifo_string_cache_free(&fcache);
}

// Test an unbounded maximum (max_entries == 0)
static void
test_fifo_string_cache_04(void)
{
    fifo_string_cache_t fcache;
    gboolean has;
    fifo_string_cache_init(&fcache, 0, g_free);

    // Insert; we call g_strdup because in this test, the cache owns the string
    has = fifo_string_cache_insert(&fcache, g_strdup("alpha"));
    g_assert_false(has);

    // Check
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_true(has);

    // Insert; we call g_strdup because in this test, the cache owns the string
    has = fifo_string_cache_insert(&fcache, g_strdup("beta"));
    g_assert_false(has);

    // Check
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_true(has);

    // Insert many
    int i;
    char *s;
    for (i = 0; i < 1000 ; i++) {
        s = g_strdup_printf("%d", i);
        has = fifo_string_cache_insert(&fcache, s);
        g_assert_false(has);
    }

    // Check everything
    has = fifo_string_cache_contains(&fcache, "alpha");
    g_assert_true(has);
    has = fifo_string_cache_contains(&fcache, "beta");
    g_assert_true(has);
    for (i = 0; i < 1000 ; i++) {
        s = g_strdup_printf("%d", i);
        has = fifo_string_cache_contains(&fcache, s);
        g_assert_true(has);
    }
    fifo_string_cache_free(&fcache);
}

int
main(int argc, char **argv)
{
    int result;

    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/fifo_string_cache/01",    test_fifo_string_cache_01);
    g_test_add_func("/fifo_string_cache/02",    test_fifo_string_cache_02);
    g_test_add_func("/fifo_string_cache/03",    test_fifo_string_cache_03);
    g_test_add_func("/fifo_string_cache/04",    test_fifo_string_cache_04);

    result = g_test_run();

    return result;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */