PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/tests/coroutine.cpp

http://github.com/mozy/mordor
C++ | 65 lines | 56 code | 8 blank | 1 comment | 10 complexity | ce9f963a3aaf98a21fee46b51fd9cbba MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (c) 2009 - Mozy, Inc.
  2. #include "mordor/coroutine.h"
  3. #include "mordor/test/test.h"
  4. using namespace Mordor;
  5. using namespace Mordor::Test;
  6. static void countTo5(Coroutine<int> &self)
  7. {
  8. self.yield(1);
  9. self.yield(2);
  10. self.yield(3);
  11. self.yield(4);
  12. self.yield(5);
  13. }
  14. MORDOR_UNITTEST(Coroutine, basic)
  15. {
  16. Coroutine<int> coro(&countTo5);
  17. int sequence = 0;
  18. MORDOR_TEST_ASSERT_EQUAL(coro.state(), Fiber::INIT);
  19. int value;
  20. while (true) {
  21. value = coro.call();
  22. if (coro.state() == Fiber::TERM)
  23. break;
  24. MORDOR_TEST_ASSERT_EQUAL(value, ++sequence);
  25. }
  26. MORDOR_TEST_ASSERT_EQUAL(value, 0);
  27. MORDOR_TEST_ASSERT_EQUAL(sequence, 5);
  28. }
  29. static void echo(Coroutine<int, int> &self, int arg)
  30. {
  31. while (arg <= 5) {
  32. arg = self.yield(arg);
  33. }
  34. }
  35. MORDOR_UNITTEST(Coroutine, basicWithArg)
  36. {
  37. Coroutine<int, int> coro(&echo);
  38. MORDOR_TEST_ASSERT_EQUAL(coro.state(), Fiber::INIT);
  39. for (int i = 0; i <= 5; ++i) {
  40. MORDOR_TEST_ASSERT(coro.state() == Fiber::INIT || coro.state() == Fiber::HOLD);
  41. MORDOR_TEST_ASSERT_EQUAL(coro.call(i), i);
  42. }
  43. }
  44. static void countTo5Arg(Coroutine<void, int> &self, int arg)
  45. {
  46. int sequence = 0;
  47. for (int i = 0; i < 5; ++i) {
  48. MORDOR_TEST_ASSERT_EQUAL(arg, sequence++);
  49. arg = self.yield();
  50. }
  51. }
  52. MORDOR_UNITTEST(Coroutine, voidWithArg)
  53. {
  54. Coroutine<void, int> coro(&countTo5Arg);
  55. for (int i = 0; i < 5; ++i)
  56. coro.call(i);
  57. }