LCOV - code coverage report
Current view: top level - tests - catch_stack_trace.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 31 100.0 %
Date: 2021-08-28 17:55:24 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2012-2021  Made to Order Software Corp.  All Rights Reserved
       2             : //
       3             : // https://snapwebsites.org/project/libexcept
       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 2 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 along
      17             : // with this program; if not, write to the Free Software Foundation, Inc.,
      18             : // 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
      19             : 
      20             : // self
      21             : //
      22             : #include    "catch_main.h"
      23             : 
      24             : 
      25             : // eventdispatcher lib
      26             : //
      27             : #include    <libexcept/exception.h>
      28             : 
      29             : 
      30             : 
      31             : 
      32             : 
      33             : namespace
      34             : {
      35             : 
      36             : 
      37             : 
      38             : class object
      39             : {
      40             : public:
      41           1 :     object(int count)
      42           1 :         : f_count(count)
      43             :     {
      44           1 :     }
      45             : 
      46             :     libexcept::stack_trace_t direct_stack_trace();
      47             : 
      48           1 :     int get_line() const
      49             :     {
      50           1 :         return f_line;
      51             :     }
      52             : 
      53             : private:
      54             :     int     f_count = 0;
      55             :     int     f_line = 0;
      56             : };
      57             : 
      58             : 
      59             : 
      60           1 : libexcept::stack_trace_t object::direct_stack_trace()
      61             : {
      62           1 :     f_line = __LINE__;
      63           1 :     return libexcept::collect_stack_trace_with_line_numbers(f_count);
      64             : }
      65             : 
      66             : 
      67             : 
      68             : }
      69             : 
      70             : 
      71           3 : CATCH_TEST_CASE("stack_trace", "[trace]")
      72             : {
      73           2 :     CATCH_START_SECTION("stack trace")
      74             :     {
      75           1 :         object o(5);
      76           2 :         libexcept::stack_trace_t stack(o.direct_stack_trace());
      77           1 :         int const local_line(__LINE__);
      78             : 
      79           1 :         CATCH_CHECK(stack.size() == 5);
      80             : 
      81             :         // in the coverage test (and if you're testing with the sanitizer
      82             :         // version) then we have an extra entry for the sanitizer
      83             :         //
      84           1 :         auto s(stack.begin());
      85           1 :         if(s->find("sanitizer") != std::string::npos)
      86             :         {
      87           1 :             ++s;
      88             :         }
      89             : 
      90             : //{
      91             : //for(auto l : stack)
      92             : //{
      93             : //std::cerr << "--- " << l << "\n";
      94             : //}
      95             : //}
      96             : 
      97             :         // first line is inside the libexcept function
      98             :         //
      99           1 :         CATCH_CHECK(s->find("stack_trace.cpp") != std::string::npos);
     100             : 
     101             :         // second line is here in our test, the direct_stack_trace() function
     102             :         //
     103           1 :         ++s;
     104           1 :         std::string::size_type pos(s->find("catch_stack_trace.cpp"));
     105           1 :         CATCH_CHECK(pos != std::string::npos);
     106           2 :         std::string line_number(s->substr(pos + 21 + 1));     // +1 to skip the ':'
     107           1 :         int line(atoi(line_number.c_str()));
     108             :         //bool correct_line(line == o.get_line() + 1
     109             :         //                || line == o.get_line() + 2);
     110           1 :         CATCH_CHECK((line == o.get_line() + 1
     111             :                         || line == o.get_line() + 2));
     112             : 
     113             :         // third line is in this very function
     114             :         //
     115           1 :         ++s;
     116           1 :         pos = s->find("catch_stack_trace.cpp");
     117           1 :         CATCH_CHECK(pos != std::string::npos);
     118           1 :         line_number = s->substr(pos + 21 + 1);     // +1 to skip the ':'
     119           1 :         line = atoi(line_number.c_str());
     120           1 :         CATCH_CHECK((line == local_line
     121             :                     || line == local_line - 1));
     122             : 
     123             :         // further lines are caller's info... that could change on a whimp
     124             :         // so we ignore them; but we already proved that our addr2line worked
     125             :     }
     126             :     CATCH_END_SECTION()
     127           7 : }
     128             : 
     129             : 
     130             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13