]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/strings/stdstrings.cpp
changed wxColourData::GetCustomColour to const; there's no reason for it to be non...
[wxWidgets.git] / tests / strings / stdstrings.cpp
index 454818265165ed7b35caa734a698d2f84b3c0b71..6aad8e9901311b545f767c4bda1151d3bb2708e7 100644 (file)
@@ -1,4 +1,4 @@
-///////////////////////////////////////////////////////////////////////////////
+//////////////////////////////////////////////////////////////////////////////
 // Name:        tests/strings/stdstrings.cpp
 // Purpose:     wxString unit test
 // Author:      Vadim Zeitlin, Wlodzimierz ABX Skiba
@@ -11,7 +11,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
-#include "wx/wxprec.h"
+#include "testprec.h"
 
 #ifdef __BORLANDC__
     #pragma hdrstop
@@ -21,8 +21,6 @@
     #include "wx/wx.h"
 #endif // WX_PRECOMP
 
-#include "wx/cppunit.h"
-
 // ----------------------------------------------------------------------------
 // test class
 // ----------------------------------------------------------------------------
@@ -35,6 +33,7 @@ public:
 private:
     CPPUNIT_TEST_SUITE( StdStringTestCase );
         CPPUNIT_TEST( StdConstructors );
+        CPPUNIT_TEST( StdIterators );
         CPPUNIT_TEST( StdAppend );
         CPPUNIT_TEST( StdAssign );
         CPPUNIT_TEST( StdCompare );
@@ -46,10 +45,15 @@ private:
         CPPUNIT_TEST( StdReplace );
         CPPUNIT_TEST( StdRFind );
         CPPUNIT_TEST( StdResize );
+        CPPUNIT_TEST( StdRiter );
         CPPUNIT_TEST( StdSubstr );
+#if wxUSE_STD_STRING
+        CPPUNIT_TEST( StdConversion );
+#endif
     CPPUNIT_TEST_SUITE_END();
 
     void StdConstructors();
+    void StdIterators();
     void StdAppend();
     void StdAssign();
     void StdCompare();
@@ -61,7 +65,11 @@ private:
     void StdReplace();
     void StdRFind();
     void StdResize();
+    void StdRiter();
     void StdSubstr();
+#if wxUSE_STD_STRING
+    void StdConversion();
+#endif
 
     DECLARE_NO_COPY_CLASS(StdStringTestCase)
 };
@@ -87,33 +95,66 @@ void StdStringTestCase::StdConstructors()
              s7(s3.begin(), s3.begin() + 8);
     wxString s8(s1, 4, 8);
 
