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
10#include <boost/signals2.hpp>
11#define BOOST_TEST_MODULE ordering_test
12#include <boost/test/included/unit_test.hpp>
13#include <iostream>
14#include <vector>
15#include <algorithm>
16#include <cstdlib>
17#include <ctime>
18#include <functional>
19
20std::vector<int> valuesOutput;
21bool ungrouped1 = false;
22bool ungrouped2 = false;
23bool ungrouped3 = false;
24
25struct 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(x: value);
32 std::cout << value << ' ';
33 }
34
35private:
36 int value;
37};
38
39struct write_ungrouped1 {
40 void operator()() const
41 {
42 BOOST_CHECK(!ungrouped1);
43 ungrouped1 = true;
44 std::cout << "(Ungrouped #1)" << ' ';
45 }
46};
47
48struct write_ungrouped2 {
49 void operator()() const
50 {
51 BOOST_CHECK(!ungrouped2);
52 ungrouped2 = true;
53 std::cout << "(Ungrouped #2)" << ' ';
54 }
55};
56
57struct write_ungrouped3 {
58 void operator()() const
59 {
60 BOOST_CHECK(!ungrouped3);
61 ungrouped3 = true;
62 std::cout << "(Ungrouped #3)" << ' ';
63 }
64};
65
66int return_argument(int x)
67{
68 return x;
69}
70
71void 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( group: 1, slot: boost::bind( f: &return_argument, a1: 1) );
82 sig.connect( group: 2, slot: boost::bind( f: &return_argument, a1: 2) );
83
84 BOOST_CHECK(sig() == 1);
85}
86
87BOOST_AUTO_TEST_CASE(test_main)
88{
89 using namespace std;
90 srand(seed: static_cast<unsigned>(time(timer: 0)));
91
92 std::vector<int> sortedValues;
93
94 boost::signals2::signal<void ()> sig;
95 sig.connect(slot: 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(x: v);
103 sig.connect(group: v, slot: emit_int(v));
104
105 if (i == 50) {
106 sig.connect(slot: write_ungrouped2());
107 }
108 }
109 sig.connect(slot: write_ungrouped3());
110
111 std::sort(first: sortedValues.begin(), last: sortedValues.end());
112
113 // 17 at beginning, 42 at end
114 sortedValues.insert(position: sortedValues.begin(), x: 17);
115 sig.connect(slot: emit_int(17), position: boost::signals2::at_front);
116 sortedValues.push_back(x: 42);
117 sig.connect(slot: 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();
128}
129

source code of boost/libs/signals2/test/ordering_test.cpp