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