-    CPPUNIT_ASSERT( s1 == _T("abcdefgh") );
-    CPPUNIT_ASSERT( s2 == s1 );
-    CPPUNIT_ASSERT( s4 == _T("aaaaaaaa") );
-    CPPUNIT_ASSERT( s5 == _T("abcdefgh") );
-    CPPUNIT_ASSERT( s6 == s1 );
-    CPPUNIT_ASSERT( s7 == s1 );
-    CPPUNIT_ASSERT( s8 == _T("efgh") );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdefgh"), s1 );
+    CPPUNIT_ASSERT_EQUAL( s1, s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("aaaaaaaa"), s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdefgh"), s5 );
+    CPPUNIT_ASSERT_EQUAL( s1, s6 );
+    CPPUNIT_ASSERT_EQUAL( s1, s7 );
+    CPPUNIT_ASSERT_EQUAL( _T("efgh"), s8 );
+
+    const char *pc = s1.c_str();
+    CPPUNIT_ASSERT_EQUAL( "bcd", wxString(pc + 1, pc + 4) );
+
+    const wchar_t *pw = s2.c_str();
+    CPPUNIT_ASSERT_EQUAL( "a", wxString(pw, pw + 1) );
+}
+
+void StdStringTestCase::StdIterators()
+{
+    // test compilation of default iterators ctors:
+    wxString::iterator i1;
+    wxString::const_iterator i2;
+    wxString::reverse_iterator i3;
+    wxString::const_reverse_iterator i4;
 }
 
 void StdStringTestCase::StdAppend()
 {
     wxString s1, s2, s3, s4, s5, s6, s7, s8;
 
-    s1 = s2 = s3 = s4 = s5 = s6 = s7 = s8 = _T("abc");
+    s1 = s2 = s3 = s4 = s5 = s6 = _T("abc");
     s1.append(_T("def"));
     s2.append(_T("defgh"), 3);
     s3.append(wxString(_T("abcdef")), 3, 6);
     s4.append(s1);
     s5.append(3, _T('a'));
+    s5.append(2, 'x');
+    s5.append(1, (unsigned char)'y');
     s6.append(s1.begin() + 3, s1.end());
 
-    CPPUNIT_ASSERT( s1 == _T("abcdef") );
-    CPPUNIT_ASSERT( s2 == _T("abcdef") );
-    CPPUNIT_ASSERT( s3 == _T("abcdef") );
-    CPPUNIT_ASSERT( s4 == _T("abcabcdef") );
-    CPPUNIT_ASSERT( s5 == _T("abcaaa") );
-    CPPUNIT_ASSERT( s6 == _T("abcdef") );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdef"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdef"), s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdef"), s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcabcdef"), s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcaaaxxy"), s5 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdef"), s6 );
+
+    const char *pc = s1.c_str() + 2;
+    s7.append(pc, pc + 4);
+    CPPUNIT_ASSERT_EQUAL( "cdef", s7 );
+
+    const wchar_t *pw = s2.c_str() + 2;
+    s8.append(pw, pw + 4);
+    CPPUNIT_ASSERT_EQUAL( "cdef", s8 );
+
+    s7 = s8 = wxString(_T("null\0time"), 9);
+
+    s7.append(_T("def"));
+    s8.append(_T("defgh"), 3);
+
+    CPPUNIT_ASSERT_EQUAL( wxString(_T("null\0timedef"), 12), s7 );
+    CPPUNIT_ASSERT_EQUAL( wxString(_T("null\0timedef"), 12), s8 );
 }
 
 void StdStringTestCase::StdAssign()
@@ -128,12 +169,23 @@ void StdStringTestCase::StdAssign()
     s5.assign(3, _T('a'));
     s6.assign(s1.begin() + 1, s1.end());
 
-    CPPUNIT_ASSERT( s1 == _T("def") );
-    CPPUNIT_ASSERT( s2 == _T("def") );
-    CPPUNIT_ASSERT( s3 == _T("def") );
-    CPPUNIT_ASSERT( s4 == _T("def") );
-    CPPUNIT_ASSERT( s5 == _T("aaa") );
-    CPPUNIT_ASSERT( s6 == _T("ef") );
+    CPPUNIT_ASSERT_EQUAL( _T("def"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("def"), s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("def"), s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("def"), s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("aaa"), s5 );
+    CPPUNIT_ASSERT_EQUAL( _T("ef"),  s6 );
+
+    const char *pc = s1.c_str();
+    s7.assign(pc, pc + 2);
+    CPPUNIT_ASSERT_EQUAL( "de", s7 );
+
+    const wchar_t *pw = s1.c_str();
+    s8.assign(pw + 2, pw + 3);
+    CPPUNIT_ASSERT_EQUAL( "f", s8 );
+
+    s1.assign(s1, 1, 1);
+    CPPUNIT_ASSERT_EQUAL("e", s1);
 }
 
 void StdStringTestCase::StdCompare()
@@ -176,11 +228,11 @@ void StdStringTestCase::StdErase()
     wxString::iterator it2 = s4.erase(s4.begin() + 4, s4.begin() + 6);
     wxString::iterator it3 = s7.erase(s7.begin() + 4, s7.begin() + 8);
 
