File: test_exception_catch.cc

package info (click to toggle)
libsigc%2B%2B-2.0 2.10.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,180 kB
  • sloc: sh: 4,246; cpp: 3,990; xml: 313; perl: 236; makefile: 174; ansic: 44
file content (84 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Copyright 2002, The libsigc++ Development Team
 *  Assigned to public domain.  Use as you wish without restriction.
 */

#include "testutilities.h"
#include <sigc++/adaptors/exception_catch.h>
#include <sstream>
#include <stdexcept>
#include <cstdlib>

namespace
{
std::ostringstream result_stream;

struct f : public sigc::functor_base
{
  typedef int result_type;

  int operator()(int i)
  {
    result_stream << "f(int " << i << ") ";
    throw std::range_error("out of range ");
  }
};

struct g : public sigc::functor_base
{
  typedef int result_type;

  int operator()()
  {
    result_stream << "g() ";
    throw std::range_error("out of range ");
  }
};

struct g_void : public sigc::functor_base
{
  typedef void result_type;

  void operator()()
  {
    result_stream << "g_void() ";
    throw std::range_error("out of range ");
  }
};

struct my_catch
{
  int operator()()
  {
    try
    {
      throw;
    }
    catch (const std::range_error& e) // catch what types we know
    {
      result_stream << "caught " << e.what();
    }
    return 1;
    // all else continues out.
  }
};

} // end anonymous namespace

int main(int argc, char* argv[])
{
  auto util = TestUtilities::get_instance();

  if (!util->check_command_args(argc, argv))
    return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;

  result_stream << sigc::exception_catch(f(), my_catch())(2);
  util->check_result(result_stream, "f(int 2) caught out of range 1");

  result_stream << sigc::exception_catch(g(), my_catch())();
  util->check_result(result_stream, "g() caught out of range 1");

  sigc::exception_catch(g_void(), my_catch())(); // void test
  util->check_result(result_stream, "g_void() caught out of range ");

  return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
}