mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-13 02:22:57 +01:00
StringTokenizer::find implementation and tests
This commit is contained in:
parent
600161cbf3
commit
66dc4abff0
@ -84,6 +84,11 @@ public:
|
||||
/// Returns the index'th token.
|
||||
/// Throws a RangeException if the index is out of range.
|
||||
|
||||
std::size_t find(const std::string& key, std::size_t pos = 0) const;
|
||||
/// Returns the index of the first occurence of the key token
|
||||
/// starting at position pos.
|
||||
/// Throws a NotFoundException if the key is not found.
|
||||
|
||||
std::size_t count() const;
|
||||
/// Returns the number of tokens.
|
||||
|
||||
|
@ -83,4 +83,15 @@ StringTokenizer::~StringTokenizer()
|
||||
}
|
||||
|
||||
|
||||
std::size_t StringTokenizer::find(const std::string& key, std::size_t pos) const
|
||||
{
|
||||
Iterator it = begin();
|
||||
Iterator stop = end();
|
||||
for (it += pos; it != stop; ++it)
|
||||
if (*it == key) return it - begin();
|
||||
|
||||
throw NotFoundException(key);
|
||||
}
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
@ -34,9 +34,12 @@
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/StringTokenizer.h"
|
||||
#include "Poco/Exception.h"
|
||||
|
||||
|
||||
using Poco::StringTokenizer;
|
||||
using Poco::RangeException;
|
||||
using Poco::NotFoundException;
|
||||
|
||||
|
||||
StringTokenizerTest::StringTokenizerTest(const std::string& name): CppUnit::TestCase(name)
|
||||
@ -70,6 +73,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it == st.end());
|
||||
@ -77,6 +81,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc ", "", StringTokenizer::TOK_TRIM);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it == st.end());
|
||||
@ -84,6 +89,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" abc ", "", StringTokenizer::TOK_TRIM);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it == st.end());
|
||||
@ -91,6 +97,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" abc", "", StringTokenizer::TOK_TRIM);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it == st.end());
|
||||
@ -98,6 +105,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "b");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (st.find("c") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it != st.end());
|
||||
@ -107,6 +116,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "b", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (st.find("c") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it != st.end());
|
||||
@ -116,6 +127,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "bc");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (st.find("") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it != st.end());
|
||||
@ -125,6 +138,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (st.find("") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it != st.end());
|
||||
@ -134,6 +149,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "bc", StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it == st.end());
|
||||
@ -141,6 +157,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it == st.end());
|
||||
@ -148,6 +165,7 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc", "bc", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it == st.end());
|
||||
@ -155,6 +173,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("a a,c c", ",");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a a") == 0);
|
||||
assert (st.find("c c") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a a");
|
||||
assert (it != st.end());
|
||||
@ -164,6 +184,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("a a,c c", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a a") == 0);
|
||||
assert (st.find("c c") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a a");
|
||||
assert (it != st.end());
|
||||
@ -173,6 +195,9 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" a a , , c c ", ",");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find(" a a ") == 0);
|
||||
assert (st.find(" ") == 1);
|
||||
assert (st.find(" c c ") == 2);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == " a a ");
|
||||
assert (it != st.end());
|
||||
@ -184,6 +209,9 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a a") == 0);
|
||||
assert (st.find("") == 1);
|
||||
assert (st.find("c c") == 2);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a a");
|
||||
assert (it != st.end());
|
||||
@ -195,6 +223,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" a a , , c c ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a a") == 0);
|
||||
assert (st.find("c c") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a a");
|
||||
assert (it != st.end());
|
||||
@ -204,6 +234,11 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc,def,,ghi , jk, l ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (st.find("def") == 1);
|
||||
assert (st.find("ghi") == 2);
|
||||
assert (st.find("jk") == 3);
|
||||
assert (st.find("l") == 4);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it != st.end());
|
||||
@ -219,6 +254,11 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("abc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("abc") == 0);
|
||||
assert (st.find("def") == 1);
|
||||
assert (st.find("ghi") == 2);
|
||||
assert (st.find("jk") == 3);
|
||||
assert (st.find("l") == 4);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "abc");
|
||||
assert (it != st.end());
|
||||
@ -234,6 +274,12 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st("a/bc,def,,ghi // jk, l ", ",/", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("a") == 0);
|
||||
assert (st.find("bc") == 1);
|
||||
assert (st.find("def") == 2);
|
||||
assert (st.find("ghi") == 3);
|
||||
assert (st.find("jk") == 4);
|
||||
assert (st.find("l") == 5);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "a");
|
||||
assert (it != st.end());
|
||||
@ -251,6 +297,9 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(",ab,cd,", ",");
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("") == 0);
|
||||
assert (st.find("ab") == 1);
|
||||
assert (st.find("cd") == 2);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "");
|
||||
assert (it != st.end());
|
||||
@ -262,6 +311,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(",ab,cd,", ",", StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("ab") == 0);
|
||||
assert (st.find("cd") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "ab");
|
||||
assert (it != st.end());
|
||||
@ -271,6 +322,8 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
{
|
||||
StringTokenizer st(" , ab , cd , ", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
StringTokenizer::Iterator it = st.begin();
|
||||
assert (st.find("ab") == 0);
|
||||
assert (st.find("cd") == 1);
|
||||
assert (it != st.end());
|
||||
assert (*it++ == "ab");
|
||||
assert (it != st.end());
|
||||
@ -283,10 +336,51 @@ void StringTokenizerTest::testStringTokenizer()
|
||||
assert (st[0] == "1");
|
||||
assert (st[1] == "2");
|
||||
assert (st[2] == "3");
|
||||
assert (st.find("1") == 0);
|
||||
assert (st.find("2") == 1);
|
||||
assert (st.find("3") == 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void StringTokenizerTest::testFind()
|
||||
{
|
||||
StringTokenizer st("0,1,2,3,3,2,1,0", ",", StringTokenizer::TOK_TRIM | StringTokenizer::TOK_IGNORE_EMPTY);
|
||||
assert (st.count() == 8);
|
||||
assert (st[0] == "0");
|
||||
assert (st[1] == "1");
|
||||
assert (st[2] == "2");
|
||||
assert (st[3] == "3");
|
||||
assert (st[4] == "3");
|
||||
assert (st[5] == "2");
|
||||
assert (st[6] == "1");
|
||||
assert (st[7] == "0");
|
||||
assert (st.find("0") == 0);
|
||||
assert (st.find("1") == 1);
|
||||
assert (st.find("2") == 2);
|
||||
assert (st.find("3") == 3);
|
||||
|
||||
assert (st.find("0", 1) == 7);
|
||||
assert (st.find("1", 2) == 6);
|
||||
assert (st.find("2", 3) == 5);
|
||||
assert (st.find("3", 4) == 4);
|
||||
|
||||
try
|
||||
{
|
||||
std::size_t p = st.find("4");
|
||||
fail ("must fail");
|
||||
}
|
||||
catch (NotFoundException&) { }
|
||||
|
||||
try
|
||||
{
|
||||
std::string s = st[8];
|
||||
fail ("must fail");
|
||||
}
|
||||
catch (RangeException&) { }
|
||||
}
|
||||
|
||||
|
||||
void StringTokenizerTest::setUp()
|
||||
{
|
||||
}
|
||||
@ -302,6 +396,7 @@ CppUnit::Test* StringTokenizerTest::suite()
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("StringTokenizerTest");
|
||||
|
||||
CppUnit_addTest(pSuite, StringTokenizerTest, testStringTokenizer);
|
||||
CppUnit_addTest(pSuite, StringTokenizerTest, testFind);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ public:
|
||||
~StringTokenizerTest();
|
||||
|
||||
void testStringTokenizer();
|
||||
void testFind();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
|
Loading…
Reference in New Issue
Block a user