Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* unit-test-cleanup.c: Test low level cleanup functionality
3 :
4 : Copyright (C) 2007 Stefan Walter
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stef@memberwebs.com>
21 : */
22 :
23 : #include <stdlib.h>
24 : #include <stdio.h>
25 : #include <string.h>
26 :
27 : #include "egg/egg-cleanup.h"
28 :
29 : #define DATA "some string"
30 :
31 : typedef struct _CleanupParam {
32 : gpointer value;
33 : } CleanupParam;
34 :
35 : static void
36 1 : cleanup_callback (gpointer user_data)
37 : {
38 1 : CleanupParam *param = (CleanupParam*)user_data;
39 1 : g_assert (param->value && strcmp(param->value, DATA) == 0);
40 1 : param->value = NULL;
41 1 : }
42 :
43 : static void
44 1 : test_cleanup (void)
45 : {
46 : CleanupParam param;
47 :
48 1 : param.value = DATA;
49 :
50 1 : egg_cleanup_register (cleanup_callback, ¶m);
51 :
52 1 : egg_cleanup_perform ();
53 :
54 1 : g_assert (param.value == NULL);
55 1 : }
56 :
57 : /* -----------------------------------------------------------------------------
58 : * Cleanup handlers are called in the opposite order as installed
59 : */
60 :
61 : static gint order_value = 0;
62 :
63 : typedef struct _OrderParam {
64 : gint reference;
65 : } OrderParam;
66 :
67 : static void
68 8 : order_callback (gpointer user_data)
69 : {
70 8 : OrderParam *param = (OrderParam*)user_data;
71 : /* cleanup handler called out of order */
72 8 : g_assert_cmpint (order_value, ==, param->reference);
73 8 : param->reference = -1;
74 8 : --order_value;
75 8 : }
76 :
77 : static void
78 1 : test_order (void)
79 : {
80 : OrderParam param[8];
81 : int i;
82 :
83 9 : for (i = 0; i < 8; ++i) {
84 8 : param[i].reference = i;
85 8 : egg_cleanup_register (order_callback, ¶m[i]);
86 : }
87 :
88 1 : order_value = i - 1;
89 :
90 1 : egg_cleanup_perform ();
91 :
92 9 : for (i = 0; i < 8; ++i)
93 : /* "cleanup handler not called" */
94 8 : g_assert (param[i].reference == -1);
95 :
96 : /* "not all cleanup handlers called" */
97 1 : g_assert_cmpint (order_value, ==, -1);
98 1 : }
99 :
100 : /* -----------------------------------------------------------------------------
101 : * A cleanup handler might cause another to be registered.
102 : */
103 :
104 : static gboolean cleaned_up = FALSE;
105 :
106 : static void
107 1 : second_callback (gpointer user_data)
108 : {
109 1 : cleaned_up = TRUE;
110 1 : }
111 :
112 : static void
113 1 : reregister_callback (gpointer user_data)
114 : {
115 1 : egg_cleanup_register (second_callback, NULL);
116 1 : }
117 :
118 : static void
119 1 : test_reregister (void)
120 : {
121 1 : cleaned_up = FALSE;
122 :
123 1 : egg_cleanup_register (reregister_callback, NULL);
124 :
125 1 : egg_cleanup_perform ();
126 :
127 : /* "second cleanup handler not called" */
128 1 : g_assert (cleaned_up == TRUE);
129 1 : }
130 :
131 : /* -----------------------------------------------------------------------------
132 : * Cleanup handlers can be removed
133 : */
134 :
135 : static gboolean test_cleaned_up = FALSE;
136 :
137 : static void
138 0 : remove_callback (gpointer user_data)
139 : {
140 0 : test_cleaned_up = TRUE;
141 0 : }
142 :
143 : static void
144 1 : test_remove (void)
145 : {
146 1 : egg_cleanup_register (remove_callback, NULL);
147 1 : egg_cleanup_register (remove_callback, DATA);
148 1 : egg_cleanup_unregister (remove_callback, DATA);
149 1 : egg_cleanup_unregister (remove_callback, NULL);
150 1 : egg_cleanup_perform ();
151 :
152 : /* "removed callback was called" */
153 1 : g_assert (test_cleaned_up == FALSE);
154 1 : }
155 :
156 : int
157 1 : main (int argc, char **argv)
158 : {
159 1 : g_test_init (&argc, &argv, NULL);
160 :
161 1 : g_test_add_func ("/cleanup/cleanup", test_cleanup);
162 1 : g_test_add_func ("/cleanup/order", test_order);
163 1 : g_test_add_func ("/cleanup/reregister", test_reregister);
164 1 : g_test_add_func ("/cleanup/remove", test_remove);
165 :
166 1 : return g_test_run ();
167 : }
|