A C++ toolbox repo until the pair uTL/dTL arives
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
3 роки тому
3 роки тому
3 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/deque.h>
  32. #include <gtest/gtest.h>
  33. namespace Tdeque {
  34. using namespace tbx;
  35. // Test construction
  36. TEST(Tdeque, contruct) {
  37. deque<int, 8> q1;
  38. deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  39. deque<int, 8> q3{1, 2, 3, 4, 5};
  40. EXPECT_EQ (8UL, q1.capacity());
  41. EXPECT_EQ (0UL, q1.size());
  42. EXPECT_EQ (8UL, q2.capacity());
  43. EXPECT_EQ (8UL, q2.size());
  44. EXPECT_EQ (8UL, q3.capacity());
  45. EXPECT_EQ (5UL, q3.size());
  46. }
  47. // simple push-pop functionality
  48. TEST(Tdeque, push_pop) {
  49. deque<int, 8> q1;
  50. deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  51. q1.push_front(1);
  52. q1.push_front(2);
  53. EXPECT_EQ (1, q1.pop_back());
  54. EXPECT_EQ (2, q1.pop_back());
  55. q1.push_back(1);
  56. q1.push_back(2);
  57. EXPECT_EQ (1, q1.pop_front());
  58. EXPECT_EQ (2, q1.pop_front());
  59. q1.push_front(2);
  60. q1.push_back(3);
  61. q1.push_front(1);
  62. q1.push_back(4);
  63. for (int i=1 ; i<= 4 ; ++i)
  64. EXPECT_EQ ((int)i, q1.pop_front());
  65. }
  66. // front-back
  67. TEST(Tdeque, front_back) {
  68. deque<int, 8> q1;
  69. deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  70. q1.push_front(2);
  71. q1.push_front(1);
  72. q1.push_back(3);
  73. q1.push_back(4);
  74. EXPECT_EQ (1, q1.front());
  75. EXPECT_EQ (4, q1.back());
  76. EXPECT_EQ (1, q2.front());
  77. EXPECT_EQ (8, q2.back());
  78. }
  79. // capacity
  80. TEST(Tdeque, capacity) {
  81. deque<int, 8> q1;
  82. deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  83. q1.push_back(1);
  84. q1.clear();
  85. EXPECT_EQ (true, q1.empty());
  86. EXPECT_EQ (true, q2.full());
  87. EXPECT_EQ (8UL, q1.capacity());
  88. EXPECT_EQ (8UL, q2.capacity());
  89. EXPECT_EQ (0UL, q1.size());
  90. EXPECT_EQ (8UL, q2.size());
  91. q1.push_back(2);
  92. EXPECT_EQ (1UL, q1.size());
  93. q1.push_front(1);
  94. EXPECT_EQ (2UL, q1.size());
  95. q1.pop_back();
  96. EXPECT_EQ (1UL, q1.size());
  97. q1.pop_front();
  98. EXPECT_EQ (0UL, q1.size());
  99. }
  100. // push-pop limits
  101. TEST (Tdeque, push_pop_limits) {
  102. deque<int, 8> q1;
  103. deque<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  104. EXPECT_EQ (int{}, q1.pop_back());
  105. EXPECT_EQ (0UL, q1.size());
  106. EXPECT_EQ (true, q1.empty());
  107. EXPECT_EQ (false, q1.full());
  108. EXPECT_EQ (int{}, q1.pop_front());
  109. EXPECT_EQ (0UL, q1.size());
  110. EXPECT_EQ (true, q1.empty());
  111. EXPECT_EQ (false, q1.full());
  112. q2.push_front(0);
  113. EXPECT_EQ (1, q2.front());
  114. EXPECT_EQ (8, q2.back());
  115. EXPECT_EQ (8UL, q2.size());
  116. EXPECT_EQ (false, q2.empty());
  117. EXPECT_EQ (true, q2.full());
  118. q2.push_back(9);
  119. EXPECT_EQ (1, q2.front());
  120. EXPECT_EQ (8, q2.back());
  121. EXPECT_EQ (8UL, q2.size());
  122. EXPECT_EQ (false, q2.empty());
  123. EXPECT_EQ (true, q2.full());
  124. }
  125. // iterators
  126. TEST (Tdeque, iterators) {
  127. deque<int, 8> q1{1, 2, 3, 4, 5, 6, 7, 8};
  128. int check_it=1;
  129. EXPECT_EQ (q1.begin().base(), q1.end().base());
  130. EXPECT_NE (q1.begin().iter(), q1.end().iter());
  131. EXPECT_EQ (1, *q1.begin());
  132. EXPECT_EQ (true, (q1.begin() == ++q1.end())); // loop edge iterators
  133. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  134. EXPECT_EQ(*it, check_it++);
  135. EXPECT_EQ(9, check_it); // run through all
  136. EXPECT_EQ (1, q1.front()); // queue stays intact
  137. EXPECT_EQ (8, q1.back());
  138. EXPECT_EQ (8UL, q1.size());
  139. EXPECT_EQ (false, q1.empty());
  140. EXPECT_EQ (true, q1.full());
  141. q1.pop_front();
  142. q1.pop_back();
  143. check_it=2;
  144. for (auto& it : q1)
  145. EXPECT_EQ(it, check_it++);
  146. EXPECT_EQ(8, check_it); // run through all
  147. EXPECT_EQ (2, q1.front()); // queue stays intact
  148. EXPECT_EQ (7, q1.back());
  149. EXPECT_EQ (6UL, q1.size());
  150. EXPECT_EQ (false, q1.empty());
  151. EXPECT_EQ (false, q1.full());
  152. deque<int, 8> q2;
  153. q2.push_front(2);
  154. q2.push_front(1);
  155. q2.push_back(3);
  156. q2.push_back(4);
  157. q2.push_back(5);
  158. check_it =1;
  159. for (auto& it : q2)
  160. EXPECT_EQ(it, check_it++);
  161. EXPECT_EQ(6, check_it); // run through all
  162. }
  163. }