]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Signals2 library |
2 | ||
3 | // Copyright Douglas Gregor 2002-2004. Use, modification and | |
4 | // distribution is subject to the Boost Software License, Version | |
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt) | |
7 | ||
8 | // For more information, see http://www.boost.org | |
9 | ||
7c673cae | 10 | #include <boost/signals2.hpp> |
f67539c2 TL |
11 | #define BOOST_TEST_MODULE ordering_test |
12 | #include <boost/test/included/unit_test.hpp> | |
7c673cae FG |
13 | #include <iostream> |
14 | #include <vector> | |
15 | #include <algorithm> | |
16 | #include <cstdlib> | |
17 | #include <ctime> | |
18 | #include <functional> | |
19 | ||
20 | std::vector<int> valuesOutput; | |
21 | bool ungrouped1 = false; | |
22 | bool ungrouped2 = false; | |
23 | bool ungrouped3 = false; | |
24 | ||
25 | struct emit_int { | |
26 | emit_int(int v) : value(v) {} | |
27 | ||
28 | void operator()() const | |
29 | { | |
30 | BOOST_CHECK(value == 42 || (!ungrouped1 && !ungrouped2 && !ungrouped3)); | |
31 | valuesOutput.push_back(value); | |
32 | std::cout << value << ' '; | |
33 | } | |
34 | ||
35 | private: | |
36 | int value; | |
37 | }; | |
38 | ||
39 | struct write_ungrouped1 { | |
40 | void operator()() const | |
41 | { | |
42 | BOOST_CHECK(!ungrouped1); | |
43 | ungrouped1 = true; | |
44 | std::cout << "(Ungrouped #1)" << ' '; | |
45 | } | |
46 | }; | |
47 | ||
48 | struct write_ungrouped2 { | |
49 | void operator()() const | |
50 | { | |
51 | BOOST_CHECK(!ungrouped2); | |
52 | ungrouped2 = true; | |
53 | std::cout << "(Ungrouped #2)" << ' '; | |
54 | } | |
55 | }; | |
56 | ||
57 | struct write_ungrouped3 { | |
58 | void operator()() const | |
59 | { | |
60 | BOOST_CHECK(!ungrouped3); | |
61 | ungrouped3 = true; | |
62 | std::cout << "(Ungrouped #3)" << ' '; | |
63 | } | |
64 | }; | |
65 | ||
66 | int return_argument(int x) | |
67 | { | |
68 | return x; | |
69 | } | |
70 | ||
71 | void test_group_compare() | |
72 | { | |
73 | boost::signals2::signal | |
74 | < | |
75 | int (), | |
76 | boost::signals2::last_value<int>, | |
77 | int, | |
78 | std::greater< int > | |
79 | > sig; | |
80 | ||
81 | sig.connect( 1, boost::bind( &return_argument, 1) ); | |
82 | sig.connect( 2, boost::bind( &return_argument, 2) ); | |
83 | ||
84 | BOOST_CHECK(sig() == 1); | |
85 | } | |
86 | ||
f67539c2 | 87 | BOOST_AUTO_TEST_CASE(test_main) |
7c673cae FG |
88 | { |
89 | using namespace std; | |
20effc67 | 90 | srand(static_cast<unsigned>(time(0))); |
7c673cae FG |
91 | |
92 | std::vector<int> sortedValues; | |
93 | ||
94 | boost::signals2::signal<void ()> sig; | |
95 | sig.connect(write_ungrouped1()); | |
96 | for (int i = 0; i < 100; ++i) { | |
97 | #ifdef BOOST_NO_STDC_NAMESPACE | |
98 | int v = rand() % 100; | |
99 | #else | |
100 | int v = std::rand() % 100; | |
101 | #endif | |
102 | sortedValues.push_back(v); | |
103 | sig.connect(v, emit_int(v)); | |
104 | ||
105 | if (i == 50) { | |
106 | sig.connect(write_ungrouped2()); | |
107 | } | |
108 | } | |
109 | sig.connect(write_ungrouped3()); | |
110 | ||
111 | std::sort(sortedValues.begin(), sortedValues.end()); | |
112 | ||
113 | // 17 at beginning, 42 at end | |
114 | sortedValues.insert(sortedValues.begin(), 17); | |
115 | sig.connect(emit_int(17), boost::signals2::at_front); | |
116 | sortedValues.push_back(42); | |
117 | sig.connect(emit_int(42)); | |
118 | ||
119 | sig(); | |
120 | std::cout << std::endl; | |
121 | ||
122 | BOOST_CHECK(valuesOutput == sortedValues); | |
123 | BOOST_CHECK(ungrouped1); | |
124 | BOOST_CHECK(ungrouped2); | |
125 | BOOST_CHECK(ungrouped3); | |
126 | ||
127 | test_group_compare(); | |
7c673cae | 128 | } |