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

120 рядки
3.5 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. EXPECT_EQ (8UL, q1.capacity());
  40. EXPECT_EQ (8UL, q2.capacity());
  41. }
  42. // simple push-pop functionality
  43. TEST(Tqueue, base_class) {
  44. queue<int, 8> q1;
  45. // Access of base class functionality
  46. EXPECT_EQ (8UL, q1.capacity());
  47. EXPECT_EQ (0UL, q1.size());
  48. EXPECT_EQ (true, q1.empty());
  49. EXPECT_EQ (false, q1.full());
  50. q1.push_back(7);
  51. EXPECT_EQ (7, q1.front());
  52. EXPECT_EQ (7, q1.back());
  53. EXPECT_EQ (7, q1.pop_front());
  54. q1.push_front(42);
  55. EXPECT_EQ (42, q1.front());
  56. EXPECT_EQ (42, q1.back());
  57. EXPECT_EQ (42, q1.pop_back());
  58. q1.push_back(1);
  59. q1.push_back(2);
  60. q1.push_back(3);
  61. int check_it=1;
  62. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  63. EXPECT_EQ(*it, check_it++);
  64. EXPECT_EQ(4, check_it); // run through all
  65. }
  66. // push-pop
  67. TEST(Tqueue, front_back) {
  68. queue<int, 8> q1;
  69. q1.push(7);
  70. EXPECT_EQ (7, q1.front());
  71. EXPECT_EQ (7, q1.back());
  72. EXPECT_EQ (7, q1.pop());
  73. q1.push(1);
  74. q1.push(2);
  75. q1.push(3);
  76. int check_it=1;
  77. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  78. EXPECT_EQ(*it, check_it++);
  79. EXPECT_EQ(4, check_it); // run through all
  80. }
  81. // stream operations
  82. TEST(Tqueue, capacity) {
  83. queue<int, 8> q1;
  84. // stream 5 items
  85. q1 << 1 << 2 << 3 << 4 << 5;
  86. int check_it=1;
  87. for (auto it = q1.begin() ; it != q1.end() ; ++it)
  88. EXPECT_EQ(*it, check_it++);
  89. EXPECT_EQ (6, check_it); // run through all
  90. // get all back
  91. int it;
  92. for (int check_it=1 ; check_it<=5 ; ++check_it) {
  93. q1 >> it;
  94. EXPECT_EQ (check_it, it);
  95. }
  96. }
  97. }