A C++ toolbox repo until the pair uTL/dTL arives
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

189 lines
5.8 KiB

  1. /*!
  2. * \file queue.cpp
  3. * \brief
  4. * Unit tests for queue
  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/queue.h>
  32. #include <gtest/gtest.h>
  33. namespace Tqueue {
  34. using namespace tbx;
  35. // Test construction
  36. TEST(Tqueue, contruct) {
  37. queue<int, 8> q1;
  38. queue<int, 8> q2{1, 2, 3, 4, 5, 6, 7, 8};
  39. queue<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. // base class functionality check
  48. TEST(Tqueue, base_class) {
  49. queue<int, 8> q1;
  50. // Access of base class functionality
  51. EXPECT_EQ (8UL, q1.capacity());
  52. EXPECT_EQ (0UL, q1.size());
  53. EXPECT_EQ (true, q1.empty());
  54. EXPECT_EQ (false, q1.full());
  55. q1.push(42);
  56. EXPECT_EQ (42, q1.front());
  57. EXPECT_EQ (42, q1.back());
  58. EXPECT_EQ (42, q1.pop());
  59. q1.push(1);
  60. q1.push(2);
  61. q1.push(3);
  62. int check_it=1;
  63. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  64. EXPECT_EQ(*it, check_it++);
  65. EXPECT_EQ(4, check_it); // run through all
  66. }
  67. // stream push-pop
  68. TEST(Tqueue, stream_push_pop) {
  69. queue<int, 8> q1;
  70. q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
  71. EXPECT_EQ (8UL, q1.capacity());
  72. EXPECT_EQ (8UL, q1.size());
  73. EXPECT_EQ (false, q1.empty());
  74. EXPECT_EQ (true, q1.full());
  75. q1 << 9; // try to insert in full queue
  76. EXPECT_EQ (8UL, q1.capacity());
  77. EXPECT_EQ (8UL, q1.size());
  78. EXPECT_EQ (false, q1.empty());
  79. EXPECT_EQ (true, q1.full());
  80. int check_it=1;
  81. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  82. EXPECT_EQ(*it, check_it++);
  83. EXPECT_EQ(9, check_it); // run through all
  84. for (int i =1 ; i <= 8 ; ++i) {
  85. check_it << q1;
  86. EXPECT_EQ(i, check_it);
  87. }
  88. EXPECT_EQ (8UL, q1.capacity());
  89. EXPECT_EQ (0UL, q1.size());
  90. EXPECT_EQ (true, q1.empty());
  91. EXPECT_EQ (false, q1.full());
  92. q1 >> check_it;
  93. EXPECT_EQ (int{}, check_it);
  94. }
  95. // Test construction
  96. TEST(Tqueue, contruct_atomic) {
  97. queue<int, 8, true> q1;
  98. queue<int, 8, true> q2{1, 2, 3, 4, 5, 6, 7, 8};
  99. queue<int, 8, true> q3{1, 2, 3, 4, 5};
  100. EXPECT_EQ (8UL, q1.capacity());
  101. EXPECT_EQ (0UL, q1.size());
  102. EXPECT_EQ (8UL, q2.capacity());
  103. EXPECT_EQ (8UL, q2.size());
  104. EXPECT_EQ (8UL, q3.capacity());
  105. EXPECT_EQ (5UL, q3.size());
  106. }
  107. // base class functionality check
  108. TEST(Tqueue, base_class_atomic) {
  109. queue<int, 8, true> q1;
  110. // Access of base class functionality
  111. EXPECT_EQ (8UL, q1.capacity());
  112. EXPECT_EQ (0UL, q1.size());
  113. EXPECT_EQ (true, q1.empty());
  114. EXPECT_EQ (false, q1.full());
  115. q1.push(42);
  116. EXPECT_EQ (42, q1.front());
  117. EXPECT_EQ (42, q1.back());
  118. EXPECT_EQ (42, q1.pop());
  119. q1.push(1);
  120. q1.push(2);
  121. q1.push(3);
  122. int check_it=1;
  123. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  124. EXPECT_EQ(*it, check_it++);
  125. EXPECT_EQ(4, check_it); // run through all
  126. }
  127. // stream push-pop
  128. TEST(Tqueue, stream_push_pop_atomic) {
  129. queue<int, 8, true> q1;
  130. q1 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8;
  131. EXPECT_EQ (8UL, q1.capacity());
  132. EXPECT_EQ (8UL, q1.size());
  133. EXPECT_EQ (false, q1.empty());
  134. EXPECT_EQ (true, q1.full());
  135. q1 << 9; // try to insert in full queue
  136. EXPECT_EQ (8UL, q1.capacity());
  137. EXPECT_EQ (8UL, q1.size());
  138. EXPECT_EQ (false, q1.empty());
  139. EXPECT_EQ (true, q1.full());
  140. int check_it=1;
  141. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  142. EXPECT_EQ(*it, check_it++);
  143. EXPECT_EQ(9, check_it); // run through all
  144. for (int i =1 ; i <= 8 ; ++i) {
  145. check_it << q1;
  146. EXPECT_EQ(i, check_it);
  147. }
  148. EXPECT_EQ (8UL, q1.capacity());
  149. EXPECT_EQ (0UL, q1.size());
  150. EXPECT_EQ (true, q1.empty());
  151. EXPECT_EQ (false, q1.full());
  152. q1 >> check_it;
  153. EXPECT_EQ (int{}, check_it);
  154. }
  155. }