container_test.cc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. // Copyright 2017 The Abseil Authors.
  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. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/algorithm/container.h"
  15. #include <functional>
  16. #include <initializer_list>
  17. #include <iterator>
  18. #include <list>
  19. #include <memory>
  20. #include <ostream>
  21. #include <random>
  22. #include <set>
  23. #include <unordered_set>
  24. #include <utility>
  25. #include <valarray>
  26. #include <vector>
  27. #include "gmock/gmock.h"
  28. #include "gtest/gtest.h"
  29. #include "absl/base/casts.h"
  30. #include "absl/base/macros.h"
  31. #include "absl/memory/memory.h"
  32. #include "absl/types/span.h"
  33. namespace {
  34. using ::testing::Each;
  35. using ::testing::ElementsAre;
  36. using ::testing::Gt;
  37. using ::testing::IsNull;
  38. using ::testing::Lt;
  39. using ::testing::Pointee;
  40. using ::testing::Truly;
  41. using ::testing::UnorderedElementsAre;
  42. // Most of these tests just check that the code compiles, not that it
  43. // does the right thing. That's fine since the functions just forward
  44. // to the STL implementation.
  45. class NonMutatingTest : public testing::Test {
  46. protected:
  47. std::unordered_set<int> container_ = {1, 2, 3};
  48. std::list<int> sequence_ = {1, 2, 3};
  49. std::vector<int> vector_ = {1, 2, 3};
  50. int array_[3] = {1, 2, 3};
  51. };
  52. struct AccumulateCalls {
  53. void operator()(int value) { calls.push_back(value); }
  54. std::vector<int> calls;
  55. };
  56. bool Predicate(int value) { return value < 3; }
  57. bool BinPredicate(int v1, int v2) { return v1 < v2; }
  58. bool Equals(int v1, int v2) { return v1 == v2; }
  59. bool IsOdd(int x) { return x % 2 != 0; }
  60. TEST_F(NonMutatingTest, Distance) {
  61. EXPECT_EQ(container_.size(), absl::c_distance(container_));
  62. EXPECT_EQ(sequence_.size(), absl::c_distance(sequence_));
  63. EXPECT_EQ(vector_.size(), absl::c_distance(vector_));
  64. EXPECT_EQ(ABSL_ARRAYSIZE(array_), absl::c_distance(array_));
  65. // Works with a temporary argument.
  66. EXPECT_EQ(vector_.size(), absl::c_distance(std::vector<int>(vector_)));
  67. }
  68. TEST_F(NonMutatingTest, Distance_OverloadedBeginEnd) {
  69. // Works with classes which have custom ADL-selected overloads of std::begin
  70. // and std::end.
  71. std::initializer_list<int> a = {1, 2, 3};
  72. std::valarray<int> b = {1, 2, 3};
  73. EXPECT_EQ(3, absl::c_distance(a));
  74. EXPECT_EQ(3, absl::c_distance(b));
  75. // It is assumed that other c_* functions use the same mechanism for
  76. // ADL-selecting begin/end overloads.
  77. }
  78. TEST_F(NonMutatingTest, ForEach) {
  79. AccumulateCalls c = absl::c_for_each(container_, AccumulateCalls());
  80. // Don't rely on the unordered_set's order.
  81. std::sort(c.calls.begin(), c.calls.end());
  82. EXPECT_EQ(vector_, c.calls);
  83. // Works with temporary container, too.
  84. AccumulateCalls c2 =
  85. absl::c_for_each(std::unordered_set<int>(container_), AccumulateCalls());
  86. std::sort(c2.calls.begin(), c2.calls.end());
  87. EXPECT_EQ(vector_, c2.calls);
  88. }
  89. TEST_F(NonMutatingTest, FindReturnsCorrectType) {
  90. auto it = absl::c_find(container_, 3);
  91. EXPECT_EQ(3, *it);
  92. absl::c_find(absl::implicit_cast<const std::list<int>&>(sequence_), 3);
  93. }
  94. TEST_F(NonMutatingTest, FindIf) { absl::c_find_if(container_, Predicate); }
  95. TEST_F(NonMutatingTest, FindIfNot) {
  96. absl::c_find_if_not(container_, Predicate);
  97. }
  98. TEST_F(NonMutatingTest, FindEnd) {
  99. absl::c_find_end(sequence_, vector_);
  100. absl::c_find_end(vector_, sequence_);
  101. }
  102. TEST_F(NonMutatingTest, FindEndWithPredicate) {
  103. absl::c_find_end(sequence_, vector_, BinPredicate);
  104. absl::c_find_end(vector_, sequence_, BinPredicate);
  105. }
  106. TEST_F(NonMutatingTest, FindFirstOf) {
  107. absl::c_find_first_of(container_, sequence_);
  108. absl::c_find_first_of(sequence_, container_);
  109. }
  110. TEST_F(NonMutatingTest, FindFirstOfWithPredicate) {
  111. absl::c_find_first_of(container_, sequence_, BinPredicate);
  112. absl::c_find_first_of(sequence_, container_, BinPredicate);
  113. }
  114. TEST_F(NonMutatingTest, AdjacentFind) { absl::c_adjacent_find(sequence_); }
  115. TEST_F(NonMutatingTest, AdjacentFindWithPredicate) {
  116. absl::c_adjacent_find(sequence_, BinPredicate);
  117. }
  118. TEST_F(NonMutatingTest, Count) { EXPECT_EQ(1, absl::c_count(container_, 3)); }
  119. TEST_F(NonMutatingTest, CountIf) {
  120. EXPECT_EQ(2, absl::c_count_if(container_, Predicate));
  121. const std::unordered_set<int>& const_container = container_;
  122. EXPECT_EQ(2, absl::c_count_if(const_container, Predicate));
  123. }
  124. TEST_F(NonMutatingTest, Mismatch) {
  125. // Testing necessary as absl::c_mismatch executes logic.
  126. {
  127. auto result = absl::c_mismatch(vector_, sequence_);
  128. EXPECT_EQ(result.first, vector_.end());
  129. EXPECT_EQ(result.second, sequence_.end());
  130. }
  131. {
  132. auto result = absl::c_mismatch(sequence_, vector_);
  133. EXPECT_EQ(result.first, sequence_.end());
  134. EXPECT_EQ(result.second, vector_.end());
  135. }
  136. sequence_.back() = 5;
  137. {
  138. auto result = absl::c_mismatch(vector_, sequence_);
  139. EXPECT_EQ(result.first, std::prev(vector_.end()));
  140. EXPECT_EQ(result.second, std::prev(sequence_.end()));
  141. }
  142. {
  143. auto result = absl::c_mismatch(sequence_, vector_);
  144. EXPECT_EQ(result.first, std::prev(sequence_.end()));
  145. EXPECT_EQ(result.second, std::prev(vector_.end()));
  146. }
  147. sequence_.pop_back();
  148. {
  149. auto result = absl::c_mismatch(vector_, sequence_);
  150. EXPECT_EQ(result.first, std::prev(vector_.end()));
  151. EXPECT_EQ(result.second, sequence_.end());
  152. }
  153. {
  154. auto result = absl::c_mismatch(sequence_, vector_);
  155. EXPECT_EQ(result.first, sequence_.end());
  156. EXPECT_EQ(result.second, std::prev(vector_.end()));
  157. }
  158. {
  159. struct NoNotEquals {
  160. constexpr bool operator==(NoNotEquals) const { return true; }
  161. constexpr bool operator!=(NoNotEquals) const = delete;
  162. };
  163. std::vector<NoNotEquals> first;
  164. std::list<NoNotEquals> second;
  165. // Check this still compiles.
  166. absl::c_mismatch(first, second);
  167. }
  168. }
  169. TEST_F(NonMutatingTest, MismatchWithPredicate) {
  170. // Testing necessary as absl::c_mismatch executes logic.
  171. {
  172. auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
  173. EXPECT_EQ(result.first, vector_.begin());
  174. EXPECT_EQ(result.second, sequence_.begin());
  175. }
  176. {
  177. auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
  178. EXPECT_EQ(result.first, sequence_.begin());
  179. EXPECT_EQ(result.second, vector_.begin());
  180. }
  181. sequence_.front() = 0;
  182. {
  183. auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
  184. EXPECT_EQ(result.first, vector_.begin());
  185. EXPECT_EQ(result.second, sequence_.begin());
  186. }
  187. {
  188. auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
  189. EXPECT_EQ(result.first, std::next(sequence_.begin()));
  190. EXPECT_EQ(result.second, std::next(vector_.begin()));
  191. }
  192. sequence_.clear();
  193. {
  194. auto result = absl::c_mismatch(vector_, sequence_, BinPredicate);
  195. EXPECT_EQ(result.first, vector_.begin());
  196. EXPECT_EQ(result.second, sequence_.end());
  197. }
  198. {
  199. auto result = absl::c_mismatch(sequence_, vector_, BinPredicate);
  200. EXPECT_EQ(result.first, sequence_.end());
  201. EXPECT_EQ(result.second, vector_.begin());
  202. }
  203. }
  204. TEST_F(NonMutatingTest, Equal) {
  205. EXPECT_TRUE(absl::c_equal(vector_, sequence_));
  206. EXPECT_TRUE(absl::c_equal(sequence_, vector_));
  207. EXPECT_TRUE(absl::c_equal(sequence_, array_));
  208. EXPECT_TRUE(absl::c_equal(array_, vector_));
  209. // Test that behavior appropriately differs from that of equal().
  210. std::vector<int> vector_plus = {1, 2, 3};
  211. vector_plus.push_back(4);
  212. EXPECT_FALSE(absl::c_equal(vector_plus, sequence_));
  213. EXPECT_FALSE(absl::c_equal(sequence_, vector_plus));
  214. EXPECT_FALSE(absl::c_equal(array_, vector_plus));
  215. }
  216. TEST_F(NonMutatingTest, EqualWithPredicate) {
  217. EXPECT_TRUE(absl::c_equal(vector_, sequence_, Equals));
  218. EXPECT_TRUE(absl::c_equal(sequence_, vector_, Equals));
  219. EXPECT_TRUE(absl::c_equal(array_, sequence_, Equals));
  220. EXPECT_TRUE(absl::c_equal(vector_, array_, Equals));
  221. // Test that behavior appropriately differs from that of equal().
  222. std::vector<int> vector_plus = {1, 2, 3};
  223. vector_plus.push_back(4);
  224. EXPECT_FALSE(absl::c_equal(vector_plus, sequence_, Equals));
  225. EXPECT_FALSE(absl::c_equal(sequence_, vector_plus, Equals));
  226. EXPECT_FALSE(absl::c_equal(vector_plus, array_, Equals));
  227. }
  228. TEST_F(NonMutatingTest, IsPermutation) {
  229. auto vector_permut_ = vector_;
  230. std::next_permutation(vector_permut_.begin(), vector_permut_.end());
  231. EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_));
  232. EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_));
  233. // Test that behavior appropriately differs from that of is_permutation().
  234. std::vector<int> vector_plus = {1, 2, 3};
  235. vector_plus.push_back(4);
  236. EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_));
  237. EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus));
  238. }
  239. TEST_F(NonMutatingTest, IsPermutationWithPredicate) {
  240. auto vector_permut_ = vector_;
  241. std::next_permutation(vector_permut_.begin(), vector_permut_.end());
  242. EXPECT_TRUE(absl::c_is_permutation(vector_permut_, sequence_, Equals));
  243. EXPECT_TRUE(absl::c_is_permutation(sequence_, vector_permut_, Equals));
  244. // Test that behavior appropriately differs from that of is_permutation().
  245. std::vector<int> vector_plus = {1, 2, 3};
  246. vector_plus.push_back(4);
  247. EXPECT_FALSE(absl::c_is_permutation(vector_plus, sequence_, Equals));
  248. EXPECT_FALSE(absl::c_is_permutation(sequence_, vector_plus, Equals));
  249. }
  250. TEST_F(NonMutatingTest, Search) {
  251. absl::c_search(sequence_, vector_);
  252. absl::c_search(vector_, sequence_);
  253. absl::c_search(array_, sequence_);
  254. }
  255. TEST_F(NonMutatingTest, SearchWithPredicate) {
  256. absl::c_search(sequence_, vector_, BinPredicate);
  257. absl::c_search(vector_, sequence_, BinPredicate);
  258. }
  259. TEST_F(NonMutatingTest, SearchN) { absl::c_search_n(sequence_, 3, 1); }
  260. TEST_F(NonMutatingTest, SearchNWithPredicate) {
  261. absl::c_search_n(sequence_, 3, 1, BinPredicate);
  262. }
  263. TEST_F(NonMutatingTest, LowerBound) {
  264. std::list<int>::iterator i = absl::c_lower_bound(sequence_, 3);
  265. ASSERT_TRUE(i != sequence_.end());
  266. EXPECT_EQ(2, std::distance(sequence_.begin(), i));
  267. EXPECT_EQ(3, *i);
  268. }
  269. TEST_F(NonMutatingTest, LowerBoundWithPredicate) {
  270. std::vector<int> v(vector_);
  271. std::sort(v.begin(), v.end(), std::greater<int>());
  272. std::vector<int>::iterator i = absl::c_lower_bound(v, 3, std::greater<int>());
  273. EXPECT_TRUE(i == v.begin());
  274. EXPECT_EQ(3, *i);
  275. }
  276. TEST_F(NonMutatingTest, UpperBound) {
  277. std::list<int>::iterator i = absl::c_upper_bound(sequence_, 1);
  278. ASSERT_TRUE(i != sequence_.end());
  279. EXPECT_EQ(1, std::distance(sequence_.begin(), i));
  280. EXPECT_EQ(2, *i);
  281. }
  282. TEST_F(NonMutatingTest, UpperBoundWithPredicate) {
  283. std::vector<int> v(vector_);
  284. std::sort(v.begin(), v.end(), std::greater<int>());
  285. std::vector<int>::iterator i = absl::c_upper_bound(v, 1, std::greater<int>());
  286. EXPECT_EQ(3, i - v.begin());
  287. EXPECT_TRUE(i == v.end());
  288. }
  289. TEST_F(NonMutatingTest, EqualRange) {
  290. std::pair<std::list<int>::iterator, std::list<int>::iterator> p =
  291. absl::c_equal_range(sequence_, 2);
  292. EXPECT_EQ(1, std::distance(sequence_.begin(), p.first));
  293. EXPECT_EQ(2, std::distance(sequence_.begin(), p.second));
  294. }
  295. TEST_F(NonMutatingTest, EqualRangeArray) {
  296. auto p = absl::c_equal_range(array_, 2);
  297. EXPECT_EQ(1, std::distance(std::begin(array_), p.first));
  298. EXPECT_EQ(2, std::distance(std::begin(array_), p.second));
  299. }
  300. TEST_F(NonMutatingTest, EqualRangeWithPredicate) {
  301. std::vector<int> v(vector_);
  302. std::sort(v.begin(), v.end(), std::greater<int>());
  303. std::pair<std::vector<int>::iterator, std::vector<int>::iterator> p =
  304. absl::c_equal_range(v, 2, std::greater<int>());
  305. EXPECT_EQ(1, std::distance(v.begin(), p.first));
  306. EXPECT_EQ(2, std::distance(v.begin(), p.second));
  307. }
  308. TEST_F(NonMutatingTest, BinarySearch) {
  309. EXPECT_TRUE(absl::c_binary_search(vector_, 2));
  310. EXPECT_TRUE(absl::c_binary_search(std::vector<int>(vector_), 2));
  311. }
  312. TEST_F(NonMutatingTest, BinarySearchWithPredicate) {
  313. std::vector<int> v(vector_);
  314. std::sort(v.begin(), v.end(), std::greater<int>());
  315. EXPECT_TRUE(absl::c_binary_search(v, 2, std::greater<int>()));
  316. EXPECT_TRUE(
  317. absl::c_binary_search(std::vector<int>(v), 2, std::greater<int>()));
  318. }
  319. TEST_F(NonMutatingTest, MinElement) {
  320. std::list<int>::iterator i = absl::c_min_element(sequence_);
  321. ASSERT_TRUE(i != sequence_.end());
  322. EXPECT_EQ(*i, 1);
  323. }
  324. TEST_F(NonMutatingTest, MinElementWithPredicate) {
  325. std::list<int>::iterator i =
  326. absl::c_min_element(sequence_, std::greater<int>());
  327. ASSERT_TRUE(i != sequence_.end());
  328. EXPECT_EQ(*i, 3);
  329. }
  330. TEST_F(NonMutatingTest, MaxElement) {
  331. std::list<int>::iterator i = absl::c_max_element(sequence_);
  332. ASSERT_TRUE(i != sequence_.end());
  333. EXPECT_EQ(*i, 3);
  334. }
  335. TEST_F(NonMutatingTest, MaxElementWithPredicate) {
  336. std::list<int>::iterator i =
  337. absl::c_max_element(sequence_, std::greater<int>());
  338. ASSERT_TRUE(i != sequence_.end());
  339. EXPECT_EQ(*i, 1);
  340. }
  341. TEST_F(NonMutatingTest, LexicographicalCompare) {
  342. EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_));
  343. std::vector<int> v;
  344. v.push_back(1);
  345. v.push_back(2);
  346. v.push_back(4);
  347. EXPECT_TRUE(absl::c_lexicographical_compare(sequence_, v));
  348. EXPECT_TRUE(absl::c_lexicographical_compare(std::list<int>(sequence_), v));
  349. }
  350. TEST_F(NonMutatingTest, LexicographicalCopmareWithPredicate) {
  351. EXPECT_FALSE(absl::c_lexicographical_compare(sequence_, sequence_,
  352. std::greater<int>()));
  353. std::vector<int> v;
  354. v.push_back(1);
  355. v.push_back(2);
  356. v.push_back(4);
  357. EXPECT_TRUE(
  358. absl::c_lexicographical_compare(v, sequence_, std::greater<int>()));
  359. EXPECT_TRUE(absl::c_lexicographical_compare(
  360. std::vector<int>(v), std::list<int>(sequence_), std::greater<int>()));
  361. }
  362. TEST_F(NonMutatingTest, Includes) {
  363. std::set<int> s(vector_.begin(), vector_.end());
  364. s.insert(4);
  365. EXPECT_TRUE(absl::c_includes(s, vector_));
  366. }
  367. TEST_F(NonMutatingTest, IncludesWithPredicate) {
  368. std::vector<int> v = {3, 2, 1};
  369. std::set<int, std::greater<int>> s(v.begin(), v.end());
  370. s.insert(4);
  371. EXPECT_TRUE(absl::c_includes(s, v, std::greater<int>()));
  372. }
  373. class NumericMutatingTest : public testing::Test {
  374. protected:
  375. std::list<int> list_ = {1, 2, 3};
  376. std::vector<int> output_;
  377. };
  378. TEST_F(NumericMutatingTest, Iota) {
  379. absl::c_iota(list_, 5);
  380. std::list<int> expected{5, 6, 7};
  381. EXPECT_EQ(list_, expected);
  382. }
  383. TEST_F(NonMutatingTest, Accumulate) {
  384. EXPECT_EQ(absl::c_accumulate(sequence_, 4), 1 + 2 + 3 + 4);
  385. }
  386. TEST_F(NonMutatingTest, AccumulateWithBinaryOp) {
  387. EXPECT_EQ(absl::c_accumulate(sequence_, 4, std::multiplies<int>()),
  388. 1 * 2 * 3 * 4);
  389. }
  390. TEST_F(NonMutatingTest, AccumulateLvalueInit) {
  391. int lvalue = 4;
  392. EXPECT_EQ(absl::c_accumulate(sequence_, lvalue), 1 + 2 + 3 + 4);
  393. }
  394. TEST_F(NonMutatingTest, AccumulateWithBinaryOpLvalueInit) {
  395. int lvalue = 4;
  396. EXPECT_EQ(absl::c_accumulate(sequence_, lvalue, std::multiplies<int>()),
  397. 1 * 2 * 3 * 4);
  398. }
  399. TEST_F(NonMutatingTest, InnerProduct) {
  400. EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 1000),
  401. 1000 + 1 * 1 + 2 * 2 + 3 * 3);
  402. }
  403. TEST_F(NonMutatingTest, InnerProductWithBinaryOps) {
  404. EXPECT_EQ(absl::c_inner_product(sequence_, vector_, 10,
  405. std::multiplies<int>(), std::plus<int>()),
  406. 10 * (1 + 1) * (2 + 2) * (3 + 3));
  407. }
  408. TEST_F(NonMutatingTest, InnerProductLvalueInit) {
  409. int lvalue = 1000;
  410. EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue),
  411. 1000 + 1 * 1 + 2 * 2 + 3 * 3);
  412. }
  413. TEST_F(NonMutatingTest, InnerProductWithBinaryOpsLvalueInit) {
  414. int lvalue = 10;
  415. EXPECT_EQ(absl::c_inner_product(sequence_, vector_, lvalue,
  416. std::multiplies<int>(), std::plus<int>()),
  417. 10 * (1 + 1) * (2 + 2) * (3 + 3));
  418. }
  419. TEST_F(NumericMutatingTest, AdjacentDifference) {
  420. auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_));
  421. *last = 1000;
  422. std::vector<int> expected{1, 2 - 1, 3 - 2, 1000};
  423. EXPECT_EQ(output_, expected);
  424. }
  425. TEST_F(NumericMutatingTest, AdjacentDifferenceWithBinaryOp) {
  426. auto last = absl::c_adjacent_difference(list_, std::back_inserter(output_),
  427. std::multiplies<int>());
  428. *last = 1000;
  429. std::vector<int> expected{1, 2 * 1, 3 * 2, 1000};
  430. EXPECT_EQ(output_, expected);
  431. }
  432. TEST_F(NumericMutatingTest, PartialSum) {
  433. auto last = absl::c_partial_sum(list_, std::back_inserter(output_));
  434. *last = 1000;
  435. std::vector<int> expected{1, 1 + 2, 1 + 2 + 3, 1000};
  436. EXPECT_EQ(output_, expected);
  437. }
  438. TEST_F(NumericMutatingTest, PartialSumWithBinaryOp) {
  439. auto last = absl::c_partial_sum(list_, std::back_inserter(output_),
  440. std::multiplies<int>());
  441. *last = 1000;
  442. std::vector<int> expected{1, 1 * 2, 1 * 2 * 3, 1000};
  443. EXPECT_EQ(output_, expected);
  444. }
  445. TEST_F(NonMutatingTest, LinearSearch) {
  446. EXPECT_TRUE(absl::c_linear_search(container_, 3));
  447. EXPECT_FALSE(absl::c_linear_search(container_, 4));
  448. }
  449. TEST_F(NonMutatingTest, AllOf) {
  450. const std::vector<int>& v = vector_;
  451. EXPECT_FALSE(absl::c_all_of(v, [](int x) { return x > 1; }));
  452. EXPECT_TRUE(absl::c_all_of(v, [](int x) { return x > 0; }));
  453. }
  454. TEST_F(NonMutatingTest, AnyOf) {
  455. const std::vector<int>& v = vector_;
  456. EXPECT_TRUE(absl::c_any_of(v, [](int x) { return x > 2; }));
  457. EXPECT_FALSE(absl::c_any_of(v, [](int x) { return x > 5; }));
  458. }
  459. TEST_F(NonMutatingTest, NoneOf) {
  460. const std::vector<int>& v = vector_;
  461. EXPECT_FALSE(absl::c_none_of(v, [](int x) { return x > 2; }));
  462. EXPECT_TRUE(absl::c_none_of(v, [](int x) { return x > 5; }));
  463. }
  464. TEST_F(NonMutatingTest, MinMaxElementLess) {
  465. std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
  466. p = absl::c_minmax_element(vector_, std::less<int>());
  467. EXPECT_TRUE(p.first == vector_.begin());
  468. EXPECT_TRUE(p.second == vector_.begin() + 2);
  469. }
  470. TEST_F(NonMutatingTest, MinMaxElementGreater) {
  471. std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
  472. p = absl::c_minmax_element(vector_, std::greater<int>());
  473. EXPECT_TRUE(p.first == vector_.begin() + 2);
  474. EXPECT_TRUE(p.second == vector_.begin());
  475. }
  476. TEST_F(NonMutatingTest, MinMaxElementNoPredicate) {
  477. std::pair<std::vector<int>::const_iterator, std::vector<int>::const_iterator>
  478. p = absl::c_minmax_element(vector_);
  479. EXPECT_TRUE(p.first == vector_.begin());
  480. EXPECT_TRUE(p.second == vector_.begin() + 2);
  481. }
  482. class SortingTest : public testing::Test {
  483. protected:
  484. std::list<int> sorted_ = {1, 2, 3, 4};
  485. std::list<int> unsorted_ = {2, 4, 1, 3};
  486. std::list<int> reversed_ = {4, 3, 2, 1};
  487. };
  488. TEST_F(SortingTest, IsSorted) {
  489. EXPECT_TRUE(absl::c_is_sorted(sorted_));
  490. EXPECT_FALSE(absl::c_is_sorted(unsorted_));
  491. EXPECT_FALSE(absl::c_is_sorted(reversed_));
  492. }
  493. TEST_F(SortingTest, IsSortedWithPredicate) {
  494. EXPECT_FALSE(absl::c_is_sorted(sorted_, std::greater<int>()));
  495. EXPECT_FALSE(absl::c_is_sorted(unsorted_, std::greater<int>()));
  496. EXPECT_TRUE(absl::c_is_sorted(reversed_, std::greater<int>()));
  497. }
  498. TEST_F(SortingTest, IsSortedUntil) {
  499. EXPECT_EQ(1, *absl::c_is_sorted_until(unsorted_));
  500. EXPECT_EQ(4, *absl::c_is_sorted_until(unsorted_, std::greater<int>()));
  501. }
  502. TEST_F(SortingTest, NthElement) {
  503. std::vector<int> unsorted = {2, 4, 1, 3};
  504. absl::c_nth_element(unsorted, unsorted.begin() + 2);
  505. EXPECT_THAT(unsorted, ElementsAre(Lt(3), Lt(3), 3, Gt(3)));
  506. absl::c_nth_element(unsorted, unsorted.begin() + 2, std::greater<int>());
  507. EXPECT_THAT(unsorted, ElementsAre(Gt(2), Gt(2), 2, Lt(2)));
  508. }
  509. TEST(MutatingTest, IsPartitioned) {
  510. EXPECT_TRUE(
  511. absl::c_is_partitioned(std::vector<int>{1, 3, 5, 2, 4, 6}, IsOdd));
  512. EXPECT_FALSE(
  513. absl::c_is_partitioned(std::vector<int>{1, 2, 3, 4, 5, 6}, IsOdd));
  514. EXPECT_FALSE(
  515. absl::c_is_partitioned(std::vector<int>{2, 4, 6, 1, 3, 5}, IsOdd));
  516. }
  517. TEST(MutatingTest, Partition) {
  518. std::vector<int> actual = {1, 2, 3, 4, 5};
  519. absl::c_partition(actual, IsOdd);
  520. EXPECT_THAT(actual, Truly([](const std::vector<int>& c) {
  521. return absl::c_is_partitioned(c, IsOdd);
  522. }));
  523. }
  524. TEST(MutatingTest, StablePartition) {
  525. std::vector<int> actual = {1, 2, 3, 4, 5};
  526. absl::c_stable_partition(actual, IsOdd);
  527. EXPECT_THAT(actual, ElementsAre(1, 3, 5, 2, 4));
  528. }
  529. TEST(MutatingTest, PartitionCopy) {
  530. const std::vector<int> initial = {1, 2, 3, 4, 5};
  531. std::vector<int> odds, evens;
  532. auto ends = absl::c_partition_copy(initial, back_inserter(odds),
  533. back_inserter(evens), IsOdd);
  534. *ends.first = 7;
  535. *ends.second = 6;
  536. EXPECT_THAT(odds, ElementsAre(1, 3, 5, 7));
  537. EXPECT_THAT(evens, ElementsAre(2, 4, 6));
  538. }
  539. TEST(MutatingTest, PartitionPoint) {
  540. const std::vector<int> initial = {1, 3, 5, 2, 4};
  541. auto middle = absl::c_partition_point(initial, IsOdd);
  542. EXPECT_EQ(2, *middle);
  543. }
  544. TEST(MutatingTest, CopyMiddle) {
  545. const std::vector<int> initial = {4, -1, -2, -3, 5};
  546. const std::list<int> input = {1, 2, 3};
  547. const std::vector<int> expected = {4, 1, 2, 3, 5};
  548. std::list<int> test_list(initial.begin(), initial.end());
  549. absl::c_copy(input, ++test_list.begin());
  550. EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
  551. std::vector<int> test_vector = initial;
  552. absl::c_copy(input, test_vector.begin() + 1);
  553. EXPECT_EQ(expected, test_vector);
  554. }
  555. TEST(MutatingTest, CopyFrontInserter) {
  556. const std::list<int> initial = {4, 5};
  557. const std::list<int> input = {1, 2, 3};
  558. const std::list<int> expected = {3, 2, 1, 4, 5};
  559. std::list<int> test_list = initial;
  560. absl::c_copy(input, std::front_inserter(test_list));
  561. EXPECT_EQ(expected, test_list);
  562. }
  563. TEST(MutatingTest, CopyBackInserter) {
  564. const std::vector<int> initial = {4, 5};
  565. const std::list<int> input = {1, 2, 3};
  566. const std::vector<int> expected = {4, 5, 1, 2, 3};
  567. std::list<int> test_list(initial.begin(), initial.end());
  568. absl::c_copy(input, std::back_inserter(test_list));
  569. EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
  570. std::vector<int> test_vector = initial;
  571. absl::c_copy(input, std::back_inserter(test_vector));
  572. EXPECT_EQ(expected, test_vector);
  573. }
  574. TEST(MutatingTest, CopyN) {
  575. const std::vector<int> initial = {1, 2, 3, 4, 5};
  576. const std::vector<int> expected = {1, 2};
  577. std::vector<int> actual;
  578. absl::c_copy_n(initial, 2, back_inserter(actual));
  579. EXPECT_EQ(expected, actual);
  580. }
  581. TEST(MutatingTest, CopyIf) {
  582. const std::list<int> input = {1, 2, 3};
  583. std::vector<int> output;
  584. absl::c_copy_if(input, std::back_inserter(output),
  585. [](int i) { return i != 2; });
  586. EXPECT_THAT(output, ElementsAre(1, 3));
  587. }
  588. TEST(MutatingTest, CopyBackward) {
  589. std::vector<int> actual = {1, 2, 3, 4, 5};
  590. std::vector<int> expected = {1, 2, 1, 2, 3};
  591. absl::c_copy_backward(absl::MakeSpan(actual.data(), 3), actual.end());
  592. EXPECT_EQ(expected, actual);
  593. }
  594. TEST(MutatingTest, Move) {
  595. std::vector<std::unique_ptr<int>> src;
  596. src.emplace_back(absl::make_unique<int>(1));
  597. src.emplace_back(absl::make_unique<int>(2));
  598. src.emplace_back(absl::make_unique<int>(3));
  599. src.emplace_back(absl::make_unique<int>(4));
  600. src.emplace_back(absl::make_unique<int>(5));
  601. std::vector<std::unique_ptr<int>> dest = {};
  602. absl::c_move(src, std::back_inserter(dest));
  603. EXPECT_THAT(src, Each(IsNull()));
  604. EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(4),
  605. Pointee(5)));
  606. }
  607. TEST(MutatingTest, MoveBackward) {
  608. std::vector<std::unique_ptr<int>> actual;
  609. actual.emplace_back(absl::make_unique<int>(1));
  610. actual.emplace_back(absl::make_unique<int>(2));
  611. actual.emplace_back(absl::make_unique<int>(3));
  612. actual.emplace_back(absl::make_unique<int>(4));
  613. actual.emplace_back(absl::make_unique<int>(5));
  614. auto subrange = absl::MakeSpan(actual.data(), 3);
  615. absl::c_move_backward(subrange, actual.end());
  616. EXPECT_THAT(actual, ElementsAre(IsNull(), IsNull(), Pointee(1), Pointee(2),
  617. Pointee(3)));
  618. }
  619. TEST(MutatingTest, MoveWithRvalue) {
  620. auto MakeRValueSrc = [] {
  621. std::vector<std::unique_ptr<int>> src;
  622. src.emplace_back(absl::make_unique<int>(1));
  623. src.emplace_back(absl::make_unique<int>(2));
  624. src.emplace_back(absl::make_unique<int>(3));
  625. return src;
  626. };
  627. std::vector<std::unique_ptr<int>> dest = MakeRValueSrc();
  628. absl::c_move(MakeRValueSrc(), std::back_inserter(dest));
  629. EXPECT_THAT(dest, ElementsAre(Pointee(1), Pointee(2), Pointee(3), Pointee(1),
  630. Pointee(2), Pointee(3)));
  631. }
  632. TEST(MutatingTest, SwapRanges) {
  633. std::vector<int> odds = {2, 4, 6};
  634. std::vector<int> evens = {1, 3, 5};
  635. absl::c_swap_ranges(odds, evens);
  636. EXPECT_THAT(odds, ElementsAre(1, 3, 5));
  637. EXPECT_THAT(evens, ElementsAre(2, 4, 6));
  638. odds.pop_back();
  639. absl::c_swap_ranges(odds, evens);
  640. EXPECT_THAT(odds, ElementsAre(2, 4));
  641. EXPECT_THAT(evens, ElementsAre(1, 3, 6));
  642. absl::c_swap_ranges(evens, odds);
  643. EXPECT_THAT(odds, ElementsAre(1, 3));
  644. EXPECT_THAT(evens, ElementsAre(2, 4, 6));
  645. }
  646. TEST_F(NonMutatingTest, Transform) {
  647. std::vector<int> x{0, 2, 4}, y, z;
  648. auto end = absl::c_transform(x, back_inserter(y), std::negate<int>());
  649. EXPECT_EQ(std::vector<int>({0, -2, -4}), y);
  650. *end = 7;
  651. EXPECT_EQ(std::vector<int>({0, -2, -4, 7}), y);
  652. y = {1, 3, 0};
  653. end = absl::c_transform(x, y, back_inserter(z), std::plus<int>());
  654. EXPECT_EQ(std::vector<int>({1, 5, 4}), z);
  655. *end = 7;
  656. EXPECT_EQ(std::vector<int>({1, 5, 4, 7}), z);
  657. z.clear();
  658. y.pop_back();
  659. end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>());
  660. EXPECT_EQ(std::vector<int>({1, 5}), z);
  661. *end = 7;
  662. EXPECT_EQ(std::vector<int>({1, 5, 7}), z);
  663. z.clear();
  664. std::swap(x, y);
  665. end = absl::c_transform(x, y, std::back_inserter(z), std::plus<int>());
  666. EXPECT_EQ(std::vector<int>({1, 5}), z);
  667. *end = 7;
  668. EXPECT_EQ(std::vector<int>({1, 5, 7}), z);
  669. }
  670. TEST(MutatingTest, Replace) {
  671. const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
  672. const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
  673. std::vector<int> test_vector = initial;
  674. absl::c_replace(test_vector, 1, 4);
  675. EXPECT_EQ(expected, test_vector);
  676. std::list<int> test_list(initial.begin(), initial.end());
  677. absl::c_replace(test_list, 1, 4);
  678. EXPECT_EQ(std::list<int>(expected.begin(), expected.end()), test_list);
  679. }
  680. TEST(MutatingTest, ReplaceIf) {
  681. std::vector<int> actual = {1, 2, 3, 4, 5};
  682. const std::vector<int> expected = {0, 2, 0, 4, 0};
  683. absl::c_replace_if(actual, IsOdd, 0);
  684. EXPECT_EQ(expected, actual);
  685. }
  686. TEST(MutatingTest, ReplaceCopy) {
  687. const std::vector<int> initial = {1, 2, 3, 1, 4, 5};
  688. const std::vector<int> expected = {4, 2, 3, 4, 4, 5};
  689. std::vector<int> actual;
  690. absl::c_replace_copy(initial, back_inserter(actual), 1, 4);
  691. EXPECT_EQ(expected, actual);
  692. }
  693. TEST(MutatingTest, Sort) {
  694. std::vector<int> test_vector = {2, 3, 1, 4};
  695. absl::c_sort(test_vector);
  696. EXPECT_THAT(test_vector, ElementsAre(1, 2, 3, 4));
  697. }
  698. TEST(MutatingTest, SortWithPredicate) {
  699. std::vector<int> test_vector = {2, 3, 1, 4};
  700. absl::c_sort(test_vector, std::greater<int>());
  701. EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
  702. }
  703. // For absl::c_stable_sort tests. Needs an operator< that does not cover all
  704. // fields so that the test can check the sort preserves order of equal elements.
  705. struct Element {
  706. int key;
  707. int value;
  708. friend bool operator<(const Element& e1, const Element& e2) {
  709. return e1.key < e2.key;
  710. }
  711. // Make gmock print useful diagnostics.
  712. friend std::ostream& operator<<(std::ostream& o, const Element& e) {
  713. return o << "{" << e.key << ", " << e.value << "}";
  714. }
  715. };
  716. MATCHER_P2(IsElement, key, value, "") {
  717. return arg.key == key && arg.value == value;
  718. }
  719. TEST(MutatingTest, StableSort) {
  720. std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
  721. absl::c_stable_sort(test_vector);
  722. EXPECT_THAT(test_vector,
  723. ElementsAre(IsElement(1, 1), IsElement(1, 0), IsElement(2, 1),
  724. IsElement(2, 0), IsElement(2, 2)));
  725. }
  726. TEST(MutatingTest, StableSortWithPredicate) {
  727. std::vector<Element> test_vector = {{1, 1}, {2, 1}, {2, 0}, {1, 0}, {2, 2}};
  728. absl::c_stable_sort(test_vector, [](const Element& e1, const Element& e2) {
  729. return e2 < e1;
  730. });
  731. EXPECT_THAT(test_vector,
  732. ElementsAre(IsElement(2, 1), IsElement(2, 0), IsElement(2, 2),
  733. IsElement(1, 1), IsElement(1, 0)));
  734. }
  735. TEST(MutatingTest, ReplaceCopyIf) {
  736. const std::vector<int> initial = {1, 2, 3, 4, 5};
  737. const std::vector<int> expected = {0, 2, 0, 4, 0};
  738. std::vector<int> actual;
  739. absl::c_replace_copy_if(initial, back_inserter(actual), IsOdd, 0);
  740. EXPECT_EQ(expected, actual);
  741. }
  742. TEST(MutatingTest, Fill) {
  743. std::vector<int> actual(5);
  744. absl::c_fill(actual, 1);
  745. EXPECT_THAT(actual, ElementsAre(1, 1, 1, 1, 1));
  746. }
  747. TEST(MutatingTest, FillN) {
  748. std::vector<int> actual(5, 0);
  749. absl::c_fill_n(actual, 2, 1);
  750. EXPECT_THAT(actual, ElementsAre(1, 1, 0, 0, 0));
  751. }
  752. TEST(MutatingTest, Generate) {
  753. std::vector<int> actual(5);
  754. int x = 0;
  755. absl::c_generate(actual, [&x]() { return ++x; });
  756. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
  757. }
  758. TEST(MutatingTest, GenerateN) {
  759. std::vector<int> actual(5, 0);
  760. int x = 0;
  761. absl::c_generate_n(actual, 3, [&x]() { return ++x; });
  762. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 0, 0));
  763. }
  764. TEST(MutatingTest, RemoveCopy) {
  765. std::vector<int> actual;
  766. absl::c_remove_copy(std::vector<int>{1, 2, 3}, back_inserter(actual), 2);
  767. EXPECT_THAT(actual, ElementsAre(1, 3));
  768. }
  769. TEST(MutatingTest, RemoveCopyIf) {
  770. std::vector<int> actual;
  771. absl::c_remove_copy_if(std::vector<int>{1, 2, 3}, back_inserter(actual),
  772. IsOdd);
  773. EXPECT_THAT(actual, ElementsAre(2));
  774. }
  775. TEST(MutatingTest, UniqueCopy) {
  776. std::vector<int> actual;
  777. absl::c_unique_copy(std::vector<int>{1, 2, 2, 2, 3, 3, 2},
  778. back_inserter(actual));
  779. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 2));
  780. }
  781. TEST(MutatingTest, UniqueCopyWithPredicate) {
  782. std::vector<int> actual;
  783. absl::c_unique_copy(std::vector<int>{1, 2, 3, -1, -2, -3, 1},
  784. back_inserter(actual),
  785. [](int x, int y) { return (x < 0) == (y < 0); });
  786. EXPECT_THAT(actual, ElementsAre(1, -1, 1));
  787. }
  788. TEST(MutatingTest, Reverse) {
  789. std::vector<int> test_vector = {1, 2, 3, 4};
  790. absl::c_reverse(test_vector);
  791. EXPECT_THAT(test_vector, ElementsAre(4, 3, 2, 1));
  792. std::list<int> test_list = {1, 2, 3, 4};
  793. absl::c_reverse(test_list);
  794. EXPECT_THAT(test_list, ElementsAre(4, 3, 2, 1));
  795. }
  796. TEST(MutatingTest, ReverseCopy) {
  797. std::vector<int> actual;
  798. absl::c_reverse_copy(std::vector<int>{1, 2, 3, 4}, back_inserter(actual));
  799. EXPECT_THAT(actual, ElementsAre(4, 3, 2, 1));
  800. }
  801. TEST(MutatingTest, Rotate) {
  802. std::vector<int> actual = {1, 2, 3, 4};
  803. auto it = absl::c_rotate(actual, actual.begin() + 2);
  804. EXPECT_THAT(actual, testing::ElementsAreArray({3, 4, 1, 2}));
  805. EXPECT_EQ(*it, 1);
  806. }
  807. TEST(MutatingTest, RotateCopy) {
  808. std::vector<int> initial = {1, 2, 3, 4};
  809. std::vector<int> actual;
  810. auto end =
  811. absl::c_rotate_copy(initial, initial.begin() + 2, back_inserter(actual));
  812. *end = 5;
  813. EXPECT_THAT(actual, ElementsAre(3, 4, 1, 2, 5));
  814. }
  815. TEST(MutatingTest, Shuffle) {
  816. std::vector<int> actual = {1, 2, 3, 4, 5};
  817. absl::c_shuffle(actual, std::random_device());
  818. EXPECT_THAT(actual, UnorderedElementsAre(1, 2, 3, 4, 5));
  819. }
  820. TEST(MutatingTest, PartialSort) {
  821. std::vector<int> sequence{5, 3, 42, 0};
  822. absl::c_partial_sort(sequence, sequence.begin() + 2);
  823. EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(0, 3));
  824. absl::c_partial_sort(sequence, sequence.begin() + 2, std::greater<int>());
  825. EXPECT_THAT(absl::MakeSpan(sequence.data(), 2), ElementsAre(42, 5));
  826. }
  827. TEST(MutatingTest, PartialSortCopy) {
  828. const std::vector<int> initial = {5, 3, 42, 0};
  829. std::vector<int> actual(2);
  830. absl::c_partial_sort_copy(initial, actual);
  831. EXPECT_THAT(actual, ElementsAre(0, 3));
  832. absl::c_partial_sort_copy(initial, actual, std::greater<int>());
  833. EXPECT_THAT(actual, ElementsAre(42, 5));
  834. }
  835. TEST(MutatingTest, Merge) {
  836. std::vector<int> actual;
  837. absl::c_merge(std::vector<int>{1, 3, 5}, std::vector<int>{2, 4},
  838. back_inserter(actual));
  839. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
  840. }
  841. TEST(MutatingTest, MergeWithComparator) {
  842. std::vector<int> actual;
  843. absl::c_merge(std::vector<int>{5, 3, 1}, std::vector<int>{4, 2},
  844. back_inserter(actual), std::greater<int>());
  845. EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
  846. }
  847. TEST(MutatingTest, InplaceMerge) {
  848. std::vector<int> actual = {1, 3, 5, 2, 4};
  849. absl::c_inplace_merge(actual, actual.begin() + 3);
  850. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 4, 5));
  851. }
  852. TEST(MutatingTest, InplaceMergeWithComparator) {
  853. std::vector<int> actual = {5, 3, 1, 4, 2};
  854. absl::c_inplace_merge(actual, actual.begin() + 3, std::greater<int>());
  855. EXPECT_THAT(actual, ElementsAre(5, 4, 3, 2, 1));
  856. }
  857. class SetOperationsTest : public testing::Test {
  858. protected:
  859. std::vector<int> a_ = {1, 2, 3};
  860. std::vector<int> b_ = {1, 3, 5};
  861. std::vector<int> a_reversed_ = {3, 2, 1};
  862. std::vector<int> b_reversed_ = {5, 3, 1};
  863. };
  864. TEST_F(SetOperationsTest, SetUnion) {
  865. std::vector<int> actual;
  866. absl::c_set_union(a_, b_, back_inserter(actual));
  867. EXPECT_THAT(actual, ElementsAre(1, 2, 3, 5));
  868. }
  869. TEST_F(SetOperationsTest, SetUnionWithComparator) {
  870. std::vector<int> actual;
  871. absl::c_set_union(a_reversed_, b_reversed_, back_inserter(actual),
  872. std::greater<int>());
  873. EXPECT_THAT(actual, ElementsAre(5, 3, 2, 1));
  874. }
  875. TEST_F(SetOperationsTest, SetIntersection) {
  876. std::vector<int> actual;
  877. absl::c_set_intersection(a_, b_, back_inserter(actual));
  878. EXPECT_THAT(actual, ElementsAre(1, 3));
  879. }
  880. TEST_F(SetOperationsTest, SetIntersectionWithComparator) {
  881. std::vector<int> actual;
  882. absl::c_set_intersection(a_reversed_, b_reversed_, back_inserter(actual),
  883. std::greater<int>());
  884. EXPECT_THAT(actual, ElementsAre(3, 1));
  885. }
  886. TEST_F(SetOperationsTest, SetDifference) {
  887. std::vector<int> actual;
  888. absl::c_set_difference(a_, b_, back_inserter(actual));
  889. EXPECT_THAT(actual, ElementsAre(2));
  890. }
  891. TEST_F(SetOperationsTest, SetDifferenceWithComparator) {
  892. std::vector<int> actual;
  893. absl::c_set_difference(a_reversed_, b_reversed_, back_inserter(actual),
  894. std::greater<int>());
  895. EXPECT_THAT(actual, ElementsAre(2));
  896. }
  897. TEST_F(SetOperationsTest, SetSymmetricDifference) {
  898. std::vector<int> actual;
  899. absl::c_set_symmetric_difference(a_, b_, back_inserter(actual));
  900. EXPECT_THAT(actual, ElementsAre(2, 5));
  901. }
  902. TEST_F(SetOperationsTest, SetSymmetricDifferenceWithComparator) {
  903. std::vector<int> actual;
  904. absl::c_set_symmetric_difference(a_reversed_, b_reversed_,
  905. back_inserter(actual), std::greater<int>());
  906. EXPECT_THAT(actual, ElementsAre(5, 2));
  907. }
  908. TEST(HeapOperationsTest, WithoutComparator) {
  909. std::vector<int> heap = {1, 2, 3};
  910. EXPECT_FALSE(absl::c_is_heap(heap));
  911. absl::c_make_heap(heap);
  912. EXPECT_TRUE(absl::c_is_heap(heap));
  913. heap.push_back(4);
  914. EXPECT_EQ(3, absl::c_is_heap_until(heap) - heap.begin());
  915. absl::c_push_heap(heap);
  916. EXPECT_EQ(4, heap[0]);
  917. absl::c_pop_heap(heap);
  918. EXPECT_EQ(4, heap[3]);
  919. absl::c_make_heap(heap);
  920. absl::c_sort_heap(heap);
  921. EXPECT_THAT(heap, ElementsAre(1, 2, 3, 4));
  922. EXPECT_FALSE(absl::c_is_heap(heap));
  923. }
  924. TEST(HeapOperationsTest, WithComparator) {
  925. using greater = std::greater<int>;
  926. std::vector<int> heap = {3, 2, 1};
  927. EXPECT_FALSE(absl::c_is_heap(heap, greater()));
  928. absl::c_make_heap(heap, greater());
  929. EXPECT_TRUE(absl::c_is_heap(heap, greater()));
  930. heap.push_back(0);
  931. EXPECT_EQ(3, absl::c_is_heap_until(heap, greater()) - heap.begin());
  932. absl::c_push_heap(heap, greater());
  933. EXPECT_EQ(0, heap[0]);
  934. absl::c_pop_heap(heap, greater());
  935. EXPECT_EQ(0, heap[3]);
  936. absl::c_make_heap(heap, greater());
  937. absl::c_sort_heap(heap, greater());
  938. EXPECT_THAT(heap, ElementsAre(3, 2, 1, 0));
  939. EXPECT_FALSE(absl::c_is_heap(heap, greater()));
  940. }
  941. TEST(MutatingTest, PermutationOperations) {
  942. std::vector<int> initial = {1, 2, 3, 4};
  943. std::vector<int> permuted = initial;
  944. absl::c_next_permutation(permuted);
  945. EXPECT_TRUE(absl::c_is_permutation(initial, permuted));
  946. EXPECT_TRUE(absl::c_is_permutation(initial, permuted, std::equal_to<int>()));
  947. std::vector<int> permuted2 = initial;
  948. absl::c_prev_permutation(permuted2, std::greater<int>());
  949. EXPECT_EQ(permuted, permuted2);
  950. absl::c_prev_permutation(permuted);
  951. EXPECT_EQ(initial, permuted);
  952. }
  953. } // namespace