release 0.5.4
[swfdec.git] / test / various / gc.c
blobebc87a8b168f39720cecdac6fc3c86ea0cb8eabe
1 /* Swfdec
2 * Copyright (C) 2006 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "libswfdec/swfdec_as_context.h"
25 #include "libswfdec/swfdec_as_object.h"
26 #include "libswfdec/swfdec_as_strings.h"
28 #define ERROR(...) G_STMT_START { \
29 g_printerr ("ERROR (line %u): ", __LINE__); \
30 g_printerr (__VA_ARGS__); \
31 g_printerr ("\n"); \
32 errors++; \
33 }G_STMT_END
35 static guint
36 check_strings (void)
38 const char *s;
39 guint errors = 0;
40 SwfdecAsContext *context;
42 context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
43 swfdec_as_context_startup (context, 7);
45 s = swfdec_as_context_get_string (context, "hi mom");
46 if (!g_str_equal (s, "hi mom")) {
47 ERROR ("swfdec_as_context_get_string returns different string from input");
50 g_object_unref (context);
51 return errors;
54 static guint
55 check_objects (void)
57 SwfdecAsObject *object;
58 guint errors = 0;
59 SwfdecAsContext *context;
60 SwfdecAsValue val;
61 gpointer check = GUINT_TO_POINTER (-1); /* NOT NULL */
63 context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
64 swfdec_as_context_startup (context, 7);
65 g_assert (check != NULL);
67 object = swfdec_as_object_new (context);
68 g_object_add_weak_pointer (G_OBJECT (object), &check);
69 SWFDEC_AS_VALUE_SET_OBJECT (&val, object);
70 swfdec_as_object_set_variable (context->global, SWFDEC_AS_STR__root, &val);
71 swfdec_as_context_gc (context);
72 if (check == NULL) {
73 ERROR ("GC collected a rooted object, bailing");
74 g_object_unref (context);
75 return errors;
77 swfdec_as_object_delete_variable (context->global, SWFDEC_AS_STR__root);
78 swfdec_as_context_gc (context);
79 if (check != NULL) {
80 ERROR ("GC did not collect an unreferenced object");
83 g_object_unref (context);
84 return errors;
87 static guint
88 check_object_variables (void)
90 SwfdecAsObject *o, *o2;
91 guint errors = 0;
92 SwfdecAsContext *context;
93 const char *s;
94 gpointer check = GUINT_TO_POINTER (-1); /* NOT NULL */
95 gpointer check2 = GUINT_TO_POINTER (-1); /* NOT NULL */
96 SwfdecAsValue v1, v2;
98 context = g_object_new (SWFDEC_TYPE_AS_CONTEXT, NULL);
99 swfdec_as_context_startup (context, 7);
100 g_assert (check != NULL);
102 o = swfdec_as_object_new (context);
103 o2 = swfdec_as_object_new (context);
104 g_object_add_weak_pointer (G_OBJECT (o), &check);
105 g_object_add_weak_pointer (G_OBJECT (o2), &check2);
106 s = swfdec_as_context_get_string (context, "foo");
107 /* step one: set a variable */
108 SWFDEC_AS_VALUE_SET_OBJECT (&v1, o);
109 swfdec_as_object_set_variable (context->global, s, &v1);
110 SWFDEC_AS_VALUE_SET_OBJECT (&v2, o2);
111 swfdec_as_object_set_variable (o, s, &v2);
112 SWFDEC_AS_VALUE_SET_UNDEFINED (&v2);
113 swfdec_as_object_get_variable (o, s, &v2);
114 if (!SWFDEC_AS_VALUE_IS_OBJECT (&v2)) {
115 ERROR ("variable changed type");
116 } else if (o2 != SWFDEC_AS_VALUE_GET_OBJECT (&v2)) {
117 ERROR ("variable changed value");
119 /* step 2: gc */
120 swfdec_as_context_gc (context);
121 if (check == NULL || check2 == NULL) {
122 ERROR ("GC collected a used object, bailing");
123 g_object_unref (context);
124 return errors;
126 /* step 3: unset root reference and set cyclic variable */
127 swfdec_as_object_delete_variable (context->global, s);
128 swfdec_as_object_set_variable (o2, s, &v1);
129 SWFDEC_AS_VALUE_SET_UNDEFINED (&v1);
130 swfdec_as_object_get_variable (o2, s, &v1);
131 if (!SWFDEC_AS_VALUE_IS_OBJECT (&v1)) {
132 ERROR ("variable changed type");
133 } else if (o != SWFDEC_AS_VALUE_GET_OBJECT (&v1)) {
134 ERROR ("variable changed value");
136 /* step 4: gc, ensure that both objects disappears */
137 swfdec_as_context_gc (context);
138 if (check != NULL || check2 != NULL) {
139 ERROR ("GC didn't collect unused object");
142 g_object_unref (context);
143 return errors;
148 main (int argc, char **argv)
150 guint errors = 0;
152 g_type_init ();
154 errors += check_strings ();
155 errors += check_objects ();
156 errors += check_object_variables ();
158 g_print ("TOTAL ERRORS: %u\n", errors);
159 return errors;