add more tests for worksheet iterator, add operator--

* operator-- completes the naive bidirectional iterator requirements
* tests for various construction and assignment behaviours
* tests for iteration and dereferencing behaviours
This commit is contained in:
Crzyrndm 2018-06-09 11:57:45 +12:00
parent 8b885c6a8d
commit dc8ab8dce6
3 changed files with 122 additions and 4 deletions

View File

@ -119,6 +119,18 @@ public:
/// </summary>
worksheet_iterator &operator++();
/// <summary>
/// Post-decrement the iterator's internal workseet index. Returns a copy of the
/// iterator as it was before being incremented.
/// </summary>
worksheet_iterator operator--(int);
/// <summary>
/// Pre-decrement the iterator's internal workseet index. Returns a refernce
/// to the same iterator.
/// </summary>
worksheet_iterator &operator--();
private:
/// <summary>
/// The target workbook of this iterator.
@ -206,6 +218,18 @@ public:
/// </summary>
const_worksheet_iterator &operator++();
/// <summary>
/// Post-decrement the iterator's internal workseet index. Returns a copy of the
/// iterator as it was before being incremented.
/// </summary>
const_worksheet_iterator operator--(int);
/// <summary>
/// Pre-decrement the iterator's internal workseet index. Returns a refernce
/// to the same iterator.
/// </summary>
const_worksheet_iterator &operator--();
private:
/// <summary>
/// The target workbook of this iterator.

View File

@ -55,6 +55,19 @@ worksheet_iterator worksheet_iterator::operator++(int)
return old;
}
worksheet_iterator &worksheet_iterator::operator--()
{
--index_;
return *this;
}
worksheet_iterator worksheet_iterator::operator--(int)
{
worksheet_iterator old(*wb_, index_);
--(*this);
return old;
}
bool worksheet_iterator::operator==(const worksheet_iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;
@ -88,6 +101,19 @@ const_worksheet_iterator const_worksheet_iterator::operator++(int)
return old;
}
const_worksheet_iterator &const_worksheet_iterator::operator--()
{
--index_;
return *this;
}
const_worksheet_iterator const_worksheet_iterator::operator--(int)
{
const_worksheet_iterator old(*wb_, index_);
--(*this);
return old;
}
bool const_worksheet_iterator::operator==(const const_worksheet_iterator &comparand) const
{
return index_ == comparand.index_ && wb_ == comparand.wb_;

View File

@ -45,6 +45,7 @@ public:
register_test(test_index_operator);
register_test(test_contains);
register_test(test_iter);
register_test(test_const_iter);
register_test(test_get_index);
register_test(test_get_sheet_names);
register_test(test_add_named_range);
@ -153,17 +154,84 @@ public:
xlnt_assert(wb.contains("Sheet1"));
xlnt_assert(!wb.contains("NotThere"));
}
void test_iter()
{
xlnt::workbook wb;
for(auto ws : wb)
for (auto ws : wb)
{
xlnt_assert_equals(ws.title(), "Sheet1");
}
xlnt::workbook wb2;
auto iter = wb.begin();
auto iter_copy(iter); // copy ctor
xlnt_assert_equals(iter, iter_copy);
auto begin_2(wb2.begin());
xlnt_assert_differs(begin_2, iter);
iter = begin_2; // copy assign
xlnt_assert_equals(iter, begin_2);
iter = wb.begin();
iter = std::move(begin_2);
xlnt_assert_equals(iter, wb2.begin());
auto citer = wb.cbegin();
auto citer_copy(citer); // copy ctor
xlnt_assert_equals(citer, citer_copy);
auto cbegin_2(wb2.cbegin());
xlnt_assert_differs(cbegin_2, citer);
citer = cbegin_2; // copy assign
xlnt_assert_equals(citer, cbegin_2);
citer = wb.cbegin();
citer = std::move(cbegin_2);
xlnt_assert_equals(citer, wb2.cbegin());
wb2.create_sheet(); // wb2 iterators assumed invalidated
iter = wb2.begin();
xlnt_assert_equals((*iter).title(), "Sheet1");
++iter;
xlnt_assert_differs(iter, wb2.begin());
xlnt_assert_equals((*iter).title(), "Sheet2");
--iter;
xlnt_assert_equals((*iter).title(), "Sheet1");
std::advance(iter, 2);
xlnt_assert_equals(iter, wb2.end());
}
void test_const_iter()
{
const xlnt::workbook wb;
for (auto ws : wb)
{
xlnt_assert_equals(ws.title(), "Sheet1");
}
xlnt::workbook wb2;
auto iter = wb.cbegin();
auto iter_copy(iter); // copy ctor
xlnt_assert_equals(iter, iter_copy);
auto begin_2(wb2.cbegin());
xlnt_assert_differs(begin_2, iter);
iter = begin_2; // copy assign
xlnt_assert_equals(iter, begin_2);
iter = wb.cbegin();
iter = std::move(begin_2);
xlnt_assert_equals(iter, wb2.cbegin());
wb2.create_sheet(); // wb2 iterators assumed invalidated
iter = wb2.cbegin();
xlnt_assert_equals((*iter).title(), "Sheet1");
++iter;
xlnt_assert_differs(iter, wb2.cbegin());
xlnt_assert_equals((*iter).title(), "Sheet2");
--iter;
xlnt_assert_equals((*iter).title(), "Sheet1");
std::advance(iter, 2);
xlnt_assert_equals(iter, wb2.cend());
}
void test_get_index()
{
xlnt::workbook wb;