6426d43a71f9ce82efbb69d9a3f97a8ecc69fcb2
[platform/upstream/glib.git] / glib / tests / cxx.cpp
1 /*
2  * Copyright 2020 Xavier Claessens
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 2.1 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 Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <glib.h>
19
20 typedef struct
21 {
22   int dummy;
23 } MyObject;
24
25 static void
26 test_typeof (void)
27 {
28 #if __cplusplus >= 201103L
29   // Test that with C++11 we don't get those kind of errors:
30   // error: invalid conversion from ‘gpointer’ {aka ‘void*’} to ‘MyObject*’ [-fpermissive]
31   MyObject *obj = g_rc_box_new0 (MyObject);
32   MyObject *obj2 = g_rc_box_acquire (obj);
33   g_assert_true (obj2 == obj);
34
35   MyObject *obj3 = g_atomic_pointer_get (&obj2);
36   g_assert_true (obj3 == obj);
37
38   MyObject *obj4 = nullptr;
39   g_atomic_pointer_set (&obj4, obj3);
40   g_assert_true (obj4 == obj);
41
42   MyObject *obj5 = nullptr;
43   g_atomic_pointer_compare_and_exchange (&obj5, nullptr, obj4);
44   g_assert_true (obj5 == obj);
45
46   MyObject *obj6 = g_steal_pointer (&obj5);
47   g_assert_true (obj6 == obj);
48
49   g_clear_pointer (&obj6, g_rc_box_release);
50   g_rc_box_release (obj);
51 #else
52   g_test_skip ("This test requires a C++11 compiler");
53 #endif
54 }
55
56 static void
57 test_atomic_pointer_compare_and_exchange (void)
58 {
59 #if __cplusplus >= 201103L
60   const gchar *str1 = "str1";
61   const gchar *str2 = "str2";
62   const gchar *atomic_string = str1;
63
64   g_test_message ("Test that g_atomic_pointer_compare_and_exchange() with a "
65                   "non-void* pointer doesn’t have any compiler warnings in C++ mode");
66
67   g_assert_true (g_atomic_pointer_compare_and_exchange (&atomic_string, str1, str2));
68   g_assert_true (atomic_string == str2);
69 #else
70   g_test_skip ("This test requires a C++11 compiler");
71 #endif
72 }
73
74 static void
75 test_atomic_int_compare_and_exchange (void)
76 {
77 #if __cplusplus >= 201103L
78   gint atomic_int = 5;
79
80   g_test_message ("Test that g_atomic_int_compare_and_exchange() doesn’t have "
81                   "any compiler warnings in C++ mode");
82
83   g_assert_true (g_atomic_int_compare_and_exchange (&atomic_int, 5, 50));
84   g_assert_cmpint (atomic_int, ==, 50);
85 #else
86   g_test_skip ("This test requires a C++11 compiler");
87 #endif
88 }
89
90 int
91 main (int argc, char *argv[])
92 {
93 #if __cplusplus >= 201103L
94   g_test_init (&argc, &argv, nullptr);
95 #else
96   g_test_init (&argc, &argv, NULL);
97 #endif
98
99   g_test_add_func ("/C++/typeof", test_typeof);
100   g_test_add_func ("/C++/atomic-pointer-compare-and-exchange", test_atomic_pointer_compare_and_exchange);
101   g_test_add_func ("/C++/atomic-int-compare-and-exchange", test_atomic_int_compare_and_exchange);
102
103   return g_test_run ();
104 }