From ab56f0459cb73ba0bb05188c9b44b559156bd7a3 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 21 Dec 2018 13:22:55 +0000 Subject: [PATCH] tests: Add test for G_ALIGNOF() To guarantee that its behaviour is always the same as the G_STRUCT_OFFSET fallback. Signed-off-by: Philip Withnall https://gitlab.gnome.org/GNOME/glib/issues/1055 --- glib/tests/Makefile.am | 1 + glib/tests/macros.c | 53 ++++++++++++++++++++++++++++++++++++++++++ glib/tests/meson.build | 1 + 3 files changed, 55 insertions(+) create mode 100644 glib/tests/macros.c diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index a4f0b27cd..0cf9c9627 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -70,6 +70,7 @@ test_programs = \ keyfile \ list \ logging \ + macros \ mainloop \ mappedfile \ markup \ diff --git a/glib/tests/macros.c b/glib/tests/macros.c new file mode 100644 index 000000000..7fc1d6086 --- /dev/null +++ b/glib/tests/macros.c @@ -0,0 +1,53 @@ +/* GLib testing framework examples and tests + * + * Copyright © 2018 Endless Mobile, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see . + * + * Author: Philip Withnall + */ + +#include + +/* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This + * should be the minimal alignment for the given type. + * + * This is necessary because the implementation of G_ALIGNOF() varies depending + * on the compiler in use. We want all implementations to be consistent. + * + * In the case that the compiler uses the G_STRUCT_OFFSET fallback, this test + * is a no-op. */ +static void +test_alignof_fallback (void) +{ +#define check_alignof(type) \ + g_assert_cmpint (G_ALIGNOF (type), ==, G_STRUCT_OFFSET (struct { char a; type b; }, b)) + + check_alignof (char); + check_alignof (int); + check_alignof (float); + check_alignof (double); + check_alignof (struct { char a; int b; }); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/alignof/fallback", test_alignof_fallback); + + return g_test_run (); +} diff --git a/glib/tests/meson.build b/glib/tests/meson.build index 157708068..59d120e41 100644 --- a/glib/tests/meson.build +++ b/glib/tests/meson.build @@ -38,6 +38,7 @@ glib_tests = { 'keyfile' : {}, 'list' : {}, 'logging' : {}, + 'macros' : {}, 'mainloop' : {}, 'mappedfile' : {}, 'markup' : {},