ThreadPoolTest.cc 799 B

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