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 | gchar *to_string; |
19 | |
20 | type = g_enum_register_static (name: "MyEnum" , const_static_values: my_enum_values); |
21 | |
22 | g_value_init (value: &value, g_type: type); |
23 | g_assert (G_VALUE_HOLDS_ENUM (&value)); |
24 | |
25 | g_value_set_enum (value: &value, v_enum: 2); |
26 | g_assert_cmpint (g_value_get_enum (&value), ==, 2); |
27 | g_value_unset (value: &value); |
28 | |
29 | class = g_type_class_ref (type); |
30 | |
31 | g_assert_cmpint (class->minimum, ==, 1); |
32 | g_assert_cmpint (class->maximum, ==, 3); |
33 | g_assert_cmpint (class->n_values, ==, 3); |
34 | |
35 | val = g_enum_get_value (enum_class: class, value: 2); |
36 | g_assert (val != NULL); |
37 | g_assert_cmpstr (val->value_name, ==, "the second value" ); |
38 | val = g_enum_get_value (enum_class: class, value: 15); |
39 | g_assert (val == NULL); |
40 | |
41 | val = g_enum_get_value_by_name (enum_class: class, name: "the third value" ); |
42 | g_assert (val != NULL); |
43 | g_assert_cmpint (val->value, ==, 3); |
44 | val = g_enum_get_value_by_name (enum_class: class, name: "the color purple" ); |
45 | g_assert (val == NULL); |
46 | |
47 | val = g_enum_get_value_by_nick (enum_class: class, nick: "one" ); |
48 | g_assert (val != NULL); |
49 | g_assert_cmpint (val->value, ==, 1); |
50 | val = g_enum_get_value_by_nick (enum_class: class, nick: "purple" ); |
51 | g_assert (val == NULL); |
52 | |
53 | to_string = g_enum_to_string (g_enum_type: type, value: 2); |
54 | g_assert_cmpstr (to_string, ==, "the second value" ); |
55 | g_free (mem: to_string); |
56 | |
57 | to_string = g_enum_to_string (g_enum_type: type, value: 15); |
58 | g_assert_cmpstr (to_string, ==, "15" ); |
59 | g_free (mem: to_string); |
60 | |
61 | g_type_class_unref (g_class: class); |
62 | } |
63 | |
64 | static const GFlagsValue my_flag_values[] = |
65 | { |
66 | { 0, "no flags" , "none" }, |
67 | { 1, "the first flag" , "one" }, |
68 | { 2, "the second flag" , "two" }, |
69 | { 8, "the third flag" , "three" }, |
70 | { 0, NULL, NULL } |
71 | }; |
72 | |
73 | static const GFlagsValue no_default_flag_values[] = |
74 | { |
75 | { 1, "the first flag" , "one" }, |
76 | { 0, NULL, NULL } |
77 | }; |
78 | |
79 | static void |
80 | test_flags_transform_to_string (const GValue *value) |
81 | { |
82 | GValue tmp = G_VALUE_INIT; |
83 | |
84 | g_value_init (value: &tmp, G_TYPE_STRING); |
85 | g_value_transform (src_value: value, dest_value: &tmp); |
86 | g_value_unset (value: &tmp); |
87 | } |
88 | |
89 | static void |
90 | test_flags_basic (void) |
91 | { |
92 | GType type, no_default_type; |
93 | GFlagsClass *class; |
94 | GFlagsValue *val; |
95 | GValue value = G_VALUE_INIT; |
96 | gchar *to_string; |
97 | |
98 | type = g_flags_register_static (name: "MyFlags" , const_static_values: my_flag_values); |
99 | no_default_type = g_flags_register_static (name: "NoDefaultFlags" , |
100 | const_static_values: no_default_flag_values); |
101 | |
102 | g_value_init (value: &value, g_type: type); |
103 | g_assert (G_VALUE_HOLDS_FLAGS (&value)); |
104 | |
105 | g_value_set_flags (value: &value, v_flags: 2|8); |
106 | g_assert_cmpint (g_value_get_flags (&value), ==, 2|8); |
107 | |
108 | class = g_type_class_ref (type); |
109 | |
110 | g_assert_cmpint (class->mask, ==, 1|2|8); |
111 | g_assert_cmpint (class->n_values, ==, 4); |
112 | |
113 | val = g_flags_get_first_value (flags_class: class, value: 2|8); |
114 | g_assert (val != NULL); |
115 | g_assert_cmpstr (val->value_name, ==, "the second flag" ); |
116 | val = g_flags_get_first_value (flags_class: class, value: 16); |
117 | g_assert (val == NULL); |
118 | |
119 | val = g_flags_get_value_by_name (flags_class: class, name: "the third flag" ); |
120 | g_assert (val != NULL); |
121 | g_assert_cmpint (val->value, ==, 8); |
122 | val = g_flags_get_value_by_name (flags_class: class, name: "the color purple" ); |
123 | g_assert (val == NULL); |
124 | |
125 | val = g_flags_get_value_by_nick (flags_class: class, nick: "one" ); |
126 | g_assert (val != NULL); |
127 | g_assert_cmpint (val->value, ==, 1); |
128 | val = g_flags_get_value_by_nick (flags_class: class, nick: "purple" ); |
129 | g_assert (val == NULL); |
130 | |
131 | test_flags_transform_to_string (value: &value); |
132 | g_value_unset (value: &value); |
133 | |
134 | to_string = g_flags_to_string (flags_type: type, value: 1|8); |
135 | g_assert_cmpstr (to_string, ==, "the first flag | the third flag" ); |
136 | g_free (mem: to_string); |
137 | |
138 | to_string = g_flags_to_string (flags_type: type, value: 0); |
139 | g_assert_cmpstr (to_string, ==, "no flags" ); |
140 | g_free (mem: to_string); |
141 | |
142 | to_string = g_flags_to_string (flags_type: type, value: 16); |
143 | g_assert_cmpstr (to_string, ==, "0x10" ); |
144 | g_free (mem: to_string); |
145 | |
146 | to_string = g_flags_to_string (flags_type: type, value: 1|16); |
147 | g_assert_cmpstr (to_string, ==, "the first flag | 0x10" ); |
148 | g_free (mem: to_string); |
149 | |
150 | to_string = g_flags_to_string (flags_type: no_default_type, value: 0); |
151 | g_assert_cmpstr (to_string, ==, "0x0" ); |
152 | g_free (mem: to_string); |
153 | |
154 | to_string = g_flags_to_string (flags_type: no_default_type, value: 16); |
155 | g_assert_cmpstr (to_string, ==, "0x10" ); |
156 | g_free (mem: to_string); |
157 | |
158 | g_type_class_unref (g_class: class); |
159 | } |
160 | |
161 | int |
162 | main (int argc, char *argv[]) |
163 | { |
164 | g_test_init (argc: &argc, argv: &argv, NULL); |
165 | |
166 | g_test_add_func (testpath: "/enum/basic" , test_func: test_enum_basic); |
167 | g_test_add_func (testpath: "/flags/basic" , test_func: test_flags_basic); |
168 | |
169 | return g_test_run (); |
170 | } |
171 | |