]> git.mxchange.org Git - simgear.git/blob - simgear/structure/function_list_test.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / structure / function_list_test.cxx
1 #define BOOST_TEST_MODULE structurce
2 #include <BoostTestTargetConfig.h>
3
4 #include "function_list.hxx"
5
6 static int func_called = 0,
7            func2_called = 0;
8
9 int func(int x)
10 {
11   ++func_called;
12   return x;
13 }
14
15 int func2(int x)
16 {
17   ++func2_called;
18   return 2 * x;
19 }
20
21 int func_add(int x, int y)
22 {
23   return func(x) + y;
24 }
25
26 int func2_add(int x, int y)
27 {
28   return func2(x) + y;
29 }
30
31 static int func_void_called = 0;
32 void func_void()
33 {
34   ++func_void_called;
35 }
36
37 BOOST_AUTO_TEST_CASE( create_and_call )
38 {
39   using namespace simgear;
40
41   function_list<int (int)> func_list;
42
43   BOOST_REQUIRE(func_list.empty());
44
45   func_list.push_back(&func);
46   BOOST_REQUIRE_EQUAL(func_list(2), 2);
47   BOOST_REQUIRE_EQUAL(func_called, 1);
48
49   func_list.push_back(&func2);
50   BOOST_REQUIRE_EQUAL(func_list(2), 4);
51   BOOST_REQUIRE_EQUAL(func_called, 2);
52   BOOST_REQUIRE_EQUAL(func2_called, 1);
53
54   function_list<boost::function<int (int)> > func_list2;
55   func_list2.push_back(&func);
56   func_list2.push_back(&func2);
57   func_list2.push_back(&func2);
58
59   BOOST_REQUIRE_EQUAL(func_list2(2), 4);
60   BOOST_REQUIRE_EQUAL(func_called, 3);
61   BOOST_REQUIRE_EQUAL(func2_called, 3);
62
63   // two parameters
64   function_list<boost::function<int (int, int)> > func_list3;
65   func_list3.push_back(&func_add);
66   func_list3.push_back(&func2_add);
67
68   BOOST_REQUIRE_EQUAL(func_list3(2, 3), 7);
69   BOOST_REQUIRE_EQUAL(func_called, 4);
70   BOOST_REQUIRE_EQUAL(func2_called, 4);
71
72   // returning void/no params
73   function_list<void()> void_func_list;
74   void_func_list.push_back(&func_void);
75   void_func_list();
76   BOOST_REQUIRE_EQUAL(func_void_called, 1);
77 }