Skip to content

Commit

Permalink
Update string unittests to test valid/invalid nullptr
Browse files Browse the repository at this point in the history
We have nullptr validation in these functions. Test also that. Test also
that with length 0 we do not get AssertionViolationException.
  • Loading branch information
Kari Argillander committed May 16, 2024
1 parent c9cdf69 commit ce5c0bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Foundation/testsuite/src/StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,12 @@ void StringTest::testIcompare()
assertTrue (icompare(ss1, 2, 2, "bb") < 0);

assertTrue (icompare(ss1, 2, "aaa") > 0);

assertTrue (icompare(ss1, 0, 0, nullptr) == 0);
try {
icompare(ss1, 0, 1, nullptr);
fail ("must fail");
} catch (Poco::AssertionViolationException&) {};
}


Expand Down
17 changes: 17 additions & 0 deletions Foundation/testsuite/src/TextConverterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "Poco/Windows1251Encoding.h"
#include "Poco/Windows1252Encoding.h"
#include "Poco/UTF8Encoding.h"
#include "Poco/Exception.h"


using namespace Poco;
Expand Down Expand Up @@ -326,6 +327,21 @@ void TextConverterTest::testErrors()
}


void TextConverterTest::testPointerSafety()
{
UTF8Encoding utf8Encoding;
Latin1Encoding latin1Encoding;
TextConverter converter(utf8Encoding, latin1Encoding);

std::string result;
try {
converter.convert(nullptr, 1, result);
fail ("must fail");
} catch (AssertionViolationException&) {};
converter.convert(nullptr, 0, result);
}


void TextConverterTest::setUp()
{
}
Expand All @@ -350,6 +366,7 @@ CppUnit::Test* TextConverterTest::suite()
CppUnit_addTest(pSuite, TextConverterTest, testCP1251toUTF8);
CppUnit_addTest(pSuite, TextConverterTest, testCP1252toUTF8);
CppUnit_addTest(pSuite, TextConverterTest, testErrors);
CppUnit_addTest(pSuite, TextConverterTest, testPointerSafety);

return pSuite;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/TextConverterTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TextConverterTest: public CppUnit::TestCase
void testCP1251toUTF8();
void testCP1252toUTF8();
void testErrors();
void testPointerSafety();

void setUp();
void tearDown();
Expand Down
9 changes: 9 additions & 0 deletions Foundation/testsuite/src/UTF8StringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/UTF8String.h"
#include "Poco/Exception.h"


using Poco::UTF8;
Expand Down Expand Up @@ -58,6 +59,14 @@ void UTF8StringTest::testCompare()
std::string b6("\303\234\303\226\303\204"); // "U"O"A

assertTrue (UTF8::icompare(a6, b6) == 0);

std::string a7("a");

assertTrue (UTF8::icompare(a7, 0, 0, nullptr) == 0);
try {
UTF8::icompare(a7, 0, 1, nullptr);
fail ("must fail");
} catch (Poco::AssertionViolationException&) {};
}


Expand Down

0 comments on commit ce5c0bc

Please sign in to comment.