-    CPPUNIT_ASSERT( s1 == _T("acdefgh") );
-    CPPUNIT_ASSERT( s2 == _T("abcd") );
-    CPPUNIT_ASSERT( s3 == _T("ac") );
-    CPPUNIT_ASSERT( s4 == _T("abcdghi") );
-    CPPUNIT_ASSERT( s7 == _T("zabc") );
+    CPPUNIT_ASSERT_EQUAL( _T("acdefgh"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcd"), s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("ac"), s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcdghi"), s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("zabc"), s7 );
     CPPUNIT_ASSERT( *it == _T('c') );
     CPPUNIT_ASSERT( *it2 == _T('g') );
     CPPUNIT_ASSERT( it3 == s7.end() );
@@ -208,18 +260,18 @@ void StdStringTestCase::StdFind()
     CPPUNIT_ASSERT( s1.find(s2) == 6u );
     CPPUNIT_ASSERT( s1.find(s2, 7) == wxString::npos );
     CPPUNIT_ASSERT( s1.find(s2, 6) == 6u );
-    
+
     //                   0          1          2
     //                   0123456 78901234567 8901234567
     //wxString _s1 = _T("abcdefg\0ABCDEFGabc\0ABCabcABC");
     //wxString _s2 = _T("g\0AB");
     wxString _s1 = _T("abcdefgABCDEFGabcABCabcABC");
     wxString _s2 = _T("gAB");
-    
+
     _s1.insert(7, 1, '\0');
     _s1.insert(18, 1, '\0');
     _s2.insert(1, 1, '\0');
-    
+
     CPPUNIT_ASSERT( _s1.find(_T('A')) == 8u );
     CPPUNIT_ASSERT( _s1.find(_T('A'), 8) == 8u );
     CPPUNIT_ASSERT( _s1.find(_T('Z')) == wxString::npos );
@@ -286,6 +338,33 @@ void StdStringTestCase::StdFindLast()
     CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 16u );
     CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 33, 4) == 16u );
     CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 17) == 17u );
+
+
+
+    //                  0          1          2         3
+    //                  012345 67890123456789 01234567890123456
+//    wxString s1 = _T("aaaaaa\0bcdefghlkjiaa\0aaaabcdbcdbcdbcd");
+//    wxString s2 = _T("aaaaaa\0");
+    s1.insert(6,1,'\0');
+    s1.insert(20,1,'\0');
+    s2.insert(6,1,'\0');
+
+    CPPUNIT_ASSERT( s2.find_last_not_of(_T('a')) == 6u );
+    CPPUNIT_ASSERT( s1.find_last_not_of(_T('d')) == 35u );
+    CPPUNIT_ASSERT( s1.find_last_not_of(_T('d'), 27) == 26u );
+
+    CPPUNIT_ASSERT( s1.find_last_not_of(_T("bcd")) == 24u );
+    CPPUNIT_ASSERT( s1.find_last_not_of(_T("abc"), 26) == 20u );
+
+    CPPUNIT_ASSERT( s1.find_last_not_of(_T("abcdefghijklmnopqrstuv"), 26, 3) == 20u );
+
+    CPPUNIT_ASSERT( s2.find_last_of(_T('c')) == wxString::npos );
+    CPPUNIT_ASSERT( s1.find_last_of(_T('a')) == 24u );
+    CPPUNIT_ASSERT( s1.find_last_of(_T('b'), 26) == 25u );
+
+    CPPUNIT_ASSERT( s1.find_last_of(_T("ijklm")) == 17u );
+    CPPUNIT_ASSERT( s1.find_last_of(_T("ijklma"), 35, 4) == 17u );
+    CPPUNIT_ASSERT( s1.find_last_of(_T("a"), 18) == 18u );
 }
 
 void StdStringTestCase::StdInsert()
@@ -304,21 +383,21 @@ void StdStringTestCase::StdInsert()
     s7.insert(s7.begin(), s9.begin(), s9.end() - 1);
     s8.insert(s8.begin(), 2, _T('c'));
 
