A C++ toolbox repo until the pair uTL/dTL arives
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

edeque.cpp 8.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*!
  2. * \file deque.cpp
  3. * \brief
  4. * Unit tests for deque
  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/edeque.h>
  32. #include <gtest/gtest.h>
  33. #include <functional>
  34. namespace Tdeque {
  35. using namespace tbx;
  36. int global_flag =0;
  37. // Callable mocks
  38. void vfun(void) { ++global_flag; }
  39. struct vfoo {
  40. void operator() (void) { ++global_flag; }
  41. };
  42. TEST (Tedeque, construct) {
  43. using Edeque = edeque<int, 8>;
  44. struct T { int a,b; };
  45. int local{};
  46. Edeque e1(Edeque::size_match::GE, 3, [](){
  47. ++global_flag;
  48. });
  49. Edeque e2(Edeque::size_match::GE, 3, [&](){
  50. ++local;
  51. });
  52. Edeque e3(Edeque::size_match::EQ, 7, vfun);
  53. edeque<T, 8> e4(edeque<T, 8>::size_match::EQ, 2, vfoo{});
  54. EXPECT_EQ (8UL, e1.capacity());
  55. EXPECT_EQ (8UL, e2.capacity());
  56. EXPECT_EQ (8UL, e3.capacity());
  57. EXPECT_EQ (8UL, e4.capacity());
  58. }
  59. TEST (Tedeque, base_class) {
  60. using Edeque = edeque<int, 8>;
  61. Edeque e1(Edeque::size_match::GE, 3, [](){
  62. ++global_flag;
  63. });
  64. // Access of base class functionality
  65. EXPECT_EQ (8UL, e1.capacity());
  66. EXPECT_EQ (0UL, e1.size());
  67. EXPECT_EQ (true, e1.empty());
  68. EXPECT_EQ (false, e1.full());
  69. e1.push_back(7);
  70. EXPECT_EQ (7, e1.front());
  71. EXPECT_EQ (7, e1.back());
  72. EXPECT_EQ (7, e1.pop_front());
  73. e1.push_front(42);
  74. EXPECT_EQ (42, e1.front());
  75. EXPECT_EQ (42, e1.back());
  76. EXPECT_EQ (42, e1.pop_back());
  77. e1.push_back(1);
  78. e1.push_back(2);
  79. e1.push_back(3);
  80. int check_it=1;
  81. for (auto it = e1.begin() ; it != e1.end() ; ++it)
  82. EXPECT_EQ(*it, check_it++);
  83. EXPECT_EQ(4, check_it); // run through all
  84. }
  85. TEST (Tedeque, set_clear_check_trigger) {
  86. using Edeque = edeque<int, 8>;
  87. bool flag{};
  88. Edeque e1(Edeque::size_match::GE, 1, [&](){ flag = true; });
  89. flag = false;
  90. e1.clear_trigger();
  91. EXPECT_EQ (false, flag);
  92. e1.push_back(1); // 1, no-trigger cleared
  93. EXPECT_EQ (false, flag);
  94. flag = false;
  95. e1.clear();
  96. e1.clear_trigger();
  97. EXPECT_EQ (false, flag); // no spurious triggers
  98. e1.push_back(1); // 1
  99. e1.push_back(2); // 2
  100. e1.set_trigger(Edeque::size_match::GE, 1, [&](){ flag = true; });
  101. EXPECT_EQ (false, flag); // no spurious triggers
  102. e1.check_trigger(); // manual trigger
  103. EXPECT_EQ (true, flag);
  104. flag = false;
  105. e1.check_trigger(); // manual trigger attempt
  106. EXPECT_EQ (false, flag); // [SIZE triggers are auto clear]
  107. Edeque e2(Edeque::data_match::MATCH, 42, [&](){ flag = true; });
  108. flag = false;
  109. e2.clear_trigger();
  110. EXPECT_EQ (false, flag);
  111. e2.push_back(42); // push 42, no-trigger cleared
  112. EXPECT_EQ (false, flag);
  113. e2.set_trigger(Edeque::data_match::MATCH, 42, [&](){ flag = true; });
  114. EXPECT_EQ (false, flag); // no spurious triggers
  115. e2.pop_back(); // pop 42, trigger
  116. EXPECT_EQ (true, flag);
  117. flag = false;
  118. e2.push_back(42); // push 42, re-trigger [DATA re-triggers]
  119. EXPECT_EQ (true, flag);
  120. }
  121. TEST (Tedeque, size_triggers) {
  122. using Edeque = edeque<int, 8>;
  123. bool flag{};
  124. // size_match::GE (size()>= 2)
  125. Edeque ee(Edeque::size_match::GE, 2, [&](){ flag = true; });
  126. flag = false;
  127. ee.clear();
  128. ee.push_back(1); // 1
  129. EXPECT_EQ (false, flag);
  130. ee.push_back(2); // 2, trigger
  131. EXPECT_EQ (true, flag);
  132. flag = false;
  133. ee.push_back(3); // 3, no-trigger cleared
  134. EXPECT_EQ (false, flag);
  135. // size_match::GT (size()> 1)
  136. flag = false;
  137. ee.clear();
  138. ee.set_trigger(Edeque::size_match::GT, 1, [&](){ flag = true; });
  139. ee.push_back(1); // 1
  140. EXPECT_EQ (false, flag);
  141. ee.push_back(2); // 2, trigger
  142. EXPECT_EQ (true, flag);
  143. flag = false;
  144. ee.push_back(3); // 3, no-trigger cleared
  145. EXPECT_EQ (false, flag);
  146. // size_match::LE (size()<= 1)
  147. flag = false;
  148. ee.clear();
  149. ee.push_back(1); // 1
  150. ee.push_back(2); // 2
  151. ee.push_back(3); // 3
  152. ee.set_trigger(Edeque::size_match::LE, 1, [&](){ flag = true; });
  153. ee.pop_front(); // 2
  154. EXPECT_EQ (false, flag);
  155. ee.pop_front(); // 1, trigger
  156. EXPECT_EQ (true, flag);
  157. flag = false;
  158. ee.pop_front(); // 0, no-trigger cleared
  159. EXPECT_EQ (false, flag);
  160. // size_match::LT (size()< 2)
  161. flag = false;
  162. ee.clear();
  163. ee.push_back(1); // 1
  164. ee.push_back(2); // 2
  165. ee.push_back(3); // 3
  166. ee.set_trigger(Edeque::size_match::LT, 2, [&](){ flag = true; });
  167. ee.pop_front(); // 2
  168. EXPECT_EQ (false, flag);
  169. ee.pop_front(); // 1, trigger
  170. EXPECT_EQ (true, flag);
  171. flag = false;
  172. ee.pop_front(); // 0, no-trigger cleared
  173. EXPECT_EQ (false, flag);
  174. // size_match::EQ (size()== 2)
  175. flag = false;
  176. ee.clear();
  177. ee.set_trigger(Edeque::size_match::EQ, 2, [&](){ flag = true; });
  178. ee.push_back(1); // 1
  179. EXPECT_EQ (false, flag);
  180. ee.push_back(2); // 2, trigger
  181. EXPECT_EQ (true, flag);
  182. flag = false;
  183. ee.push_back(3); // 3
  184. ee.pop_front(); // 2, no-trigger cleared
  185. EXPECT_EQ (false, flag);
  186. // size_match::NE (size()!= 0)
  187. flag = false;
  188. ee.clear();
  189. ee.set_trigger(Edeque::size_match::NE, 0, [&](){ flag = true; });
  190. EXPECT_EQ (false, flag);
  191. ee.push_back(1); // 1, trigger
  192. EXPECT_EQ (true, flag);
  193. flag = false;
  194. ee.push_back(2); // 2, no-trigger
  195. EXPECT_EQ (false, flag);
  196. }
  197. TEST (Tedeque, data_triggers) {
  198. using Edeque = edeque<int, 8>;
  199. bool flag{};
  200. // data_match::MATCH (item == 42)
  201. Edeque ee(Edeque::data_match::MATCH, 42, [&](){ flag = true; });
  202. flag = false;
  203. ee.push_back(7); // 7
  204. EXPECT_EQ (false, flag);
  205. ee.push_back(42); // push:42, trigger
  206. EXPECT_EQ (true, flag);
  207. flag = false;
  208. ee.pop_back(); // pop:42, trigger
  209. EXPECT_EQ (true, flag);
  210. // data_match::MATCH (item != 42)
  211. flag = false;
  212. ee.clear();
  213. ee.push_back(7); // 7
  214. ee.set_trigger(Edeque::data_match::MISMATCH, 42, [&](){ flag = true; });
  215. EXPECT_EQ (false, flag); // no spurious triggers
  216. ee.push_back(42); // 42, no-trigger
  217. EXPECT_EQ (false, flag);
  218. ee.push_back(0); // 0, trigger
  219. EXPECT_EQ (true, flag);
  220. flag = false;
  221. ee.push_back(1); // 1, re-trigger
  222. EXPECT_EQ (true, flag);
  223. }
  224. }