Home | History | Annotate | Line # | Download | only in unittests
      1      1.1  christos /* Self tests for packed for GDB, the GNU debugger.
      2      1.1  christos 
      3  1.1.1.2  christos    Copyright (C) 2022-2024 Free Software Foundation, Inc.
      4      1.1  christos 
      5      1.1  christos    This file is part of GDB.
      6      1.1  christos 
      7      1.1  christos    This program is free software; you can redistribute it and/or modify
      8      1.1  christos    it under the terms of the GNU General Public License as published by
      9      1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10      1.1  christos    (at your option) any later version.
     11      1.1  christos 
     12      1.1  christos    This program is distributed in the hope that it will be useful,
     13      1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14      1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15      1.1  christos    GNU General Public License for more details.
     16      1.1  christos 
     17      1.1  christos    You should have received a copy of the GNU General Public License
     18      1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19      1.1  christos 
     20      1.1  christos #include "gdbsupport/selftest.h"
     21      1.1  christos #include "gdbsupport/packed.h"
     22      1.1  christos 
     23      1.1  christos namespace selftests {
     24      1.1  christos namespace packed_tests {
     25      1.1  christos 
     26      1.1  christos enum test_enum
     27      1.1  christos {
     28      1.1  christos   TE_A = 1,
     29      1.1  christos   TE_B = 2,
     30      1.1  christos   TE_C = 3,
     31      1.1  christos   TE_D = 4,
     32      1.1  christos };
     33      1.1  christos 
     34  1.1.1.2  christos static_assert (sizeof (packed<test_enum, 1>) == 1);
     35  1.1.1.2  christos static_assert (sizeof (packed<test_enum, 2>) == 2);
     36  1.1.1.2  christos static_assert (sizeof (packed<test_enum, 3>) == 3);
     37  1.1.1.2  christos static_assert (sizeof (packed<test_enum, 4>) == 4);
     38  1.1.1.2  christos 
     39  1.1.1.2  christos static_assert (alignof (packed<test_enum, 1>) == 1);
     40  1.1.1.2  christos static_assert (alignof (packed<test_enum, 2>) == 1);
     41  1.1.1.2  christos static_assert (alignof (packed<test_enum, 3>) == 1);
     42  1.1.1.2  christos static_assert (alignof (packed<test_enum, 4>) == 1);
     43      1.1  christos 
     44      1.1  christos /* Triviality checks.  */
     45      1.1  christos #define CHECK_TRAIT(TRAIT)			\
     46      1.1  christos   static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")
     47      1.1  christos 
     48      1.1  christos CHECK_TRAIT (is_trivially_copyable);
     49      1.1  christos CHECK_TRAIT (is_trivially_copy_constructible);
     50      1.1  christos CHECK_TRAIT (is_trivially_move_constructible);
     51      1.1  christos CHECK_TRAIT (is_trivially_copy_assignable);
     52      1.1  christos CHECK_TRAIT (is_trivially_move_assignable);
     53      1.1  christos 
     54      1.1  christos #undef CHECK_TRAIT
     55      1.1  christos 
     56      1.1  christos /* Entry point.  */
     57      1.1  christos 
     58      1.1  christos static void
     59      1.1  christos run_tests ()
     60      1.1  christos {
     61      1.1  christos   typedef packed<unsigned int, 2> packed_2;
     62      1.1  christos 
     63      1.1  christos   packed_2 p1;
     64      1.1  christos   packed_2 p2 (0x0102);
     65      1.1  christos   p1 = 0x0102;
     66      1.1  christos 
     67      1.1  christos   SELF_CHECK (p1 == p1);
     68      1.1  christos   SELF_CHECK (p1 == p2);
     69      1.1  christos   SELF_CHECK (p1 == 0x0102);
     70      1.1  christos   SELF_CHECK (0x0102 == p1);
     71      1.1  christos 
     72      1.1  christos   SELF_CHECK (p1 != 0);
     73      1.1  christos   SELF_CHECK (0 != p1);
     74      1.1  christos 
     75      1.1  christos   SELF_CHECK (p1 != 0x0103);
     76      1.1  christos   SELF_CHECK (0x0103 != p1);
     77      1.1  christos 
     78      1.1  christos   SELF_CHECK (p1 != 0x01020102);
     79      1.1  christos   SELF_CHECK (0x01020102 != p1);
     80      1.1  christos 
     81      1.1  christos   SELF_CHECK (p1 != 0x01020000);
     82      1.1  christos   SELF_CHECK (0x01020000 != p1);
     83      1.1  christos 
     84      1.1  christos   /* Check truncation.  */
     85      1.1  christos   p1 = 0x030102;
     86      1.1  christos   SELF_CHECK (p1 == 0x0102);
     87      1.1  christos   SELF_CHECK (p1 != 0x030102);
     88      1.1  christos 
     89      1.1  christos   /* Check that the custom std::atomic/packed/T relational operators
     90      1.1  christos      work as intended.  No need for fully comprehensive tests, as all
     91      1.1  christos      operators are defined in the same way, via a macro.  We just want
     92      1.1  christos      to make sure that we can compare atomic-wrapped packed, with
     93      1.1  christos      packed, and with the packed underlying type.  */
     94      1.1  christos 
     95      1.1  christos   std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
     96      1.1  christos 
     97      1.1  christos   SELF_CHECK (atomic_packed_2 == atomic_packed_2);
     98      1.1  christos   SELF_CHECK (atomic_packed_2 == p1);
     99      1.1  christos   SELF_CHECK (p1 == atomic_packed_2);
    100      1.1  christos   SELF_CHECK (atomic_packed_2 == 0x0102u);
    101      1.1  christos   SELF_CHECK (0x0102u == atomic_packed_2);
    102      1.1  christos 
    103      1.1  christos   SELF_CHECK (atomic_packed_2 >= 0x0102u);
    104      1.1  christos   SELF_CHECK (atomic_packed_2 <= 0x0102u);
    105      1.1  christos   SELF_CHECK (atomic_packed_2 > 0u);
    106      1.1  christos   SELF_CHECK (atomic_packed_2 < 0x0103u);
    107      1.1  christos   SELF_CHECK (atomic_packed_2 >= 0u);
    108      1.1  christos   SELF_CHECK (atomic_packed_2 <= 0x0102u);
    109      1.1  christos   SELF_CHECK (!(atomic_packed_2 > 0x0102u));
    110      1.1  christos   SELF_CHECK (!(atomic_packed_2 < 0x0102u));
    111      1.1  christos 
    112      1.1  christos   /* Check std::atomic<packed> truncation behaves the same as without
    113      1.1  christos      std::atomic.  */
    114      1.1  christos   atomic_packed_2 = 0x030102;
    115      1.1  christos   SELF_CHECK (atomic_packed_2 == 0x0102u);
    116      1.1  christos   SELF_CHECK (atomic_packed_2 != 0x030102u);
    117      1.1  christos }
    118      1.1  christos 
    119      1.1  christos } /* namespace packed_tests */
    120      1.1  christos } /* namespace selftests */
    121      1.1  christos 
    122      1.1  christos void _initialize_packed_selftests ();
    123      1.1  christos void
    124      1.1  christos _initialize_packed_selftests ()
    125      1.1  christos {
    126      1.1  christos   selftests::register_test ("packed", selftests::packed_tests::run_tests);
    127      1.1  christos }
    128