A C++ toolbox repo until the pair uTL/dTL arives
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

268 řádky
8.9 KiB

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