2 * Copyright (C) 2018 Arthur Demchenkov
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see
18 * <http://www.gnu.org/licenses/>.
21 #include <glib-object.h>
23 /* Check that validation of flags works on architectures where
24 * #gint and #glong are different sizes, as the flags are cast
25 * between types a few times.
27 * See: https://gitlab.gnome.org/GNOME/glib/issues/1572
34 typedef struct _GTest GTest;
35 typedef struct _GTestClass GTestClass;
40 HIGHEST_FLAG = 1 << 31
49 GObjectClass parent_class;
52 static GType my_test_get_type (void);
53 static GType my_test_flags_get_type (void);
55 #define G_TYPE_TEST (my_test_get_type())
56 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
57 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
59 static void my_test_class_init (GTestClass * klass);
60 static void my_test_init (GTest * test);
61 static void my_test_get_property (GObject *object,
65 static void my_test_set_property (GObject *object,
71 my_test_flags_get_type (void)
73 static GType flags_type = 0;
75 if (G_UNLIKELY(flags_type == 0))
77 static const GFlagsValue values[] = {
78 { LOWEST_FLAG, "LOWEST_FLAG", "lowest" },
79 { HIGHEST_FLAG, "HIGHEST_FLAG", "highest" },
83 flags_type = g_flags_register_static (g_intern_static_string ("GTestFlags"), values);
89 my_test_class_init (GTestClass *klass)
91 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93 gobject_class->get_property = my_test_get_property;
94 gobject_class->set_property = my_test_set_property;
96 g_object_class_install_property (gobject_class, 1,
97 g_param_spec_flags ("flags",
99 "Flags test property",
100 my_test_flags_get_type(), 0,
101 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
104 static void my_test_init (GTest *test)
109 my_test_get_property (GObject *object,
114 GTest *test = MY_TEST (object);
119 g_value_set_flags (value, test->flags);
122 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
128 my_test_set_property (GObject *object,
133 GTest *test = MY_TEST (object);
138 test->flags = g_value_get_flags (value);
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 check_flags_validation (void)
149 guint test_flags[] = {
153 LOWEST_FLAG | HIGHEST_FLAG
158 for (i = 0; i < G_N_ELEMENTS (test_flags); i++)
160 guint flag_set = test_flags[i];
161 GObject *test = g_object_new (G_TYPE_TEST,
165 g_object_get (test, "flags", &flag_read, NULL);
167 /* This check will fail in case of gint -> glong conversion
168 * in value_flags_enum_collect_value() */
169 g_assert_cmpint (flag_read, ==, flag_set);
171 g_object_unref (test);
176 main (int argc, char **argv)
178 g_test_init (&argc, &argv, NULL);
179 g_test_add_func ("/gobject/flags/validate", check_flags_validation);
180 return g_test_run ();