Home | History | Annotate | Line # | Download | only in unittests
      1 /* Self tests for packed for GDB, the GNU debugger.
      2 
      3    Copyright (C) 2022-2024 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 "gdbsupport/selftest.h"
     21 #include "gdbsupport/packed.h"
     22 
     23 namespace selftests {
     24 namespace packed_tests {
     25 
     26 enum test_enum
     27 {
     28   TE_A = 1,
     29   TE_B = 2,
     30   TE_C = 3,
     31   TE_D = 4,
     32 };
     33 
     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);
     38 
     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);
     43 
     44 /* Triviality checks.  */
     45 #define CHECK_TRAIT(TRAIT)			\
     46   static_assert (std::TRAIT<packed<test_enum, 1>>::value, "")
     47 
     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);
     53 
     54 #undef CHECK_TRAIT
     55 
     56 /* Entry point.  */
     57 
     58 static void
     59 run_tests ()
     60 {
     61   typedef packed<unsigned int, 2> packed_2;
     62 
     63   packed_2 p1;
     64   packed_2 p2 (0x0102);
     65   p1 = 0x0102;
     66 
     67   SELF_CHECK (p1 == p1);
     68   SELF_CHECK (p1 == p2);
     69   SELF_CHECK (p1 == 0x0102);
     70   SELF_CHECK (0x0102 == p1);
     71 
     72   SELF_CHECK (p1 != 0);
     73   SELF_CHECK (0 != p1);
     74 
     75   SELF_CHECK (p1 != 0x0103);
     76   SELF_CHECK (0x0103 != p1);
     77 
     78   SELF_CHECK (p1 != 0x01020102);
     79   SELF_CHECK (0x01020102 != p1);
     80 
     81   SELF_CHECK (p1 != 0x01020000);
     82   SELF_CHECK (0x01020000 != p1);
     83 
     84   /* Check truncation.  */
     85   p1 = 0x030102;
     86   SELF_CHECK (p1 == 0x0102);
     87   SELF_CHECK (p1 != 0x030102);
     88 
     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.  */
     94 
     95   std::atomic<packed<unsigned int, 2>> atomic_packed_2 (0x0102);
     96 
     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);
    102 
    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));
    111 
    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);
    117 }
    118 
    119 } /* namespace packed_tests */
    120 } /* namespace selftests */
    121 
    122 void _initialize_packed_selftests ();
    123 void
    124 _initialize_packed_selftests ()
    125 {
    126   selftests::register_test ("packed", selftests::packed_tests::run_tests);
    127 }
    128