-    CPPUNIT_ASSERT( s1 == _T("accaaa") );
-    CPPUNIT_ASSERT( s2 == _T("aacdeaa") );
-    CPPUNIT_ASSERT( s3 == _T("aacdefgaa") );
-    CPPUNIT_ASSERT( s4 == _T("aafgaa") );
-    CPPUNIT_ASSERT( s5 == _T("accaaa") );
-    CPPUNIT_ASSERT( s6 == _T("aaaXa") );
-    CPPUNIT_ASSERT( s7 == _T("cdefaaaa") );
-    CPPUNIT_ASSERT( s8 == _T("ccaaaa") );
+    CPPUNIT_ASSERT_EQUAL( _T("accaaa")   , s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("aacdeaa")  , s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("aacdefgaa"), s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("aafgaa")   , s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("accaaa")   , s5 );
+    CPPUNIT_ASSERT_EQUAL( _T("aaaXa")    , s6 );
+    CPPUNIT_ASSERT_EQUAL( _T("cdefaaaa") , s7 );
+    CPPUNIT_ASSERT_EQUAL( _T("ccaaaa")   , s8 );
 
     s1 = s2 = s3 = _T("aaaa");
     s1.insert(0, _T("ccc"), 2);
     s2.insert(4, _T("ccc"), 2);
 
-    CPPUNIT_ASSERT( s1 == _T("ccaaaa") );
-    CPPUNIT_ASSERT( s2 == _T("aaaacc") );
+    CPPUNIT_ASSERT_EQUAL( _T("ccaaaa"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("aaaacc"), s2 );
 }
 
 void StdStringTestCase::StdReplace()
@@ -337,13 +416,13 @@ void StdStringTestCase::StdReplace()
     s6.replace(0, 123, s9, 0, 123);
     s7.replace(2, 7, s9);
 
-    CPPUNIT_ASSERT( s1 == _T("QWErtyuIopopop") );
-    CPPUNIT_ASSERT( s2 == _T("QWERTYUIOPWWWW") );
-    CPPUNIT_ASSERT( s3 == _T("QwertyUIOP") );
-    CPPUNIT_ASSERT( s4 == _T("QwertYUIOP") );
-    CPPUNIT_ASSERT( s5 == _T("QertyRTYUIOP") );
-    CPPUNIT_ASSERT( s6 == s9);
-    CPPUNIT_ASSERT( s7 == _T("QWwertyP") );
+    CPPUNIT_ASSERT_EQUAL( _T("QWErtyuIopopop"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("QWERTYUIOPWWWW"), s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("QwertyUIOP")    , s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("QwertYUIOP")    , s4 );
+    CPPUNIT_ASSERT_EQUAL( _T("QertyRTYUIOP")  , s5 );
+    CPPUNIT_ASSERT_EQUAL( s9, s6 );
+    CPPUNIT_ASSERT_EQUAL( _T("QWwertyP"), s7 );
 }
 
 void StdStringTestCase::StdRFind()
@@ -377,7 +456,7 @@ void StdStringTestCase::StdRFind()
 //    wxString s1 = _T("abcde\0fgABCDE\0FGabcABCabcABC");
 //    wxString s2 = _T("gAB");
 //    wxString s3 = _T("ab");
-    
+
     s1.insert(5,1,'\0');
     s1.insert(13,1,'\0');
 
@@ -405,10 +484,35 @@ void StdStringTestCase::StdResize()
     s3.resize( 14, _T(' ') );
     s4.resize( 14, _T('W') );
 
