1 #include <glib-object.h>
3 static const GEnumValue my_enum_values[] =
5 { 1, "the first value", "one" },
6 { 2, "the second value", "two" },
7 { 3, "the third value", "three" },
12 test_enum_basic (void)
17 GValue value = G_VALUE_INIT;
20 type = g_enum_register_static ("MyEnum", my_enum_values);
22 g_value_init (&value, type);
23 g_assert_true (G_VALUE_HOLDS_ENUM (&value));
25 g_value_set_enum (&value, 2);
26 g_assert_cmpint (g_value_get_enum (&value), ==, 2);
27 g_value_unset (&value);
29 class = g_type_class_ref (type);
31 g_assert_cmpint (class->minimum, ==, 1);
32 g_assert_cmpint (class->maximum, ==, 3);
33 g_assert_cmpint (class->n_values, ==, 3);
35 val = g_enum_get_value (class, 2);
36 g_assert_nonnull (val);
37 g_assert_cmpstr (val->value_name, ==, "the second value");
38 val = g_enum_get_value (class, 15);
41 val = g_enum_get_value_by_name (class, "the third value");
42 g_assert_nonnull (val);
43 g_assert_cmpint (val->value, ==, 3);
44 val = g_enum_get_value_by_name (class, "the color purple");
47 val = g_enum_get_value_by_nick (class, "one");
48 g_assert_nonnull (val);
49 g_assert_cmpint (val->value, ==, 1);
50 val = g_enum_get_value_by_nick (class, "purple");
53 to_string = g_enum_to_string (type, 2);
54 g_assert_cmpstr (to_string, ==, "the second value");
57 to_string = g_enum_to_string (type, 15);
58 g_assert_cmpstr (to_string, ==, "15");
61 g_type_class_unref (class);
64 static const GFlagsValue my_flag_values[] =
66 { 0, "no flags", "none" },
67 { 1, "the first flag", "one" },
68 { 2, "the second flag", "two" },
69 { 8, "the third flag", "three" },
73 static const GFlagsValue no_default_flag_values[] =
75 { 1, "the first flag", "one" },
80 test_flags_transform_to_string (const GValue *value)
82 GValue tmp = G_VALUE_INIT;
84 g_value_init (&tmp, G_TYPE_STRING);
85 g_value_transform (value, &tmp);
90 test_flags_basic (void)
92 GType type, no_default_type;
95 GValue value = G_VALUE_INIT;
98 type = g_flags_register_static ("MyFlags", my_flag_values);
99 no_default_type = g_flags_register_static ("NoDefaultFlags",
100 no_default_flag_values);
102 g_value_init (&value, type);
103 g_assert_true (G_VALUE_HOLDS_FLAGS (&value));
105 g_value_set_flags (&value, 2|8);
106 g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
108 class = g_type_class_ref (type);
110 g_assert_cmpint (class->mask, ==, 1|2|8);
111 g_assert_cmpint (class->n_values, ==, 4);
113 val = g_flags_get_first_value (class, 2|8);
114 g_assert_nonnull (val);
115 g_assert_cmpstr (val->value_name, ==, "the second flag");
116 val = g_flags_get_first_value (class, 16);
119 val = g_flags_get_value_by_name (class, "the third flag");
120 g_assert_nonnull (val);
121 g_assert_cmpint (val->value, ==, 8);
122 val = g_flags_get_value_by_name (class, "the color purple");
125 val = g_flags_get_value_by_nick (class, "one");
126 g_assert_nonnull (val);
127 g_assert_cmpint (val->value, ==, 1);
128 val = g_flags_get_value_by_nick (class, "purple");
131 test_flags_transform_to_string (&value);
132 g_value_unset (&value);
134 to_string = g_flags_to_string (type, 1|8);
135 g_assert_cmpstr (to_string, ==, "the first flag | the third flag");
138 to_string = g_flags_to_string (type, 0);
139 g_assert_cmpstr (to_string, ==, "no flags");
142 to_string = g_flags_to_string (type, 16);
143 g_assert_cmpstr (to_string, ==, "0x10");
146 to_string = g_flags_to_string (type, 1|16);
147 g_assert_cmpstr (to_string, ==, "the first flag | 0x10");
150 to_string = g_flags_to_string (no_default_type, 0);
151 g_assert_cmpstr (to_string, ==, "0x0");
154 to_string = g_flags_to_string (no_default_type, 16);
155 g_assert_cmpstr (to_string, ==, "0x10");
158 g_type_class_unref (class);
162 TEST_ENUM_FIRST_VALUE,
163 TEST_ENUM_SECOND_VALUE,
164 TEST_ENUM_THIRD_VALUE
167 GType test_enum_get_type (void);
169 G_DEFINE_ENUM_TYPE (TestEnum, test_enum,
170 G_DEFINE_ENUM_VALUE (TEST_ENUM_FIRST_VALUE, "first-value"),
171 G_DEFINE_ENUM_VALUE (TEST_ENUM_SECOND_VALUE, "second-value"),
172 G_DEFINE_ENUM_VALUE (TEST_ENUM_THIRD_VALUE, "third-value"))
175 test_enum_define_type (void)
177 GEnumClass *class = g_type_class_ref (test_enum_get_type ());
180 g_assert_cmpint (class->minimum, ==, 0);
181 g_assert_cmpint (class->maximum, ==, 2);
182 g_assert_cmpint (class->n_values, ==, 3);
184 val = g_enum_get_value (class, 2);
185 g_assert_nonnull (val);
186 g_assert_cmpstr (val->value_nick, ==, "third-value");
187 val = g_enum_get_value (class, 15);
190 g_type_class_unref (class);
194 TEST_FLAGS_DEFAULT = 0,
195 TEST_FLAGS_FIRST = 1 << 0,
196 TEST_FLAGS_SECOND = 1 << 1,
197 TEST_FLAGS_THIRD = 1 << 2
200 GType test_flags_get_type (void);
202 G_DEFINE_FLAGS_TYPE (TestFlags, test_flags,
203 G_DEFINE_ENUM_VALUE (TEST_FLAGS_DEFAULT, "default"),
204 G_DEFINE_ENUM_VALUE (TEST_FLAGS_FIRST, "first"),
205 G_DEFINE_ENUM_VALUE (TEST_FLAGS_SECOND, "second"),
206 G_DEFINE_ENUM_VALUE (TEST_FLAGS_THIRD, "third"))
209 test_flags_define_type (void)
211 GFlagsClass *class = g_type_class_ref (test_flags_get_type ());
215 g_assert_cmpint (class->mask, ==, 1 | 2 | 4);
216 g_assert_cmpint (class->n_values, ==, 4);
218 val = g_flags_get_first_value (class, 2|4);
219 g_assert_nonnull (val);
220 g_assert_cmpstr (val->value_nick, ==, "second");
222 val = g_flags_get_first_value (class, 8);
225 to_string = g_flags_to_string (test_flags_get_type (), 0);
226 g_assert_cmpstr (to_string, ==, "TEST_FLAGS_DEFAULT");
229 g_type_class_unref (class);
233 main (int argc, char *argv[])
235 g_test_init (&argc, &argv, NULL);
237 g_test_add_func ("/enum/basic", test_enum_basic);
238 g_test_add_func ("/enum/define-type", test_enum_define_type);
239 g_test_add_func ("/flags/basic", test_flags_basic);
240 g_test_add_func ("/flags/define-type", test_flags_define_type);
242 return g_test_run ();