xref: /netbsd-src/external/gpl3/gdb/dist/gdb/unittests/packed-selftests.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* Self tests for packed for GDB, the GNU debugger.
2 
3    Copyright (C) 2022-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #include "defs.h"
21 #include "gdbsupport/selftest.h"
22 #include "gdbsupport/packed.h"
23 
24 namespace selftests {
25 namespace packed_tests {
26 
27 enum test_enum
28 {
29   TE_A = 1,
30   TE_B = 2,
31   TE_C = 3,
32   TE_D = 4,
33 };
34 
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);
39 
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);
44 
45 /* Triviality checks.  */
46 #define CHECK_TRAIT(TRAIT)			\
47   static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")
48 
49 #if HAVE_IS_TRIVIALLY_COPYABLE
50 
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);
56 
57 #endif
58 
59 #undef CHECK_TRAIT
60 
61 /* Entry point.  */
62 
63 static void
64 run_tests ()
65 {
66   typedef packed<unsigned int, 2> packed_2;
67 
68   packed_2 p1;
69   packed_2 p2 (0x0102);
70   p1 = 0x0102;
71 
72   SELF_CHECK (p1 == p1);
73   SELF_CHECK (p1 == p2);
74   SELF_CHECK (p1 == 0x0102);
75   SELF_CHECK (0x0102 == p1);
76 
77   SELF_CHECK (p1 != 0);
78   SELF_CHECK (0 != p1);
79 
80   SELF_CHECK (p1 != 0x0103);
81   SELF_CHECK (0x0103 != p1);
82 
83   SELF_CHECK (p1 != 0x01020102);
84   SELF_CHECK (0x01020102 != p1);
85 
86   SELF_CHECK (p1 != 0x01020000);
87   SELF_CHECK (0x01020000 != p1);
88 
89   /* Check truncation.  */
90   p1 = 0x030102;
91   SELF_CHECK (p1 == 0x0102);
92   SELF_CHECK (p1 != 0x030102);
93 
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.  */
99 
100   std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
101 
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);
107 
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));
116 
117   /* Check std::atomic<packed> truncation behaves the same as without
118      std::atomic.  */
119   atomic_packed_2 = 0x030102;
120   SELF_CHECK (atomic_packed_2 == 0x0102u);
121   SELF_CHECK (atomic_packed_2 != 0x030102u);
122 }
123 
124 } /* namespace packed_tests */
125 } /* namespace selftests */
126 
127 void _initialize_packed_selftests ();
128 void
129 _initialize_packed_selftests ()
130 {
131   selftests::register_test ("packed", selftests::packed_tests::run_tests);
132 }
133