From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1551) id 9B6F53875A25; Mon, 25 Jul 2022 15:12:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9B6F53875A25 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pedro Alves To: gdb-cvs@sourceware.org Subject: [binutils-gdb] struct packed: Unit tests and more operators X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: 4ca26ad7dec88ab6fa8507ba069e9f1b3c5196da X-Git-Newrev: e249e6b8012ea0a14e5768d31becd7b4caff8e77 Message-Id: <20220725151215.9B6F53875A25@sourceware.org> Date: Mon, 25 Jul 2022 15:12:15 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 25 Jul 2022 15:12:15 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3De249e6b8012e= a0a14e5768d31becd7b4caff8e77 commit e249e6b8012ea0a14e5768d31becd7b4caff8e77 Author: Pedro Alves Date: Tue Jul 19 00:26:33 2022 +0100 struct packed: Unit tests and more operators =20 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. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D29373 =20 Change-Id: I023315ee03622c59c397bf4affc0b68179c32374 Diff: --- gdb/Makefile.in | 1 + gdb/unittests/packed-selftests.c | 132 +++++++++++++++++++++++++++++++++++= ++++ gdbsupport/packed.h | 79 ++++++++++++++--------- 3 files changed, 182 insertions(+), 30 deletions(-) diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 57c29a78b7a..aebb7dc5ea3 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -464,6 +464,7 @@ SELFTESTS_SRCS =3D \ unittests/offset-type-selftests.c \ unittests/observable-selftests.c \ unittests/optional-selftests.c \ + unittests/packed-selftests.c \ unittests/parallel-for-selftests.c \ unittests/parse-connection-spec-selftests.c \ unittests/path-join-selftests.c \ diff --git a/gdb/unittests/packed-selftests.c b/gdb/unittests/packed-selfte= sts.c new file mode 100644 index 00000000000..3438a5a2555 --- /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 =3D 1, + TE_B =3D 2, + TE_C =3D 3, + TE_D =3D 4, +}; + +gdb_static_assert (sizeof (packed) =3D=3D 1); +gdb_static_assert (sizeof (packed) =3D=3D 2); +gdb_static_assert (sizeof (packed) =3D=3D 3); +gdb_static_assert (sizeof (packed) =3D=3D 4); + +gdb_static_assert (alignof (packed) =3D=3D 1); +gdb_static_assert (alignof (packed) =3D=3D 1); +gdb_static_assert (alignof (packed) =3D=3D 1); +gdb_static_assert (alignof (packed) =3D=3D 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 =3D 0x0102; + + SELF_CHECK (p1 =3D=3D p1); + SELF_CHECK (p1 =3D=3D p2); + SELF_CHECK (p1 =3D=3D 0x0102); + SELF_CHECK (0x0102 =3D=3D p1); + + SELF_CHECK (p1 !=3D 0); + SELF_CHECK (0 !=3D p1); + + SELF_CHECK (p1 !=3D 0x0103); + SELF_CHECK (0x0103 !=3D p1); + + SELF_CHECK (p1 !=3D 0x01020102); + SELF_CHECK (0x01020102 !=3D p1); + + SELF_CHECK (p1 !=3D 0x01020000); + SELF_CHECK (0x01020000 !=3D p1); + + /* Check truncation. */ + p1 =3D 0x030102; + SELF_CHECK (p1 =3D=3D 0x0102); + SELF_CHECK (p1 !=3D 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 =3D=3D atomic_packed_2); + SELF_CHECK (atomic_packed_2 =3D=3D p1); + SELF_CHECK (p1 =3D=3D atomic_packed_2); + SELF_CHECK (atomic_packed_2 =3D=3D 0x0102u); + SELF_CHECK (0x0102u =3D=3D atomic_packed_2); + + SELF_CHECK (atomic_packed_2 >=3D 0x0102u); + SELF_CHECK (atomic_packed_2 <=3D 0x0102u); + SELF_CHECK (atomic_packed_2 > 0u); + SELF_CHECK (atomic_packed_2 < 0x0103u); + SELF_CHECK (atomic_packed_2 >=3D 0u); + SELF_CHECK (atomic_packed_2 <=3D 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 =3D 0x030102; + SELF_CHECK (atomic_packed_2 =3D=3D 0x0102u); + SELF_CHECK (atomic_packed_2 !=3D 0x030102u); +} + +} /* namespace packed_tests */ +} /* namespace selftests */ + +void _initialize_packed_selftests (); +void +_initialize_packed_selftests () +{ + selftests::register_test ("packed", selftests::packed_tests::run_tests); +} diff --git a/gdbsupport/packed.h b/gdbsupport/packed.h index 53164a9e0c3..d721b02c056 100644 --- a/gdbsupport/packed.h +++ b/gdbsupport/packed.h @@ -19,6 +19,7 @@ #define PACKED_H =20 #include "traits.h" +#include =20 /* Each instantiation and full specialization of the packed template defines a type that behaves like a given scalar type, but that has @@ -68,37 +69,55 @@ private: T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED; }; =20 -/* Add some comparisons between std::atomic> and T. We need - this because the regular comparisons would require two implicit - conversions to go from T to std::atomic>: - - T -> packed - packed -> std::atomic> - - and C++ only does one. */ - -template -bool operator=3D=3D (T lhs, const std::atomic> &rhs) -{ - return lhs =3D=3D rhs.load (); -} - -template -bool operator=3D=3D (const std::atomic> &lhs, T rhs) -{ - return lhs.load () =3D=3D rhs; -} +/* Add some comparisons between std::atomic> and packed + and T. We need this because even though std::atomic doesn't + define these operators, the relational expressions still work via + implicit conversions. Those wouldn't work when wrapped in packed + without these operators, because they'd require two implicit + conversions to go from T to packed to std::atomic> + (and back), and C++ only does one. */ + +#define PACKED_ATOMIC_OP(OP) \ + template \ + bool operator OP (const std::atomic> &lhs, \ + const std::atomic> &rhs) \ + { \ + return lhs.load () OP rhs.load (); \ + } \ + \ + template \ + bool operator OP (T lhs, const std::atomic> &rhs) \ + { \ + return lhs OP rhs.load (); \ + } \ + \ + template \ + bool operator OP (const std::atomic> &lhs, T rhs) \ + { \ + return lhs.load () OP rhs; \ + } \ + \ + template \ + bool operator OP (const std::atomic> &lhs, \ + packed rhs) \ + { \ + return lhs.load () OP rhs; \ + } \ + \ + template \ + bool operator OP (packed lhs, \ + const std::atomic> &rhs) \ + { \ + return lhs OP rhs.load (); \ + } =20 -template -bool operator!=3D (T lhs, const std::atomic> &rhs) -{ - return !(lhs =3D=3D rhs); -} +PACKED_ATOMIC_OP (=3D=3D) +PACKED_ATOMIC_OP (!=3D) +PACKED_ATOMIC_OP (>) +PACKED_ATOMIC_OP (<) +PACKED_ATOMIC_OP (>=3D) +PACKED_ATOMIC_OP (<=3D) =20 -template -bool operator!=3D (const std::atomic> &lhs, T rhs) -{ - return !(lhs =3D=3D rhs); -} +#undef PACKED_ATOMIC_OP =20 #endif