Improve GCache test coverage
[platform/upstream/glib.git] / glib / tests / cache.c
1 /* Copyright (C) 2011 Red Hat, Inc.
2  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <glib.h>
20
21 static gint value_create_count = 0;
22 static gint value_destroy_count = 0;
23
24 static gpointer
25 value_create (gpointer key)
26 {
27   gint *value;
28
29   value_create_count++;
30
31   value = g_new (gint, 1);
32   *value = *(gint*)key * 2;
33
34   return value;
35 }
36
37 static void
38 value_destroy (gpointer value)
39 {
40   value_destroy_count++;
41   g_free (value);
42 }
43
44 static gpointer
45 key_dup (gpointer key)
46 {
47   gint *newkey;
48
49   newkey = g_new (gint, 1);
50   *newkey = *(gint*)key;
51
52   return newkey;
53 }
54
55 static void
56 key_destroy (gpointer key)
57 {
58   g_free (key);
59 }
60
61 static guint
62 key_hash (gconstpointer key)
63 {
64   return *(guint*)key;
65 }
66
67 static guint
68 value_hash (gconstpointer value)
69 {
70   return *(guint*)value;
71 }
72
73 static gboolean
74 key_equal (gconstpointer key1, gconstpointer key2)
75 {
76   return *(gint*)key1 == *(gint*)key2;
77 }
78
79 static void
80 key_foreach (gpointer valuep, gpointer keyp, gpointer data)
81 {
82   gint *count = data;
83   gint *key = keyp;
84
85   (*count)++;
86
87   g_assert_cmpint (*key, ==, 2);
88 }
89
90 static void
91 value_foreach (gpointer keyp, gpointer nodep, gpointer data)
92 {
93   gint *count = data;
94   gint *key = keyp;
95
96   (*count)++;
97
98   g_assert_cmpint (*key, ==, 2);
99 }
100
101 static void
102 test_cache_basic (void)
103 {
104   GCache *c;
105   gint *key;
106   gint *value;
107   gint count;
108
109   value_create_count = 0;
110   value_destroy_count = 0;
111
112   c = g_cache_new (value_create, value_destroy,
113                    key_dup, key_destroy,
114                    key_hash, value_hash, key_equal);
115
116   key = g_new (gint, 1);
117   *key = 2;
118
119   value = g_cache_insert (c, key);
120   g_assert_cmpint (*value, ==, 4);
121   g_assert_cmpint (value_create_count, ==, 1);
122   g_assert_cmpint (value_destroy_count, ==, 0);
123
124   count = 0;
125   g_cache_key_foreach (c, key_foreach, &count);
126   g_assert_cmpint (count, ==, 1);
127
128   count = 0;
129   g_cache_value_foreach (c, value_foreach, &count);
130   g_assert_cmpint (count, ==, 1);
131
132   value = g_cache_insert (c, key);
133   g_assert_cmpint (*value, ==, 4);
134   g_assert_cmpint (value_create_count, ==, 1);
135   g_assert_cmpint (value_destroy_count, ==, 0);
136
137   g_cache_remove (c, value);
138   g_assert_cmpint (value_create_count, ==, 1);
139   g_assert_cmpint (value_destroy_count, ==, 0);
140
141   g_cache_remove (c, value);
142   g_assert_cmpint (value_create_count, ==, 1);
143   g_assert_cmpint (value_destroy_count, ==, 1);
144
145   value = g_cache_insert (c, key);
146   g_assert_cmpint (*value, ==, 4);
147   g_assert_cmpint (value_create_count, ==, 2);
148   g_assert_cmpint (value_destroy_count, ==, 1);
149
150   g_cache_destroy (c);
151 }
152
153 int
154 main (int argc, char *argv[])
155 {
156   g_test_init (&argc, &argv, NULL);
157
158   g_test_add_func ("/cache/basic", test_cache_basic);
159
160   return g_test_run ();
161
162 }