Add translations for various sub-directories
[binutils-gdb.git] / gdb / unittests / packed-selftests.c
blob060c87c4e1594454f04067ddcf034907ba86cf79
1 /* Self tests for packed for GDB, the GNU debugger.
3 Copyright (C) 2022-2024 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/>. */
20 #include "gdbsupport/selftest.h"
21 #include "gdbsupport/packed.h"
23 namespace selftests {
24 namespace packed_tests {
26 enum test_enum
28 TE_A = 1,
29 TE_B = 2,
30 TE_C = 3,
31 TE_D = 4,
34 static_assert (sizeof (packed<test_enum, 1>) == 1);
35 static_assert (sizeof (packed<test_enum, 2>) == 2);
36 static_assert (sizeof (packed<test_enum, 3>) == 3);
37 static_assert (sizeof (packed<test_enum, 4>) == 4);
39 static_assert (alignof (packed<test_enum, 1>) == 1);
40 static_assert (alignof (packed<test_enum, 2>) == 1);
41 static_assert (alignof (packed<test_enum, 3>) == 1);
42 static_assert (alignof (packed<test_enum, 4>) == 1);
44 /* Triviality checks. */
45 #define CHECK_TRAIT(TRAIT) \
46 static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")
48 CHECK_TRAIT (is_trivially_copyable);
49 CHECK_TRAIT (is_trivially_copy_constructible);
50 CHECK_TRAIT (is_trivially_move_constructible);
51 CHECK_TRAIT (is_trivially_copy_assignable);
52 CHECK_TRAIT (is_trivially_move_assignable);
54 #undef CHECK_TRAIT
56 /* Entry point. */
58 static void
59 run_tests ()
61 typedef packed<unsigned int, 2> packed_2;
63 packed_2 p1;
64 packed_2 p2 (0x0102);
65 p1 = 0x0102;
67 SELF_CHECK (p1 == p1);
68 SELF_CHECK (p1 == p2);
69 SELF_CHECK (p1 == 0x0102);
70 SELF_CHECK (0x0102 == p1);
72 SELF_CHECK (p1 != 0);
73 SELF_CHECK (0 != p1);
75 SELF_CHECK (p1 != 0x0103);
76 SELF_CHECK (0x0103 != p1);
78 SELF_CHECK (p1 != 0x01020102);
79 SELF_CHECK (0x01020102 != p1);
81 SELF_CHECK (p1 != 0x01020000);
82 SELF_CHECK (0x01020000 != p1);
84 /* Check truncation. */
85 p1 = 0x030102;
86 SELF_CHECK (p1 == 0x0102);
87 SELF_CHECK (p1 != 0x030102);
89 /* Check that the custom std::atomic/packed/T relational operators
90 work as intended. No need for fully comprehensive tests, as all
91 operators are defined in the same way, via a macro. We just want
92 to make sure that we can compare atomic-wrapped packed, with
93 packed, and with the packed underlying type. */
95 std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
97 SELF_CHECK (atomic_packed_2 == atomic_packed_2);
98 SELF_CHECK (atomic_packed_2 == p1);
99 SELF_CHECK (p1 == atomic_packed_2);
100 SELF_CHECK (atomic_packed_2 == 0x0102u);
101 SELF_CHECK (0x0102u == atomic_packed_2);
103 SELF_CHECK (atomic_packed_2 >= 0x0102u);
104 SELF_CHECK (atomic_packed_2 <= 0x0102u);
105 SELF_CHECK (atomic_packed_2 > 0u);
106 SELF_CHECK (atomic_packed_2 < 0x0103u);
107 SELF_CHECK (atomic_packed_2 >= 0u);
108 SELF_CHECK (atomic_packed_2 <= 0x0102u);
109 SELF_CHECK (!(atomic_packed_2 > 0x0102u));
110 SELF_CHECK (!(atomic_packed_2 < 0x0102u));
112 /* Check std::atomic<packed> truncation behaves the same as without
113 std::atomic. */
114 atomic_packed_2 = 0x030102;
115 SELF_CHECK (atomic_packed_2 == 0x0102u);
116 SELF_CHECK (atomic_packed_2 != 0x030102u);
119 } /* namespace packed_tests */
120 } /* namespace selftests */
122 void _initialize_packed_selftests ();
123 void
124 _initialize_packed_selftests ()
126 selftests::register_test ("packed", selftests::packed_tests::run_tests);