From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) by sourceware.org (Postfix) with ESMTPS id F118F384D19F for ; Thu, 21 Jul 2022 15:21:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F118F384D19F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f48.google.com with SMTP id x23-20020a05600c179700b003a30e3e7989so1023737wmo.0 for ; Thu, 21 Jul 2022 08:21:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p3KZIes19BiXQoqt1hkKb3yBWVJoIr1j/Z3wtU86OOs=; b=e9Q5Q3wlmrLfJ9BF+mleXikyD7QaYKOA0B2gnpOZZgXmfsIRvdlIFnkwmDOkqyUqY/ AHAx8+ROcy0rDwA70QNKZSLHDlu7n95YO+7ICH8vi/gBl3TijTUAuNb2cT19GaoVec4T bOpCtdZO0fd2/S9i9+a6vjfc+jTLwqvWRTviaeMGaJ7NvCB4R5M034zQiU41/zIALuFQ DZsdjm3yV9Q0tnVdILgQXqlmLoFt0/b82MsPvCZxjfW4SHDWX0kloiwdXOW511IU7jEL hoiQRlFYCRQ3CRNQUZmzQCBLETpn5RdZpD7r/fUAKhZuaN5eDiXiDRX314GhU1DOAVpw rUNA== X-Gm-Message-State: AJIora/VyhA04BZe7KwbKfhub/Z51Ne+NpZkOle1lZVRdPFPxry+npOp XyOflrqm+Hdqofnqj1HOEMhu4vzJWC0= X-Google-Smtp-Source: AGRyM1sVcgmr5/glpEqMRWuJ7Y6Z+BVngL2Se8ZmTBO5H2Xoo60VB9wcuQQq7oDhwjZXE7m5mdh6iw== X-Received: by 2002:a05:600c:1d12:b0:3a3:297a:bb13 with SMTP id l18-20020a05600c1d1200b003a3297abb13mr7809137wms.136.1658416900129; Thu, 21 Jul 2022 08:21:40 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id h6-20020a5d4fc6000000b0021d76a1b0e3sm2188407wrw.6.2022.07.21.08.21.38 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Jul 2022 08:21:39 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 2/3] struct packed: Unit tests and more operators Date: Thu, 21 Jul 2022 16:21:31 +0100 Message-Id: <20220721152132.3489524-3-pedro@palves.net> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220721152132.3489524-1-pedro@palves.net> References: <20220721152132.3489524-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jul 2022 15:21:44 -0000 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/Makefile.in | 1 + gdb/unittests/packed-selftests.c | 132 +++++++++++++++++++++++++++++++ gdbsupport/packed.h | 79 +++++++++++------- 3 files changed, 182 insertions(+), 30 deletions(-) create mode 100644 gdb/unittests/packed-selftests.c 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 = \ 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-selftests.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 = 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); +} 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 #include "traits.h" +#include /* 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 @@ struct ATTRIBUTE_GCC_STRUCT packed T m_val : (Bytes * HOST_CHAR_BIT) ATTRIBUTE_PACKED; }; -/* 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== (T lhs, const std::atomic> &rhs) -{ - return lhs == rhs.load (); -} - -template -bool operator== (const std::atomic> &lhs, T rhs) -{ - return lhs.load () == 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 (); \ + } -template -bool operator!= (T lhs, const std::atomic> &rhs) -{ - return !(lhs == rhs); -} +PACKED_ATOMIC_OP (==) +PACKED_ATOMIC_OP (!=) +PACKED_ATOMIC_OP (>) +PACKED_ATOMIC_OP (<) +PACKED_ATOMIC_OP (>=) +PACKED_ATOMIC_OP (<=) -template -bool operator!= (const std::atomic> &lhs, T rhs) -{ - return !(lhs == rhs); -} +#undef PACKED_ATOMIC_OP #endif -- 2.36.0