aboutsummaryrefslogtreecommitdiff
path: root/gdb/unittests/packed-selftests.c
blob: 852a7d364ce9f79ec64568398f7fcd9ea4ddc465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Self tests for packed for GDB, the GNU debugger.

   Copyright (C) 2022-2024 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 <http://www.gnu.org/licenses/>.  */

#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,
};

static_assert (sizeof (packed<test_enum, 1>) == 1);
static_assert (sizeof (packed<test_enum, 2>) == 2);
static_assert (sizeof (packed<test_enum, 3>) == 3);
static_assert (sizeof (packed<test_enum, 4>) == 4);

static_assert (alignof (packed<test_enum, 1>) == 1);
static_assert (alignof (packed<test_enum, 2>) == 1);
static_assert (alignof (packed<test_enum, 3>) == 1);
static_assert (alignof (packed<test_enum, 4>) == 1);

/* Triviality checks.  */
#define CHECK_TRAIT(TRAIT)			\
  static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")

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);

#undef CHECK_TRAIT

/* Entry point.  */

static void
run_tests ()
{
  typedef packed<unsigned int, 2> 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<packed<unsigned int, 2>> 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<packed> 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);
}