Initial packaging for Tizen
[profile/ivi/gobject-introspection.git] / tests / repository / gitypelibtest.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  */
4
5 #include "girepository.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 static void
12 test_enum_and_flags_cidentifier(GIRepository *repo)
13 {
14     GITypelib *ret;
15     GError *error = NULL;
16     gint n_infos, i;
17
18     ret = g_irepository_require (repo, "GIMarshallingTests", NULL, 0, &error);
19     if (!ret)
20         g_error ("%s", error->message);
21
22     n_infos = g_irepository_get_n_infos (repo, "GIMarshallingTests");
23
24     for (i = 0; i < n_infos; i++) {
25         GIBaseInfo *info;
26
27         info = g_irepository_get_info (repo, "GIMarshallingTests", i);
28
29         /* both GI_INFO_TYPE_ENUM and GI_INFO_TYPE_FLAGS use GIEnumInfo */
30         if (GI_IS_ENUM_INFO (info)) {
31             gint n_values, j;
32
33             n_values = g_enum_info_get_n_values ((GIEnumInfo *) info);
34             for (j = 0; j < n_values; j++) {
35                 GIValueInfo *value_info;
36                 const gchar *c_identifier = NULL;
37
38                 value_info = g_enum_info_get_value ((GIEnumInfo *) info, j);
39                 c_identifier = g_base_info_get_attribute ((GIBaseInfo *) value_info, "c:identifier");
40
41                 if (c_identifier == NULL) {
42                     g_error ("Error: no 'c:identifier' attribute on GIMarshallingTests.%s.%s\n",
43                              g_base_info_get_name (info),
44                              g_base_info_get_name ((GIBaseInfo *) value_info));
45                 }
46
47                 g_base_info_unref ((GIBaseInfo *) value_info);
48             }
49         }
50
51         g_base_info_unref (info);
52     }
53 }
54
55 static void
56 _check_enum_methods (GIBaseInfo *info, const gchar *name, const gchar *prefix)
57 {
58     gint n_methods, i;
59
60     n_methods = g_enum_info_get_n_methods ((GIEnumInfo *) info);
61     if (n_methods <= 0)
62         g_error ("%s should have methods", name);
63
64     for (i = 0; i < n_methods; i += n_methods-1) {
65         GIBaseInfo *function_info;
66         GIFunctionInfoFlags flags;
67         const gchar *symbol;
68         function_info = g_enum_info_get_method ((GIEnumInfo *) info, i);
69         if (!function_info)
70             g_error ("Could not find %s method nr. %d", name, i+1);
71         flags = g_function_info_get_flags ((GIFunctionInfo *) function_info);
72         if (flags != 0)
73             g_error ("%s methods should be static", name);
74         symbol = g_function_info_get_symbol ((GIFunctionInfo *) function_info);
75         if (!symbol || !g_str_has_prefix (symbol, prefix))
76             g_error ("Could not find valid function symbol");
77         g_base_info_unref (function_info);
78     }
79 }
80
81 static void
82 test_enum_and_flags_static_methods(GIRepository *repo)
83 {
84     GITypelib *ret;
85     GError *error = NULL;
86     GIBaseInfo *enum_info;
87
88     ret = g_irepository_require (repo, "GIMarshallingTests", NULL, 0, &error);
89     if (!ret)
90         g_error ("%s", error->message);
91
92     enum_info = g_irepository_find_by_name (repo, "GIMarshallingTests", "GEnum");
93     if (!enum_info)
94         g_error ("Could not find GIMarshallingTests.GEnum");
95     _check_enum_methods (enum_info,
96                          "GIMarshallingTests.GEnum",
97                          "gi_marshalling_tests_genum_");
98     g_base_info_unref (enum_info);
99
100     enum_info = g_irepository_find_by_name (repo, "GIMarshallingTests", "Flags");
101     if (!enum_info)
102         g_error ("Could not find GIMarshallingTests.Flags");
103     _check_enum_methods (enum_info,
104                          "GIMarshallingTests.Flags",
105                          "gi_marshalling_tests_flags_");
106     g_base_info_unref (enum_info);
107 }
108
109 static void
110 test_size_of_gvalue(GIRepository *repo)
111 {
112     GIBaseInfo *struct_info;
113
114     struct_info = g_irepository_find_by_name (repo, "GObject", "Value");
115     if (!struct_info)
116         g_error ("Could not find GObject.Value");
117     g_assert_cmpuint (g_struct_info_get_size (struct_info), ==, sizeof (GValue));
118     g_base_info_unref (struct_info);
119 }
120
121 static void
122 test_is_pointer_for_struct_arg (GIRepository *repo)
123 {
124     GITypelib *ret;
125     GError *error = NULL;
126     GIStructInfo *variant_info;
127     GIFunctionInfo *equal_info;
128     GIArgInfo *arg_info;
129     GITypeInfo *type_info;
130
131     ret = g_irepository_require (repo, "GLib", NULL, 0, &error);
132     if (!ret)
133         g_error ("%s", error->message);
134
135     variant_info = g_irepository_find_by_name (repo, "GLib", "Variant");
136     if (!variant_info)
137         g_error ("Could not find GLib.Variant");
138
139     equal_info = g_struct_info_find_method (variant_info, "equal");
140     if (!equal_info)
141         g_error ("Could not find GLib.Variant.equal()");
142
143     arg_info = g_callable_info_get_arg (equal_info, 0);
144     if (!arg_info)
145         g_error ("Could not find 1st arg of GLib.Variant.equal()");
146
147     type_info = g_arg_info_get_type (arg_info);
148     if (!type_info)
149         g_error ("Could not find typeinfo of 1st arg of GLib.Variant.equal()");
150
151     g_assert (g_type_info_is_pointer (type_info));
152
153     g_base_info_unref (type_info);
154     g_base_info_unref (arg_info);
155     g_base_info_unref (equal_info);
156     g_base_info_unref (variant_info);
157 }
158
159 static void
160 test_fundamental_get_ref_function_pointer (GIRepository *repo)
161 {
162     GIObjectInfo *info;
163
164     g_assert (g_irepository_require (repo, "Regress", NULL, 0, NULL));
165     info = g_irepository_find_by_name (repo, "Regress",
166                                        "TestFundamentalObject");
167     g_object_info_get_ref_function_pointer (info);
168     g_base_info_unref (info);
169 }
170
171 int
172 main(int argc, char **argv)
173 {
174     GIRepository *repo;
175
176     g_type_init ();
177
178     repo = g_irepository_get_default ();
179
180     /* do tests */
181     test_enum_and_flags_cidentifier (repo);
182     test_enum_and_flags_static_methods (repo);
183     test_size_of_gvalue (repo);
184     test_is_pointer_for_struct_arg (repo);
185     test_fundamental_get_ref_function_pointer (repo);
186
187     exit(0);
188 }