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
|
/* rcbox.c: Reference counted data
*
* Copyright 2018 Emmanuele Bassi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
typedef struct {
float x, y;
} Point;
static Point *global_point;
/* test_rcbox_new: Test g_rc_box_new() */
static void
test_rcbox_new (void)
{
Point *a = g_rc_box_new (Point);
g_assert_nonnull (a);
g_assert_cmpuint (g_rc_box_get_size (a), ==, sizeof (Point));
g_rc_box_release (a);
}
static void
point_clear (Point *p)
{
g_assert_nonnull (p);
g_assert_true (global_point == p);
g_assert_cmpfloat (p->x, ==, 42.0f);
g_assert_cmpfloat (p->y, ==, 47.0f);
g_test_message ("global_point = %p", p);
global_point = NULL;
}
/* test_rcbox_release_full: Verify that g_rc_box_release_full() calls
* the clear function only when the last reference is released
*/
static void
test_rcbox_release_full (void)
{
Point *p = g_rc_box_new (Point);
g_assert_nonnull (p);
global_point = p;
p->x = 42.0f;
p->y = 47.0f;
g_assert_true (g_rc_box_acquire (p) == p);
g_rc_box_release_full (p, (GDestroyNotify) point_clear);
g_assert_nonnull (global_point);
g_assert_true (p == global_point);
g_rc_box_release_full (p, (GDestroyNotify) point_clear);
g_assert_null (global_point);
}
static Point *global_point_a;
static Point *global_point_b;
static void
point_clear_dup_a (Point *a)
{
g_assert_true (a == global_point_a);
g_test_message ("global_point_a = %p", a);
global_point_a = NULL;
}
static void
point_clear_dup_b (Point *b)
{
g_assert_true (b == global_point_b);
g_test_message ("global_point_b = %p", b);
global_point_b = NULL;
}
/* test_rcbox_dup: Verify that g_rc_box_dup() copies only the
* data and does not change the reference count of the original
*/
static void
test_rcbox_dup (void)
{
Point *a, *b;
a = g_rc_box_new (Point);
a->x = 10.f;
a->y = 5.f;
b = g_rc_box_dup (sizeof (Point), a);
g_assert_true (a != b);
g_assert_cmpfloat (a->x, ==, b->x);
g_assert_cmpfloat (a->y, ==, b->y);
global_point_a = a;
global_point_b = b;
a->x = 1.f;
a->y = 1.f;
g_assert_cmpfloat (a->x, !=, b->x);
g_assert_cmpfloat (a->y, !=, b->y);
b->x = 5.f;
b->y = 10.f;
g_assert_cmpfloat (a->x, !=, b->x);
g_assert_cmpfloat (a->y, !=, b->y);
g_rc_box_release_full (a, (GDestroyNotify) point_clear_dup_a);
g_assert_null (global_point_a);
g_assert_nonnull (global_point_b);
g_rc_box_release_full (b, (GDestroyNotify) point_clear_dup_b);
g_assert_null (global_point_b);
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/rcbox/new", test_rcbox_new);
g_test_add_func ("/rcbox/release-full", test_rcbox_release_full);
g_test_add_func ("/rcbox/dup", test_rcbox_dup);
return g_test_run ();
}
|