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ů.
 
 
 

113 řádky
3.6 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. }