Branch data Line data Source code
1 : : /* Copyright (C) 2011 Red Hat, Inc.
2 : : *
3 : : * SPDX-License-Identifier: LGPL-2.1-or-later
4 : : *
5 : : * This library is free software; you can redistribute it and/or
6 : : * modify it under the terms of the GNU Lesser General Public
7 : : * License as published by the Free Software Foundation; either
8 : : * version 2.1 of the License, or (at your option) any later version.
9 : : *
10 : : * This library is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public
16 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 : : */
18 : :
19 : : /* We are testing some deprecated APIs here */
20 : : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
21 : : #define GLIB_DISABLE_DEPRECATION_WARNINGS
22 : : #endif
23 : :
24 : : #include <glib.h>
25 : :
26 : : static gint value_create_count = 0;
27 : : static gint value_destroy_count = 0;
28 : :
29 : : static gpointer
30 : 2 : value_create (gpointer key)
31 : : {
32 : : gint *value;
33 : :
34 : 2 : value_create_count++;
35 : :
36 : 2 : value = g_new (gint, 1);
37 : 2 : *value = *(gint*)key * 2;
38 : :
39 : 2 : return value;
40 : : }
41 : :
42 : : static void
43 : 2 : value_destroy (gpointer value)
44 : : {
45 : 2 : value_destroy_count++;
46 : 2 : g_free (value);
47 : 2 : }
48 : :
49 : : static gpointer
50 : 2 : key_dup (gpointer key)
51 : : {
52 : : gint *newkey;
53 : :
54 : 2 : newkey = g_new (gint, 1);
55 : 2 : *newkey = *(gint*)key;
56 : :
57 : 2 : return newkey;
58 : : }
59 : :
60 : : static void
61 : 2 : key_destroy (gpointer key)
62 : : {
63 : 2 : g_free (key);
64 : 2 : }
65 : :
66 : : static guint
67 : 10 : key_hash (gconstpointer key)
68 : : {
69 : 10 : return *(guint*)key;
70 : : }
71 : :
72 : : static guint
73 : 7 : value_hash (gconstpointer value)
74 : : {
75 : 7 : return *(guint*)value;
76 : : }
77 : :
78 : : static gboolean
79 : 6 : key_equal (gconstpointer key1, gconstpointer key2)
80 : : {
81 : 6 : return *(gint*)key1 == *(gint*)key2;
82 : : }
83 : :
84 : : static void
85 : 1 : key_foreach (gpointer valuep, gpointer keyp, gpointer data)
86 : : {
87 : 1 : gint *count = data;
88 : 1 : gint *key = keyp;
89 : :
90 : 1 : (*count)++;
91 : :
92 : 1 : g_assert_cmpint (*key, ==, 2);
93 : 1 : }
94 : :
95 : : static void
96 : 1 : value_foreach (gpointer keyp, gpointer nodep, gpointer data)
97 : : {
98 : 1 : gint *count = data;
99 : 1 : gint *key = keyp;
100 : :
101 : 1 : (*count)++;
102 : :
103 : 1 : g_assert_cmpint (*key, ==, 2);
104 : 1 : }
105 : :
106 : : static void
107 : 1 : test_cache_basic (void)
108 : : {
109 : : GCache *c;
110 : : gint *key;
111 : : gint *value;
112 : : gint count;
113 : :
114 : 1 : value_create_count = 0;
115 : 1 : value_destroy_count = 0;
116 : :
117 : 1 : c = g_cache_new (value_create, value_destroy,
118 : : key_dup, key_destroy,
119 : : key_hash, value_hash, key_equal);
120 : :
121 : 1 : key = g_new (gint, 1);
122 : 1 : *key = 2;
123 : :
124 : 1 : value = g_cache_insert (c, key);
125 : 1 : g_assert_cmpint (*value, ==, 4);
126 : 1 : g_assert_cmpint (value_create_count, ==, 1);
127 : 1 : g_assert_cmpint (value_destroy_count, ==, 0);
128 : :
129 : 1 : count = 0;
130 : 1 : g_cache_key_foreach (c, key_foreach, &count);
131 : 1 : g_assert_cmpint (count, ==, 1);
132 : :
133 : 1 : count = 0;
134 : 1 : g_cache_value_foreach (c, value_foreach, &count);
135 : 1 : g_assert_cmpint (count, ==, 1);
136 : :
137 : 1 : value = g_cache_insert (c, key);
138 : 1 : g_assert_cmpint (*value, ==, 4);
139 : 1 : g_assert_cmpint (value_create_count, ==, 1);
140 : 1 : g_assert_cmpint (value_destroy_count, ==, 0);
141 : :
142 : 1 : g_cache_remove (c, value);
143 : 1 : g_assert_cmpint (value_create_count, ==, 1);
144 : 1 : g_assert_cmpint (value_destroy_count, ==, 0);
145 : :
146 : 1 : g_cache_remove (c, value);
147 : 1 : g_assert_cmpint (value_create_count, ==, 1);
148 : 1 : g_assert_cmpint (value_destroy_count, ==, 1);
149 : :
150 : 1 : value = g_cache_insert (c, key);
151 : 1 : g_assert_cmpint (*value, ==, 4);
152 : 1 : g_assert_cmpint (value_create_count, ==, 2);
153 : 1 : g_assert_cmpint (value_destroy_count, ==, 1);
154 : :
155 : 1 : g_cache_remove (c, value);
156 : 1 : g_cache_destroy (c);
157 : 1 : g_free (key);
158 : 1 : }
159 : :
160 : : int
161 : 1 : main (int argc, char *argv[])
162 : : {
163 : 1 : g_test_init (&argc, &argv, NULL);
164 : :
165 : 1 : g_test_add_func ("/cache/basic", test_cache_basic);
166 : :
167 : 1 : return g_test_run ();
168 : :
169 : : }
|