2
0
mirror of https://github.com/boostorg/thread.git synced 2026-02-09 11:32:12 +00:00
Files
thread/test/test_thread_launching.cpp
Eric Niebler 2252441d0e Merged revisions 43518-43532 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r43523 | eric_niebler | 2008-03-05 17:56:57 -0800 (Wed, 05 Mar 2008) | 1 line
  
  fix documentation about droppable, replace accumulator_set_wrapper example with bind() and ref()
........
  r43524 | hkaiser | 2008-03-05 19:28:47 -0800 (Wed, 05 Mar 2008) | 1 line
  
  Wave: Fixed #1673.
........
  r43525 | emildotchevski | 2008-03-05 21:08:22 -0800 (Wed, 05 Mar 2008) | 1 line
  
  hooking up boost exception for testing
........
  r43526 | vladimir_prus | 2008-03-05 22:06:17 -0800 (Wed, 05 Mar 2008) | 6 lines
  
  Add 'pythonpath' feature to specify additional PYTHONPATH elements.
  Make bpl-test correctly set dependencies if used with multiple .pyd
  sources.
  
  Patch from Joshua Napoli.
........
  r43527 | vladimir_prus | 2008-03-05 22:57:27 -0800 (Wed, 05 Mar 2008) | 6 lines
  
  Arrange for HPP source to be scanned for #includes, too.
  This fixes PCH not being rebuild when some other headers
  included from HPP being compiled change.
  
  Thanks to Pierre-Luc Neron for the bug report.
........
  r43528 | anthonyw | 2008-03-05 23:59:16 -0800 (Wed, 05 Mar 2008) | 1 line
  
  made the callable_no_args function object a named object rather than a temporary, in order to avoid gratuitous breakage on some compilers
........
  r43530 | eric_niebler | 2008-03-06 09:56:42 -0800 (Thu, 06 Mar 2008) | 1 line
  
  work around msvc-7.1 bug
........
  r43531 | eric_niebler | 2008-03-06 10:07:22 -0800 (Thu, 06 Mar 2008) | 1 line
  
  fix warnings
........


[SVN r43533]
2008-03-06 23:03:09 +00:00

166 lines
4.3 KiB
C++

// Copyright (C) 2007 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);
}
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()
{
boost::thread callable(callable_one_arg(),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);
}
boost::thread callable3(callable_multiple_arg(),"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(callable_multiple_arg(),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);
}
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_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));
return test;
}