enums: Fix leaks in tests
[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   g_type_class_unref (class);
53 }
54
55 static const GFlagsValue my_flag_values[] =
56 {
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 }
62 };
63
64 static void
65 test_flags_transform_to_string (const GValue *value)
66 {
67   GValue tmp = G_VALUE_INIT;
68
69   g_value_init (&tmp, G_TYPE_STRING);
70   g_value_transform (value, &tmp);
71   g_value_unset (&tmp);
72 }
73
74 static void
75 test_flags_basic (void)
76 {
77   GType type;
78   GFlagsClass *class;
79   GFlagsValue *val;
80   GValue value = G_VALUE_INIT;
81
82   type = g_flags_register_static ("MyFlags", my_flag_values);
83
84   g_value_init (&value, type);
85   g_assert (G_VALUE_HOLDS_FLAGS (&value));
86
87   g_value_set_flags (&value, 2|8);
88   g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
89
90   class = g_type_class_ref (type);
91
92   g_assert_cmpint (class->mask, ==, 1|2|8);
93   g_assert_cmpint (class->n_values, ==, 4);
94
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);
100
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);
106
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);
112
113   test_flags_transform_to_string (&value);
114   g_value_unset (&value);
115
116   g_type_class_unref (class);
117 }
118
119 int
120 main (int argc, char *argv[])
121 {
122   g_test_init (&argc, &argv, NULL);
123
124   g_test_add_func ("/enum/basic", test_enum_basic);
125   g_test_add_func ("/flags/basic", test_flags_basic);
126
127   return g_test_run ();
128 }