ThreadTest.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <boost/test/unit_test.hpp>
  2. #include "../Thread.h"
  3. #include "../Mutex.h"
  4. using namespace vconnect;
  5. using namespace std;
  6. BOOST_AUTO_TEST_SUITE(ThreadTest)
  7. class ByRef
  8. {
  9. public:
  10. int value;
  11. };
  12. static string result = "";
  13. static Mutex *mutex = NULL;
  14. static ThreadWorkerReturnType ThreadWorkerDeclspec taskA( void *argument )
  15. {
  16. int *count = (int *)argument;
  17. mutex->lock();
  18. for( int i = 0; i < *count; i++ ){
  19. Thread::sleep( 1 );
  20. result += "a";
  21. }
  22. mutex->unlock();
  23. Thread::tellThreadEnd();
  24. return NULL;
  25. }
  26. static ThreadWorkerReturnType ThreadWorkerDeclspec taskB( void *argument )
  27. {
  28. int *count = (int *)argument;
  29. mutex->lock();
  30. for( int i = 0; i < *count; i++ ){
  31. Thread::sleep( 1 );
  32. result += "b";
  33. }
  34. mutex->unlock();
  35. Thread::tellThreadEnd();
  36. return NULL;
  37. }
  38. BOOST_AUTO_TEST_CASE(test)
  39. {
  40. result = "";
  41. int count = 5;
  42. mutex = new Mutex();
  43. Thread a( &taskA, &count );
  44. Thread b( &taskB, &count );
  45. a.join();
  46. b.join();
  47. string expected = "";
  48. for( int i = 0; i < count; i++ ){
  49. expected += "a";
  50. }
  51. for( int i = 0; i < count; i++ ){
  52. expected += "b";
  53. }
  54. BOOST_CHECK_EQUAL( expected, result );
  55. }
  56. BOOST_AUTO_TEST_SUITE_END()