-    CPPUNIT_ASSERT( s1 == _T("abcABCdefDEF") );
-    CPPUNIT_ASSERT( s2 == _T("abcABCdefD") );
-    CPPUNIT_ASSERT( s3 == _T("abcABCdefDEF  ") );
-    CPPUNIT_ASSERT( s4 == _T("abcABCdefDEFWW") );
+    CPPUNIT_ASSERT_EQUAL( _T("abcABCdefDEF"), s1 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcABCdefD"), s2 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcABCdefDEF  "), s3 );
+    CPPUNIT_ASSERT_EQUAL( _T("abcABCdefDEFWW"), s4 );
+
+    wxString s =
+        wxString::FromUTF8("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82");
+    s.resize(3);
+    CPPUNIT_ASSERT_EQUAL("\xd0\x9f\xd1\x80\xd0\xb8", s);
+}
+
+void StdStringTestCase::StdRiter()
+{
+    const wxString s(_T("fozbar"));
+
+    wxString::const_reverse_iterator ri(s.rbegin());
+    CPPUNIT_ASSERT( _T('r') == *ri );
+    CPPUNIT_ASSERT( _T('a') == *++ri );
+    CPPUNIT_ASSERT( _T('r') == *--ri );
+
+    ri = s.rend();
+    ri--;
+    CPPUNIT_ASSERT( _T('f') == *ri );
+
+    --ri;
+    CPPUNIT_ASSERT( _T('o') == *ri );
+
+    wxString::const_iterator i = ri.base();
+    CPPUNIT_ASSERT( _T('z') == *i );
 }
 
 void StdStringTestCase::StdSubstr()
@@ -420,11 +524,6 @@ void StdStringTestCase::StdSubstr()
     CPPUNIT_ASSERT( s1.substr( 1, 20 ) == _T("bcdefgABCDEFG") );
     CPPUNIT_ASSERT( s1.substr( 14, 30 ) == _T("") );
 
-
-//                                                                 1
-//                                             012 34567 89012 3456
-//    wxString s1 = _T("abc\0defg\0ABCD\0EFG");
-
     s1.insert(3,1,'\0');
     s1.insert(8,1,'\0');
     s1.insert(13,1,'\0');
@@ -433,10 +532,47 @@ void StdStringTestCase::StdSubstr()
     s2.insert(2,1,'\0');
     s2.insert(7,1,'\0');
     s2.insert(12,1,'\0');
-    
+
     CPPUNIT_ASSERT( s1.substr( 0, 17 ) == s1 );
     CPPUNIT_ASSERT( s1.substr( 1, 17 ) == s2 );
     CPPUNIT_ASSERT( s1.substr( 1, 20 ) == s2 );
     CPPUNIT_ASSERT( s1.substr( 17, 30 ) == _T("") );
 }
 
+#if wxUSE_STD_STRING
+void StdStringTestCase::StdConversion()
+{
+    std::string strStd("std::string value");
+    wxStdWideString strStdWide(L"std::wstring value");
+
+    wxString s1(strStd);
+    CPPUNIT_ASSERT_EQUAL( "std::string value", s1 );
+
+    wxString s2(strStdWide);
+    CPPUNIT_ASSERT_EQUAL( "std::wstring value", s2 );
+
+    wxString s3;
+    s3 = strStd;
+    CPPUNIT_ASSERT_EQUAL( "std::string value", s3 );
+    s3 = strStdWide;
+    CPPUNIT_ASSERT_EQUAL( "std::wstring value", s3 );
+
+    wxString s4("hello");
+
+    // wxString -> std::string conversion is only available in wxUSE_STL case,
+    // because it conflicts with conversion to const char*/wchar_t*:
+#if wxUSE_STL
+    std::string s5 = s4;
+    CPPUNIT_ASSERT_EQUAL( "hello", s5 );
+
+    wxStdWideString s6 = s4;
+    CPPUNIT_ASSERT_EQUAL( "hello", s6 );
+#endif
+
+    std::string s7(s4);
+    CPPUNIT_ASSERT( s7 == "hello" );
+
+    wxStdWideString s8(s4);
+    CPPUNIT_ASSERT( s8 == "hello" );
+}
+#endif // wxUSE_STD_STRING