1 /* Copyright (C) 2011 Red Hat, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 02111-1307, USA.
19 /* We are testing some deprecated APIs here */
20 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24 static gint value_create_count = 0;
25 static gint value_destroy_count = 0;
28 value_create (gpointer key)
34 value = g_new (gint, 1);
35 *value = *(gint*)key * 2;
41 value_destroy (gpointer value)
43 value_destroy_count++;
48 key_dup (gpointer key)
52 newkey = g_new (gint, 1);
53 *newkey = *(gint*)key;
59 key_destroy (gpointer key)
65 key_hash (gconstpointer key)
71 value_hash (gconstpointer value)
73 return *(guint*)value;
77 key_equal (gconstpointer key1, gconstpointer key2)
79 return *(gint*)key1 == *(gint*)key2;
83 key_foreach (gpointer valuep, gpointer keyp, gpointer data)
90 g_assert_cmpint (*key, ==, 2);
94 value_foreach (gpointer keyp, gpointer nodep, gpointer data)
101 g_assert_cmpint (*key, ==, 2);
105 test_cache_basic (void)
112 value_create_count = 0;
113 value_destroy_count = 0;
115 c = g_cache_new (value_create, value_destroy,
116 key_dup, key_destroy,
117 key_hash, value_hash, key_equal);
119 key = g_new (gint, 1);
122 value = g_cache_insert (c, key);
123 g_assert_cmpint (*value, ==, 4);
124 g_assert_cmpint (value_create_count, ==, 1);
125 g_assert_cmpint (value_destroy_count, ==, 0);
128 g_cache_key_foreach (c, key_foreach, &count);
129 g_assert_cmpint (count, ==, 1);
132 g_cache_value_foreach (c, value_foreach, &count);
133 g_assert_cmpint (count, ==, 1);
135 value = g_cache_insert (c, key);
136 g_assert_cmpint (*value, ==, 4);
137 g_assert_cmpint (value_create_count, ==, 1);
138 g_assert_cmpint (value_destroy_count, ==, 0);
140 g_cache_remove (c, value);
141 g_assert_cmpint (value_create_count, ==, 1);
142 g_assert_cmpint (value_destroy_count, ==, 0);
144 g_cache_remove (c, value);
145 g_assert_cmpint (value_create_count, ==, 1);
146 g_assert_cmpint (value_destroy_count, ==, 1);
148 value = g_cache_insert (c, key);
149 g_assert_cmpint (*value, ==, 4);
150 g_assert_cmpint (value_create_count, ==, 2);
151 g_assert_cmpint (value_destroy_count, ==, 1);
153 g_cache_remove (c, value);
159 main (int argc, char *argv[])
161 g_test_init (&argc, &argv, NULL);
163 g_test_add_func ("/cache/basic", test_cache_basic);
165 return g_test_run ();