PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/libfplutil/unit_tests/test_intrusive_list/test_intrusive_list.cc

https://gitlab.com/adam.lukaitis/fplutil
C++ | 1155 lines | 995 code | 136 blank | 24 comment | 1 complexity | 7d8de0a85bfcc5458f34a2439e49afac MD5 | raw file
  1. // Copyright 2015 Google Inc. All rights reserved.
  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. // http://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 <algorithm>
  15. #include <functional>
  16. #include "gtest/gtest.h"
  17. #include "fplutil/intrusive_list.h"
  18. // EXPECT_DEATH tests don't work on Android.
  19. #ifdef __ANDROID__
  20. #define NO_DEATH_TESTS
  21. #endif // __ANDROID__
  22. class IntegerListNode {
  23. public:
  24. IntegerListNode(int value) : node(), value_(value) {}
  25. // Older versions of Visual Studio don't generate move constructors or move
  26. // assignment operators.
  27. IntegerListNode(IntegerListNode&& other) {
  28. *this = std::move(other);
  29. }
  30. IntegerListNode& operator=(IntegerListNode&& other) {
  31. value_ = other.value_;
  32. node = std::move(other.node);
  33. return *this;
  34. }
  35. int value() const { return value_; }
  36. fpl::intrusive_list_node node;
  37. private:
  38. int value_;
  39. // Disallow copying.
  40. IntegerListNode(IntegerListNode&);
  41. IntegerListNode& operator=(IntegerListNode&);
  42. };
  43. bool IntegerListNodeComparitor(const IntegerListNode& a,
  44. const IntegerListNode& b) {
  45. return a.value() < b.value();
  46. }
  47. bool operator<(const IntegerListNode& a, const IntegerListNode& b) {
  48. return a.value() < b.value();
  49. }
  50. bool operator==(const IntegerListNode& a, const IntegerListNode& b) {
  51. return a.value() == b.value();
  52. }
  53. class intrusive_list_test : public testing::Test {
  54. protected:
  55. intrusive_list_test()
  56. : one_(1),
  57. two_(2),
  58. three_(3),
  59. four_(4),
  60. five_(5),
  61. six_(6),
  62. seven_(7),
  63. eight_(8),
  64. nine_(9),
  65. ten_(10),
  66. twenty_(20),
  67. thirty_(30),
  68. fourty_(40),
  69. fifty_(50) {}
  70. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> list_;
  71. IntegerListNode one_;
  72. IntegerListNode two_;
  73. IntegerListNode three_;
  74. IntegerListNode four_;
  75. IntegerListNode five_;
  76. IntegerListNode six_;
  77. IntegerListNode seven_;
  78. IntegerListNode eight_;
  79. IntegerListNode nine_;
  80. IntegerListNode ten_;
  81. IntegerListNode twenty_;
  82. IntegerListNode thirty_;
  83. IntegerListNode fourty_;
  84. IntegerListNode fifty_;
  85. };
  86. TEST_F(intrusive_list_test, push_back) {
  87. EXPECT_TRUE(!one_.node.in_list());
  88. EXPECT_TRUE(!two_.node.in_list());
  89. EXPECT_TRUE(!three_.node.in_list());
  90. EXPECT_TRUE(!four_.node.in_list());
  91. EXPECT_TRUE(!five_.node.in_list());
  92. list_.push_back(one_);
  93. list_.push_back(two_);
  94. list_.push_back(three_);
  95. list_.push_back(four_);
  96. list_.push_back(five_);
  97. EXPECT_TRUE(one_.node.in_list());
  98. EXPECT_TRUE(two_.node.in_list());
  99. EXPECT_TRUE(three_.node.in_list());
  100. EXPECT_TRUE(four_.node.in_list());
  101. EXPECT_TRUE(five_.node.in_list());
  102. auto iter = list_.begin();
  103. EXPECT_EQ(1, iter->value());
  104. ++iter;
  105. EXPECT_EQ(2, iter->value());
  106. ++iter;
  107. EXPECT_EQ(3, iter->value());
  108. ++iter;
  109. EXPECT_EQ(4, iter->value());
  110. ++iter;
  111. EXPECT_EQ(5, iter->value());
  112. ++iter;
  113. EXPECT_EQ(list_.end(), iter);
  114. EXPECT_EQ(1, list_.front().value());
  115. EXPECT_EQ(5, list_.back().value());
  116. }
  117. #ifndef NO_DEATH_TESTS
  118. TEST_F(intrusive_list_test, push_back_failure) {
  119. list_.push_back(one_);
  120. list_.push_back(two_);
  121. list_.push_back(three_);
  122. list_.push_back(four_);
  123. list_.push_back(five_);
  124. EXPECT_DEATH(list_.push_back(five_), ".");
  125. }
  126. #endif // NO_DEATH_TESTS
  127. TEST_F(intrusive_list_test, pop_back) {
  128. list_.push_back(one_);
  129. list_.push_back(two_);
  130. list_.push_back(three_);
  131. list_.push_back(four_);
  132. list_.push_back(five_);
  133. EXPECT_EQ(5, list_.back().value());
  134. list_.pop_back();
  135. EXPECT_EQ(4, list_.back().value());
  136. list_.pop_back();
  137. EXPECT_EQ(3, list_.back().value());
  138. list_.pop_back();
  139. list_.push_back(four_);
  140. EXPECT_EQ(4, list_.back().value());
  141. }
  142. TEST_F(intrusive_list_test, push_front) {
  143. list_.push_front(one_);
  144. list_.push_front(two_);
  145. list_.push_front(three_);
  146. list_.push_front(four_);
  147. list_.push_front(five_);
  148. auto iter = list_.begin();
  149. EXPECT_EQ(5, iter->value());
  150. ++iter;
  151. EXPECT_EQ(4, iter->value());
  152. ++iter;
  153. EXPECT_EQ(3, iter->value());
  154. ++iter;
  155. EXPECT_EQ(2, iter->value());
  156. ++iter;
  157. EXPECT_EQ(1, iter->value());
  158. ++iter;
  159. EXPECT_EQ(list_.end(), iter);
  160. EXPECT_EQ(5, list_.front().value());
  161. EXPECT_EQ(1, list_.back().value());
  162. }
  163. #ifndef NO_DEATH_TESTS
  164. TEST_F(intrusive_list_test, push_front_failure) {
  165. list_.push_front(five_);
  166. list_.push_front(four_);
  167. list_.push_front(three_);
  168. list_.push_front(two_);
  169. list_.push_front(one_);
  170. EXPECT_DEATH(list_.push_front(one_), ".");
  171. }
  172. #endif // NO_DEATH_TESTS
  173. TEST_F(intrusive_list_test, destructor) {
  174. list_.push_back(one_);
  175. list_.push_back(two_);
  176. {
  177. // These should remove themselves when they go out of scope.
  178. IntegerListNode one_hundred(100);
  179. IntegerListNode two_hundred(200);
  180. list_.push_back(one_hundred);
  181. list_.push_back(two_hundred);
  182. }
  183. list_.push_back(three_);
  184. list_.push_back(four_);
  185. list_.push_back(five_);
  186. auto iter = list_.begin();
  187. EXPECT_EQ(1, iter->value());
  188. ++iter;
  189. EXPECT_EQ(2, iter->value());
  190. ++iter;
  191. EXPECT_EQ(3, iter->value());
  192. ++iter;
  193. EXPECT_EQ(4, iter->value());
  194. ++iter;
  195. EXPECT_EQ(5, iter->value());
  196. ++iter;
  197. EXPECT_EQ(list_.end(), iter);
  198. EXPECT_EQ(1, list_.front().value());
  199. EXPECT_EQ(5, list_.back().value());
  200. }
  201. TEST_F(intrusive_list_test, move_node) {
  202. list_.push_back(one_);
  203. list_.push_back(two_);
  204. list_.push_back(three_);
  205. list_.push_back(four_);
  206. list_.push_back(five_);
  207. // Generally, when moving something it would be done implicitly when the
  208. // object holding it moves. This is just to demonstrate that it moves the
  209. // pointers around correctly when it does move.
  210. //
  211. // two_.node has four_.node's location in the list moved into it. four_.node
  212. // is left in a valid but unspecified state.
  213. two_.node = std::move(four_.node);
  214. auto iter = list_.begin();
  215. EXPECT_EQ(1, iter->value());
  216. ++iter;
  217. EXPECT_EQ(3, iter->value());
  218. ++iter;
  219. EXPECT_EQ(2, iter->value());
  220. ++iter;
  221. EXPECT_EQ(5, iter->value());
  222. ++iter;
  223. EXPECT_EQ(list_.end(), iter);
  224. }
  225. TEST_F(intrusive_list_test, rbegin_rend) {
  226. list_.push_back(one_);
  227. list_.push_back(two_);
  228. list_.push_back(three_);
  229. list_.push_back(four_);
  230. list_.push_back(five_);
  231. auto iter = list_.rbegin();
  232. EXPECT_EQ(5, iter->value());
  233. ++iter;
  234. EXPECT_EQ(4, iter->value());
  235. ++iter;
  236. EXPECT_EQ(3, iter->value());
  237. ++iter;
  238. EXPECT_EQ(2, iter->value());
  239. ++iter;
  240. EXPECT_EQ(1, iter->value());
  241. ++iter;
  242. EXPECT_EQ(list_.rend(), iter);
  243. }
  244. TEST_F(intrusive_list_test, crbegin_crend) {
  245. list_.push_back(one_);
  246. list_.push_back(two_);
  247. list_.push_back(three_);
  248. list_.push_back(four_);
  249. list_.push_back(five_);
  250. auto iter = list_.crbegin();
  251. EXPECT_EQ(5, iter->value());
  252. ++iter;
  253. EXPECT_EQ(4, iter->value());
  254. ++iter;
  255. EXPECT_EQ(3, iter->value());
  256. ++iter;
  257. EXPECT_EQ(2, iter->value());
  258. ++iter;
  259. EXPECT_EQ(1, iter->value());
  260. ++iter;
  261. EXPECT_EQ(list_.crend(), iter);
  262. }
  263. TEST_F(intrusive_list_test, clear) {
  264. EXPECT_TRUE(list_.empty());
  265. list_.push_back(one_);
  266. list_.push_back(two_);
  267. list_.push_back(three_);
  268. list_.push_back(four_);
  269. list_.push_back(five_);
  270. EXPECT_FALSE(list_.empty());
  271. list_.clear();
  272. EXPECT_TRUE(list_.empty());
  273. }
  274. TEST_F(intrusive_list_test, insert) {
  275. list_.push_back(one_);
  276. list_.push_back(two_);
  277. list_.push_back(three_);
  278. list_.push_back(four_);
  279. list_.push_back(five_);
  280. auto iter = list_.begin();
  281. ++iter;
  282. ++iter;
  283. list_.insert(iter, ten_);
  284. iter = list_.begin();
  285. EXPECT_EQ(1, iter->value());
  286. ++iter;
  287. EXPECT_EQ(2, iter->value());
  288. ++iter;
  289. EXPECT_EQ(10, iter->value());
  290. ++iter;
  291. EXPECT_EQ(3, iter->value());
  292. ++iter;
  293. EXPECT_EQ(4, iter->value());
  294. ++iter;
  295. EXPECT_EQ(5, iter->value());
  296. ++iter;
  297. EXPECT_EQ(list_.end(), iter);
  298. }
  299. TEST_F(intrusive_list_test, insert_before) {
  300. list_.push_back(one_);
  301. list_.push_back(two_);
  302. list_.push_back(three_);
  303. list_.push_back(four_);
  304. list_.push_back(five_);
  305. auto iter = list_.begin();
  306. ++iter;
  307. ++iter;
  308. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node>::insert_before(
  309. *iter, ten_);
  310. iter = list_.begin();
  311. EXPECT_EQ(1, iter->value());
  312. ++iter;
  313. EXPECT_EQ(2, iter->value());
  314. ++iter;
  315. EXPECT_EQ(10, iter->value());
  316. ++iter;
  317. EXPECT_EQ(3, iter->value());
  318. ++iter;
  319. EXPECT_EQ(4, iter->value());
  320. ++iter;
  321. EXPECT_EQ(5, iter->value());
  322. ++iter;
  323. EXPECT_EQ(list_.end(), iter);
  324. }
  325. TEST_F(intrusive_list_test, insert_after) {
  326. list_.push_back(one_);
  327. list_.push_back(two_);
  328. list_.push_back(three_);
  329. list_.push_back(four_);
  330. list_.push_back(five_);
  331. auto iter = list_.begin();
  332. ++iter;
  333. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node>::insert_after(
  334. *iter, ten_);
  335. iter = list_.begin();
  336. EXPECT_EQ(1, iter->value());
  337. ++iter;
  338. EXPECT_EQ(2, iter->value());
  339. ++iter;
  340. EXPECT_EQ(10, iter->value());
  341. ++iter;
  342. EXPECT_EQ(3, iter->value());
  343. ++iter;
  344. EXPECT_EQ(4, iter->value());
  345. ++iter;
  346. EXPECT_EQ(5, iter->value());
  347. ++iter;
  348. EXPECT_EQ(list_.end(), iter);
  349. }
  350. TEST_F(intrusive_list_test, insert_begin) {
  351. list_.push_back(one_);
  352. list_.push_back(two_);
  353. list_.push_back(three_);
  354. list_.push_back(four_);
  355. list_.push_back(five_);
  356. auto iter = list_.begin();
  357. list_.insert(iter, ten_);
  358. iter = list_.begin();
  359. EXPECT_EQ(10, iter->value());
  360. ++iter;
  361. EXPECT_EQ(1, iter->value());
  362. ++iter;
  363. EXPECT_EQ(2, iter->value());
  364. ++iter;
  365. EXPECT_EQ(3, iter->value());
  366. ++iter;
  367. EXPECT_EQ(4, iter->value());
  368. ++iter;
  369. EXPECT_EQ(5, iter->value());
  370. ++iter;
  371. EXPECT_EQ(list_.end(), iter);
  372. }
  373. TEST_F(intrusive_list_test, insert_end) {
  374. list_.push_back(one_);
  375. list_.push_back(two_);
  376. list_.push_back(three_);
  377. list_.push_back(four_);
  378. list_.push_back(five_);
  379. auto iter = list_.begin();
  380. ++iter;
  381. ++iter;
  382. ++iter;
  383. ++iter;
  384. ++iter;
  385. list_.insert(iter, ten_);
  386. iter = list_.begin();
  387. EXPECT_EQ(1, iter->value());
  388. ++iter;
  389. EXPECT_EQ(2, iter->value());
  390. ++iter;
  391. EXPECT_EQ(3, iter->value());
  392. ++iter;
  393. EXPECT_EQ(4, iter->value());
  394. ++iter;
  395. EXPECT_EQ(5, iter->value());
  396. ++iter;
  397. EXPECT_EQ(10, iter->value());
  398. ++iter;
  399. EXPECT_EQ(list_.end(), iter);
  400. }
  401. TEST_F(intrusive_list_test, insert_iter) {
  402. list_.push_back(one_);
  403. list_.push_back(two_);
  404. list_.push_back(three_);
  405. list_.push_back(four_);
  406. list_.push_back(five_);
  407. std::vector<IntegerListNode> list_nodes;
  408. list_nodes.push_back(IntegerListNode(100));
  409. list_nodes.push_back(IntegerListNode(200));
  410. list_nodes.push_back(IntegerListNode(300));
  411. auto iter = list_.begin();
  412. ++iter;
  413. ++iter;
  414. list_.insert(iter, list_nodes.begin(), list_nodes.end());
  415. iter = list_.begin();
  416. EXPECT_EQ(1, iter->value());
  417. ++iter;
  418. EXPECT_EQ(2, iter->value());
  419. ++iter;
  420. EXPECT_EQ(100, iter->value());
  421. ++iter;
  422. EXPECT_EQ(200, iter->value());
  423. ++iter;
  424. EXPECT_EQ(300, iter->value());
  425. ++iter;
  426. EXPECT_EQ(3, iter->value());
  427. ++iter;
  428. EXPECT_EQ(4, iter->value());
  429. ++iter;
  430. EXPECT_EQ(5, iter->value());
  431. ++iter;
  432. EXPECT_EQ(list_.end(), iter);
  433. }
  434. TEST_F(intrusive_list_test, size) {
  435. EXPECT_EQ(0u, list_.size());
  436. EXPECT_TRUE(list_.empty());
  437. list_.push_back(one_);
  438. EXPECT_EQ(1u, list_.size());
  439. EXPECT_FALSE(list_.empty());
  440. list_.push_back(two_);
  441. EXPECT_EQ(2u, list_.size());
  442. EXPECT_FALSE(list_.empty());
  443. list_.push_front(three_);
  444. EXPECT_EQ(3u, list_.size());
  445. EXPECT_FALSE(list_.empty());
  446. list_.push_back(four_);
  447. EXPECT_EQ(4u, list_.size());
  448. EXPECT_FALSE(list_.empty());
  449. list_.push_front(five_);
  450. EXPECT_EQ(5u, list_.size());
  451. EXPECT_FALSE(list_.empty());
  452. list_.pop_front();
  453. EXPECT_EQ(4u, list_.size());
  454. EXPECT_FALSE(list_.empty());
  455. list_.pop_back();
  456. EXPECT_EQ(3u, list_.size());
  457. EXPECT_FALSE(list_.empty());
  458. list_.pop_front();
  459. EXPECT_EQ(2u, list_.size());
  460. EXPECT_FALSE(list_.empty());
  461. list_.pop_back();
  462. EXPECT_EQ(1u, list_.size());
  463. EXPECT_FALSE(list_.empty());
  464. list_.pop_front();
  465. EXPECT_EQ(0u, list_.size());
  466. EXPECT_TRUE(list_.empty());
  467. }
  468. TEST_F(intrusive_list_test, unique) {
  469. IntegerListNode another_one(1);
  470. IntegerListNode another_three(3);
  471. IntegerListNode another_five(5);
  472. IntegerListNode another_five_again(5);
  473. list_.push_back(one_);
  474. list_.push_back(another_one);
  475. list_.push_back(two_);
  476. list_.push_back(three_);
  477. list_.push_back(another_three);
  478. list_.push_back(four_);
  479. list_.push_back(five_);
  480. list_.push_back(another_five);
  481. list_.push_back(another_five_again);
  482. list_.unique();
  483. auto iter = list_.begin();
  484. EXPECT_EQ(1, iter->value());
  485. ++iter;
  486. EXPECT_EQ(2, iter->value());
  487. ++iter;
  488. EXPECT_EQ(3, iter->value());
  489. ++iter;
  490. EXPECT_EQ(4, iter->value());
  491. ++iter;
  492. EXPECT_EQ(5, iter->value());
  493. ++iter;
  494. EXPECT_EQ(list_.end(), iter);
  495. EXPECT_TRUE(one_.node.in_list());
  496. EXPECT_TRUE(two_.node.in_list());
  497. EXPECT_TRUE(three_.node.in_list());
  498. EXPECT_TRUE(four_.node.in_list());
  499. EXPECT_TRUE(five_.node.in_list());
  500. EXPECT_TRUE(!another_one.node.in_list());
  501. EXPECT_TRUE(!another_three.node.in_list());
  502. EXPECT_TRUE(!another_five.node.in_list());
  503. EXPECT_TRUE(!another_five_again.node.in_list());
  504. }
  505. TEST_F(intrusive_list_test, unique_predicate) {
  506. IntegerListNode another_one(1);
  507. IntegerListNode another_three(3);
  508. IntegerListNode another_five(5);
  509. IntegerListNode another_five_again(5);
  510. list_.push_back(one_);
  511. list_.push_back(another_one);
  512. list_.push_back(two_);
  513. list_.push_back(three_);
  514. list_.push_back(another_three);
  515. list_.push_back(four_);
  516. list_.push_back(five_);
  517. list_.push_back(another_five);
  518. list_.push_back(another_five_again);
  519. list_.unique(std::equal_to<IntegerListNode>());
  520. auto iter = list_.begin();
  521. EXPECT_EQ(1, iter->value());
  522. ++iter;
  523. EXPECT_EQ(2, iter->value());
  524. ++iter;
  525. EXPECT_EQ(3, iter->value());
  526. ++iter;
  527. EXPECT_EQ(4, iter->value());
  528. ++iter;
  529. EXPECT_EQ(5, iter->value());
  530. ++iter;
  531. EXPECT_EQ(list_.end(), iter);
  532. EXPECT_TRUE(one_.node.in_list());
  533. EXPECT_TRUE(two_.node.in_list());
  534. EXPECT_TRUE(three_.node.in_list());
  535. EXPECT_TRUE(four_.node.in_list());
  536. EXPECT_TRUE(five_.node.in_list());
  537. EXPECT_TRUE(!another_one.node.in_list());
  538. EXPECT_TRUE(!another_three.node.in_list());
  539. EXPECT_TRUE(!another_five.node.in_list());
  540. EXPECT_TRUE(!another_five_again.node.in_list());
  541. }
  542. TEST_F(intrusive_list_test, sort_in_order) {
  543. list_.push_back(one_);
  544. list_.push_back(two_);
  545. list_.push_back(three_);
  546. list_.push_back(four_);
  547. list_.push_back(five_);
  548. list_.sort();
  549. auto iter = list_.begin();
  550. EXPECT_EQ(1, iter->value());
  551. ++iter;
  552. EXPECT_EQ(2, iter->value());
  553. ++iter;
  554. EXPECT_EQ(3, iter->value());
  555. ++iter;
  556. EXPECT_EQ(4, iter->value());
  557. ++iter;
  558. EXPECT_EQ(5, iter->value());
  559. ++iter;
  560. EXPECT_EQ(list_.end(), iter);
  561. }
  562. TEST_F(intrusive_list_test, sort_reverse_order) {
  563. list_.push_back(five_);
  564. list_.push_back(four_);
  565. list_.push_back(three_);
  566. list_.push_back(two_);
  567. list_.push_back(one_);
  568. list_.sort();
  569. auto iter = list_.begin();
  570. EXPECT_EQ(1, iter->value());
  571. ++iter;
  572. EXPECT_EQ(2, iter->value());
  573. ++iter;
  574. EXPECT_EQ(3, iter->value());
  575. ++iter;
  576. EXPECT_EQ(4, iter->value());
  577. ++iter;
  578. EXPECT_EQ(5, iter->value());
  579. ++iter;
  580. EXPECT_EQ(list_.end(), iter);
  581. }
  582. TEST_F(intrusive_list_test, sort_random_order) {
  583. list_.push_back(two_);
  584. list_.push_back(four_);
  585. list_.push_back(five_);
  586. list_.push_back(one_);
  587. list_.push_back(three_);
  588. list_.sort();
  589. auto iter = list_.begin();
  590. EXPECT_EQ(1, iter->value());
  591. ++iter;
  592. EXPECT_EQ(2, iter->value());
  593. ++iter;
  594. EXPECT_EQ(3, iter->value());
  595. ++iter;
  596. EXPECT_EQ(4, iter->value());
  597. ++iter;
  598. EXPECT_EQ(5, iter->value());
  599. ++iter;
  600. EXPECT_EQ(list_.end(), iter);
  601. }
  602. TEST_F(intrusive_list_test, sort_short_list) {
  603. list_.push_back(two_);
  604. list_.push_back(one_);
  605. list_.sort();
  606. auto iter = list_.begin();
  607. EXPECT_EQ(1, iter->value());
  608. ++iter;
  609. EXPECT_EQ(2, iter->value());
  610. ++iter;
  611. EXPECT_EQ(list_.end(), iter);
  612. }
  613. TEST_F(intrusive_list_test, splice_empty) {
  614. list_.push_back(one_);
  615. list_.push_back(two_);
  616. list_.push_back(three_);
  617. list_.push_back(four_);
  618. list_.push_back(five_);
  619. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  620. list_.splice(list_.begin(), other_list);
  621. auto iter = list_.begin();
  622. EXPECT_EQ(1, iter->value());
  623. ++iter;
  624. EXPECT_EQ(2, iter->value());
  625. ++iter;
  626. EXPECT_EQ(3, iter->value());
  627. ++iter;
  628. EXPECT_EQ(4, iter->value());
  629. ++iter;
  630. EXPECT_EQ(5, iter->value());
  631. ++iter;
  632. EXPECT_EQ(list_.end(), iter);
  633. iter = other_list.begin();
  634. EXPECT_EQ(other_list.end(), iter);
  635. }
  636. TEST_F(intrusive_list_test, splice_other_at_beginning) {
  637. list_.push_back(one_);
  638. list_.push_back(two_);
  639. list_.push_back(three_);
  640. list_.push_back(four_);
  641. list_.push_back(five_);
  642. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  643. other_list.push_back(ten_);
  644. other_list.push_back(twenty_);
  645. other_list.push_back(thirty_);
  646. other_list.push_back(fourty_);
  647. other_list.push_back(fifty_);
  648. list_.splice(list_.begin(), other_list);
  649. auto iter = list_.begin();
  650. EXPECT_EQ(10, iter->value());
  651. ++iter;
  652. EXPECT_EQ(20, iter->value());
  653. ++iter;
  654. EXPECT_EQ(30, iter->value());
  655. ++iter;
  656. EXPECT_EQ(40, iter->value());
  657. ++iter;
  658. EXPECT_EQ(50, iter->value());
  659. ++iter;
  660. EXPECT_EQ(1, iter->value());
  661. ++iter;
  662. EXPECT_EQ(2, iter->value());
  663. ++iter;
  664. EXPECT_EQ(3, iter->value());
  665. ++iter;
  666. EXPECT_EQ(4, iter->value());
  667. ++iter;
  668. EXPECT_EQ(5, iter->value());
  669. ++iter;
  670. EXPECT_EQ(list_.end(), iter);
  671. iter = other_list.begin();
  672. EXPECT_EQ(other_list.end(), iter);
  673. }
  674. TEST_F(intrusive_list_test, splice_other_at_end) {
  675. list_.push_back(one_);
  676. list_.push_back(two_);
  677. list_.push_back(three_);
  678. list_.push_back(four_);
  679. list_.push_back(five_);
  680. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  681. other_list.push_back(ten_);
  682. other_list.push_back(twenty_);
  683. other_list.push_back(thirty_);
  684. other_list.push_back(fourty_);
  685. other_list.push_back(fifty_);
  686. list_.splice(list_.end(), other_list);
  687. auto iter = list_.begin();
  688. EXPECT_EQ(1, iter->value());
  689. ++iter;
  690. EXPECT_EQ(2, iter->value());
  691. ++iter;
  692. EXPECT_EQ(3, iter->value());
  693. ++iter;
  694. EXPECT_EQ(4, iter->value());
  695. ++iter;
  696. EXPECT_EQ(5, iter->value());
  697. ++iter;
  698. EXPECT_EQ(10, iter->value());
  699. ++iter;
  700. EXPECT_EQ(20, iter->value());
  701. ++iter;
  702. EXPECT_EQ(30, iter->value());
  703. ++iter;
  704. EXPECT_EQ(40, iter->value());
  705. ++iter;
  706. EXPECT_EQ(50, iter->value());
  707. ++iter;
  708. EXPECT_EQ(list_.end(), iter);
  709. iter = other_list.begin();
  710. EXPECT_EQ(other_list.end(), iter);
  711. }
  712. TEST_F(intrusive_list_test, splice_other_at_middle) {
  713. list_.push_back(one_);
  714. list_.push_back(two_);
  715. list_.push_back(three_);
  716. list_.push_back(four_);
  717. list_.push_back(five_);
  718. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  719. other_list.push_back(ten_);
  720. other_list.push_back(twenty_);
  721. other_list.push_back(thirty_);
  722. other_list.push_back(fourty_);
  723. other_list.push_back(fifty_);
  724. auto iter = list_.begin();
  725. ++iter;
  726. ++iter;
  727. ++iter;
  728. list_.splice(iter, other_list);
  729. iter = list_.begin();
  730. EXPECT_EQ(1, iter->value());
  731. ++iter;
  732. EXPECT_EQ(2, iter->value());
  733. ++iter;
  734. EXPECT_EQ(3, iter->value());
  735. ++iter;
  736. EXPECT_EQ(10, iter->value());
  737. ++iter;
  738. EXPECT_EQ(20, iter->value());
  739. ++iter;
  740. EXPECT_EQ(30, iter->value());
  741. ++iter;
  742. EXPECT_EQ(40, iter->value());
  743. ++iter;
  744. EXPECT_EQ(50, iter->value());
  745. ++iter;
  746. EXPECT_EQ(4, iter->value());
  747. ++iter;
  748. EXPECT_EQ(5, iter->value());
  749. ++iter;
  750. EXPECT_EQ(list_.end(), iter);
  751. iter = other_list.begin();
  752. EXPECT_EQ(other_list.end(), iter);
  753. }
  754. TEST_F(intrusive_list_test, merge_alternating) {
  755. list_.push_back(one_);
  756. list_.push_back(three_);
  757. list_.push_back(five_);
  758. list_.push_back(seven_);
  759. list_.push_back(nine_);
  760. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  761. other_list.push_back(two_);
  762. other_list.push_back(four_);
  763. other_list.push_back(six_);
  764. other_list.push_back(eight_);
  765. other_list.push_back(ten_);
  766. list_.merge(other_list);
  767. auto iter = list_.begin();
  768. EXPECT_EQ(1, iter->value());
  769. ++iter;
  770. EXPECT_EQ(2, iter->value());
  771. ++iter;
  772. EXPECT_EQ(3, iter->value());
  773. ++iter;
  774. EXPECT_EQ(4, iter->value());
  775. ++iter;
  776. EXPECT_EQ(5, iter->value());
  777. ++iter;
  778. EXPECT_EQ(6, iter->value());
  779. ++iter;
  780. EXPECT_EQ(7, iter->value());
  781. ++iter;
  782. EXPECT_EQ(8, iter->value());
  783. ++iter;
  784. EXPECT_EQ(9, iter->value());
  785. ++iter;
  786. EXPECT_EQ(10, iter->value());
  787. ++iter;
  788. EXPECT_EQ(list_.end(), iter);
  789. iter = other_list.begin();
  790. EXPECT_EQ(other_list.end(), iter);
  791. }
  792. TEST_F(intrusive_list_test, merge_alternating2) {
  793. list_.push_back(one_);
  794. list_.push_back(two_);
  795. list_.push_back(five_);
  796. list_.push_back(six_);
  797. list_.push_back(nine_);
  798. list_.push_back(ten_);
  799. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  800. other_list.push_back(three_);
  801. other_list.push_back(four_);
  802. other_list.push_back(seven_);
  803. other_list.push_back(eight_);
  804. list_.merge(other_list);
  805. auto iter = list_.begin();
  806. EXPECT_EQ(1, iter->value());
  807. ++iter;
  808. EXPECT_EQ(2, iter->value());
  809. ++iter;
  810. EXPECT_EQ(3, iter->value());
  811. ++iter;
  812. EXPECT_EQ(4, iter->value());
  813. ++iter;
  814. EXPECT_EQ(5, iter->value());
  815. ++iter;
  816. EXPECT_EQ(6, iter->value());
  817. ++iter;
  818. EXPECT_EQ(7, iter->value());
  819. ++iter;
  820. EXPECT_EQ(8, iter->value());
  821. ++iter;
  822. EXPECT_EQ(9, iter->value());
  823. ++iter;
  824. EXPECT_EQ(10, iter->value());
  825. ++iter;
  826. EXPECT_EQ(list_.end(), iter);
  827. iter = other_list.begin();
  828. EXPECT_EQ(other_list.end(), iter);
  829. }
  830. TEST_F(intrusive_list_test, merge_this_other) {
  831. list_.push_back(one_);
  832. list_.push_back(two_);
  833. list_.push_back(three_);
  834. list_.push_back(four_);
  835. list_.push_back(five_);
  836. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  837. other_list.push_back(six_);
  838. other_list.push_back(seven_);
  839. other_list.push_back(eight_);
  840. other_list.push_back(nine_);
  841. other_list.push_back(ten_);
  842. list_.merge(other_list);
  843. auto iter = list_.begin();
  844. EXPECT_EQ(1, iter->value());
  845. ++iter;
  846. EXPECT_EQ(2, iter->value());
  847. ++iter;
  848. EXPECT_EQ(3, iter->value());
  849. ++iter;
  850. EXPECT_EQ(4, iter->value());
  851. ++iter;
  852. EXPECT_EQ(5, iter->value());
  853. ++iter;
  854. EXPECT_EQ(6, iter->value());
  855. ++iter;
  856. EXPECT_EQ(7, iter->value());
  857. ++iter;
  858. EXPECT_EQ(8, iter->value());
  859. ++iter;
  860. EXPECT_EQ(9, iter->value());
  861. ++iter;
  862. EXPECT_EQ(10, iter->value());
  863. ++iter;
  864. EXPECT_EQ(list_.end(), iter);
  865. iter = other_list.begin();
  866. EXPECT_EQ(other_list.end(), iter);
  867. }
  868. TEST_F(intrusive_list_test, merge_other_this) {
  869. list_.push_back(six_);
  870. list_.push_back(seven_);
  871. list_.push_back(eight_);
  872. list_.push_back(nine_);
  873. list_.push_back(ten_);
  874. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other_list;
  875. other_list.push_back(one_);
  876. other_list.push_back(two_);
  877. other_list.push_back(three_);
  878. other_list.push_back(four_);
  879. other_list.push_back(five_);
  880. list_.merge(other_list);
  881. auto iter = list_.begin();
  882. EXPECT_EQ(1, iter->value());
  883. ++iter;
  884. EXPECT_EQ(2, iter->value());
  885. ++iter;
  886. EXPECT_EQ(3, iter->value());
  887. ++iter;
  888. EXPECT_EQ(4, iter->value());
  889. ++iter;
  890. EXPECT_EQ(5, iter->value());
  891. ++iter;
  892. EXPECT_EQ(6, iter->value());
  893. ++iter;
  894. EXPECT_EQ(7, iter->value());
  895. ++iter;
  896. EXPECT_EQ(8, iter->value());
  897. ++iter;
  898. EXPECT_EQ(9, iter->value());
  899. ++iter;
  900. EXPECT_EQ(10, iter->value());
  901. ++iter;
  902. EXPECT_EQ(list_.end(), iter);
  903. iter = other_list.begin();
  904. EXPECT_EQ(other_list.end(), iter);
  905. }
  906. TEST_F(intrusive_list_test, move_constructor) {
  907. list_.push_back(one_);
  908. list_.push_back(two_);
  909. list_.push_back(three_);
  910. list_.push_back(four_);
  911. list_.push_back(five_);
  912. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other(
  913. std::move(list_));
  914. EXPECT_TRUE(one_.node.in_list());
  915. EXPECT_TRUE(two_.node.in_list());
  916. EXPECT_TRUE(three_.node.in_list());
  917. EXPECT_TRUE(four_.node.in_list());
  918. EXPECT_TRUE(five_.node.in_list());
  919. auto iter = other.begin();
  920. EXPECT_EQ(1, iter->value());
  921. ++iter;
  922. EXPECT_EQ(2, iter->value());
  923. ++iter;
  924. EXPECT_EQ(3, iter->value());
  925. ++iter;
  926. EXPECT_EQ(4, iter->value());
  927. ++iter;
  928. EXPECT_EQ(5, iter->value());
  929. ++iter;
  930. EXPECT_EQ(other.end(), iter);
  931. }
  932. TEST_F(intrusive_list_test, move_assignment) {
  933. list_.push_back(one_);
  934. list_.push_back(two_);
  935. list_.push_back(three_);
  936. list_.push_back(four_);
  937. list_.push_back(five_);
  938. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other;
  939. other = std::move(list_);
  940. EXPECT_TRUE(one_.node.in_list());
  941. EXPECT_TRUE(two_.node.in_list());
  942. EXPECT_TRUE(three_.node.in_list());
  943. EXPECT_TRUE(four_.node.in_list());
  944. EXPECT_TRUE(five_.node.in_list());
  945. auto iter = other.begin();
  946. EXPECT_EQ(1, iter->value());
  947. ++iter;
  948. EXPECT_EQ(2, iter->value());
  949. ++iter;
  950. EXPECT_EQ(3, iter->value());
  951. ++iter;
  952. EXPECT_EQ(4, iter->value());
  953. ++iter;
  954. EXPECT_EQ(5, iter->value());
  955. ++iter;
  956. EXPECT_EQ(other.end(), iter);
  957. }
  958. TEST_F(intrusive_list_test, swap) {
  959. list_.push_back(one_);
  960. list_.push_back(two_);
  961. list_.push_back(three_);
  962. list_.push_back(four_);
  963. list_.push_back(five_);
  964. fpl::intrusive_list<IntegerListNode, &IntegerListNode::node> other;
  965. other.push_back(ten_);
  966. other.push_back(twenty_);
  967. other.push_back(thirty_);
  968. other.push_back(fourty_);
  969. other.push_back(fifty_);
  970. list_.swap(other);
  971. auto iter = list_.begin();
  972. EXPECT_EQ(10, iter->value());
  973. ++iter;
  974. EXPECT_EQ(20, iter->value());
  975. ++iter;
  976. EXPECT_EQ(30, iter->value());
  977. ++iter;
  978. EXPECT_EQ(40, iter->value());
  979. ++iter;
  980. EXPECT_EQ(50, iter->value());
  981. ++iter;
  982. EXPECT_EQ(list_.end(), iter);
  983. iter = other.begin();
  984. EXPECT_EQ(1, iter->value());
  985. ++iter;
  986. EXPECT_EQ(2, iter->value());
  987. ++iter;
  988. EXPECT_EQ(3, iter->value());
  989. ++iter;
  990. EXPECT_EQ(4, iter->value());
  991. ++iter;
  992. EXPECT_EQ(5, iter->value());
  993. ++iter;
  994. EXPECT_EQ(other.end(), iter);
  995. }
  996. TEST_F(intrusive_list_test, swap_self) {
  997. list_.push_back(one_);
  998. list_.push_back(two_);
  999. list_.push_back(three_);
  1000. list_.push_back(four_);
  1001. list_.push_back(five_);
  1002. list_.swap(list_);
  1003. auto iter = list_.begin();
  1004. EXPECT_EQ(1, iter->value());
  1005. ++iter;
  1006. EXPECT_EQ(2, iter->value());
  1007. ++iter;
  1008. EXPECT_EQ(3, iter->value());
  1009. ++iter;
  1010. EXPECT_EQ(4, iter->value());
  1011. ++iter;
  1012. EXPECT_EQ(5, iter->value());
  1013. ++iter;
  1014. EXPECT_EQ(list_.end(), iter);
  1015. }
  1016. int main(int argc, char** argv) {
  1017. ::testing::InitGoogleTest(&argc, argv);
  1018. return RUN_ALL_TESTS();
  1019. }