diff options
author | Kevin Smith <git@kismith.co.uk> | 2009-12-22 21:00:48 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2009-12-22 21:15:38 (GMT) |
commit | 308ff634379e73a0c8668ffb0593d23f95b4dfa5 (patch) | |
tree | 21b0dd5fd9cbd90e141fd32449e298b65a40e04c /Swiften | |
parent | 333136df0bf355ef3ae3566f12bbdaf2795bcdaf (diff) | |
download | swift-308ff634379e73a0c8668ffb0593d23f95b4dfa5.zip swift-308ff634379e73a0c8668ffb0593d23f95b4dfa5.tar.bz2 |
Add a PreviousStatusStore.
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Base/String.h | 4 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/StringTest.cpp | 16 |
2 files changed, 20 insertions, 0 deletions
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h index 0a5530c..7a6a9cc 100644 --- a/Swiften/Base/String.h +++ b/Swiften/Base/String.h @@ -49,79 +49,83 @@ namespace Swift { bool beginsWith(char c) const { return data_.size() > 0 && data_[0] == c; } bool beginsWith(const String& s) const { return data_.substr(0, s.data_.size()) == s; } bool endsWith(char c) const { return data_.size() > 0 && data_[data_.size()-1] == c; } String getSubstring(size_t begin, size_t end) const { return String(data_.substr(begin, end)); } size_t find(char c) const { assert((c & 0x80) == 0); return data_.find(c); } size_t npos() const { return data_.npos; } friend String operator+(const String& a, const String& b) { return String(a.data_ + b.data_); } friend String operator+(const String& a, char b) { return String(a.data_ + b); } String& operator+=(const String& o) { data_ += o.data_; return *this; } String& operator+=(char c) { data_ += c; return *this; } String& operator=(const String& o) { data_ = o.data_; return *this; } + bool contains(const String& o) { + return data_.find(o.data_) != std::string::npos; + } + char operator[](size_t i) const { return data_[i]; } friend bool operator>(const String& a, const String& b) { return a.data_ > b.data_; } friend bool operator<(const String& a, const String& b) { return a.data_ < b.data_; } friend bool operator!=(const String& a, const String& b) { return a.data_ != b.data_; } friend bool operator==(const String& a, const String& b) { return a.data_ == b.data_; } friend std::ostream& operator<<(std::ostream& os, const String& s) { os << s.data_; return os; } private: std::string data_; }; } #endif diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp index 6ad12fd..87e1a99 100644 --- a/Swiften/Base/UnitTest/StringTest.cpp +++ b/Swiften/Base/UnitTest/StringTest.cpp @@ -1,75 +1,78 @@ #include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/TestFactoryRegistry.h> #include "Swiften/Base/String.h" using namespace Swift; class StringTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(StringTest); CPPUNIT_TEST(testGetLength); CPPUNIT_TEST(testGetLength_EncodedLength2); CPPUNIT_TEST(testGetLength_EncodedLength3); CPPUNIT_TEST(testGetLength_EncodedLength4); CPPUNIT_TEST(testGetUnicodeCodePoints); CPPUNIT_TEST(testGetSplittedAtFirst); CPPUNIT_TEST(testGetSplittedAtFirst_CharacterAtEnd); CPPUNIT_TEST(testGetSplittedAtFirst_NoSuchCharacter); CPPUNIT_TEST(testRemoveAll); CPPUNIT_TEST(testRemoveAll_LastChar); CPPUNIT_TEST(testRemoveAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll); CPPUNIT_TEST(testReplaceAll_LastChar); CPPUNIT_TEST(testReplaceAll_ConsecutiveChars); CPPUNIT_TEST(testReplaceAll_MatchingReplace); CPPUNIT_TEST(testGetLowerCase); CPPUNIT_TEST(testSplit); + CPPUNIT_TEST(testContains); + CPPUNIT_TEST(testContainsFalse); + CPPUNIT_TEST(testContainsExact); CPPUNIT_TEST_SUITE_END(); public: StringTest() {} void testGetLength() { String testling("xyz$xyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength2() { String testling("xyz\xC2\xA2xyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength3() { String testling("xyz\xE2\x82\xACxyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetLength_EncodedLength4() { String testling("xyz\xf4\x8a\xaf\x8dxyz"); CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(7), testling.getLength()); } void testGetUnicodeCodePoints() { String testling("$\xc2\xa2\xe2\x82\xac\xf4\x8a\xaf\x8d"); std::vector<unsigned int> points = testling.getUnicodeCodePoints(); CPPUNIT_ASSERT_EQUAL(0x24U, points[0]); CPPUNIT_ASSERT_EQUAL(0xA2U, points[1]); CPPUNIT_ASSERT_EQUAL(0x20ACU, points[2]); CPPUNIT_ASSERT_EQUAL(0x10ABCDU, points[3]); } void testGetSplittedAtFirst() { String testling("ab@cd@ef"); std::pair<String,String> result = testling.getSplittedAtFirst('@'); CPPUNIT_ASSERT_EQUAL(String("ab"), result.first); CPPUNIT_ASSERT_EQUAL(String("cd@ef"), result.second); } void testGetSplittedAtFirst_CharacterAtEnd() { @@ -113,51 +116,64 @@ class StringTest : public CppUnit::TestFixture } void testReplaceAll() { String testling("abcbd"); testling.replaceAll('b', "xyz"); CPPUNIT_ASSERT_EQUAL(String("axyzcxyzd"), testling); } void testReplaceAll_LastChar() { String testling("abc"); testling.replaceAll('c', "xyz"); CPPUNIT_ASSERT_EQUAL(String("abxyz"), testling); } void testReplaceAll_ConsecutiveChars() { String testling("abbc"); testling.replaceAll('b',"xyz"); CPPUNIT_ASSERT_EQUAL(String("axyzxyzc"), testling); } void testReplaceAll_MatchingReplace() { String testling("abc"); testling.replaceAll('b',"bbb"); CPPUNIT_ASSERT_EQUAL(String("abbbc"), testling); } void testGetLowerCase() { String testling("aBcD e"); CPPUNIT_ASSERT_EQUAL(String("abcd e"), testling.getLowerCase()); } void testSplit() { std::vector<String> result = String("abc def ghi").split(' '); CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size())); CPPUNIT_ASSERT_EQUAL(String("abc"), result[0]); CPPUNIT_ASSERT_EQUAL(String("def"), result[1]); CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]); } + + void testContains() { + CPPUNIT_ASSERT(String("abcde").contains(String("bcd"))); + } + + void testContainsFalse() { + CPPUNIT_ASSERT(!String("abcde").contains(String("abcdef"))); + } + + void testContainsExact() { + CPPUNIT_ASSERT(String("abcde").contains(String("abcde"))); + } + }; CPPUNIT_TEST_SUITE_REGISTRATION(StringTest); |