ThreadTest.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /******************************************************************
  2. *
  3. * uEcho for C
  4. *
  5. * Copyright (C) Satoshi Konno 2015
  6. *
  7. * This is licensed under BSD-style license, see file COPYING.
  8. *
  9. ******************************************************************/
  10. #include <boost/test/unit_test.hpp>
  11. #include <boost/thread.hpp>
  12. #include <uecho/util/thread.h>
  13. #include <uecho/util/timer.h>
  14. const int THREAD_TEST_LOOP_NUM = 10;
  15. void test_theread_func(uEchoThread* thread)
  16. {
  17. int* test_counter = (int*)uecho_thread_getuserdata(thread);
  18. for (int n = 0; n < THREAD_TEST_LOOP_NUM; n++) {
  19. (*test_counter)++;
  20. }
  21. }
  22. BOOST_AUTO_TEST_CASE(ThreadTest)
  23. {
  24. uEchoThread* thread = uecho_thread_new();
  25. int test_counter = 0;
  26. uecho_thread_setaction(thread, test_theread_func);
  27. uecho_thread_setuserdata(thread, &test_counter);
  28. BOOST_CHECK_EQUAL(uecho_thread_start(thread), true);
  29. while (test_counter != THREAD_TEST_LOOP_NUM) {
  30. uecho_sleep(100);
  31. }
  32. BOOST_CHECK_EQUAL(test_counter, THREAD_TEST_LOOP_NUM);
  33. BOOST_CHECK_EQUAL(uecho_thread_stop(thread), true);
  34. uecho_thread_delete(thread);
  35. }