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