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