A C++ toolbox repo until the pair uTL/dTL arives
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*!
  2. * \file equeue.cpp
  3. * \brief
  4. * Unit tests for equeue
  5. *
  6. * \copyright Copyright (C) 2020 Christos Choutouridis <christos@choutouridis.net>
  7. *
  8. * <dl class=\"section copyright\"><dt>License</dt><dd>
  9. * The MIT License (MIT)
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27. * SOFTWARE.
  28. * </dd></dl>
  29. *
  30. */
  31. #include <cont/equeue.h>
  32. #include <gtest/gtest.h>
  33. namespace Tequeue {
  34. using namespace tbx;
  35. int global_flag =0;
  36. // Callable mocks
  37. void vfun(void) { ++global_flag; }
  38. struct vfoo {
  39. void operator() (void) { ++global_flag; }
  40. };
  41. // Test construction
  42. TEST(Tequeue, contruct) {
  43. using Equeue = equeue<int, 8>;
  44. struct T { int a,b; };
  45. int local{};
  46. Equeue e1(Equeue::size_match::GE, 3, [](){
  47. ++global_flag;
  48. });
  49. Equeue e2(Equeue::size_match::GE, 3, [&](){
  50. ++local;
  51. });
  52. Equeue e3(Equeue::size_match::EQ, 7, vfun);
  53. equeue<T, 8> e4(equeue<T, 8>::size_match::EQ, 2, vfoo{});
  54. equeue<int, 8> q1;
  55. equeue<int, 8> q2(equeue<int, 8>::size_match::DISABLED, 0, nullptr);
  56. EXPECT_EQ (8UL, e1.capacity());
  57. EXPECT_EQ (8UL, e2.capacity());
  58. EXPECT_EQ (8UL, e3.capacity());
  59. EXPECT_EQ (8UL, e4.capacity());
  60. EXPECT_EQ (8UL, q1.capacity());
  61. EXPECT_EQ (8UL, q2.capacity());
  62. }
  63. // simple push-pop functionality
  64. TEST(Tequeue, base_class) {
  65. using Equeue = equeue<int, 8>;
  66. Equeue e1(Equeue::size_match::GE, 3, [](){
  67. ++global_flag;
  68. });
  69. // Access of base class functionality
  70. EXPECT_EQ (8UL, e1.capacity());
  71. EXPECT_EQ (0UL, e1.size());
  72. EXPECT_EQ (true, e1.empty());
  73. EXPECT_EQ (false, e1.full());
  74. e1.push(42);
  75. EXPECT_EQ (42, e1.front());
  76. EXPECT_EQ (42, e1.back());
  77. EXPECT_EQ (42, e1.pop());
  78. e1.push(1);
  79. e1.push(2);
  80. e1.push(3);
  81. int check_it=1;
  82. for (auto it = e1.begin() ; it != e1.end() ; ++it)
  83. EXPECT_EQ(*it, check_it++);
  84. EXPECT_EQ(4, check_it); // run through all
  85. }
  86. // trigger functionality
  87. TEST (Tequeue, set_clear_check_trigger) {
  88. using Equeue = equeue<int, 8>;
  89. bool flag{};
  90. Equeue e1(Equeue::size_match::GE, 1, [&](){ flag = true; });
  91. flag = false;
  92. e1.clear_trigger();
  93. EXPECT_EQ (false, flag);
  94. e1.push_back(1); // 1, no-trigger cleared
  95. EXPECT_EQ (false, flag);
  96. flag = false;
  97. e1.clear();
  98. e1.clear_trigger();
  99. EXPECT_EQ (false, flag); // no spurious triggers
  100. e1.push_back(1); // 1
  101. e1.push_back(2); // 2
  102. e1.set_trigger(Equeue::size_match::GE, 1, [&](){ flag = true; });
  103. EXPECT_EQ (false, flag); // no spurious triggers
  104. e1.check_trigger(); // manual trigger
  105. EXPECT_EQ (true, flag);
  106. flag = false;
  107. e1.check_trigger(); // manual trigger attempt
  108. EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
  109. Equeue e2(Equeue::data_match::MATCH, 42, [&](){ flag = true; });
  110. flag = false;
  111. e2.clear_trigger();
  112. EXPECT_EQ (false, flag);
  113. e2.push_back(42); // push 42, no-trigger cleared
  114. EXPECT_EQ (false, flag);
  115. e2.set_trigger(Equeue::data_match::MATCH, 42, [&](){ flag = true; });
  116. EXPECT_EQ (false, flag); // no spurious triggers
  117. e2.pop_back(); // pop 42, trigger
  118. EXPECT_EQ (true, flag);
  119. flag = false;
  120. e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
  121. EXPECT_EQ (true, flag);
  122. }
  123. // stream push-pop
  124. TEST(Tequeue, stream_push_pop) {
  125. equeue<int, 8> q1;
  126. q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
  127. EXPECT_EQ (8UL, q1.capacity());
  128. EXPECT_EQ (8UL, q1.size());
  129. EXPECT_EQ (false, q1.empty());
  130. EXPECT_EQ (true, q1.full());
  131. q1 << 9; // try to insert in full queue
  132. EXPECT_EQ (8UL, q1.capacity());
  133. EXPECT_EQ (8UL, q1.size());
  134. EXPECT_EQ (false, q1.empty());
  135. EXPECT_EQ (true, q1.full());
  136. int check_it=1;
  137. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  138. EXPECT_EQ(*it, check_it++);
  139. EXPECT_EQ(9, check_it); // run through all
  140. for (int i =1 ; i <= 8 ; ++i) {
  141. check_it << q1;
  142. EXPECT_EQ(i, check_it);
  143. }
  144. EXPECT_EQ (8UL, q1.capacity());
  145. EXPECT_EQ (0UL, q1.size());
  146. EXPECT_EQ (true, q1.empty());
  147. EXPECT_EQ (false, q1.full());
  148. q1 >> check_it;
  149. EXPECT_EQ (int{}, check_it);
  150. }
  151. }