summaryrefslogtreecommitdiff
path: root/gobject/tests/enums.c
blob: f4076f2adc7fde0a8392290bbc3cc3441881ede0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <glib-object.h>

static const GEnumValue my_enum_values[] =
{
  { 1, "the first value", "one" },
  { 2, "the second value", "two" },
  { 3, "the third value", "three" },
  { 0, NULL, NULL }
};

static void
test_enum_basic (void)
{
  GType type;
  GEnumClass *class;
  GEnumValue *val;
  GValue value = { 0, };

  type = g_enum_register_static ("MyEnum", my_enum_values);

  g_value_init (&value, type);
  g_assert (G_VALUE_HOLDS_ENUM (&value));

  g_value_set_enum (&value, 2);
  g_assert_cmpint (g_value_get_enum (&value), ==, 2);
  g_value_unset (&value);

  class = g_type_class_ref (type);

  g_assert_cmpint (class->minimum, ==, 1);
  g_assert_cmpint (class->maximum, ==, 3);
  g_assert_cmpint (class->n_values, ==, 3);

  val = g_enum_get_value (class, 2);
  g_assert (val != NULL);
  g_assert_cmpstr (val->value_name, ==, "the second value");
  val = g_enum_get_value (class, 15);
  g_assert (val == NULL);

  val = g_enum_get_value_by_name (class, "the third value");
  g_assert (val != NULL);
  g_assert_cmpint (val->value, ==, 3);
  val = g_enum_get_value_by_name (class, "the color purple");
  g_assert (val == NULL);

  val = g_enum_get_value_by_nick (class, "one");
  g_assert (val != NULL);
  g_assert_cmpint (val->value, ==, 1);
  val = g_enum_get_value_by_nick (class, "purple");
  g_assert (val == NULL);
}

static const GFlagsValue my_flag_values[] =
{
  { 1, "the first flag", "one" },
  { 2, "the second flag", "two" },
  { 8, "the third flag", "three" },
  { 0, NULL, NULL }
};


static void
test_flags_basic (void)
{
  GType type;
  GFlagsClass *class;
  GFlagsValue *val;
  GValue value = { 0, };

  type = g_flags_register_static ("MyFlags", my_flag_values);

  g_value_init (&value, type);
  g_assert (G_VALUE_HOLDS_FLAGS (&value));

  g_value_set_flags (&value, 2|8);
  g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
  g_value_unset (&value);

  class = g_type_class_ref (type);

  g_assert_cmpint (class->mask, ==, 1|2|8);
  g_assert_cmpint (class->n_values, ==, 3);

  val = g_flags_get_first_value (class, 2|8);
  g_assert (val != NULL);
  g_assert_cmpstr (val->value_name, ==, "the second flag");
  val = g_flags_get_first_value (class, 16);
  g_assert (val == NULL);

  val = g_flags_get_value_by_name (class, "the third flag");
  g_assert (val != NULL);
  g_assert_cmpint (val->value, ==, 8);
  val = g_flags_get_value_by_name (class, "the color purple");
  g_assert (val == NULL);

  val = g_flags_get_value_by_nick (class, "one");
  g_assert (val != NULL);
  g_assert_cmpint (val->value, ==, 1);
  val = g_flags_get_value_by_nick (class, "purple");
  g_assert (val == NULL);
}

int
main (int argc, char *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/enum/basic", test_enum_basic);
  g_test_add_func ("/flags/basic", test_flags_basic);

  return g_test_run ();
}