Branch data Line data Source code
1 : : /* GLib testing framework examples and tests 2 : : * 3 : : * Copyright © 2018 Endless Mobile, Inc. 4 : : * 5 : : * SPDX-License-Identifier: LGPL-2.1-or-later 6 : : * 7 : : * This library is free software; you can redistribute it and/or 8 : : * modify it under the terms of the GNU Lesser General Public 9 : : * License as published by the Free Software Foundation; either 10 : : * version 2.1 of the License, or (at your option) any later version. 11 : : * 12 : : * This library is distributed in the hope that it will be useful, 13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 : : * Lesser General Public License for more details. 16 : : * 17 : : * You should have received a copy of the GNU Lesser General 18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. 19 : : * 20 : : * Author: Philip Withnall <withnall@endlessm.com> 21 : : */ 22 : : 23 : : #include <glib.h> 24 : : 25 : : #ifdef G_CXX_STD_VERSION 26 : : #error G_CXX_STD_VERSION should be undefined in C programs 27 : : #endif 28 : : 29 : : G_STATIC_ASSERT (!G_CXX_STD_CHECK_VERSION (98)); 30 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (89)); 31 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (90)); 32 : : 33 : : #if G_C_STD_VERSION >= 199000L 34 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (89)); 35 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (90)); 36 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (199000L)); 37 : : #endif 38 : : 39 : : #if G_C_STD_VERSION == 198900L 40 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (99)); 41 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (199901L)); 42 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (11)); 43 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (201112L)); 44 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (17)); 45 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (201710L)); 46 : : #endif 47 : : 48 : : #if G_C_STD_VERSION >= 199901L 49 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (99)); 50 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (199901L)); 51 : : #endif 52 : : 53 : : #if G_C_STD_VERSION == 199901L 54 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (11)); 55 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (201112L)); 56 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (17)); 57 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (201710L)); 58 : : #endif 59 : : 60 : : #if G_C_STD_VERSION >= 201112L 61 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (11)); 62 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (201112L)); 63 : : #endif 64 : : 65 : : #if G_C_STD_VERSION == 201112L 66 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (17)); 67 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (201710L)); 68 : : #endif 69 : : 70 : : #if G_C_STD_VERSION >= 201710L 71 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (17)); 72 : : G_STATIC_ASSERT (G_C_STD_CHECK_VERSION (201710L)); 73 : : #endif 74 : : 75 : : #if G_C_STD_VERSION == 201710L 76 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (23)); 77 : : G_STATIC_ASSERT (!G_C_STD_CHECK_VERSION (202300L)); 78 : : #endif 79 : : 80 : : #ifdef _G_EXPECTED_C_STANDARD 81 : : static void 82 : 4 : test_c_standard (void) 83 : : { 84 : 4 : guint64 std_version = 0; 85 : : 86 [ - + ]: 4 : if (!g_ascii_string_to_unsigned (_G_EXPECTED_C_STANDARD, 10, 0, G_MAXUINT64, 87 : : &std_version, NULL)) 88 : : { 89 : 0 : g_test_skip ("Expected standard value is non-numeric: " 90 : : _G_EXPECTED_C_STANDARD); 91 : 0 : return; 92 : : } 93 : : 94 : 4 : g_assert_true (G_C_STD_CHECK_VERSION (std_version)); 95 : : 96 [ + + + + ]: 4 : if (std_version > 80 && std_version < 99) 97 : 1 : std_version = 90; 98 : : 99 [ + + ]: 4 : if (std_version >= 90) 100 : 2 : g_assert_cmpuint (G_C_STD_VERSION, >=, (std_version + 1900) * 100); 101 : : else 102 : 2 : g_assert_cmpuint (G_C_STD_VERSION, >=, (std_version + 2000) * 100); 103 : : } 104 : : #endif 105 : : 106 : : /* Test that G_STATIC_ASSERT_EXPR can be used as an expression */ 107 : : static void 108 : 5 : test_assert_static (void) 109 : : { 110 : : G_STATIC_ASSERT (4 == 4); 111 : : if (G_STATIC_ASSERT_EXPR (1 == 1), sizeof (gchar) == 2) 112 : : g_assert_not_reached (); 113 : 5 : } 114 : : 115 : : /* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This 116 : : * should be the minimal alignment for the given type. 117 : : * 118 : : * This is necessary because the implementation of G_ALIGNOF() varies depending 119 : : * on the compiler in use. We want all implementations to be consistent. 120 : : * 121 : : * In the case that the compiler uses the G_STRUCT_OFFSET fallback, this test 122 : : * is a no-op. */ 123 : : static void 124 : 5 : test_alignof_fallback (void) 125 : : { 126 : : #define check_alignof(type) \ 127 : : g_assert_cmpint (G_ALIGNOF (type), ==, G_STRUCT_OFFSET (struct { char a; type b; }, b)) 128 : : 129 [ - + ]: 5 : check_alignof (char); 130 [ - + ]: 5 : check_alignof (int); 131 [ - + ]: 5 : check_alignof (float); 132 [ - + ]: 5 : check_alignof (double); 133 [ - + ]: 5 : check_alignof (struct { char a; int b; }); 134 : 5 : } 135 : : 136 : : static void 137 : 5 : test_struct_sizeof_member (void) 138 : : { 139 : : G_STATIC_ASSERT (G_SIZEOF_MEMBER (struct { char a; int b; }, a) == sizeof (char)); 140 : 5 : g_assert_cmpint (G_SIZEOF_MEMBER (struct { char a; int b; }, b), ==, sizeof (int)); 141 : 5 : } 142 : : 143 : : int 144 : 5 : main (int argc, 145 : : char *argv[]) 146 : : { 147 : 5 : g_test_init (&argc, &argv, NULL); 148 : : 149 : : #ifdef _G_EXPECTED_C_STANDARD 150 : 4 : g_test_add_func ("/C/standard-" _G_EXPECTED_C_STANDARD, test_c_standard); 151 : : #endif 152 : : 153 : 5 : g_test_add_func ("/alignof/fallback", test_alignof_fallback); 154 : 5 : g_test_add_func ("/assert/static", test_assert_static); 155 : 5 : g_test_add_func ("/struct/sizeof_member", test_struct_sizeof_member); 156 : : 157 : 5 : return g_test_run (); 158 : : }