1 /* Self tests for packed for GDB, the GNU debugger.
3 Copyright (C) 2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdbsupport/selftest.h"
22 #include "gdbsupport/packed.h"
25 namespace packed_tests {
35 gdb_static_assert (sizeof (packed<test_enum, 1>) == 1);
36 gdb_static_assert (sizeof (packed<test_enum, 2>) == 2);
37 gdb_static_assert (sizeof (packed<test_enum, 3>) == 3);
38 gdb_static_assert (sizeof (packed<test_enum, 4>) == 4);
40 gdb_static_assert (alignof (packed<test_enum, 1>) == 1);
41 gdb_static_assert (alignof (packed<test_enum, 2>) == 1);
42 gdb_static_assert (alignof (packed<test_enum, 3>) == 1);
43 gdb_static_assert (alignof (packed<test_enum, 4>) == 1);
45 /* Triviality checks. */
46 #define CHECK_TRAIT(TRAIT) \
47 static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")
49 #if HAVE_IS_TRIVIALLY_COPYABLE
51 CHECK_TRAIT (is_trivially_copyable);
52 CHECK_TRAIT (is_trivially_copy_constructible);
53 CHECK_TRAIT (is_trivially_move_constructible);
54 CHECK_TRAIT (is_trivially_copy_assignable);
55 CHECK_TRAIT (is_trivially_move_assignable);
66 typedef packed<unsigned int, 2> packed_2;
72 SELF_CHECK (p1 == p1);
73 SELF_CHECK (p1 == p2);
74 SELF_CHECK (p1 == 0x0102);
75 SELF_CHECK (0x0102 == p1);
80 SELF_CHECK (p1 != 0x0103);
81 SELF_CHECK (0x0103 != p1);
83 SELF_CHECK (p1 != 0x01020102);
84 SELF_CHECK (0x01020102 != p1);
86 SELF_CHECK (p1 != 0x01020000);
87 SELF_CHECK (0x01020000 != p1);
89 /* Check truncation. */
91 SELF_CHECK (p1 == 0x0102);
92 SELF_CHECK (p1 != 0x030102);
94 /* Check that the custom std::atomic/packed/T relational operators
95 work as intended. No need for fully comprehensive tests, as all
96 operators are defined in the same way, via a macro. We just want
97 to make sure that we can compare atomic-wrapped packed, with
98 packed, and with the packed underlying type. */
100 std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
102 SELF_CHECK (atomic_packed_2 == atomic_packed_2);
103 SELF_CHECK (atomic_packed_2 == p1);
104 SELF_CHECK (p1 == atomic_packed_2);
105 SELF_CHECK (atomic_packed_2 == 0x0102u);
106 SELF_CHECK (0x0102u == atomic_packed_2);
108 SELF_CHECK (atomic_packed_2 >= 0x0102u);
109 SELF_CHECK (atomic_packed_2 <= 0x0102u);
110 SELF_CHECK (atomic_packed_2 > 0u);
111 SELF_CHECK (atomic_packed_2 < 0x0103u);
112 SELF_CHECK (atomic_packed_2 >= 0u);
113 SELF_CHECK (atomic_packed_2 <= 0x0102u);
114 SELF_CHECK (!(atomic_packed_2 > 0x0102u));
115 SELF_CHECK (!(atomic_packed_2 < 0x0102u));
117 /* Check std::atomic<packed> truncation behaves the same as without
119 atomic_packed_2 = 0x030102;
120 SELF_CHECK (atomic_packed_2 == 0x0102u);
121 SELF_CHECK (atomic_packed_2 != 0x030102u);
124 } /* namespace packed_tests */
125 } /* namespace selftests */
127 void _initialize_packed_selftests ();
129 _initialize_packed_selftests ()
131 selftests::register_test ("packed", selftests::packed_tests::run_tests);