In tests use BOOST_AUTO_UNIT_TEST instead of BOOST_AUTO_TEST_CASE
[metaproxy-moved-to-github.git] / src / test_boost_threads.cpp
1 /* $Id: test_boost_threads.cpp,v 1.6 2005-12-02 12:21:07 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <boost/thread/mutex.hpp>
9 #include <boost/thread/thread.hpp>
10
11 #define BOOST_AUTO_TEST_MAIN
12 #include <boost/test/auto_unit_test.hpp>
13
14 #include <list>
15 #include <iostream>
16
17 class counter
18 {
19    public:
20       counter() : count(0) { }
21       
22     int increment() {
23         boost::mutex::scoped_lock scoped_lock(mutex);
24         return ++count;
25     }
26     
27 private:
28     boost::mutex mutex;
29     int count;
30 };
31
32
33 counter c;
34
35 class worker {
36 public:
37     void operator() (void) {
38         c.increment();
39     }
40 };
41
42 #define USE_GROUP 1
43
44
45 BOOST_AUTO_UNIT_TEST( thread_group )
46 {
47     try 
48     {
49         const int num_threads = 4;
50         boost::thread_group thrds;
51         
52         for (int i=0; i < num_threads; ++i)
53         {
54             worker w;
55             thrds.add_thread(new boost::thread(w));
56         }
57         thrds.join_all();
58     }
59     catch (...) 
60     {
61         BOOST_CHECK(false);
62     }
63     BOOST_CHECK(c.increment() == 5);
64 }
65
66
67 BOOST_AUTO_UNIT_TEST( thread_list )
68 {
69     try 
70     {
71         const int num_threads = 4;
72         std::list<boost::thread *> thread_list;
73         
74         for (int i=0; i < num_threads; ++i)
75         {
76             worker w;
77             thread_list.push_back(new boost::thread(w));
78         }
79         std::list<boost::thread *>::iterator it;
80         for (it = thread_list.begin(); it != thread_list.end(); it++)
81         {
82             (*it)->join();
83             delete *it;
84         }
85
86     }
87     catch (...) 
88     {
89         BOOST_CHECK(false);
90     }
91     BOOST_CHECK(c.increment() == 10);
92 }
93
94
95
96 /*
97  * Local variables:
98  * c-basic-offset: 4
99  * indent-tabs-mode: nil
100  * c-file-style: "stroustrup"
101  * End:
102  * vim: shiftwidth=4 tabstop=8 expandtab
103  */