test_intrusive_list.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "catch.hpp"
  14. #include <algorithm>
  15. #include <cstring>
  16. #include "intrusive_list.h"
  17. struct TestNode : public intrusive_list_node<TestNode> {
  18. TestNode(const char* name_ = "", int num_ = 0) : num(num_)
  19. {
  20. strncpy(name, name_, sizeof(name) - 1);
  21. name[sizeof(name) - 1] = 0;
  22. }
  23. char name[32];
  24. int num;
  25. };
  26. typedef intrusive_list<TestNode> TestList;
  27. TEST_CASE("can add items to the list", "[list]")
  28. {
  29. TestList list;
  30. TestNode n1("one", 1);
  31. TestNode n2("two", 2);
  32. TestNode n3("three", 3);
  33. list.push_back(&n1);
  34. REQUIRE(list.begin()->num == 1);
  35. REQUIRE(list.front().num == 1);
  36. REQUIRE(list.back().num == 1);
  37. list.push_front(&n2);
  38. REQUIRE(list.begin()->num == 2);
  39. REQUIRE(list.front().num == 2);
  40. REQUIRE(list.back().num == 1);
  41. list.insert(list.begin(), &n3);
  42. REQUIRE(list.begin()->num == 3);
  43. REQUIRE(list.front().num == 3);
  44. REQUIRE(list.back().num == 1);
  45. auto second = ++list.begin();
  46. REQUIRE(second->num == 2);
  47. second++;
  48. REQUIRE(second->num == 1);
  49. }
  50. TEST_CASE("can iterate over items", "[list]")
  51. {
  52. TestList list;
  53. TestNode n1("one", 1);
  54. TestNode n2("two", 2);
  55. TestNode n3("three", 3);
  56. list.push_back(&n1);
  57. list.push_back(&n2);
  58. list.push_back(&n3);
  59. int val = 1;
  60. for (auto it = std::begin(list); it != std::end(list); ++it) {
  61. REQUIRE(it->num == val);
  62. ++val;
  63. }
  64. }
  65. TEST_CASE("iterator's prefix and postfix increments and decrements behave as expected", "[list]")
  66. {
  67. TestList list;
  68. TestNode n1("one", 1);
  69. TestNode n2("two", 2);
  70. TestNode n3("three", 3);
  71. list.push_back(&n1);
  72. list.push_back(&n2);
  73. list.push_back(&n3);
  74. auto it = std::begin(list);
  75. REQUIRE((++it)->num == 2);
  76. REQUIRE(it++->num == 2);
  77. REQUIRE((--it)->num == 2);
  78. REQUIRE(it--->num == 2);
  79. }
  80. TEST_CASE("can pop_front from the list", "[list]")
  81. {
  82. TestList list;
  83. TestNode n1("one", 1);
  84. TestNode n2("two", 2);
  85. TestNode n3("three", 3);
  86. list.push_back(&n1);
  87. list.push_back(&n2);
  88. list.push_back(&n3);
  89. list.pop_front();
  90. list.pop_front();
  91. list.pop_front();
  92. REQUIRE(std::begin(list) == std::end(list));
  93. }
  94. TEST_CASE("can erase first item in the list", "[list]")
  95. {
  96. TestList list;
  97. TestNode n1("one", 1);
  98. TestNode n2("two", 2);
  99. TestNode n3("three", 3);
  100. list.push_back(&n1);
  101. list.push_back(&n2);
  102. list.push_back(&n3);
  103. list.erase(std::begin(list));
  104. REQUIRE(list.front().num == 2);
  105. REQUIRE(list.back().num == 3);
  106. }
  107. TEST_CASE("can erase last item in the list", "[list]")
  108. {
  109. TestList list;
  110. TestNode n1("one", 1);
  111. TestNode n2("two", 2);
  112. TestNode n3("three", 3);
  113. list.push_back(&n1);
  114. list.push_back(&n2);
  115. list.push_back(&n3);
  116. list.erase(&list.back());
  117. REQUIRE(list.front().num == 1);
  118. REQUIRE(list.back().num == 2);
  119. }
  120. TEST_CASE("can erase item in the middle of the list", "[list]")
  121. {
  122. TestList list;
  123. TestNode n1("one", 1);
  124. TestNode n2("two", 2);
  125. TestNode n3("three", 3);
  126. list.push_back(&n1);
  127. list.push_back(&n2);
  128. list.push_back(&n3);
  129. list.erase(++std::begin(list));
  130. REQUIRE(list.front().num == 1);
  131. REQUIRE(list.back().num == 3);
  132. }
  133. TEST_CASE("can erase all items in the list", "[list]")
  134. {
  135. TestList list;
  136. TestNode n1("one", 1);
  137. TestNode n2("two", 2);
  138. TestNode n3("three", 3);
  139. list.push_back(&n1);
  140. list.push_back(&n2);
  141. list.push_back(&n3);
  142. list.erase(std::begin(list));
  143. list.erase(std::begin(list));
  144. list.erase(std::begin(list));
  145. REQUIRE(std::begin(list) == std::end(list));
  146. }
  147. TEST_CASE("can erase all items in the list using clear method", "[list]")
  148. {
  149. TestList list;
  150. TestNode n1("one", 1);
  151. TestNode n2("two", 2);
  152. TestNode n3("three", 3);
  153. TestNode n4("four", 4);
  154. TestNode n5("five", 5);
  155. TestNode n6("six", 6);
  156. list.push_back(&n1);
  157. list.push_back(&n2);
  158. list.insert(++list.begin(), &n3);
  159. list.insert(++list.begin(), &n4);
  160. list.push_front(&n5);
  161. list.insert(list.begin(), &n6);
  162. list.clear();
  163. REQUIRE(std::begin(list) == std::end(list));
  164. }