Branch data Line data Source code
1 : : #include <glib-object.h>
2 : :
3 : : static const GEnumValue my_enum_values[] =
4 : : {
5 : : { 1, "the first value", "one" },
6 : : { 2, "the second value", "two" },
7 : : { 3, "the third value", "three" },
8 : : { 0, NULL, NULL }
9 : : };
10 : :
11 : : static void
12 : 1 : test_enum_basic (void)
13 : : {
14 : : GType type;
15 : : GEnumClass *class;
16 : : GEnumValue *val;
17 : 1 : GValue value = G_VALUE_INIT;
18 : : gchar *to_string;
19 : :
20 : 1 : type = g_enum_register_static ("MyEnum", my_enum_values);
21 : :
22 : 1 : g_value_init (&value, type);
23 : 1 : g_assert_true (G_VALUE_HOLDS_ENUM (&value));
24 : :
25 : 1 : g_value_set_enum (&value, 2);
26 : 1 : g_assert_cmpint (g_value_get_enum (&value), ==, 2);
27 : 1 : g_value_unset (&value);
28 : :
29 : 1 : class = g_type_class_ref (type);
30 : :
31 : 1 : g_assert_cmpint (class->minimum, ==, 1);
32 : 1 : g_assert_cmpint (class->maximum, ==, 3);
33 : 1 : g_assert_cmpint (class->n_values, ==, 3);
34 : :
35 : 1 : val = g_enum_get_value (class, 2);
36 : 1 : g_assert_nonnull (val);
37 : 1 : g_assert_cmpstr (val->value_name, ==, "the second value");
38 : 1 : val = g_enum_get_value (class, 15);
39 : 1 : g_assert_null (val);
40 : :
41 : 1 : val = g_enum_get_value_by_name (class, "the third value");
42 : 1 : g_assert_nonnull (val);
43 : 1 : g_assert_cmpint (val->value, ==, 3);
44 : 1 : g_assert_true (g_enum_get_value_by_name (class, "The Third Value") == val);
45 : 1 : val = g_enum_get_value_by_name (class, "the color purple");
46 : 1 : g_assert_null (val);
47 : :
48 : 1 : val = g_enum_get_value_by_nick (class, "one");
49 : 1 : g_assert_nonnull (val);
50 : 1 : g_assert_cmpint (val->value, ==, 1);
51 : 1 : g_assert_true (g_enum_get_value_by_nick (class, "One") == val);
52 : 1 : val = g_enum_get_value_by_nick (class, "purple");
53 : 1 : g_assert_null (val);
54 : :
55 : 1 : to_string = g_enum_to_string (type, 2);
56 : 1 : g_assert_cmpstr (to_string, ==, "the second value");
57 : 1 : g_free (to_string);
58 : :
59 : 1 : to_string = g_enum_to_string (type, 15);
60 : 1 : g_assert_cmpstr (to_string, ==, "15");
61 : 1 : g_free (to_string);
62 : :
63 : 1 : g_type_class_unref (class);
64 : 1 : }
65 : :
66 : : static const GFlagsValue my_flag_values[] =
67 : : {
68 : : { 0, "no flags", "none" },
69 : : { 1, "the first flag", "one" },
70 : : { 2, "the second flag", "two" },
71 : : { 8, "the third flag", "three" },
72 : : { 0, NULL, NULL }
73 : : };
74 : :
75 : : static const GFlagsValue no_default_flag_values[] =
76 : : {
77 : : { 1, "the first flag", "one" },
78 : : { 0, NULL, NULL }
79 : : };
80 : :
81 : : static void
82 : 1 : test_flags_transform_to_string (const GValue *value)
83 : : {
84 : 1 : GValue tmp = G_VALUE_INIT;
85 : :
86 : 1 : g_value_init (&tmp, G_TYPE_STRING);
87 : 1 : g_value_transform (value, &tmp);
88 : 1 : g_value_unset (&tmp);
89 : 1 : }
90 : :
91 : : static void
92 : 1 : test_flags_basic (void)
93 : : {
94 : : GType type, no_default_type;
95 : : GFlagsClass *class;
96 : : GFlagsValue *val;
97 : 1 : GValue value = G_VALUE_INIT;
98 : : gchar *to_string;
99 : :
100 : 1 : type = g_flags_register_static ("MyFlags", my_flag_values);
101 : 1 : no_default_type = g_flags_register_static ("NoDefaultFlags",
102 : : no_default_flag_values);
103 : :
104 : 1 : g_value_init (&value, type);
105 : 1 : g_assert_true (G_VALUE_HOLDS_FLAGS (&value));
106 : :
107 : 1 : g_value_set_flags (&value, 2|8);
108 : 1 : g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
109 : :
110 : 1 : class = g_type_class_ref (type);
111 : :
112 : 1 : g_assert_cmpint (class->mask, ==, 1|2|8);
113 : 1 : g_assert_cmpint (class->n_values, ==, 4);
114 : :
115 : 1 : val = g_flags_get_first_value (class, 2|8);
116 : 1 : g_assert_nonnull (val);
117 : 1 : g_assert_cmpstr (val->value_name, ==, "the second flag");
118 : 1 : val = g_flags_get_first_value (class, 16);
119 : 1 : g_assert_null (val);
120 : :
121 : 1 : val = g_flags_get_value_by_name (class, "the third flag");
122 : 1 : g_assert_nonnull (val);
123 : 1 : g_assert_true (g_flags_get_value_by_name (class, "The Third Flag") == val);
124 : 1 : g_assert_cmpint (val->value, ==, 8);
125 : 1 : val = g_flags_get_value_by_name (class, "the color purple");
126 : 1 : g_assert_null (val);
127 : :
128 : 1 : val = g_flags_get_value_by_nick (class, "one");
129 : 1 : g_assert_nonnull (val);
130 : 1 : g_assert_cmpint (val->value, ==, 1);
131 : 1 : g_assert_true (g_flags_get_value_by_nick (class, "One") == val);
132 : 1 : val = g_flags_get_value_by_nick (class, "purple");
133 : 1 : g_assert_null (val);
134 : :
135 : 1 : test_flags_transform_to_string (&value);
136 : 1 : g_value_unset (&value);
137 : :
138 : 1 : to_string = g_flags_to_string (type, 1|8);
139 : 1 : g_assert_cmpstr (to_string, ==, "the first flag | the third flag");
140 : 1 : g_free (to_string);
141 : :
142 : 1 : to_string = g_flags_to_string (type, 0);
143 : 1 : g_assert_cmpstr (to_string, ==, "no flags");
144 : 1 : g_free (to_string);
145 : :
146 : 1 : to_string = g_flags_to_string (type, 16);
147 : 1 : g_assert_cmpstr (to_string, ==, "0x10");
148 : 1 : g_free (to_string);
149 : :
150 : 1 : to_string = g_flags_to_string (type, 1|16);
151 : 1 : g_assert_cmpstr (to_string, ==, "the first flag | 0x10");
152 : 1 : g_free (to_string);
153 : :
154 : 1 : to_string = g_flags_to_string (no_default_type, 0);
155 : 1 : g_assert_cmpstr (to_string, ==, "0x0");
156 : 1 : g_free (to_string);
157 : :
158 : 1 : to_string = g_flags_to_string (no_default_type, 16);
159 : 1 : g_assert_cmpstr (to_string, ==, "0x10");
160 : 1 : g_free (to_string);
161 : :
162 : 1 : g_type_class_unref (class);
163 : 1 : }
164 : :
165 : : typedef enum {
166 : : TEST_ENUM_FIRST_VALUE,
167 : : TEST_ENUM_SECOND_VALUE,
168 : : TEST_ENUM_THIRD_VALUE
169 : : } TestEnum;
170 : :
171 : : GType test_enum_get_type (void);
172 : :
173 : 1 : G_DEFINE_ENUM_TYPE (TestEnum, test_enum,
174 : : G_DEFINE_ENUM_VALUE (TEST_ENUM_FIRST_VALUE, "first-value"),
175 : : G_DEFINE_ENUM_VALUE (TEST_ENUM_SECOND_VALUE, "second-value"),
176 : : G_DEFINE_ENUM_VALUE (TEST_ENUM_THIRD_VALUE, "third-value"))
177 : :
178 : : static void
179 : 1 : test_enum_define_type (void)
180 : : {
181 : 1 : GEnumClass *class = g_type_class_ref (test_enum_get_type ());
182 : : GEnumValue *val;
183 : :
184 : 1 : g_assert_cmpint (class->minimum, ==, 0);
185 : 1 : g_assert_cmpint (class->maximum, ==, 2);
186 : 1 : g_assert_cmpint (class->n_values, ==, 3);
187 : :
188 : 1 : val = g_enum_get_value (class, 2);
189 : 1 : g_assert_nonnull (val);
190 : 1 : g_assert_cmpstr (val->value_nick, ==, "third-value");
191 : 1 : val = g_enum_get_value (class, 15);
192 : 1 : g_assert_null (val);
193 : :
194 : 1 : g_type_class_unref (class);
195 : 1 : }
196 : :
197 : : typedef enum {
198 : : TEST_FLAGS_DEFAULT = 0,
199 : : TEST_FLAGS_FIRST = 1 << 0,
200 : : TEST_FLAGS_SECOND = 1 << 1,
201 : : TEST_FLAGS_THIRD = 1 << 2
202 : : } G_GNUC_FLAG_ENUM TestFlags;
203 : :
204 : : GType test_flags_get_type (void);
205 : :
206 : 2 : G_DEFINE_FLAGS_TYPE (TestFlags, test_flags,
207 : : G_DEFINE_ENUM_VALUE (TEST_FLAGS_DEFAULT, "default"),
208 : : G_DEFINE_ENUM_VALUE (TEST_FLAGS_FIRST, "first"),
209 : : G_DEFINE_ENUM_VALUE (TEST_FLAGS_SECOND, "second"),
210 : : G_DEFINE_ENUM_VALUE (TEST_FLAGS_THIRD, "third"))
211 : :
212 : : static void
213 : 1 : test_flags_define_type (void)
214 : : {
215 : 1 : GFlagsClass *class = g_type_class_ref (test_flags_get_type ());
216 : : GFlagsValue *val;
217 : : char *to_string;
218 : :
219 : 1 : g_assert_cmpint (class->mask, ==, 1 | 2 | 4);
220 : 1 : g_assert_cmpint (class->n_values, ==, 4);
221 : :
222 : 1 : val = g_flags_get_first_value (class, 2|4);
223 : 1 : g_assert_nonnull (val);
224 : 1 : g_assert_cmpstr (val->value_nick, ==, "second");
225 : :
226 : 1 : val = g_flags_get_first_value (class, 8);
227 : 1 : g_assert_null (val);
228 : :
229 : 1 : to_string = g_flags_to_string (test_flags_get_type (), 0);
230 : 1 : g_assert_cmpstr (to_string, ==, "TEST_FLAGS_DEFAULT");
231 : 1 : g_free (to_string);
232 : :
233 : 1 : g_type_class_unref (class);
234 : 1 : }
235 : :
236 : : int
237 : 1 : main (int argc, char *argv[])
238 : : {
239 : 1 : g_test_init (&argc, &argv, NULL);
240 : :
241 : 1 : g_test_add_func ("/enum/basic", test_enum_basic);
242 : 1 : g_test_add_func ("/enum/define-type", test_enum_define_type);
243 : 1 : g_test_add_func ("/flags/basic", test_flags_basic);
244 : 1 : g_test_add_func ("/flags/define-type", test_flags_define_type);
245 : :
246 : 1 : return g_test_run ();
247 : : }
|