LCOV - code coverage report
Current view: top level - tests - catch_not_reached.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 96.6 % 29 28
Test Date: 2025-07-03 19:05:49 Functions: 100.0 % 2 2
Legend: Lines: hit not hit

            Line data    Source code
       1              : // Copyright (c) 2011-2025  Made to Order Software Corp.  All Rights Reserved
       2              : //
       3              : // https://snapwebsites.org/project/snapdev
       4              : // contact@m2osw.com
       5              : //
       6              : // This program is free software: you can redistribute it and/or modify
       7              : // it under the terms of the GNU General Public License as published by
       8              : // the Free Software Foundation, either version 3 of the License, or
       9              : // (at your option) any later version.
      10              : //
      11              : // This program is distributed in the hope that it will be useful,
      12              : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      13              : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14              : // GNU General Public License for more details.
      15              : //
      16              : // You should have received a copy of the GNU General Public License
      17              : // along with this program.  If not, see <https://www.gnu.org/licenses/>.
      18              : 
      19              : /** \file
      20              :  * \brief Verify the unique_number class.
      21              :  *
      22              :  * This file implements tests to verify that the unique_number implementation
      23              :  * works properly even when multiple processes use it simultaneously.
      24              :  */
      25              : 
      26              : // file being tested
      27              : //
      28              : #include    <snapdev/not_reached.h>
      29              : 
      30              : 
      31              : // self
      32              : //
      33              : #include    "catch_main.h"
      34              : 
      35              : 
      36              : // C
      37              : //
      38              : #include    <signal.h>
      39              : #include    <sys/wait.h>
      40              : 
      41              : 
      42              : // last include
      43              : //
      44              : #include    <snapdev/poison.h>
      45              : 
      46              : 
      47              : 
      48              : namespace
      49              : {
      50              : 
      51              : 
      52              : 
      53              : extern "C" {
      54            2 : void catch_abort(int sig)
      55              : {
      56              :     static_cast<void>(sig);
      57            2 :     exit(0);
      58              : }
      59              : }
      60              : 
      61              : 
      62              : 
      63              : } // no name namespace
      64              : 
      65              : 
      66              : 
      67            2 : CATCH_TEST_CASE("not_reached", "[not_reached]")
      68              : {
      69            2 :     CATCH_START_SECTION("not_reached: verify that snapdev::NOT_REACHED() generates a SIGABRT")
      70              :     {
      71            1 :         pid_t const child(fork());
      72            2 :         CATCH_REQUIRE(child != -1);
      73              : 
      74            2 :         if(child == 0)
      75              :         {
      76              :             // we are the child
      77              :             //
      78            1 :             signal(SIGABRT, catch_abort);
      79            1 :             snapdev::NOT_REACHED();
      80              :             exit(1);
      81              :             CATCH_REQUIRE(!"this statement should never be reached");
      82              :         }
      83              : 
      84            1 :         int status(0);
      85            1 :         pid_t const r(waitpid(child, &status, 0));
      86            1 :         CATCH_REQUIRE(r == child);
      87            1 :         CATCH_REQUIRE(WIFEXITED(status));
      88            1 :         CATCH_REQUIRE(WEXITSTATUS(status) == 0);
      89              :     }
      90            2 :     CATCH_END_SECTION()
      91              : 
      92            2 :     CATCH_START_SECTION("not_reached: verify that snapdev::NOT_REACHED_IN_TEST() generates a SIGABRT when the sanitizer is active")
      93              :     {
      94            1 :         pid_t const child(fork());
      95            2 :         CATCH_REQUIRE(child != -1);
      96              : 
      97            2 :         if(child == 0)
      98              :         {
      99              :             // we are the child
     100              :             //
     101            1 :             signal(SIGABRT, catch_abort);
     102            1 :             snapdev::NOT_REACHED_IN_TEST();
     103            0 :             exit(1);
     104              :             CATCH_REQUIRE(!"this statement should never be reached");
     105              :         }
     106              : 
     107            1 :         int status(0);
     108            1 :         pid_t const r(waitpid(child, &status, 0));
     109            1 :         CATCH_REQUIRE(r == child);
     110            1 :         CATCH_REQUIRE(WIFEXITED(status));
     111              : #ifdef __SANITIZE_ADDRESS__
     112            1 :         CATCH_REQUIRE(WEXITSTATUS(status) == 0);
     113              : #else
     114              :         CATCH_REQUIRE(WEXITSTATUS(status) == 1);
     115              : #endif
     116              :     }
     117            2 :     CATCH_END_SECTION()
     118            2 : }
     119              : 
     120              : 
     121              : 
     122              : // vim: ts=4 sw=4 et
        

Generated by: LCOV version 2.0-1

Snap C++ | List of projects | List of versions