Cambridge SMT System
multithreading.gtest.cpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use these files except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 // Copyright 2012 - Gonzalo Iglesias, AdriĆ  de Gispert, William Byrne
14 
21 #include <googletesting.h>
22 
23 #ifndef GMAINTEST
24 #include "main.custom_assert.hpp"
25 #include "main.logger.hpp"
26 #endif
27 
28 #include "multithreading.hpp"
29 
30 extern bool user_check_ok;
31 
32 namespace googletesting {
33 
34 struct functor_test {
35  unsigned silly_;
36  functor_test ( unsigned a = 0 ) : silly_ ( a ) {};
37  void operator() () {
38  silly_ *= 10;
39  };
40 };
41 
42 struct functor_test2 {
43  unsigned *silly_;
44  functor_test2 ( unsigned a ) : silly_ ( new unsigned ( a ) ) {
45  LINFO ("Hello " << uu::toString (*silly_) );
46  };
47  void operator() () {
48  *silly_ *= 10;
49  LINFO ( "silly=" << *silly_ );
50  };
51 };
52 
53 };
54 
58 
59 TEST ( multithreading, threadpool1 ) {
60  googletesting::functor_test a1 ( 1 ), a2 ( 2 ), a3 ( 3 ), a4 ( 4 );
61  {
62  uu::TrivialThreadPool tp ( 4 );
63  tp ( a1 );
64  tp ( a2 );
65  tp ( a3 );
66  tp ( a4 );
67  }
68  EXPECT_EQ ( a1.silly_, 1 );
69  EXPECT_EQ ( a2.silly_, 2 );
70  EXPECT_EQ ( a3.silly_, 3 );
71  EXPECT_EQ ( a4.silly_, 4 );
72 }
73 
76 TEST ( multithreading, threadpool2 ) {
81  {
82  uu::TrivialThreadPool tp ( 4 );
83  tp ( a1 );
84  tp ( a2 );
85  tp ( a3 );
86  tp ( a4 );
87  }
89  EXPECT_EQ ( *a1.silly_, 10 );
90  EXPECT_EQ ( *a2.silly_, 20 );
91  EXPECT_EQ ( *a3.silly_, 30 );
92  EXPECT_EQ ( *a4.silly_, 40 );
93  //Extra careful memory management...
94  delete a1.silly_;
95  delete a2.silly_;
96  delete a3.silly_;
97  delete a4.silly_;
98  user_check_ok = true;
100  {
101  uu::TrivialThreadPool tp ( 0 );
102  }
103  EXPECT_EQ ( user_check_ok, false );
104  user_check_ok = true;
105 }
106 
107 #ifndef GMAINTEST
108 
113 int main ( int argc, char **argv ) {
114  ::testing::InitGoogleTest ( &argc, argv );
115  return RUN_ALL_TESTS();
116 }
117 #endif
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
#define LINFO(msg)
int main(int argc, char **argv)
main function. If compiled individualy, will kickoff any tests in this file.
bool user_check_ok
Trivial implementation of a threadpool based on boost::asio methods When initiated, creates a threadpool of n threads (n <= number of cpus). Jobs should be submitted with the templated operator(). When the object is deleted it will wait for all threads to finish.
TEST(FstIo, basic_test)
Definition: fstio.gtest.cpp:38
test-specific classes and functions
Definition: fstio.gtest.cpp:34
Static variables for logger. Include only once from main file.
Implements trivial threadpool using boost::asio library.
Unit testing: google testing common header.
Static variable for custom_assert. Include only once from main file.