Home | History | Annotate | Line # | Download | only in unittests
ptid-selftests.c revision 1.1.1.3
      1      1.1  christos /* Self tests for ptid_t for GDB, the GNU debugger.
      2      1.1  christos 
      3  1.1.1.3  christos    Copyright (C) 2017-2020 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 "defs.h"
     21  1.1.1.3  christos #include "gdbsupport/ptid.h"
     22      1.1  christos #include <type_traits>
     23      1.1  christos 
     24      1.1  christos namespace selftests {
     25      1.1  christos namespace ptid {
     26      1.1  christos 
     27      1.1  christos /* Check that the ptid_t class is POD.
     28      1.1  christos 
     29      1.1  christos    This is a requirement for as long as we have ptids embedded in
     30      1.1  christos    structures allocated with malloc. */
     31      1.1  christos 
     32      1.1  christos static_assert (std::is_pod<ptid_t>::value, "ptid_t is POD");
     33      1.1  christos 
     34      1.1  christos /* We want to avoid implicit conversion from int to ptid_t.  */
     35      1.1  christos 
     36      1.1  christos static_assert (!std::is_convertible<int, ptid_t>::value,
     37      1.1  christos 	       "constructor is explicit");
     38      1.1  christos 
     39      1.1  christos /* Build some useful ptids.  */
     40      1.1  christos 
     41      1.1  christos static constexpr ptid_t pid = ptid_t (1);
     42      1.1  christos static constexpr ptid_t lwp = ptid_t (1, 2, 0);
     43      1.1  christos static constexpr ptid_t tid = ptid_t (1, 0, 2);
     44      1.1  christos static constexpr ptid_t both = ptid_t (1, 2, 2);
     45      1.1  christos 
     46      1.1  christos /* Build some constexpr version of null_ptid and minus_one_ptid to use in
     47      1.1  christos    static_assert.  Once the real ones are made constexpr, we can get rid of
     48      1.1  christos    these.  */
     49      1.1  christos 
     50      1.1  christos static constexpr ptid_t null = ptid_t::make_null ();
     51      1.1  christos static constexpr ptid_t minus_one = ptid_t::make_minus_one ();
     52      1.1  christos 
     53      1.1  christos /* Verify pid.  */
     54      1.1  christos 
     55      1.1  christos static_assert (pid.pid () == 1, "pid's pid is right");
     56      1.1  christos static_assert (lwp.pid () == 1, "lwp's pid is right");
     57      1.1  christos static_assert (tid.pid () == 1, "tid's pid is right");
     58      1.1  christos static_assert (both.pid () == 1, "both's pid is right");
     59      1.1  christos 
     60      1.1  christos /* Verify lwp_p.  */
     61      1.1  christos 
     62      1.1  christos static_assert (!pid.lwp_p (), "pid's lwp_p is right");
     63      1.1  christos static_assert (lwp.lwp_p (), "lwp's lwp_p is right");
     64      1.1  christos static_assert (!tid.lwp_p (), "tid's lwp_p is right");
     65      1.1  christos static_assert (both.lwp_p (), "both's lwp_p is right");
     66      1.1  christos 
     67      1.1  christos /* Verify lwp.  */
     68      1.1  christos 
     69      1.1  christos static_assert (pid.lwp () == 0, "pid's lwp is right");
     70      1.1  christos static_assert (lwp.lwp () == 2, "lwp's lwp is right");
     71      1.1  christos static_assert (tid.lwp () == 0, "tid's lwp is right");
     72      1.1  christos static_assert (both.lwp () == 2, "both's lwp is right");
     73      1.1  christos 
     74      1.1  christos /* Verify tid_p.  */
     75      1.1  christos 
     76      1.1  christos static_assert (!pid.tid_p (), "pid's tid_p is right");
     77      1.1  christos static_assert (!lwp.tid_p (), "lwp's tid_p is right");
     78      1.1  christos static_assert (tid.tid_p (), "tid's tid_p is right");
     79      1.1  christos static_assert (both.tid_p (), "both's tid_p is right");
     80      1.1  christos 
     81      1.1  christos /* Verify tid.  */
     82      1.1  christos 
     83      1.1  christos static_assert (pid.tid () == 0, "pid's tid is right");
     84      1.1  christos static_assert (lwp.tid () == 0, "lwp's tid is right");
     85      1.1  christos static_assert (tid.tid () == 2, "tid's tid is right");
     86      1.1  christos static_assert (both.tid () == 2, "both's tid is right");
     87      1.1  christos 
     88      1.1  christos /* Verify is_pid.  */
     89      1.1  christos 
     90      1.1  christos static_assert (pid.is_pid (), "pid is a pid");
     91      1.1  christos static_assert (!lwp.is_pid (), "lwp isn't a pid");
     92      1.1  christos static_assert (!tid.is_pid (), "tid isn't a pid");
     93      1.1  christos static_assert (!both.is_pid (), "both isn't a pid");
     94      1.1  christos static_assert (!null.is_pid (), "null ptid isn't a pid");
     95      1.1  christos static_assert (!minus_one.is_pid (), "minus one ptid isn't a pid");
     96      1.1  christos 
     97      1.1  christos /* Verify operator ==.  */
     98      1.1  christos 
     99      1.1  christos static_assert (pid == ptid_t (1, 0, 0), "pid operator== is right");
    100      1.1  christos static_assert (lwp == ptid_t (1, 2, 0), "lwp operator== is right");
    101      1.1  christos static_assert (tid == ptid_t (1, 0, 2), "tid operator== is right");
    102      1.1  christos static_assert (both == ptid_t (1, 2, 2), "both operator== is right");
    103      1.1  christos 
    104      1.1  christos /* Verify operator !=.  */
    105      1.1  christos 
    106      1.1  christos static_assert (pid != ptid_t (2, 0, 0), "pid isn't equal to a different pid");
    107      1.1  christos static_assert (pid != lwp, "pid isn't equal to one of its thread");
    108      1.1  christos static_assert (lwp != tid, "lwp isn't equal to tid");
    109      1.1  christos static_assert (both != lwp, "both isn't equal to lwp");
    110      1.1  christos static_assert (both != tid, "both isn't equal to tid");
    111      1.1  christos 
    112      1.1  christos /* Verify matches against minus_one.  */
    113      1.1  christos 
    114      1.1  christos static_assert (pid.matches (minus_one), "pid matches minus one");
    115      1.1  christos static_assert (lwp.matches (minus_one), "lwp matches minus one");
    116      1.1  christos static_assert (tid.matches (minus_one), "tid matches minus one");
    117      1.1  christos static_assert (both.matches (minus_one), "both matches minus one");
    118      1.1  christos 
    119      1.1  christos /* Verify matches against pid.  */
    120      1.1  christos 
    121      1.1  christos static_assert (pid.matches (pid), "pid matches pid");
    122      1.1  christos static_assert (lwp.matches (pid), "lwp matches pid");
    123      1.1  christos static_assert (tid.matches (pid), "tid matches pid");
    124      1.1  christos static_assert (both.matches (pid), "both matches pid");
    125      1.1  christos static_assert (!ptid_t (2, 0, 0).matches (pid), "other pid doesn't match pid");
    126      1.1  christos static_assert (!ptid_t (2, 2, 0).matches (pid), "other lwp doesn't match pid");
    127      1.1  christos static_assert (!ptid_t (2, 0, 2).matches (pid), "other tid doesn't match pid");
    128      1.1  christos static_assert (!ptid_t (2, 2, 2).matches (pid), "other both doesn't match pid");
    129      1.1  christos 
    130      1.1  christos /* Verify matches against exact matches.  */
    131      1.1  christos 
    132      1.1  christos static_assert (!pid.matches (lwp), "pid doesn't match lwp");
    133      1.1  christos static_assert (lwp.matches (lwp), "lwp matches lwp");
    134      1.1  christos static_assert (!tid.matches (lwp), "tid doesn't match lwp");
    135      1.1  christos static_assert (!both.matches (lwp), "both doesn't match lwp");
    136      1.1  christos static_assert (!ptid_t (2, 2, 0).matches (lwp), "other lwp doesn't match lwp");
    137      1.1  christos 
    138      1.1  christos static_assert (!pid.matches (tid), "pid doesn't match tid");
    139      1.1  christos static_assert (!lwp.matches (tid), "lwp doesn't match tid");
    140      1.1  christos static_assert (tid.matches (tid), "tid matches tid");
    141      1.1  christos static_assert (!both.matches (tid), "both doesn't match tid");
    142      1.1  christos static_assert (!ptid_t (2, 0, 2).matches (tid), "other tid doesn't match tid");
    143      1.1  christos 
    144      1.1  christos static_assert (!pid.matches (both), "pid doesn't match both");
    145      1.1  christos static_assert (!lwp.matches (both), "lwp doesn't match both");
    146      1.1  christos static_assert (!tid.matches (both), "tid doesn't match both");
    147      1.1  christos static_assert (both.matches (both), "both matches both");
    148      1.1  christos static_assert (!ptid_t (2, 2, 2).matches (both),
    149      1.1  christos 	       "other both doesn't match both");
    150      1.1  christos 
    151      1.1  christos 
    152      1.1  christos } /* namespace ptid */
    153      1.1  christos } /* namespace selftests */
    154