stl_util_unittest.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/stl_util.h"
  5. #include <array>
  6. #include <deque>
  7. #include <forward_list>
  8. #include <functional>
  9. #include <initializer_list>
  10. #include <iterator>
  11. #include <list>
  12. #include <queue>
  13. #include <set>
  14. #include <stack>
  15. #include <string>
  16. #include <type_traits>
  17. #include <unordered_set>
  18. #include <vector>
  19. #include "base/containers/cxx20_erase_vector.h"
  20. #include "base/containers/queue.h"
  21. #include "testing/gmock/include/gmock/gmock.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. #include "third_party/abseil-cpp/absl/types/optional.h"
  24. namespace {
  25. using ::testing::IsNull;
  26. using ::testing::Pair;
  27. template <typename Container>
  28. void RunConstCastIteratorTest() {
  29. using std::begin;
  30. using std::cbegin;
  31. Container c = {1, 2, 3, 4, 5};
  32. auto c_it = std::next(cbegin(c), 3);
  33. auto it = base::ConstCastIterator(c, c_it);
  34. static_assert(std::is_same<decltype(cbegin(std::declval<Container&>())),
  35. decltype(c_it)>::value,
  36. "c_it is not a constant iterator.");
  37. static_assert(std::is_same<decltype(begin(std::declval<Container&>())),
  38. decltype(it)>::value,
  39. "it is not a iterator.");
  40. EXPECT_EQ(c_it, it);
  41. // Const casting the iterator should not modify the underlying container.
  42. Container other = {1, 2, 3, 4, 5};
  43. EXPECT_THAT(c, testing::ContainerEq(other));
  44. }
  45. } // namespace
  46. namespace base {
  47. namespace {
  48. TEST(STLUtilTest, ToUnderlying) {
  49. enum Enum : int {
  50. kOne = 1,
  51. kTwo = 2,
  52. };
  53. enum class ScopedEnum : char {
  54. kOne = 1,
  55. kTwo = 2,
  56. };
  57. static_assert(std::is_same<decltype(to_underlying(kOne)), int>::value, "");
  58. static_assert(std::is_same<decltype(to_underlying(kTwo)), int>::value, "");
  59. static_assert(to_underlying(kOne) == 1, "");
  60. static_assert(to_underlying(kTwo) == 2, "");
  61. static_assert(
  62. std::is_same<decltype(to_underlying(ScopedEnum::kOne)), char>::value, "");
  63. static_assert(
  64. std::is_same<decltype(to_underlying(ScopedEnum::kTwo)), char>::value, "");
  65. static_assert(to_underlying(ScopedEnum::kOne) == 1, "");
  66. static_assert(to_underlying(ScopedEnum::kTwo) == 2, "");
  67. }
  68. TEST(STLUtilTest, GetUnderlyingContainer) {
  69. {
  70. std::queue<int> queue({1, 2, 3, 4, 5});
  71. static_assert(std::is_same<decltype(GetUnderlyingContainer(queue)),
  72. const std::deque<int>&>::value,
  73. "GetUnderlyingContainer(queue) should be of type deque");
  74. EXPECT_THAT(GetUnderlyingContainer(queue),
  75. testing::ElementsAre(1, 2, 3, 4, 5));
  76. }
  77. {
  78. std::queue<int> queue;
  79. EXPECT_THAT(GetUnderlyingContainer(queue), testing::ElementsAre());
  80. }
  81. {
  82. base::queue<int> queue({1, 2, 3, 4, 5});
  83. static_assert(
  84. std::is_same<decltype(GetUnderlyingContainer(queue)),
  85. const base::circular_deque<int>&>::value,
  86. "GetUnderlyingContainer(queue) should be of type circular_deque");
  87. EXPECT_THAT(GetUnderlyingContainer(queue),
  88. testing::ElementsAre(1, 2, 3, 4, 5));
  89. }
  90. {
  91. std::vector<int> values = {1, 2, 3, 4, 5};
  92. std::priority_queue<int> queue(values.begin(), values.end());
  93. static_assert(std::is_same<decltype(GetUnderlyingContainer(queue)),
  94. const std::vector<int>&>::value,
  95. "GetUnderlyingContainer(queue) should be of type vector");
  96. EXPECT_THAT(GetUnderlyingContainer(queue),
  97. testing::UnorderedElementsAre(1, 2, 3, 4, 5));
  98. }
  99. {
  100. std::stack<int> stack({1, 2, 3, 4, 5});
  101. static_assert(std::is_same<decltype(GetUnderlyingContainer(stack)),
  102. const std::deque<int>&>::value,
  103. "GetUnderlyingContainer(stack) should be of type deque");
  104. EXPECT_THAT(GetUnderlyingContainer(stack),
  105. testing::ElementsAre(1, 2, 3, 4, 5));
  106. }
  107. }
  108. TEST(STLUtilTest, ConstCastIterator) {
  109. // Sequence Containers
  110. RunConstCastIteratorTest<std::forward_list<int>>();
  111. RunConstCastIteratorTest<std::list<int>>();
  112. RunConstCastIteratorTest<std::deque<int>>();
  113. RunConstCastIteratorTest<std::vector<int>>();
  114. RunConstCastIteratorTest<std::array<int, 5>>();
  115. RunConstCastIteratorTest<int[5]>();
  116. // Associative Containers
  117. RunConstCastIteratorTest<std::set<int>>();
  118. RunConstCastIteratorTest<std::multiset<int>>();
  119. // Unordered Associative Containers
  120. RunConstCastIteratorTest<std::unordered_set<int>>();
  121. RunConstCastIteratorTest<std::unordered_multiset<int>>();
  122. }
  123. TEST(STLUtilTest, STLSetDifference) {
  124. std::set<int> a1;
  125. a1.insert(1);
  126. a1.insert(2);
  127. a1.insert(3);
  128. a1.insert(4);
  129. std::set<int> a2;
  130. a2.insert(3);
  131. a2.insert(4);
  132. a2.insert(5);
  133. a2.insert(6);
  134. a2.insert(7);
  135. {
  136. std::set<int> difference;
  137. difference.insert(1);
  138. difference.insert(2);
  139. EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a1, a2));
  140. }
  141. {
  142. std::set<int> difference;
  143. difference.insert(5);
  144. difference.insert(6);
  145. difference.insert(7);
  146. EXPECT_EQ(difference, STLSetDifference<std::set<int> >(a2, a1));
  147. }
  148. {
  149. std::vector<int> difference;
  150. difference.push_back(1);
  151. difference.push_back(2);
  152. EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a1, a2));
  153. }
  154. {
  155. std::vector<int> difference;
  156. difference.push_back(5);
  157. difference.push_back(6);
  158. difference.push_back(7);
  159. EXPECT_EQ(difference, STLSetDifference<std::vector<int> >(a2, a1));
  160. }
  161. }
  162. TEST(STLUtilTest, STLSetUnion) {
  163. std::set<int> a1;
  164. a1.insert(1);
  165. a1.insert(2);
  166. a1.insert(3);
  167. a1.insert(4);
  168. std::set<int> a2;
  169. a2.insert(3);
  170. a2.insert(4);
  171. a2.insert(5);
  172. a2.insert(6);
  173. a2.insert(7);
  174. {
  175. std::set<int> result;
  176. result.insert(1);
  177. result.insert(2);
  178. result.insert(3);
  179. result.insert(4);
  180. result.insert(5);
  181. result.insert(6);
  182. result.insert(7);
  183. EXPECT_EQ(result, STLSetUnion<std::set<int> >(a1, a2));
  184. }
  185. {
  186. std::set<int> result;
  187. result.insert(1);
  188. result.insert(2);
  189. result.insert(3);
  190. result.insert(4);
  191. result.insert(5);
  192. result.insert(6);
  193. result.insert(7);
  194. EXPECT_EQ(result, STLSetUnion<std::set<int> >(a2, a1));
  195. }
  196. {
  197. std::vector<int> result;
  198. result.push_back(1);
  199. result.push_back(2);
  200. result.push_back(3);
  201. result.push_back(4);
  202. result.push_back(5);
  203. result.push_back(6);
  204. result.push_back(7);
  205. EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a1, a2));
  206. }
  207. {
  208. std::vector<int> result;
  209. result.push_back(1);
  210. result.push_back(2);
  211. result.push_back(3);
  212. result.push_back(4);
  213. result.push_back(5);
  214. result.push_back(6);
  215. result.push_back(7);
  216. EXPECT_EQ(result, STLSetUnion<std::vector<int> >(a2, a1));
  217. }
  218. }
  219. TEST(STLUtilTest, STLSetIntersection) {
  220. std::set<int> a1;
  221. a1.insert(1);
  222. a1.insert(2);
  223. a1.insert(3);
  224. a1.insert(4);
  225. std::set<int> a2;
  226. a2.insert(3);
  227. a2.insert(4);
  228. a2.insert(5);
  229. a2.insert(6);
  230. a2.insert(7);
  231. {
  232. std::set<int> result;
  233. result.insert(3);
  234. result.insert(4);
  235. EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a1, a2));
  236. }
  237. {
  238. std::set<int> result;
  239. result.insert(3);
  240. result.insert(4);
  241. EXPECT_EQ(result, STLSetIntersection<std::set<int> >(a2, a1));
  242. }
  243. {
  244. std::vector<int> result;
  245. result.push_back(3);
  246. result.push_back(4);
  247. EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a1, a2));
  248. }
  249. {
  250. std::vector<int> result;
  251. result.push_back(3);
  252. result.push_back(4);
  253. EXPECT_EQ(result, STLSetIntersection<std::vector<int> >(a2, a1));
  254. }
  255. }
  256. TEST(Erase, IsNotIn) {
  257. // Should keep both '2' but only one '4', like std::set_intersection.
  258. std::vector<int> lhs = {0, 2, 2, 4, 4, 4, 6, 8, 10};
  259. std::vector<int> rhs = {1, 2, 2, 4, 5, 6, 7};
  260. std::vector<int> expected = {2, 2, 4, 6};
  261. EXPECT_EQ(5u, EraseIf(lhs, IsNotIn<std::vector<int>>(rhs)));
  262. EXPECT_EQ(expected, lhs);
  263. }
  264. TEST(STLUtilTest, OptionalOrNullptr) {
  265. absl::optional<float> optional;
  266. EXPECT_EQ(nullptr, base::OptionalOrNullptr(optional));
  267. optional = 0.1f;
  268. EXPECT_EQ(&optional.value(), base::OptionalOrNullptr(optional));
  269. EXPECT_NE(nullptr, base::OptionalOrNullptr(optional));
  270. }
  271. } // namespace
  272. } // namespace base