20-queue.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. CHECK(queue.put<StringMessage>(0, 1, "hello"));
  22. CHECK(!queue.put<StringMessage>(0, 1, "world"));
  23. auto msg = queue.next();
  24. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  25. reset(msg);
  26. CHECK(queue.put<StringMessage>(0, 1, "world"));
  27. CHECK(!queue.put<StringMessage>(0, 1, "hello"));
  28. msg = queue.next();
  29. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  30. reset(msg);
  31. CHECK(!queue.next());
  32. }
  33. TEST_CASE("1 priority, 2 messages queue", "[queue]") {
  34. using AppQueue = Queue<MessageStorage, 2>;
  35. AppQueue queue;
  36. printf("queue sz = %lu, message sz = %lu\n", sizeof(queue),
  37. MessageStorage::item_size);
  38. CHECK(queue.put<StringMessage>(0, 1, "hello"));
  39. CHECK(queue.put<StringMessage>(0, 1, "world"));
  40. CHECK(!queue.put<StringMessage>(0, 1, "!"));
  41. auto msg = queue.next();
  42. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  43. reset(msg);
  44. msg = queue.next();
  45. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  46. reset(msg);
  47. CHECK(queue.put<StringMessage>(0, 1, "world"));
  48. CHECK(queue.put<StringMessage>(0, 1, "hello"));
  49. CHECK(!queue.put<StringMessage>(0, 1, "!"));
  50. msg = queue.next();
  51. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  52. reset(msg);
  53. msg = queue.next();
  54. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  55. reset(msg);
  56. CHECK(!queue.next());
  57. }
  58. TEST_CASE("1 priority, 3 messages queue", "[queue]") {
  59. using AppQueue = Queue<MessageStorage, 3>;
  60. AppQueue queue;
  61. SECTION("bugfix") {
  62. CHECK(queue.put<StringMessage>(0, 1, "hello"));
  63. auto msg = queue.next();
  64. CHECK(static_cast<StringMessage &>(*msg).string == std::string("hello"));
  65. CHECK(queue.put<StringMessage>(0, 1, "world"));
  66. reset(msg);
  67. msg = queue.next();
  68. CHECK(static_cast<StringMessage &>(*msg).string == std::string("world"));
  69. }
  70. }
  71. TEST_CASE("2 priorities, 2/1 messages", "[queue]") {
  72. using AppQueue = Queue<MessageStorage, 2, 1>;
  73. AppQueue queue;
  74. printf("queue sz = %lu, message sz = %lu\n", sizeof(queue),
  75. MessageStorage::item_size);
  76. SECTION("order-1") {
  77. CHECK(queue.put<StringMessage>(0, 1, "lorem"));
  78. CHECK(queue.put<StringMessage>(0, 1, "ipsum"));
  79. CHECK(queue.put<StringMessage>(1, 1, "dolor"));
  80. }
  81. SECTION("order-2") {
  82. CHECK(queue.put<StringMessage>(1, 1, "dolor"));
  83. CHECK(queue.put<StringMessage>(0, 1, "lorem"));
  84. CHECK(queue.put<StringMessage>(0, 1, "ipsum"));
  85. }
  86. SECTION("order-3") {
  87. CHECK(queue.put<StringMessage>(0, 1, "lorem"));
  88. CHECK(queue.put<StringMessage>(1, 1, "dolor"));
  89. CHECK(queue.put<StringMessage>(0, 1, "ipsum"));
  90. }
  91. auto msg = queue.next();
  92. CHECK(static_cast<StringMessage &>(*msg).string == std::string("dolor"));
  93. reset(msg);
  94. msg = queue.next();
  95. CHECK(static_cast<StringMessage &>(*msg).string == std::string("lorem"));
  96. reset(msg);
  97. msg = queue.next();
  98. CHECK(static_cast<StringMessage &>(*msg).string == std::string("ipsum"));
  99. reset(msg);
  100. CHECK(!queue.next());
  101. }