1 /* GLib testing framework examples and tests
2  *
3  * Copyright © 2018 Endless Mobile, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Philip Withnall <withnall@endlessm.com>
19  */
20 
21 #include <glib.h>
22 
23 /* Test that G_STATIC_ASSERT_EXPR can be used as an expression */
24 static void
25 test_assert_static (void)
26 {
27   G_STATIC_ASSERT (4 == 4);
28   if (G_STATIC_ASSERT_EXPR (1 == 1), sizeof (gchar) == 2)
29     g_assert_not_reached ();
30 }
31 
32 /* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This
33  * should be the minimal alignment for the given type.
34  *
35  * This is necessary because the implementation of G_ALIGNOF() varies depending
36  * on the compiler in use. We want all implementations to be consistent.
37  *
38  * In the case that the compiler uses the G_STRUCT_OFFSET fallback, this test
39  * is a no-op. */
40 static void
41 test_alignof_fallback (void)
42 {
43 #define check_alignof(type) \
44   g_assert_cmpint (G_ALIGNOF (type), ==, G_STRUCT_OFFSET (struct { char a; type b; }, b))
45 
46   check_alignof (char);
47   check_alignof (int);
48   check_alignof (float);
49   check_alignof (double);
50   check_alignof (struct { char a; int b; });
51 }
52 
53 static void
54 test_struct_sizeof_member (void)
55 {
56     G_STATIC_ASSERT (G_SIZEOF_MEMBER (struct { char a; int b; }, a) == sizeof (char));
57     g_assert_cmpint (G_SIZEOF_MEMBER (struct { char a; int b; }, b), ==, sizeof (int));
58 }
59 
60 int
61 main (int   argc,
62       char *argv[])
63 {
64   g_test_init (&argc, &argv, NULL);
65 
66   g_test_add_func ("/alignof/fallback", test_alignof_fallback);
67   g_test_add_func ("/assert/static", test_assert_static);
68   g_test_add_func ("/struct/sizeof_member", test_struct_sizeof_member);
69 
70   return g_test_run ();
71 }
72