]> git.saurik.com Git - wxWidgets.git/commitdiff
Additional tests for wxVsnprintf, originally to test the copy from the scratch,
authorMichael Wetherell <mike.wetherell@ntlworld.com>
Thu, 9 Nov 2006 01:25:23 +0000 (01:25 +0000)
committerMichael Wetherell <mike.wetherell@ntlworld.com>
Thu, 9 Nov 2006 01:25:23 +0000 (01:25 +0000)
but actually shows up problems elsewhere.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@43217 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

tests/strings/vsnprintf.cpp

index 4e5bc4b8c5eede078500d699155c6bfc548601a5..f7e900f68565043ac01a014f5099679ea7ee62c6 100644 (file)
@@ -87,9 +87,12 @@ private:
         CPPUNIT_TEST( S );
         CPPUNIT_TEST( Asterisk );
         CPPUNIT_TEST( Percent );
+#ifdef wxLongLong_t
         CPPUNIT_TEST( LongLong );
+#endif
 
         CPPUNIT_TEST( BigToSmallBuffer );
+        CPPUNIT_TEST( Scratch );
     CPPUNIT_TEST_SUITE_END();
 
     void D();
@@ -103,10 +106,13 @@ private:
     void S();
     void Asterisk();
     void Percent();
+#ifdef wxLongLong_t
     void LongLong();
+#endif
     void Unicode();
 
     void BigToSmallBuffer();
+    void Scratch();
     void Misc(wxChar *buffer, int size);
 
     DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
@@ -297,13 +303,15 @@ void VsnprintfTestCase::Percent()
     // Compare(wxT("%"), wxT("%%%"));
 }
 
+#ifdef wxLongLong_t
 void VsnprintfTestCase::LongLong()
 {
-    CMP3("123456789", "%lld", (long long int)123456789);
-    CMP3("-123456789", "%lld", (long long int)-123456789);
+    CMP3("123456789", "%lld", (wxLongLong_t)123456789);
+    CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
 
-    CMP3("123456789", "%llu", (unsigned long long int)123456789);
+    CMP3("123456789", "%llu", (wxULongLong_t)123456789);
 }
+#endif
 
 void VsnprintfTestCase::Misc(wxChar *buffer, int size)
 {
@@ -351,3 +359,73 @@ void VsnprintfTestCase::BigToSmallBuffer()
     Misc(buf3, 4);
     Misc(&buf4, 1);
 }
+
+static void DoScratch(
+        int expectedLen,
+        const wxString& expectedString,
+        size_t max,
+        const wxChar *format, ...)
+{
+    const size_t BUFSIZE = 16;
+    wxChar buf[BUFSIZE + 1];
+    size_t i;
+    static int count = 0;
+
+    wxASSERT(max <= BUFSIZE);
+
+    for (i = 0; i < BUFSIZE; i++)
+        buf[i] = '*';
+    buf[BUFSIZE] = 0;
+
+    va_list ap;
+    va_start(ap, format);
+    
+    int n = wxVsnprintf(buf, max, format, ap);
+
+    va_end(ap);
+
+    // Prepare messages so that it is possible to see from the error which
+    // test was running.
+    wxString errStr, overflowStr;
+    errStr << _T("No.: ") << ++count << _T(", expected: ") << expectedLen
+           << _T(" '") << expectedString << _T("', result: ");
+    overflowStr << errStr << _T("buffer overflow");
+    errStr << n << _T(" '") << buf << _T("'");
+
+    // turn them into std::strings
+    std::string errMsg(errStr.mb_str());
+    std::string overflowMsg(overflowStr.mb_str());
+
+    CPPUNIT_ASSERT_MESSAGE(errMsg,
+            (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
+
+    CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
+
+    for (i = max; i < BUFSIZE; i++)
+        CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
+}
+
+// Originally intended to test the final copy from the scratch, hence the
+// name, but turns up problems elsewhere too.
+//
+void VsnprintfTestCase::Scratch()
+{
+    // expectedLen, expectedString, max, format, ...
+    DoScratch(5, wxT("-1234"), 8, wxT("%d"), -1234);
+    DoScratch(7, wxT("1234567"), 8,  wxT("%d"), 1234567);
+    DoScratch(-1, wxT("1234567"), 8,  wxT("%d"), 12345678);
+    DoScratch(-1, wxT("-123456"), 8,  wxT("%d"), -1234567890);
+
+    DoScratch(6, wxT("123456"), 8,  wxT("123456"));
+    DoScratch(7, wxT("1234567"), 8,  wxT("1234567"));
+    DoScratch(-1, wxT("1234567"), 8,  wxT("12345678"));
+
+    DoScratch(6, wxT("123450"), 8,  wxT("12345%d"), 0);
+    DoScratch(7, wxT("1234560"), 8,  wxT("123456%d"), 0);
+    DoScratch(-1, wxT("1234567"), 8,  wxT("1234567%d"), 0);
+    DoScratch(-1, wxT("1234567"), 8,  wxT("12345678%d"), 0);
+
+    DoScratch(6, wxT("12%45%"), 8,  wxT("12%%45%%"));
+    DoScratch(7, wxT("12%45%7"), 8,  wxT("12%%45%%7"));
+    DoScratch(-1, wxT("12%45%7"), 8,  wxT("12%%45%%78"));
+}