A C++ toolbox repo until the pair uTL/dTL arives
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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