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;
19 type = g_enum_register_static ("MyEnum", my_enum_values);
21 g_value_init (&value, type);
22 g_assert (G_VALUE_HOLDS_ENUM (&value));
24 g_value_set_enum (&value, 2);
25 g_assert_cmpint (g_value_get_enum (&value), ==, 2);
26 g_value_unset (&value);
28 class = g_type_class_ref (type);
30 g_assert_cmpint (class->minimum, ==, 1);
31 g_assert_cmpint (class->maximum, ==, 3);
32 g_assert_cmpint (class->n_values, ==, 3);
34 val = g_enum_get_value (class, 2);
35 g_assert (val != NULL);
36 g_assert_cmpstr (val->value_name, ==, "the second value");
37 val = g_enum_get_value (class, 15);
38 g_assert (val == NULL);
40 val = g_enum_get_value_by_name (class, "the third value");
41 g_assert (val != NULL);
42 g_assert_cmpint (val->value, ==, 3);
43 val = g_enum_get_value_by_name (class, "the color purple");
44 g_assert (val == NULL);
46 val = g_enum_get_value_by_nick (class, "one");
47 g_assert (val != NULL);
48 g_assert_cmpint (val->value, ==, 1);
49 val = g_enum_get_value_by_nick (class, "purple");
50 g_assert (val == NULL);
53 static const GFlagsValue my_flag_values[] =
55 { 1, "the first flag", "one" },
56 { 2, "the second flag", "two" },
57 { 8, "the third flag", "three" },
63 test_flags_basic (void)
68 GValue value = G_VALUE_INIT;
70 type = g_flags_register_static ("MyFlags", my_flag_values);
72 g_value_init (&value, type);
73 g_assert (G_VALUE_HOLDS_FLAGS (&value));
75 g_value_set_flags (&value, 2|8);
76 g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
77 g_value_unset (&value);
79 class = g_type_class_ref (type);
81 g_assert_cmpint (class->mask, ==, 1|2|8);
82 g_assert_cmpint (class->n_values, ==, 3);
84 val = g_flags_get_first_value (class, 2|8);
85 g_assert (val != NULL);
86 g_assert_cmpstr (val->value_name, ==, "the second flag");
87 val = g_flags_get_first_value (class, 16);
88 g_assert (val == NULL);
90 val = g_flags_get_value_by_name (class, "the third flag");
91 g_assert (val != NULL);
92 g_assert_cmpint (val->value, ==, 8);
93 val = g_flags_get_value_by_name (class, "the color purple");
94 g_assert (val == NULL);
96 val = g_flags_get_value_by_nick (class, "one");
97 g_assert (val != NULL);
98 g_assert_cmpint (val->value, ==, 1);
99 val = g_flags_get_value_by_nick (class, "purple");
100 g_assert (val == NULL);
104 main (int argc, char *argv[])
107 g_test_init (&argc, &argv, NULL);
109 g_test_add_func ("/enum/basic", test_enum_basic);
110 g_test_add_func ("/flags/basic", test_flags_basic);
112 return g_test_run ();