Improve signal test coverage
[platform/upstream/glib.git] / gobject / tests / enums.c
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 test_enum_basic (void)
13 {
14   GType type;
15   GEnumClass *class;
16   GEnumValue *val;
17   GValue value = G_VALUE_INIT;
18
19   type = g_enum_register_static ("MyEnum", my_enum_values);
20
21   g_value_init (&value, type);
22   g_assert (G_VALUE_HOLDS_ENUM (&value));
23
24   g_value_set_enum (&value, 2);
25   g_assert_cmpint (g_value_get_enum (&value), ==, 2);
26   g_value_unset (&value);
27
28   class = g_type_class_ref (type);
29
30   g_assert_cmpint (class->minimum, ==, 1);
31   g_assert_cmpint (class->maximum, ==, 3);
32   g_assert_cmpint (class->n_values, ==, 3);
33
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);
39
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);
45
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);
51 }
52
53 static const GFlagsValue my_flag_values[] =
54 {
55   { 0, "no flags", "none" },
56   { 1, "the first flag", "one" },
57   { 2, "the second flag", "two" },
58   { 8, "the third flag", "three" },
59   { 0, NULL, NULL }
60 };
61
62 static void
63 test_flags_transform_to_string (const GValue *value)
64 {
65   GValue tmp = G_VALUE_INIT;
66
67   g_value_init (&tmp, G_TYPE_STRING);
68   g_value_transform (value, &tmp);
69   g_value_unset (&tmp);
70 }
71
72 static void
73 test_flags_basic (void)
74 {
75   GType type;
76   GFlagsClass *class;
77   GFlagsValue *val;
78   GValue value = G_VALUE_INIT;
79
80   type = g_flags_register_static ("MyFlags", my_flag_values);
81
82   g_value_init (&value, type);
83   g_assert (G_VALUE_HOLDS_FLAGS (&value));
84
85   g_value_set_flags (&value, 2|8);
86   g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
87
88   class = g_type_class_ref (type);
89
90   g_assert_cmpint (class->mask, ==, 1|2|8);
91   g_assert_cmpint (class->n_values, ==, 4);
92
93   val = g_flags_get_first_value (class, 2|8);
94   g_assert (val != NULL);
95   g_assert_cmpstr (val->value_name, ==, "the second flag");
96   val = g_flags_get_first_value (class, 16);
97   g_assert (val == NULL);
98
99   val = g_flags_get_value_by_name (class, "the third flag");
100   g_assert (val != NULL);
101   g_assert_cmpint (val->value, ==, 8);
102   val = g_flags_get_value_by_name (class, "the color purple");
103   g_assert (val == NULL);
104
105   val = g_flags_get_value_by_nick (class, "one");
106   g_assert (val != NULL);
107   g_assert_cmpint (val->value, ==, 1);
108   val = g_flags_get_value_by_nick (class, "purple");
109   g_assert (val == NULL);
110
111   test_flags_transform_to_string (&value);
112   g_value_unset (&value);
113 }
114
115 int
116 main (int argc, char *argv[])
117 {
118   g_test_init (&argc, &argv, NULL);
119
120   g_test_add_func ("/enum/basic", test_enum_basic);
121   g_test_add_func ("/flags/basic", test_flags_basic);
122
123   return g_test_run ();
124 }