Update German translation
[glib.git] / gobject / tests / enums.c
blob84ffed7aac866da4dcb47be83fd587e2fd4b441a
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" },
8 { 0, NULL, NULL }
9 };
11 static void
12 test_enum_basic (void)
14 GType type;
15 GEnumClass *class;
16 GEnumValue *val;
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);
52 g_type_class_unref (class);
55 static const GFlagsValue my_flag_values[] =
57 { 0, "no flags", "none" },
58 { 1, "the first flag", "one" },
59 { 2, "the second flag", "two" },
60 { 8, "the third flag", "three" },
61 { 0, NULL, NULL }
64 static void
65 test_flags_transform_to_string (const GValue *value)
67 GValue tmp = G_VALUE_INIT;
69 g_value_init (&tmp, G_TYPE_STRING);
70 g_value_transform (value, &tmp);
71 g_value_unset (&tmp);
74 static void
75 test_flags_basic (void)
77 GType type;
78 GFlagsClass *class;
79 GFlagsValue *val;
80 GValue value = G_VALUE_INIT;
82 type = g_flags_register_static ("MyFlags", my_flag_values);
84 g_value_init (&value, type);
85 g_assert (G_VALUE_HOLDS_FLAGS (&value));
87 g_value_set_flags (&value, 2|8);
88 g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
90 class = g_type_class_ref (type);
92 g_assert_cmpint (class->mask, ==, 1|2|8);
93 g_assert_cmpint (class->n_values, ==, 4);
95 val = g_flags_get_first_value (class, 2|8);
96 g_assert (val != NULL);
97 g_assert_cmpstr (val->value_name, ==, "the second flag");
98 val = g_flags_get_first_value (class, 16);
99 g_assert (val == NULL);
101 val = g_flags_get_value_by_name (class, "the third flag");
102 g_assert (val != NULL);
103 g_assert_cmpint (val->value, ==, 8);
104 val = g_flags_get_value_by_name (class, "the color purple");
105 g_assert (val == NULL);
107 val = g_flags_get_value_by_nick (class, "one");
108 g_assert (val != NULL);
109 g_assert_cmpint (val->value, ==, 1);
110 val = g_flags_get_value_by_nick (class, "purple");
111 g_assert (val == NULL);
113 test_flags_transform_to_string (&value);
114 g_value_unset (&value);
116 g_type_class_unref (class);
120 main (int argc, char *argv[])
122 g_test_init (&argc, &argv, NULL);
124 g_test_add_func ("/enum/basic", test_enum_basic);
125 g_test_add_func ("/flags/basic", test_flags_basic);
127 return g_test_run ();