- // NB: remember that wx*printf could be mapped either to system
- // implementation or to wx implementation.
- // In the first case, when the output buffer is too small, the returned
- // value can be the number of characters required for the output buffer
- // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
- // just a negative number, usually -1; (this is how e.g. MSVC's
- // *printf() behaves). Fortunately, in all implementations, when the
- // output buffer is too small, it's nonetheless filled up to its max
- // size.
+ // test how wxVsnprintf() behaves with wrong format string:
+
+#if 0
+ // NB: the next 2 tests currently return an error but it would be nice
+ // if they didn't (see ticket #9367)
+
+ // two positionals with the same index:
+ r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
+ CPPUNIT_ASSERT(r != -1);
+
+ // three positionals with the same index mixed with other pos args:
+ r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
+ CPPUNIT_ASSERT(r != -1);
+#endif
+
+ // a missing positional arg: this should result in an error but not all
+ // implementations detect it (e.g. glibc doesn't)
+ r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3);
+ CPPUNIT_ASSERT_EQUAL(-1, r);
+
+ // positional and non-positionals in the same format string:
+ r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
+ CPPUNIT_ASSERT_EQUAL(-1, r);
+}
+
+#endif // wxUSE_WXVSNPRINTF
+
+// BigToSmallBuffer() test case helper:
+template<typename T>
+void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
+{
+ // Remember that wx*printf could be mapped either to system
+ // implementation or to wx implementation.
+ // In the first case, when the output buffer is too small, the returned
+ // value can be the number of characters required for the output buffer
+ // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
+ // just a negative number, usually -1; (this is how e.g. MSVC's
+ // *printf() behaves). Luckily, in all implementations, when the
+ // output buffer is too small, it's nonetheless filled up to its max size.
+ //
+ // Note that in the second case (i.e. when we're using our own implementation),
+ // wxVsnprintf() will return the number of characters written in the standard
+ // output or
+ // -1 if there was an error in the format string
+ // maxSize+1 if the output buffer is too small
+
+ wxString errStr;
+ errStr << "The size of the buffer was " << size;
+ std::string errMsg(errStr.mb_str());