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