From e249e6b8012ea0a14e5768d31becd7b4caff8e77 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Tue, 19 Jul 2022 00:26:33 +0100 Subject: struct packed: Unit tests and more operators For PR gdb/29373, I wrote an alternative implementation of struct packed that uses a gdb_byte array for internal representation, needed for mingw+clang. While adding that, I wrote some unit tests to make sure both implementations behave the same. While at it, I implemented all relational operators. This commit adds said unit tests and relational operators. The alternative gdb_byte array implementation will come next. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29373 Change-Id: I023315ee03622c59c397bf4affc0b68179c32374 --- gdb/unittests/packed-selftests.c | 132 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 gdb/unittests/packed-selftests.c (limited to 'gdb/unittests') diff --git a/gdb/unittests/packed-selftests.c b/gdb/unittests/packed-selftests.c new file mode 100644 index 0000000..3438a5a --- /dev/null +++ b/gdb/unittests/packed-selftests.c @@ -0,0 +1,132 @@ +/* Self tests for packed for GDB, the GNU debugger. + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "defs.h" +#include "gdbsupport/selftest.h" +#include "gdbsupport/packed.h" + +namespace selftests { +namespace packed_tests { + +enum test_enum +{ + TE_A = 1, + TE_B = 2, + TE_C = 3, + TE_D = 4, +}; + +gdb_static_assert (sizeof (packed) == 1); +gdb_static_assert (sizeof (packed) == 2); +gdb_static_assert (sizeof (packed) == 3); +gdb_static_assert (sizeof (packed) == 4); + +gdb_static_assert (alignof (packed) == 1); +gdb_static_assert (alignof (packed) == 1); +gdb_static_assert (alignof (packed) == 1); +gdb_static_assert (alignof (packed) == 1); + +/* Triviality checks. */ +#define CHECK_TRAIT(TRAIT) \ + static_assert (std::TRAIT>::value, "") + +#if HAVE_IS_TRIVIALLY_COPYABLE + +CHECK_TRAIT (is_trivially_copyable); +CHECK_TRAIT (is_trivially_copy_constructible); +CHECK_TRAIT (is_trivially_move_constructible); +CHECK_TRAIT (is_trivially_copy_assignable); +CHECK_TRAIT (is_trivially_move_assignable); + +#endif + +#undef CHECK_TRAIT + +/* Entry point. */ + +static void +run_tests () +{ + typedef packed packed_2; + + packed_2 p1; + packed_2 p2 (0x0102); + p1 = 0x0102; + + SELF_CHECK (p1 == p1); + SELF_CHECK (p1 == p2); + SELF_CHECK (p1 == 0x0102); + SELF_CHECK (0x0102 == p1); + + SELF_CHECK (p1 != 0); + SELF_CHECK (0 != p1); + + SELF_CHECK (p1 != 0x0103); + SELF_CHECK (0x0103 != p1); + + SELF_CHECK (p1 != 0x01020102); + SELF_CHECK (0x01020102 != p1); + + SELF_CHECK (p1 != 0x01020000); + SELF_CHECK (0x01020000 != p1); + + /* Check truncation. */ + p1 = 0x030102; + SELF_CHECK (p1 == 0x0102); + SELF_CHECK (p1 != 0x030102); + + /* Check that the custom std::atomic/packed/T relational operators + work as intended. No need for fully comprehensive tests, as all + operators are defined in the same way, via a macro. We just want + to make sure that we can compare atomic-wrapped packed, with + packed, and with the packed underlying type. */ + + std::atomic> atomic_packed_2 (0x0102); + + SELF_CHECK (atomic_packed_2 == atomic_packed_2); + SELF_CHECK (atomic_packed_2 == p1); + SELF_CHECK (p1 == atomic_packed_2); + SELF_CHECK (atomic_packed_2 == 0x0102u); + SELF_CHECK (0x0102u == atomic_packed_2); + + SELF_CHECK (atomic_packed_2 >= 0x0102u); + SELF_CHECK (atomic_packed_2 <= 0x0102u); + SELF_CHECK (atomic_packed_2 > 0u); + SELF_CHECK (atomic_packed_2 < 0x0103u); + SELF_CHECK (atomic_packed_2 >= 0u); + SELF_CHECK (atomic_packed_2 <= 0x0102u); + SELF_CHECK (!(atomic_packed_2 > 0x0102u)); + SELF_CHECK (!(atomic_packed_2 < 0x0102u)); + + /* Check std::atomic truncation behaves the same as without + std::atomic. */ + atomic_packed_2 = 0x030102; + SELF_CHECK (atomic_packed_2 == 0x0102u); + SELF_CHECK (atomic_packed_2 != 0x030102u); +} + +} /* namespace packed_tests */ +} /* namespace selftests */ + +void _initialize_packed_selftests (); +void +_initialize_packed_selftests () +{ + selftests::register_test ("packed", selftests::packed_tests::run_tests); +} -- cgit v1.1