]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/strings/strings.cpp
Documentation corrections for /interface/log.h (#9583)
[wxWidgets.git] / tests / strings / strings.cpp
index b83b8f18c3c22e19e7b3c9470e287c903533d183..268197281f6bc13382c6cdf8c6429938c30659c5 100644 (file)
@@ -36,6 +36,7 @@ private:
         CPPUNIT_TEST( PChar );
         CPPUNIT_TEST( Format );
         CPPUNIT_TEST( Constructors );
+        CPPUNIT_TEST( StaticConstructors );
         CPPUNIT_TEST( Extraction );
         CPPUNIT_TEST( Trim );
         CPPUNIT_TEST( Find );
@@ -52,7 +53,7 @@ private:
         CPPUNIT_TEST( ToULongLong );
 #endif // wxLongLong_t
         CPPUNIT_TEST( ToDouble );
-        CPPUNIT_TEST( WriteBuf );
+        CPPUNIT_TEST( StringBuf );
         CPPUNIT_TEST( UTF8Buf );
         CPPUNIT_TEST( CStrDataTernaryOperator );
         CPPUNIT_TEST( CStrDataOperators );
@@ -64,6 +65,7 @@ private:
     void PChar();
     void Format();
     void Constructors();
+    void StaticConstructors();
     void Extraction();
     void Trim();
     void Find();
@@ -80,7 +82,7 @@ private:
     void ToULongLong();
 #endif // wxLongLong_t
     void ToDouble();
-    void WriteBuf();
+    void StringBuf();
     void UTF8Buf();
     void CStrDataTernaryOperator();
     void DoCStrDataTernaryOperator(bool cond);
@@ -159,25 +161,49 @@ void StringTestCase::Format()
 
 void StringTestCase::Constructors()
 {
-    #define TEST_CTOR(args, res)                                               \
-        {                                                                      \
-            wxString s args ;                                                  \
-            CPPUNIT_ASSERT( s == res );                                        \
-        }
+    WX_ASSERT_STR_EQUAL( "", wxString('Z', 0) );
+    WX_ASSERT_STR_EQUAL( "Z", wxString('Z') );
+    WX_ASSERT_STR_EQUAL( "ZZZZ", wxString('Z', 4) );
+    WX_ASSERT_STR_EQUAL( "Hell", wxString("Hello", 4) );
+    WX_ASSERT_STR_EQUAL( "Hello", wxString("Hello", 5) );
 
-    TEST_CTOR((_T('Z'), 4), _T("ZZZZ"));
-    TEST_CTOR((_T("Hello"), 4), _T("Hell"));
-    TEST_CTOR((_T("Hello"), 5), _T("Hello"));
+#if wxUSE_UNICODE
+    WX_ASSERT_STR_EQUAL( L"", wxString(L'Z', 0) );
+    WX_ASSERT_STR_EQUAL( L"Z", wxString(L'Z') );
+    WX_ASSERT_STR_EQUAL( L"ZZZZ", wxString(L'Z', 4) );
+    WX_ASSERT_STR_EQUAL( L"Hell", wxString(L"Hello", 4) );
+    WX_ASSERT_STR_EQUAL( L"Hello", wxString(L"Hello", 5) );
+#endif // wxUSE_UNICODE
 
-    static const wxChar *s = _T("?really!");
-    const wxChar *start = wxStrchr(s, _T('r'));
-    const wxChar *end = wxStrchr(s, _T('!'));
-    TEST_CTOR((start, end), _T("really"));
+    static const char *s = "?really!";
+    const char *start = wxStrchr(s, 'r');
+    const char *end = wxStrchr(s, '!');
+    WX_ASSERT_STR_EQUAL( "really", wxString(start, end) );
 
     // test if creating string from NULL C pointer works:
-    TEST_CTOR(((char*)NULL), "");
+    WX_ASSERT_STR_EQUAL( "", wxString((const char *)NULL) );
 }
 
+void StringTestCase::StaticConstructors()
+{
+    WX_ASSERT_STR_EQUAL( "", wxString::FromAscii("") );
+    WX_ASSERT_STR_EQUAL( "", wxString::FromAscii("Hello", 0) );
+    WX_ASSERT_STR_EQUAL( "Hell", wxString::FromAscii("Hello", 4) );
+    WX_ASSERT_STR_EQUAL( "Hello", wxString::FromAscii("Hello", 5) );
+    WX_ASSERT_STR_EQUAL( "Hello", wxString::FromAscii("Hello") );
+
+    // FIXME: this doesn't work currently but should!
+    //WX_ASSERT_SIZET_EQUAL( 1, wxString::FromAscii("", 1).length() );
+
+
+    WX_ASSERT_STR_EQUAL( "", wxString::FromUTF8("") );
+    WX_ASSERT_STR_EQUAL( "", wxString::FromUTF8("Hello", 0) );
+    WX_ASSERT_STR_EQUAL( "Hell", wxString::FromUTF8("Hello", 4) );
+    WX_ASSERT_STR_EQUAL( "Hello", wxString::FromUTF8("Hello", 5) );
+    WX_ASSERT_STR_EQUAL( "Hello", wxString::FromUTF8("Hello") );
+
+    //WX_ASSERT_SIZET_EQUAL( 1, wxString::FromUTF8("", 1).length() );
+}
 
 void StringTestCase::Extraction()
 {
@@ -348,22 +374,6 @@ void StringTestCase::CaseChanges()
     CPPUNIT_ASSERT( s1l == _T("hello!") );
     CPPUNIT_ASSERT( s2u == wxEmptyString );
     CPPUNIT_ASSERT( s2l == wxEmptyString );
-
-#if !wxUSE_UNICODE
-    wxLocale locRu(wxLANGUAGE_RUSSIAN, 0 /* flags */);
-    if ( locRu.IsOk() )
-    {
-        // try upper casing 8bit strings
-        const wchar_t capital_ya[] = { 0x42f, 0 },
-                      small_ya[]   = { 0x44f, 0 };
-
-        wxString sUpper(wxConvLibc.cWC2MB(capital_ya)),
-                 sLower(wxConvLibc.cWC2MB(small_ya));
-
-        CPPUNIT_ASSERT( sUpper.Lower() == sLower );
-        CPPUNIT_ASSERT( sLower.Upper() == sUpper );
-    }
-#endif // !wxUSE_UNICODE
 }
 
 void StringTestCase::Compare()
@@ -651,31 +661,50 @@ void StringTestCase::ToDouble()
     }
 }
 
