20-queue.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: MIT
  2. // SPDX-FileCopyrightText: 2022 Ivan Baidakou
  3. #if defined(__ANDROID__)
  4. #undef __ANDROID__
  5. #endif
  6. #include "catch.hpp"
  7. #include "rotor-light/queue.hpp"
  8. using namespace rotor_light;
  9. struct StringMessage : Message {
  10. static constexpr MessageTypeId type_id = __LINE__;
  11. StringMessage(ActorId to, const char *msg_) : Message(to), string{msg_} {}
  12. const char *string;
  13. };
  14. using MessageStorage = traits::MessageStorage<StringMessage>;
  15. void reset(ItemGuard &guard) { guard.item_queue->release(); }
  16. TEST_CASE("1 priority, 1 message queue", "[queue]") {
  17. using AppQueue = Queue<MessageStorage, 1>;
  18. AppQueue queue;
  19. printf("queue sz = %lu, message sz = %lu\n", sizeof(queue),
  20. MessageStorage::item_size);
  21. #ifdef ROTOR_LIGHT_QUEUE_SZ
  22. CHECK(queue.max_items(0) == 0);
  23. #endif
  24. CHECK(queue.put<StringMessage, false>(0, 1, "hello"));
  25. #ifdef ROTOR_LIGHT_QUEUE_SZ
  26. CHECK(queue.max_items(0) == 1);
  27. #endif
  28. CHECK(!queue.put<StringMessage, false>(0, 1, "world"));
  29. auto msg = queue.next();
  30. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  31. reset(msg);
  32. CHECK(queue.put<StringMessage, false>(0, 1, "world"));
  33. CHECK(!queue.put<StringMessage, false>(0, 1, "hello"));
  34. msg = queue.next();
  35. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  36. reset(msg);
  37. CHECK(!queue.next());
  38. #ifdef ROTOR_LIGHT_QUEUE_SZ
  39. CHECK(queue.max_items(0) == 1);
  40. #endif
  41. }
  42. TEST_CASE("1 priority, 2 messages queue", "[queue]") {
  43. using AppQueue = Queue<MessageStorage, 2>;
  44. AppQueue queue;
  45. printf("queue sz = %lu, message sz = %lu\n", sizeof(queue),
  46. MessageStorage::item_size);
  47. CHECK(queue.put<StringMessage, false>(0, 1, "hello"));
  48. CHECK(queue.put<StringMessage, false>(0, 1, "world"));
  49. CHECK(!queue.put<StringMessage, false>(0, 1, "!"));
  50. auto msg = queue.next();
  51. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  52. reset(msg);
  53. msg = queue.next();
  54. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  55. reset(msg);
  56. CHECK(queue.put<StringMessage, false>(0, 1, "world"));
  57. CHECK(queue.put<StringMessage, false>(0, 1, "hello"));
  58. CHECK(!queue.put<StringMessage, false>(0, 1, "!"));
  59. msg = queue.next();
  60. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  61. reset(msg);
  62. msg = queue.next();
  63. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  64. reset(msg);
  65. CHECK(!queue.next());
  66. #ifdef ROTOR_LIGHT_QUEUE_SZ
  67. CHECK(queue.max_items(0) == 2);
  68. #endif
  69. }
  70. TEST_CASE("1 priority, 3 messages queue, force put", "[queue]") {
  71. using AppQueue = Queue<MessageStorage, 3>;
  72. AppQueue queue;
  73. CHECK(!queue.put<StringMessage, true>(0, 1, "hello"));
  74. CHECK(!queue.put<StringMessage, true>(0, 1, "my"));
  75. CHECK(!queue.put<StringMessage, true>(0, 1, "world"));
  76. CHECK(queue.put<StringMessage, true>(0, 1, "!"));
  77. auto msg = queue.next();
  78. CHECK(static_cast<StringMessage &>(*msg).string == std::string("my"));
  79. reset(msg);
  80. msg = queue.next();
  81. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  82. reset(msg);
  83. msg = queue.next();
  84. CHECK(static_cast<StringMessage &>(*msg).string == std::string("!"));
  85. reset(msg);
  86. CHECK(!queue.next());
  87. #ifdef ROTOR_LIGHT_QUEUE_SZ
  88. CHECK(queue.max_items(0) == 3);
  89. #endif
  90. }
  91. TEST_CASE("1 priority, 3 messages queue", "[queue]") {
  92. using AppQueue = Queue<MessageStorage, 3>;
  93. AppQueue queue;
  94. CHECK(queue.put<StringMessage, false>(0, 1, "hello"));
  95. auto msg = queue.next();
  96. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  97. CHECK(queue.put<StringMessage, false>(0, 1, "world"));
  98. reset(msg);
  99. msg = queue.next();
  100. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  101. }
  102. TEST_CASE("2 priorities, 2/1 messages", "[queue]") {
  103. using AppQueue = Queue<MessageStorage, 2, 1>;
  104. AppQueue queue;
  105. printf("queue sz = %lu, message sz = %lu\n", sizeof(queue),
  106. MessageStorage::item_size);
  107. SECTION("order-1") {
  108. CHECK(queue.put<StringMessage, false>(0, 1, "lorem"));
  109. CHECK(queue.put<StringMessage, false>(0, 1, "ipsum"));
  110. CHECK(queue.put<StringMessage, false>(1, 1, "dolor"));
  111. }
  112. SECTION("order-2") {
  113. CHECK(queue.put<StringMessage, false>(1, 1, "dolor"));
  114. CHECK(queue.put<StringMessage, false>(0, 1, "lorem"));
  115. CHECK(queue.put<StringMessage, false>(0, 1, "ipsum"));
  116. }
  117. SECTION("order-3") {
  118. CHECK(queue.put<StringMessage, false>(0, 1, "lorem"));
  119. CHECK(queue.put<StringMessage, false>(1, 1, "dolor"));
  120. CHECK(queue.put<StringMessage, false>(0, 1, "ipsum"));
  121. }
  122. auto msg = queue.next();
  123. CHECK(static_cast<StringMessage &>(*msg).string == std::string("dolor"));
  124. reset(msg);
  125. msg = queue.next();
  126. CHECK(static_cast<StringMessage &>(*msg).string == std::string("lorem"));
  127. reset(msg);
  128. msg = queue.next();
  129. CHECK(static_cast<StringMessage &>(*msg).string == std::string("ipsum"));
  130. reset(msg);
  131. CHECK(!queue.next());
  132. }