]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - tests/test_exception_catch.cc
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / tests / test_exception_catch.cc
1 /* Copyright 2002, The libsigc++ Development Team
2  *  Assigned to public domain.  Use as you wish without restriction.
3  */
4
5 #include "testutilities.h"
6 #include <sigc++/adaptors/exception_catch.h>
7 #include <sstream>
8 #include <stdexcept>
9 #include <cstdlib>
10
11 namespace
12 {
13 std::ostringstream result_stream;
14
15 struct f : public sigc::functor_base
16 {
17   typedef int result_type;
18
19   int operator()(int i)
20   {
21     result_stream << "f(int " << i << ") ";
22     throw std::range_error("out of range ");
23   }
24 };
25
26 struct g : public sigc::functor_base
27 {
28   typedef int result_type;
29
30   int operator()()
31   {
32     result_stream << "g() ";
33     throw std::range_error("out of range ");
34   }
35 };
36
37 struct g_void : public sigc::functor_base
38 {
39   typedef void result_type;
40
41   void operator()()
42   {
43     result_stream << "g_void() ";
44     throw std::range_error("out of range ");
45   }
46 };
47
48 struct my_catch
49 {
50   int operator()()
51   {
52     try
53     {
54       throw;
55     }
56     catch (const std::range_error& e) // catch what types we know
57     {
58       result_stream << "caught " << e.what();
59     }
60     return 1;
61     // all else continues out.
62   }
63 };
64
65 } // end anonymous namespace
66
67 int main(int argc, char* argv[])
68 {
69   auto util = TestUtilities::get_instance();
70
71   if (!util->check_command_args(argc, argv))
72     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
73
74   result_stream << sigc::exception_catch(f(), my_catch())(2);
75   util->check_result(result_stream, "f(int 2) caught out of range 1");
76
77   result_stream << sigc::exception_catch(g(), my_catch())();
78   util->check_result(result_stream, "g() caught out of range 1");
79
80   sigc::exception_catch(g_void(), my_catch())(); // void test
81   util->check_result(result_stream, "g_void() caught out of range ");
82
83   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
84 }