LCOV - code coverage report
Current view: top level - glib/gobject/tests - enums.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 124 124 100.0 %
Date: 2024-04-30 05:17:35 Functions: 8 8 100.0 %
Branches: 8 12 66.7 %

           Branch data     Line data    Source code
       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                 :          1 : test_enum_basic (void)
      13                 :            : {
      14                 :            :   GType type;
      15                 :            :   GEnumClass *class;
      16                 :            :   GEnumValue *val;
      17                 :          1 :   GValue value = G_VALUE_INIT;
      18                 :            :   gchar *to_string;
      19                 :            : 
      20                 :          1 :   type = g_enum_register_static ("MyEnum", my_enum_values);
      21                 :            : 
      22                 :          1 :   g_value_init (&value, type);
      23                 :          1 :   g_assert_true (G_VALUE_HOLDS_ENUM (&value));
      24                 :            : 
      25                 :          1 :   g_value_set_enum (&value, 2);
      26                 :          1 :   g_assert_cmpint (g_value_get_enum (&value), ==, 2);
      27                 :          1 :   g_value_unset (&value);
      28                 :            : 
      29                 :          1 :   class = g_type_class_ref (type);
      30                 :            : 
      31                 :          1 :   g_assert_cmpint (class->minimum, ==, 1);
      32                 :          1 :   g_assert_cmpint (class->maximum, ==, 3);
      33                 :          1 :   g_assert_cmpint (class->n_values, ==, 3);
      34                 :            : 
      35                 :          1 :   val = g_enum_get_value (class, 2);
      36                 :          1 :   g_assert_nonnull (val);
      37                 :          1 :   g_assert_cmpstr (val->value_name, ==, "the second value");
      38                 :          1 :   val = g_enum_get_value (class, 15);
      39                 :          1 :   g_assert_null (val);
      40                 :            : 
      41                 :          1 :   val = g_enum_get_value_by_name (class, "the third value");
      42                 :          1 :   g_assert_nonnull (val);
      43                 :          1 :   g_assert_cmpint (val->value, ==, 3);
      44                 :          1 :   val = g_enum_get_value_by_name (class, "the color purple");
      45                 :          1 :   g_assert_null (val);
      46                 :            : 
      47                 :          1 :   val = g_enum_get_value_by_nick (class, "one");
      48                 :          1 :   g_assert_nonnull (val);
      49                 :          1 :   g_assert_cmpint (val->value, ==, 1);
      50                 :          1 :   val = g_enum_get_value_by_nick (class, "purple");
      51                 :          1 :   g_assert_null (val);
      52                 :            : 
      53                 :          1 :   to_string = g_enum_to_string (type, 2);
      54                 :          1 :   g_assert_cmpstr (to_string, ==, "the second value");
      55                 :          1 :   g_free (to_string);
      56                 :            : 
      57                 :          1 :   to_string = g_enum_to_string (type, 15);
      58                 :          1 :   g_assert_cmpstr (to_string, ==, "15");
      59                 :          1 :   g_free (to_string);
      60                 :            : 
      61                 :          1 :   g_type_class_unref (class);
      62                 :          1 : }
      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                 :          1 : test_flags_transform_to_string (const GValue *value)
      81                 :            : {
      82                 :          1 :   GValue tmp = G_VALUE_INIT;
      83                 :            : 
      84                 :          1 :   g_value_init (&tmp, G_TYPE_STRING);
      85                 :          1 :   g_value_transform (value, &tmp);
      86                 :          1 :   g_value_unset (&tmp);
      87                 :          1 : }
      88                 :            : 
      89                 :            : static void
      90                 :          1 : test_flags_basic (void)
      91                 :            : {
      92                 :            :   GType type, no_default_type;
      93                 :            :   GFlagsClass *class;
      94                 :            :   GFlagsValue *val;
      95                 :          1 :   GValue value = G_VALUE_INIT;
      96                 :            :   gchar *to_string;
      97                 :            : 
      98                 :          1 :   type = g_flags_register_static ("MyFlags", my_flag_values);
      99                 :          1 :   no_default_type = g_flags_register_static ("NoDefaultFlags",
     100                 :            :                                              no_default_flag_values);
     101                 :            : 
     102                 :          1 :   g_value_init (&value, type);
     103                 :          1 :   g_assert_true (G_VALUE_HOLDS_FLAGS (&value));
     104                 :            : 
     105                 :          1 :   g_value_set_flags (&value, 2|8);
     106                 :          1 :   g_assert_cmpint (g_value_get_flags (&value), ==, 2|8);
     107                 :            : 
     108                 :          1 :   class = g_type_class_ref (type);
     109                 :            : 
     110                 :          1 :   g_assert_cmpint (class->mask, ==, 1|2|8);
     111                 :          1 :   g_assert_cmpint (class->n_values, ==, 4);
     112                 :            : 
     113                 :          1 :   val = g_flags_get_first_value (class, 2|8);
     114                 :          1 :   g_assert_nonnull (val);
     115                 :          1 :   g_assert_cmpstr (val->value_name, ==, "the second flag");
     116                 :          1 :   val = g_flags_get_first_value (class, 16);
     117                 :          1 :   g_assert_null (val);
     118                 :            : 
     119                 :          1 :   val = g_flags_get_value_by_name (class, "the third flag");
     120                 :          1 :   g_assert_nonnull (val);
     121                 :          1 :   g_assert_cmpint (val->value, ==, 8);
     122                 :          1 :   val = g_flags_get_value_by_name (class, "the color purple");
     123                 :          1 :   g_assert_null (val);
     124                 :            : 
     125                 :          1 :   val = g_flags_get_value_by_nick (class, "one");
     126                 :          1 :   g_assert_nonnull (val);
     127                 :          1 :   g_assert_cmpint (val->value, ==, 1);
     128                 :          1 :   val = g_flags_get_value_by_nick (class, "purple");
     129                 :          1 :   g_assert_null (val);
     130                 :            : 
     131                 :          1 :   test_flags_transform_to_string (&value);
     132                 :          1 :   g_value_unset (&value);
     133                 :            : 
     134                 :          1 :   to_string = g_flags_to_string (type, 1|8);
     135                 :          1 :   g_assert_cmpstr (to_string, ==, "the first flag | the third flag");
     136                 :          1 :   g_free (to_string);
     137                 :            : 
     138                 :          1 :   to_string = g_flags_to_string (type, 0);
     139                 :          1 :   g_assert_cmpstr (to_string, ==, "no flags");
     140                 :          1 :   g_free (to_string);
     141                 :            : 
     142                 :          1 :   to_string = g_flags_to_string (type, 16);
     143                 :          1 :   g_assert_cmpstr (to_string, ==, "0x10");
     144                 :          1 :   g_free (to_string);
     145                 :            : 
     146                 :          1 :   to_string = g_flags_to_string (type, 1|16);
     147                 :          1 :   g_assert_cmpstr (to_string, ==, "the first flag | 0x10");
     148                 :          1 :   g_free (to_string);
     149                 :            : 
     150                 :          1 :   to_string = g_flags_to_string (no_default_type, 0);
     151                 :          1 :   g_assert_cmpstr (to_string, ==, "0x0");
     152                 :          1 :   g_free (to_string);
     153                 :            : 
     154                 :          1 :   to_string = g_flags_to_string (no_default_type, 16);
     155                 :          1 :   g_assert_cmpstr (to_string, ==, "0x10");
     156                 :          1 :   g_free (to_string);
     157                 :            : 
     158                 :          1 :   g_type_class_unref (class);
     159                 :          1 : }
     160                 :            : 
     161                 :            : typedef enum {
     162                 :            :   TEST_ENUM_FIRST_VALUE,
     163                 :            :   TEST_ENUM_SECOND_VALUE,
     164                 :            :   TEST_ENUM_THIRD_VALUE
     165                 :            : } TestEnum;
     166                 :            : 
     167                 :            : GType test_enum_get_type (void);
     168                 :            : 
     169   [ +  -  +  -  :          1 : G_DEFINE_ENUM_TYPE (TestEnum, test_enum,
                   +  - ]
     170                 :            :   G_DEFINE_ENUM_VALUE (TEST_ENUM_FIRST_VALUE, "first-value"),
     171                 :            :   G_DEFINE_ENUM_VALUE (TEST_ENUM_SECOND_VALUE, "second-value"),
     172                 :            :   G_DEFINE_ENUM_VALUE (TEST_ENUM_THIRD_VALUE, "third-value"))
     173                 :            : 
     174                 :            : static void
     175                 :          1 : test_enum_define_type (void)
     176                 :            : {
     177                 :          1 :   GEnumClass *class = g_type_class_ref (test_enum_get_type ());
     178                 :            :   GEnumValue *val;
     179                 :            : 
     180                 :          1 :   g_assert_cmpint (class->minimum, ==, 0);
     181                 :          1 :   g_assert_cmpint (class->maximum, ==, 2);
     182                 :          1 :   g_assert_cmpint (class->n_values, ==, 3);
     183                 :            : 
     184                 :          1 :   val = g_enum_get_value (class, 2);
     185                 :          1 :   g_assert_nonnull (val);
     186                 :          1 :   g_assert_cmpstr (val->value_nick, ==, "third-value");
     187                 :          1 :   val = g_enum_get_value (class, 15);
     188                 :          1 :   g_assert_null (val);
     189                 :            : 
     190                 :          1 :   g_type_class_unref (class);
     191                 :          1 : }
     192                 :            : 
     193                 :            : typedef enum {
     194                 :            :   TEST_FLAGS_DEFAULT = 0,
     195                 :            :   TEST_FLAGS_FIRST   = 1 << 0,
     196                 :            :   TEST_FLAGS_SECOND  = 1 << 1,
     197                 :            :   TEST_FLAGS_THIRD   = 1 << 2
     198                 :            : } TestFlags;
     199                 :            : 
     200                 :            : GType test_flags_get_type (void);
     201                 :            : 
     202   [ +  +  +  -  :          2 : G_DEFINE_FLAGS_TYPE (TestFlags, test_flags,
                   +  + ]
     203                 :            :   G_DEFINE_ENUM_VALUE (TEST_FLAGS_DEFAULT, "default"),
     204                 :            :   G_DEFINE_ENUM_VALUE (TEST_FLAGS_FIRST, "first"),
     205                 :            :   G_DEFINE_ENUM_VALUE (TEST_FLAGS_SECOND, "second"),
     206                 :            :   G_DEFINE_ENUM_VALUE (TEST_FLAGS_THIRD, "third"))
     207                 :            : 
     208                 :            : static void
     209                 :          1 : test_flags_define_type (void)
     210                 :            : {
     211                 :          1 :   GFlagsClass *class = g_type_class_ref (test_flags_get_type ());
     212                 :            :   GFlagsValue *val;
     213                 :            :   char *to_string;
     214                 :            : 
     215                 :          1 :   g_assert_cmpint (class->mask, ==, 1 | 2 | 4);
     216                 :          1 :   g_assert_cmpint (class->n_values, ==, 4);
     217                 :            : 
     218                 :          1 :   val = g_flags_get_first_value (class, 2|4);
     219                 :          1 :   g_assert_nonnull (val);
     220                 :          1 :   g_assert_cmpstr (val->value_nick, ==, "second");
     221                 :            : 
     222                 :          1 :   val = g_flags_get_first_value (class, 8);
     223                 :          1 :   g_assert_null (val);
     224                 :            : 
     225                 :          1 :   to_string = g_flags_to_string (test_flags_get_type (), 0);
     226                 :          1 :   g_assert_cmpstr (to_string, ==, "TEST_FLAGS_DEFAULT");
     227                 :          1 :   g_free (to_string);
     228                 :            : 
     229                 :          1 :   g_type_class_unref (class);
     230                 :          1 : }
     231                 :            : 
     232                 :            : int
     233                 :          1 : main (int argc, char *argv[])
     234                 :            : {
     235                 :          1 :   g_test_init (&argc, &argv, NULL);
     236                 :            : 
     237                 :          1 :   g_test_add_func ("/enum/basic", test_enum_basic);
     238                 :          1 :   g_test_add_func ("/enum/define-type", test_enum_define_type);
     239                 :          1 :   g_test_add_func ("/flags/basic", test_flags_basic);
     240                 :          1 :   g_test_add_func ("/flags/define-type", test_flags_define_type);
     241                 :            : 
     242                 :          1 :   return g_test_run ();
     243                 :            : }

Generated by: LCOV version 1.14