[glib/ebassi/enum-type-macros: 2/3] Test the newly added G_DEFINE macros for enumeration types




commit 07b45afaea56cf5eb926f79ca34bd266bb96de01
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Jun 29 14:05:22 2022 +0100

    Test the newly added G_DEFINE macros for enumeration types

 gobject/tests/enums.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
---
diff --git a/gobject/tests/enums.c b/gobject/tests/enums.c
index 3b96417909..447d76c0fc 100644
--- a/gobject/tests/enums.c
+++ b/gobject/tests/enums.c
@@ -158,13 +158,86 @@ test_flags_basic (void)
   g_type_class_unref (class);
 }
 
+typedef enum {
+  TEST_ENUM_FIRST_VALUE,
+  TEST_ENUM_SECOND_VALUE,
+  TEST_ENUM_THIRD_VALUE
+} TestEnum;
+
+GType test_enum_get_type (void);
+
+G_DEFINE_ENUM_TYPE (TestEnum, test_enum,
+  G_DEFINE_ENUM_VALUE (TEST_ENUM_FIRST_VALUE, "first-value")
+  G_DEFINE_ENUM_VALUE (TEST_ENUM_SECOND_VALUE, "second-value")
+  G_DEFINE_ENUM_VALUE (TEST_ENUM_THIRD_VALUE, "third-value"))
+
+static void
+test_enum_define_type (void)
+{
+  GEnumClass *class = g_type_class_ref (test_enum_get_type ());
+  GEnumValue *val;
+
+  g_assert_cmpint (class->minimum, ==, 0);
+  g_assert_cmpint (class->maximum, ==, 2);
+  g_assert_cmpint (class->n_values, ==, 3);
+
+  val = g_enum_get_value (class, 2);
+  g_assert (val != NULL);
+  g_assert_cmpstr (val->value_nick, ==, "third-value");
+  val = g_enum_get_value (class, 15);
+  g_assert (val == NULL);
+
+  g_type_class_unref (class);
+}
+
+typedef enum {
+  TEST_FLAGS_DEFAULT = 0,
+  TEST_FLAGS_FIRST   = 1 << 0,
+  TEST_FLAGS_SECOND  = 1 << 1,
+  TEST_FLAGS_THIRD   = 1 << 2
+} TestFlags;
+
+GType test_flags_get_type (void);
+
+G_DEFINE_FLAGS_TYPE (TestFlags, test_flags,
+  G_DEFINE_ENUM_VALUE (TEST_FLAGS_DEFAULT, "default")
+  G_DEFINE_ENUM_VALUE (TEST_FLAGS_FIRST, "first")
+  G_DEFINE_ENUM_VALUE (TEST_FLAGS_SECOND, "second")
+  G_DEFINE_ENUM_VALUE (TEST_FLAGS_THIRD, "third"))
+
+static void
+test_flags_define_type (void)
+{
+  GFlagsClass *class = g_type_class_ref (test_flags_get_type ());
+  GFlagsValue *val;
+  char *to_string;
+
+  g_assert_cmpint (class->mask, ==, 1 | 2 | 4);
+  g_assert_cmpint (class->n_values, ==, 4);
+
+  val = g_flags_get_first_value (class, 2|4);
+  g_assert (val != NULL);
+  g_assert_cmpstr (val->value_nick, ==, "second");
+
+  val = g_flags_get_first_value (class, 8);
+  g_assert (val == NULL);
+
+  to_string = g_flags_to_string (test_flags_get_type (), 0);
+  g_assert_cmpstr (to_string, ==, "TEST_FLAGS_DEFAULT");
+  g_free (to_string);
+
+  g_type_class_unref (class);
+}
+
 int
 main (int argc, char *argv[])
 {
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/enum/basic", test_enum_basic);
+  g_test_add_func ("/enum/define-type", test_enum_define_type);
   g_test_add_func ("/flags/basic", test_flags_basic);
+  g_test_add_func ("/flags/define-type", test_flags_define_type);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]