tests: Drop unnecessary % from .test pattern match rule
[platform/upstream/glib.git] / tests / gobject / dynamictype.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001, 2003 Red Hat, Inc.
3  *
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 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.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #undef  G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestDynamicType"
22
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
26
27 #include <glib-object.h>
28
29 #include "testcommon.h"
30 #include "testmodule.h"
31
32 /* This test tests the macros for defining dynamic types.
33  */
34
35 static gboolean loaded = FALSE;
36
37 struct _TestIfaceClass
38 {
39   GTypeInterface base_iface;
40   guint val;
41 };
42
43 static GType test_iface_get_type (void);
44 #define TEST_TYPE_IFACE           (test_iface_get_type ())
45 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
46 typedef struct _TestIface      TestIface;
47 typedef struct _TestIfaceClass TestIfaceClass;
48
49 static void test_iface_base_init    (TestIfaceClass *iface);
50 static void test_iface_default_init (TestIfaceClass *iface, gpointer class_data);
51
52 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_default_init)
53
54 static void
55 test_iface_default_init (TestIfaceClass *iface,
56                          gpointer        class_data)
57 {
58 }
59
60 static void
61 test_iface_base_init (TestIfaceClass *iface)
62 {
63 }
64
65 GType dynamic_object_get_type (void);
66 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
67
68 typedef GObject DynamicObject;
69 typedef struct _DynamicObjectClass DynamicObjectClass;
70
71 struct _DynamicObjectClass
72 {
73   GObjectClass parent_class;
74   guint val;
75 };
76
77 static void dynamic_object_iface_init (TestIface *iface);
78
79 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
80                                G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
81                                                               dynamic_object_iface_init));
82
83 static void 
84 dynamic_object_class_init (DynamicObjectClass *class)
85 {
86   class->val = 42;
87   loaded = TRUE;
88 }
89
90 static void
91 dynamic_object_class_finalize (DynamicObjectClass *class)
92 {
93   loaded = FALSE;
94 }
95
96 static void
97 dynamic_object_iface_init (TestIface *iface)
98 {
99 }
100
101 static void
102 dynamic_object_init (DynamicObject *dynamic_object)
103 {
104 }
105
106 static void
107 module_register (GTypeModule *module)
108 {
109   dynamic_object_register_type (module);
110 }
111
112 static void
113 test_dynamic_type (void)
114 {
115   DynamicObjectClass *class;
116
117   test_module_new (module_register);
118
119   /* Not loaded until we call ref for the first time */
120   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
121   g_assert (class == NULL);
122   g_assert (!loaded);
123
124   /* Make sure interfaces work */
125   g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
126                          TEST_TYPE_IFACE));
127
128   /* Ref loads */
129   class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
130   g_assert (class && class->val == 42);
131   g_assert (loaded);
132
133   /* Peek then works */
134   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
135   g_assert (class && class->val == 42);
136   g_assert (loaded);
137
138   /* Make sure interfaces still work */
139   g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
140                          TEST_TYPE_IFACE));
141
142   /* Unref causes finalize */
143   g_type_class_unref (class);
144
145   /* Peek returns NULL */
146   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
147 #if 0
148   g_assert (!class);
149   g_assert (!loaded);
150 #endif
151   
152   /* Ref reloads */
153   class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
154   g_assert (class && class->val == 42);
155   g_assert (loaded);
156
157   /* And Unref causes finalize once more*/
158   g_type_class_unref (class);
159   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
160 #if 0
161   g_assert (!class);
162   g_assert (!loaded);
163 #endif
164 }
165
166 int
167 main (int   argc,
168       char *argv[])
169 {
170   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
171                           G_LOG_LEVEL_WARNING |
172                           G_LOG_LEVEL_CRITICAL);
173
174   test_dynamic_type ();
175   
176   return 0;
177 }