File: | /rootdir/_build/../src/refptr-test.cc |
Warning: | line 81, column 9 Method called on moved-from object 'ptr1' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * Copyright © 2018 Christian Persch | |||
3 | * | |||
4 | * This library is free software; you can redistribute it and/or | |||
5 | * modify it under the terms of the GNU Lesser General Public | |||
6 | * License as published by the Free Software Foundation; either | |||
7 | * version 3 of the License, or (at your option) any later version. | |||
8 | * | |||
9 | * This library is distributed in the hope that it will be useful, | |||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
12 | * Lesser General Public License for more details. | |||
13 | * | |||
14 | * You should have received a copy of the GNU General Public License | |||
15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | |||
16 | */ | |||
17 | ||||
18 | #include "config.h" | |||
19 | ||||
20 | #include "refptr.hh" | |||
21 | ||||
22 | /* Test object */ | |||
23 | ||||
24 | typedef struct _TestObject TestObject; | |||
25 | typedef struct _TestObjectClass TestObjectClass; | |||
26 | ||||
27 | struct _TestObject { | |||
28 | GObject parent_instance; | |||
29 | }; | |||
30 | ||||
31 | struct _TestObjectClass{ | |||
32 | GObjectClass parent_class; | |||
33 | }; | |||
34 | ||||
35 | static GType test_object_get_type(void); | |||
36 | ||||
37 | G_DEFINE_TYPE(TestObject, test_object, G_TYPE_OBJECT)static void test_object_init (TestObject *self); static void test_object_class_init (TestObjectClass *klass); static GType test_object_get_type_once (void); static gpointer test_object_parent_class = __null; static gint TestObject_private_offset; static void test_object_class_intern_init (gpointer klass) { test_object_parent_class = g_type_class_peek_parent (klass); if (TestObject_private_offset != 0) g_type_class_adjust_private_offset (klass, &TestObject_private_offset); test_object_class_init ((TestObjectClass*) klass); } __attribute__ ((__unused__)) static inline gpointer test_object_get_instance_private (TestObject *self) { return (((gpointer) ((guint8*) (self) + (glong) (TestObject_private_offset )))); } GType test_object_get_type (void) { static gsize static_g_define_type_id = 0; if ((__extension__ ({ static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); (void ) (0 ? (gpointer) *(&static_g_define_type_id) : __null); ( !(__extension__ ({ static_assert (sizeof *(&static_g_define_type_id ) == sizeof (gpointer), "Expression evaluates to false"); gpointer gapg_temp_newval; gpointer *gapg_temp_atomic = (gpointer *)( &static_g_define_type_id); __atomic_load (gapg_temp_atomic , &gapg_temp_newval, 5); gapg_temp_newval; })) && g_once_init_enter (&static_g_define_type_id)); }))) { GType g_define_type_id = test_object_get_type_once (); (__extension__ ({ static_assert (sizeof *(&static_g_define_type_id) == sizeof (gpointer), "Expression evaluates to false"); 0 ? (void) (*( &static_g_define_type_id) = (g_define_type_id)) : (void) 0 ; g_once_init_leave ((&static_g_define_type_id), (gsize) ( g_define_type_id)); })); } return static_g_define_type_id; } __attribute__ ((__noinline__)) static GType test_object_get_type_once (void ) { GType g_define_type_id = g_type_register_static_simple (( (GType) ((20) << (2))), g_intern_static_string ("TestObject" ), sizeof (TestObjectClass), (GClassInitFunc)(void (*)(void)) test_object_class_intern_init, sizeof (TestObject), (GInstanceInitFunc )(void (*)(void)) test_object_init, (GTypeFlags) 0); { {{};} } return g_define_type_id; } | |||
38 | ||||
39 | static void | |||
40 | test_object_init(TestObject* object) | |||
41 | { | |||
42 | } | |||
43 | ||||
44 | static void | |||
45 | test_object_finalize(GObject *object) | |||
46 | { | |||
47 | G_OBJECT_CLASS(test_object_parent_class)((((GObjectClass*) (void *) g_type_check_class_cast ((GTypeClass *) ((test_object_parent_class)), (((GType) ((20) << (2) )))))))->finalize(object); | |||
48 | } | |||
49 | ||||
50 | static void | |||
51 | test_object_class_init(TestObjectClass* klass) | |||
52 | { | |||
53 | GObjectClass* gobject_class = G_OBJECT_CLASS(klass)((((GObjectClass*) (void *) g_type_check_class_cast ((GTypeClass *) ((klass)), (((GType) ((20) << (2)))))))); | |||
54 | gobject_class->finalize = test_object_finalize; | |||
55 | } | |||
56 | ||||
57 | static TestObject* | |||
58 | test_object_new(void) | |||
59 | { | |||
60 | return reinterpret_cast<TestObject*>(g_object_new(test_object_get_type(), | |||
61 | nullptr)); | |||
62 | } | |||
63 | ||||
64 | /* Tests */ | |||
65 | ||||
66 | typedef union { | |||
67 | TestObject* obj; | |||
68 | void* ptr; | |||
69 | } obj_t; | |||
70 | ||||
71 | static void | |||
72 | test_glib_refptr(void) | |||
73 | { | |||
74 | obj_t obj1; | |||
75 | obj1.obj = test_object_new(); | |||
76 | g_object_add_weak_pointer(G_OBJECT(obj1.obj)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((obj1.obj)), (((GType) ((20) << (2)))))))), &obj1.ptr); | |||
77 | auto ptr1 = bte::glib::RefPtr<TestObject>{obj1.obj}; | |||
78 | g_assert_true(ptr1.get() == obj1.obj)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if (ptr1.get() == obj1.obj) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 78, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr1.get() == obj1.obj" "' should be TRUE"); } while (0); | |||
| ||||
79 | ||||
80 | auto ptr2 = std::move(ptr1); | |||
81 | g_assert_true(ptr1.get() == nullptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if (ptr1.get() == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 81, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr1.get() == nullptr" "' should be TRUE"); } while ( 0); | |||
| ||||
82 | g_assert_true(ptr2.get() == obj1.obj)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if (ptr2.get() == obj1.obj) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 82, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr2.get() == obj1.obj" "' should be TRUE"); } while (0); | |||
83 | ||||
84 | obj_t obj2; | |||
85 | obj2.obj = test_object_new(); | |||
86 | g_object_add_weak_pointer(G_OBJECT(obj2.obj)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((obj2.obj)), (((GType) ((20) << (2)))))))), &obj2.ptr); | |||
87 | g_assert_nonnull(obj2.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj2.ptr) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 87, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj2.ptr" "' should not be nullptr"); } while (0); | |||
88 | ptr2.reset(obj2.obj); | |||
89 | g_assert_null(obj1.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj1.ptr) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 89, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj1.ptr" "' should be nullptr"); } while (0); | |||
90 | g_assert_true(ptr2.get() == obj2.obj)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if (ptr2.get() == obj2.obj) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 90, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr2.get() == obj2.obj" "' should be TRUE"); } while (0); | |||
91 | g_assert_nonnull(obj2.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj2.ptr) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 91, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj2.ptr" "' should not be nullptr"); } while (0); | |||
92 | ||||
93 | ptr2 = nullptr; | |||
94 | g_assert_null(obj2.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj2.ptr) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 94, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj2.ptr" "' should be nullptr"); } while (0); | |||
95 | ||||
96 | obj_t obj3; | |||
97 | obj3.obj = test_object_new(); | |||
98 | g_object_add_weak_pointer(G_OBJECT(obj3.obj)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((obj3.obj)), (((GType) ((20) << (2)))))))), &obj3.ptr); | |||
99 | g_assert_nonnull(obj3.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj3.ptr) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 99, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj3.ptr" "' should not be nullptr"); } while (0); | |||
100 | auto ptr3 = bte::glib::RefPtr<TestObject>{obj3.obj}; | |||
101 | TestObject* obj4 = ptr3.release(); | |||
102 | g_assert_null(ptr3.get())do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((ptr3.get()) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 102, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr3.get()" "' should be nullptr"); } while (0); | |||
103 | g_assert_nonnull(obj4)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj4) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 103, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj4" "' should not be nullptr"); } while (0); | |||
104 | g_object_unref(obj4); | |||
105 | g_assert_null(obj3.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj3.ptr) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 105, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj3.ptr" "' should be nullptr"); } while (0); | |||
106 | ||||
107 | obj_t obj5; | |||
108 | obj5.obj = test_object_new(); | |||
109 | g_object_add_weak_pointer(G_OBJECT(obj5.obj)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((obj5.obj)), (((GType) ((20) << (2)))))))), &obj5.ptr); | |||
110 | g_assert_nonnull(obj5.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj5.ptr) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 110, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj5.ptr" "' should not be nullptr"); } while (0); | |||
111 | bte::glib::RefPtr<TestObject> ptr5{obj5.obj}; | |||
112 | ||||
113 | obj_t obj6; | |||
114 | obj6.obj = test_object_new(); | |||
115 | g_object_add_weak_pointer(G_OBJECT(obj6.obj)((((GObject*) (void *) g_type_check_instance_cast ((GTypeInstance *) ((obj6.obj)), (((GType) ((20) << (2)))))))), &obj6.ptr); | |||
116 | g_assert_nonnull(obj6.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj6.ptr) != nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 116, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj6.ptr" "' should not be nullptr"); } while (0); | |||
117 | ||||
118 | ptr5.reset(obj6.obj); | |||
119 | g_assert_null(obj5.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj5.ptr) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 119, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj5.ptr" "' should be nullptr"); } while (0); | |||
120 | ||||
121 | ptr5.reset(); | |||
122 | g_assert_null(obj6.ptr)do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((obj6.ptr) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 122, ((const char*) (__PRETTY_FUNCTION__ )), "'" "obj6.ptr" "' should be nullptr"); } while (0); | |||
123 | g_assert_null(ptr5.get())do { if (__builtin_expect (__extension__ ({ int _g_boolean_var_ ; if ((ptr5.get()) == nullptr) _g_boolean_var_ = 1; else _g_boolean_var_ = 0; _g_boolean_var_; }), 1)) ; else g_assertion_message ((( gchar*) 0), "../src/refptr-test.cc", 123, ((const char*) (__PRETTY_FUNCTION__ )), "'" "ptr5.get()" "' should be nullptr"); } while (0); | |||
124 | } | |||
125 | ||||
126 | int | |||
127 | main(int argc, | |||
128 | char* argv[]) | |||
129 | { | |||
130 | g_test_init(&argc, &argv, nullptr); | |||
131 | ||||
132 | g_test_add_func("/bte/glib/refptr", test_glib_refptr); | |||
133 | ||||
134 | return g_test_run(); | |||
135 | } |