summaryrefslogtreecommitdiff
path: root/gobject/tests/flags.c
blob: afe3c2e8c449122dc036109be62990f6bbdf9c4e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* flags.c
 * Copyright (C) 2018 Arthur Demchenkov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <glib-object.h>

/* Check that validation of flags works on architectures where
 * #gint and #glong are different sizes, as the flags are cast
 * between types a few times.
 *
 * See: https://gitlab.gnome.org/GNOME/glib/issues/1572
 */

enum {
  PROP_FLAGS = 1
};

typedef struct _GTest GTest;
typedef struct _GTestClass GTestClass;

typedef enum {
  NO_FLAG      = 0,
  LOWEST_FLAG  = 1,
  HIGHEST_FLAG = 1 << 31
} MyFlagsEnum;

struct _GTest {
  GObject object;
  MyFlagsEnum flags;
};

struct _GTestClass {
  GObjectClass parent_class;
};

static GType my_test_get_type (void);
static GType my_test_flags_get_type (void);

#define G_TYPE_TEST (my_test_get_type())
#define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)

static void my_test_class_init (GTestClass * klass);
static void my_test_init (GTest * test);
static void my_test_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec);
static void my_test_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec);

static GType
my_test_flags_get_type (void)
{
  static GType flags_type = 0;

  if (G_UNLIKELY(flags_type == 0))
    {
      static const GFlagsValue values[] = {
	{ LOWEST_FLAG,  "LOWEST_FLAG",  "lowest" },
	{ HIGHEST_FLAG, "HIGHEST_FLAG", "highest" },
	{ 0, NULL, NULL }
      };

      flags_type = g_flags_register_static (g_intern_static_string ("GTestFlags"), values);
    }
  return flags_type;
}

static void
my_test_class_init (GTestClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->get_property = my_test_get_property;
  gobject_class->set_property = my_test_set_property;

  g_object_class_install_property (gobject_class, 1,
				   g_param_spec_flags ("flags",
						       "Flags",
						       "Flags test property",
						       my_test_flags_get_type(), 0,
						       G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}

static void my_test_init (GTest *test)
{
}

static void
my_test_get_property (GObject    *object,
		      guint       prop_id,
		      GValue     *value,
		      GParamSpec *pspec)
{
  GTest *test = MY_TEST (object);

  switch (prop_id)
    {
    case PROP_FLAGS:
      g_value_set_flags (value, test->flags);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
my_test_set_property (GObject      *object,
		      guint         prop_id,
		      const GValue *value,
		      GParamSpec   *pspec)
{
  GTest *test = MY_TEST (object);

  switch (prop_id)
    {
    case PROP_FLAGS:
      test->flags = g_value_get_flags (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
check_flags_validation (void)
{
  guint test_flags[] = {
    NO_FLAG,
    LOWEST_FLAG,
    HIGHEST_FLAG,
    LOWEST_FLAG | HIGHEST_FLAG
  };
  guint flag_read;
  gsize i;

  for (i = 0; i < G_N_ELEMENTS (test_flags); i++)
    {
      guint flag_set = test_flags[i];
      GObject *test = g_object_new (G_TYPE_TEST,
				    "flags", flag_set,
				    NULL);

      g_object_get (test, "flags", &flag_read, NULL);

      /* This check will fail in case of gint -> glong conversion
       * in value_flags_enum_collect_value() */
      g_assert_cmpint (flag_read, ==, flag_set);

      g_object_unref (test);
    }
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);
  g_test_add_func ("/gobject/flags/validate", check_flags_validation);
  return g_test_run ();
}