File: test_thread_launching.cpp

package info (click to toggle)
boost1.42 1.42.0-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 277,864 kB
  • ctags: 401,076
  • sloc: cpp: 1,235,659; xml: 74,142; ansic: 41,313; python: 26,756; sh: 11,840; cs: 2,118; makefile: 655; perl: 494; yacc: 456; asm: 353; csh: 6
file content (226 lines) | stat: -rw-r--r-- 5,436 bytes parent folder | download | duplicates (2)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (C) 2007-8 Anthony Williams
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying 
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/thread/thread.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/ref.hpp>
#include <boost/utility.hpp>
#include <string>
#include <vector>

bool normal_function_called=false;

void normal_function()
{
    normal_function_called=true;
}

void test_thread_function_no_arguments()
{
    boost::thread function(normal_function);
    function.join();
    BOOST_CHECK(normal_function_called);
}

int nfoa_res=0;

void normal_function_one_arg(int i)
{
    nfoa_res=i;
}

void test_thread_function_one_argument()
{
    boost::thread function(normal_function_one_arg,42);
    function.join();
    BOOST_CHECK_EQUAL(42,nfoa_res);
}

struct callable_no_args
{
    static bool called;
    
    void operator()() const
    {
        called=true;
    }
};

bool callable_no_args::called=false;

void test_thread_callable_object_no_arguments()
{
    callable_no_args func;
    boost::thread callable(func);
    callable.join();
    BOOST_CHECK(callable_no_args::called);
}

struct callable_noncopyable_no_args:
    boost::noncopyable
{
    static bool called;
    
    void operator()() const
    {
        called=true;
    }
};

bool callable_noncopyable_no_args::called=false;

void test_thread_callable_object_ref_no_arguments()
{
    callable_noncopyable_no_args func;
    
    boost::thread callable(boost::ref(func));
    callable.join();
    BOOST_CHECK(callable_noncopyable_no_args::called);
}

struct callable_one_arg
{
    static bool called;
    static int called_arg;
    
    void operator()(int arg) const
    {
        called=true;
        called_arg=arg;
    }
};

bool callable_one_arg::called=false;
int callable_one_arg::called_arg=0;

void test_thread_callable_object_one_argument()
{
    callable_one_arg func;
    boost::thread callable(func,42);
    callable.join();
    BOOST_CHECK(callable_one_arg::called);
    BOOST_CHECK_EQUAL(callable_one_arg::called_arg,42);
}

struct callable_multiple_arg
{
    static bool called_two;
    static int called_two_arg1;
    static double called_two_arg2;
    static bool called_three;
    static std::string called_three_arg1;
    static std::vector<int> called_three_arg2;
    static int called_three_arg3;
    
    void operator()(int arg1,double arg2) const
    {
        called_two=true;
        called_two_arg1=arg1;
        called_two_arg2=arg2;
    }
    void operator()(std::string const& arg1,std::vector<int> const& arg2,int arg3) const
    {
        called_three=true;
        called_three_arg1=arg1;
        called_three_arg2=arg2;
        called_three_arg3=arg3;
    }
};

bool callable_multiple_arg::called_two=false;
bool callable_multiple_arg::called_three=false;
int callable_multiple_arg::called_two_arg1;
double callable_multiple_arg::called_two_arg2;
std::string callable_multiple_arg::called_three_arg1;
std::vector<int> callable_multiple_arg::called_three_arg2;
int callable_multiple_arg::called_three_arg3;

void test_thread_callable_object_multiple_arguments()
{
    std::vector<int> x;
    for(unsigned i=0;i<7;++i)
    {
        x.push_back(i*i);
    }

    callable_multiple_arg func;
    
    boost::thread callable3(func,"hello",x,1.2);
    callable3.join();
    BOOST_CHECK(callable_multiple_arg::called_three);
    BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg1,"hello");
    BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg2.size(),x.size());
    for(unsigned j=0;j<x.size();++j)
    {
        BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg2.at(j),x[j]);
    }
    
    BOOST_CHECK_EQUAL(callable_multiple_arg::called_three_arg3,1);

    double const dbl=1.234;
    
    boost::thread callable2(func,19,dbl);
    callable2.join();
    BOOST_CHECK(callable_multiple_arg::called_two);
    BOOST_CHECK_EQUAL(callable_multiple_arg::called_two_arg1,19);
    BOOST_CHECK_EQUAL(callable_multiple_arg::called_two_arg2,dbl);
}

struct X
{
    bool function_called;
    int arg_value;

    X():
        function_called(false),
        arg_value(0)
    {}
    

    void f0()
    {
        function_called=true;
    }

    void f1(int i)
    {
        arg_value=i;
    }

};

void test_thread_member_function_no_arguments()
{
    X x;
    
    boost::thread function(&X::f0,&x);
    function.join();
    BOOST_CHECK(x.function_called);
}


void test_thread_member_function_one_argument()
{
    X x;
    boost::thread function(&X::f1,&x,42);
    function.join();
    BOOST_CHECK_EQUAL(42,x.arg_value);
}


boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
{
    boost::unit_test_framework::test_suite* test =
        BOOST_TEST_SUITE("Boost.Threads: thread launching test suite");

    test->add(BOOST_TEST_CASE(test_thread_function_no_arguments));
    test->add(BOOST_TEST_CASE(test_thread_function_one_argument));
    test->add(BOOST_TEST_CASE(test_thread_callable_object_no_arguments));
    test->add(BOOST_TEST_CASE(test_thread_callable_object_ref_no_arguments));
    test->add(BOOST_TEST_CASE(test_thread_callable_object_one_argument));
    test->add(BOOST_TEST_CASE(test_thread_callable_object_multiple_arguments));
    test->add(BOOST_TEST_CASE(test_thread_member_function_no_arguments));
    test->add(BOOST_TEST_CASE(test_thread_member_function_one_argument));
    return test;
}