ThreadPoolTest.cc 960 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <boost/function.hpp>
  2. #include <boost/bind.hpp>
  3. #include <boost/threadpool.hpp>
  4. #include <gtest/gtest.h>
  5. #include <glog/logging.h>
  6. #include <gflags/gflags.h>
  7. namespace Egametang {
  8. class ThreadPoolTest: public testing::Test
  9. {
  10. protected:
  11. boost::threadpool::fifo_pool pool;
  12. public:
  13. ThreadPoolTest() : pool(10)
  14. {
  15. }
  16. virtual ~ThreadPoolTest()
  17. {
  18. }
  19. void Max(int a, int b, int* z)
  20. {
  21. *z = a > b? a : b;
  22. }
  23. };
  24. TEST_F(ThreadPoolTest, Test1)
  25. {
  26. std::vector<int> x(100, 8);
  27. std::vector<int> y(100, 9);
  28. std::vector<int> z(100, 0);
  29. for (int i = 0; i < 100; ++i)
  30. {
  31. pool.schedule(
  32. boost::bind(&ThreadPoolTest::Max,
  33. this, x[i], y[i], &z[i]));
  34. }
  35. pool.wait();
  36. for (int i = 0; i < 100; ++i)
  37. {
  38. ASSERT_EQ(9, z[i]);
  39. }
  40. }
  41. } // namespace Egametang
  42. int main(int argc, char* argv[])
  43. {
  44. testing::InitGoogleTest(&argc, argv);
  45. google::InitGoogleLogging(argv[0]);
  46. google::ParseCommandLineFlags(&argc, &argv, true);
  47. return RUN_ALL_TESTS();
  48. }