Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. // file test.cpp //
  2.  
  3. TEST_CASE("Decrement operator moves the iterator backward", "[stage2]") {
  4. list l;
  5. append_to_list(l, { 5.55, 6.66, 7.77, 8.88 });
  6.  
  7. auto it = l.end();
  8. SECTION("Prefix") {
  9. REQUIRE(*(--it) == 8.88);
  10. REQUIRE(*(--it) == 7.77);
  11. REQUIRE(*(--it) == 6.66);
  12. REQUIRE(*(--it) == 5.55);
  13. REQUIRE(it == l.begin());
  14. }
  15.  
  16. SECTION("Postfix") {
  17. it--;
  18. REQUIRE(*(it--) == 8.88);
  19. REQUIRE(*(it--) == 7.77);
  20. REQUIRE(*(it--) == 6.66);
  21. REQUIRE(*it == 5.55);
  22. REQUIRE(it == l.begin());
  23. }
  24. }
  25.  
  26. // list.hpp //
  27.  
  28. class list{
  29. ...
  30. public:
  31. ...
  32.  
  33. class iterator{
  34. node* current_ptr = nullptr;
  35. const list* o_list = nullptr;
  36.  
  37. public:
  38. iterator(node* ptr, const list* gen);
  39.  
  40. iterator& operator++();
  41. iterator operator++(int);
  42. iterator& operator--();
  43. iterator operator--(int);
  44. }
  45. }
  46.  
  47.  
  48. // file list.cpp // implementation of class iterator //
  49.  
  50. list::iterator::iterator(node *ptr, const list *gen) {
  51. this->current_ptr = ptr;
  52. this->o_list = gen;
  53. }
  54.  
  55. list::iterator& list::iterator::operator++() {
  56. current_ptr = this->current_ptr->next;
  57. return *this;
  58. }
  59.  
  60. list::iterator& list::iterator::operator--() { // ??? Decrement operator moves the iterator backward, Prefix
  61. current_ptr = this->current_ptr->prev;
  62. return *this;
  63. }
  64.  
  65. list::iterator list::iterator::operator++(int)
  66. {
  67. iterator old(*this);
  68. ++(*this);
  69. return old;
  70. }
  71.  
  72. list::iterator list::iterator::operator--(int)
  73. {
  74. iterator left(*this);
  75. --(*this);
  76. return left;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement