Skip to content

Commit 962e777

Browse files
committed
fixed iterator ++ bugs
1 parent ef7f3c8 commit 962e777

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

include/forward_tlist/forward_tlist_iterator.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,23 @@ namespace tlib {
1212
public:
1313
using iterator_category = std::forward_iterator_tag;
1414
using value_type = T;
15-
using forward_tlist_iterator_base = typename std::iterator<std::forward_iterator_tag, T, difference_type, pointer, reference>;
15+
using forward_tlist_iterator_type = forward_tlist_iterator<value_type, difference_type, pointer, reference>;
1616
using node_pointer = tlib::forward_tlist_node<T> *;
1717

1818
reference operator*() {
1919
return pointee->element;
2020
}
21-
const forward_tlist_iterator_base& operator++();
22-
bool operator!=(const forward_tlist_iterator_base& other) const;
21+
22+
const forward_tlist_iterator_type& operator++() {
23+
pointee = pointee->next;
24+
return *this;
25+
}
26+
27+
bool operator!=(const forward_tlist_iterator_type& other) const {
28+
return pointee != other.pointee;
29+
};
30+
31+
forward_tlist_iterator() { }
2332

2433
private:
2534
forward_tlist_node<T> *pointee;

test/forward_tlist/forward_tlist_iterator_test.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <gtest/gtest.h>
22
#include "forward_tlist/forward_tlist.h"
3+
#include <forward_list>
34

45
TEST(ITERATOR, ITERATOR) {
56
tlib::forward_tlist<int> int_list;
@@ -19,4 +20,31 @@ TEST(CONST_ITERATOR, CONST_ITERATOR) {
1920
auto iterator_end = int_list.end();
2021
ASSERT_EQ(*iterator_begin, 2);
2122
ASSERT_EQ(*iterator_end, NULL);
22-
}
23+
}
24+
25+
TEST(ITERATOR_PLUS, ITERATOR_PLUS) {
26+
tlib::forward_tlist<int> int_tlist;
27+
int_tlist.push_front(1);
28+
int_tlist.push_front(2);
29+
30+
std::forward_list<int> int_list;
31+
int_list.push_front(1);
32+
int_list.push_front(2);
33+
34+
tlib::forward_tlist<int>::iterator tlist_iterator = int_tlist.begin();
35+
std::forward_list<int>::iterator list_iterator;
36+
for(list_iterator = int_list.begin();
37+
tlist_iterator != int_tlist.end() && list_iterator != int_list.end(); ++tlist_iterator, ++list_iterator) {
38+
ASSERT_EQ(*tlist_iterator, *list_iterator);
39+
}
40+
}
41+
42+
TEST(AUTO_ITERATOR, AUTO_ITERATOR_AU_Test) {
43+
tlib::forward_tlist<int> int_tlist;
44+
int_tlist.push_front(1);
45+
int_tlist.push_front(2);
46+
47+
for(auto element: int_tlist) {
48+
std::cout<<element<<" ";
49+
}std::cout<<std::endl;
50+
}

0 commit comments

Comments
 (0)