Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue 2945 #2946

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Foundation/include/Poco/Dynamic/Var.h
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ inline const std::type_info& Var::type() const

inline Var::ConstIterator Var::begin() const
{
if (isEmpty()) return ConstIterator(const_cast<Var*>(this), true);
if (size() == 0) return ConstIterator(const_cast<Var*>(this), true);

return ConstIterator(const_cast<Var*>(this), false);
}
Expand All @@ -749,7 +749,7 @@ inline Var::ConstIterator Var::end() const

inline Var::Iterator Var::begin()
{
if (isEmpty()) return Iterator(const_cast<Var*>(this), true);
if (size() == 0) return Iterator(const_cast<Var*>(this), true);

return Iterator(const_cast<Var*>(this), false);
}
Expand Down
1 change: 1 addition & 0 deletions Foundation/src/VarIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ VarIterator::VarIterator(Var* pVar, bool positionEnd):
_pVar(pVar),
_position(positionEnd ? POSITION_END : 0)
{
if (!_pVar || _pVar->isEmpty()) throw InvalidAccessException("Cannot create iterator on empty Var");
}


Expand Down
11 changes: 9 additions & 2 deletions Foundation/testsuite/src/VarTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2973,8 +2973,15 @@ void VarTest::testEmpty()
void VarTest::testIterator()
{
Var da;
assertTrue (da.isEmpty());
assertTrue (da.begin() == da.end());
try
{
auto it = da.begin();
aleks-f marked this conversation as resolved.
Show resolved Hide resolved
fail("calling begin() on empty Var must throw");
}
catch (const InvalidAccessException&) { }

da = Poco::Dynamic::Array();
assertTrue(da.begin() == da.end());

da = 1;
assertTrue (!da.isEmpty());
Expand Down
23 changes: 19 additions & 4 deletions Foundation/testsuite/src/VarTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,29 @@ class VarTest: public CppUnit::TestCase
void testContainerIterator()
{
C cont;
Poco::Dynamic::Var arr(cont);
int counter = 0;

// test empty
assertTrue (arr.size() == 0);
Poco::Dynamic::Var::Iterator it = arr.begin();
Poco::Dynamic::Var::Iterator end = arr.end();

for (; it != end; ++it)
{
*it = ++counter;
}
assertTrue(counter == 0);

// test non-empty
cont.push_back(1);
cont.push_back("2");
cont.push_back(3.5);
Poco::Dynamic::Var arr(cont);
arr = cont;
assertTrue (arr.size() == 3);
Poco::Dynamic::Var::Iterator it = arr.begin();
Poco::Dynamic::Var::Iterator end = arr.end();
int counter = 0;
it = arr.begin();
end = arr.end();
counter = 0;
for (; it != end; ++it)
{
switch (++counter)
Expand Down