-void StringTestCase::WriteBuf()
+void StringTestCase::StringBuf()
 {
+    // check that buffer can be used to write into the string
     wxString s;
     wxStrcpy(wxStringBuffer(s, 10), _T("foo"));
 
-    CPPUNIT_ASSERT(s[0u] == _T('f') );
+    WX_ASSERT_SIZET_EQUAL(3, s.length());
     CPPUNIT_ASSERT(_T('f') == s[0u]);
     CPPUNIT_ASSERT(_T('o') == s[1]);
     CPPUNIT_ASSERT(_T('o') == s[2]);
-    WX_ASSERT_SIZET_EQUAL(3, s.length());
 
+    {
+        // also check that the buffer initially contains the original string
+        // contents
+        wxStringBuffer buf(s, 10);
+        CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] );
+        CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] );
+        CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] );
+        CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] );
+    }
 
     {
         wxStringBufferLength buf(s, 10);
+        CPPUNIT_ASSERT_EQUAL( _T('f'), buf[0] );
+        CPPUNIT_ASSERT_EQUAL( _T('o'), buf[1] );
+        CPPUNIT_ASSERT_EQUAL( _T('o'), buf[2] );
+        CPPUNIT_ASSERT_EQUAL( _T('\0'), buf[3] );
+
+        // and check that it can be used to write only the specified number of
+        // characters to the string
         wxStrcpy(buf, _T("barrbaz"));
         buf.SetLength(4);
     }
 
+    WX_ASSERT_SIZET_EQUAL(4, s.length());
     CPPUNIT_ASSERT(_T('b') == s[0u]);
     CPPUNIT_ASSERT(_T('a') == s[1]);
     CPPUNIT_ASSERT(_T('r') == s[2]);
     CPPUNIT_ASSERT(_T('r') == s[3]);
-    WX_ASSERT_SIZET_EQUAL(4, s.length());
 
-    CPPUNIT_ASSERT_EQUAL( 0, wxStrcmp(_T("barr"), s) );
+    // check that creating buffer of length smaller than string works, i.e. at
+    // least doesn't crash (it would if we naively copied the entire original
+    // string contents in the buffer)
+    *wxStringBuffer(s, 1) = '!';
 }
 
 void StringTestCase::UTF8Buf()