1 /* Copyright (C) 2011 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
19 /* We are testing some deprecated APIs here */
20 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
21 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 static gint value_create_count = 0;
27 static gint value_destroy_count = 0;
30 value_create (gpointer key)
36 value = g_new (gint, 1);
37 *value = *(gint*)key * 2;
43 value_destroy (gpointer value)
45 value_destroy_count++;
50 key_dup (gpointer key)
54 newkey = g_new (gint, 1);
55 *newkey = *(gint*)key;
61 key_destroy (gpointer key)
67 key_hash (gconstpointer key)
73 value_hash (gconstpointer value)
75 return *(guint*)value;
79 key_equal (gconstpointer key1, gconstpointer key2)
81 return *(gint*)key1 == *(gint*)key2;
85 key_foreach (gpointer valuep, gpointer keyp, gpointer data)
92 g_assert_cmpint (*key, ==, 2);
96 value_foreach (gpointer keyp, gpointer nodep, gpointer data)
103 g_assert_cmpint (*key, ==, 2);
107 test_cache_basic (void)
114 value_create_count = 0;
115 value_destroy_count = 0;
117 c = g_cache_new (value_create, value_destroy,
118 key_dup, key_destroy,
119 key_hash, value_hash, key_equal);
121 key = g_new (gint, 1);
124 value = g_cache_insert (c, key);
125 g_assert_cmpint (*value, ==, 4);
126 g_assert_cmpint (value_create_count, ==, 1);
127 g_assert_cmpint (value_destroy_count, ==, 0);
130 g_cache_key_foreach (c, key_foreach, &count);
131 g_assert_cmpint (count, ==, 1);
134 g_cache_value_foreach (c, value_foreach, &count);
135 g_assert_cmpint (count, ==, 1);
137 value = g_cache_insert (c, key);
138 g_assert_cmpint (*value, ==, 4);
139 g_assert_cmpint (value_create_count, ==, 1);
140 g_assert_cmpint (value_destroy_count, ==, 0);
142 g_cache_remove (c, value);
143 g_assert_cmpint (value_create_count, ==, 1);
144 g_assert_cmpint (value_destroy_count, ==, 0);
146 g_cache_remove (c, value);
147 g_assert_cmpint (value_create_count, ==, 1);
148 g_assert_cmpint (value_destroy_count, ==, 1);
150 value = g_cache_insert (c, key);
151 g_assert_cmpint (*value, ==, 4);
152 g_assert_cmpint (value_create_count, ==, 2);
153 g_assert_cmpint (value_destroy_count, ==, 1);
155 g_cache_remove (c, value);
161 main (int argc, char *argv[])
163 g_test_init (&argc, &argv, NULL);
165 g_test_add_func ("/cache/basic", test_cache_basic);
167 return g_test_run ();