vsnprintf.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/strings/vsnprintf.cpp
  3. // Purpose: wxVsnprintf unit test
  4. // Author: Francesco Montorsi
  5. // (part of this file was taken from CMP.c of TRIO package
  6. // written by Bjorn Reese and Daniel Stenberg)
  7. // Created: 2006-04-01
  8. // Copyright: (c) 2006 Francesco Montorsi, Bjorn Reese and Daniel Stenberg
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // ----------------------------------------------------------------------------
  11. // headers
  12. // ----------------------------------------------------------------------------
  13. #include "testprec.h"
  14. #ifdef __BORLANDC__
  15. #pragma hdrstop
  16. #endif
  17. #include "wx/crt.h"
  18. #if wxUSE_WXVSNPRINTF
  19. #ifndef WX_PRECOMP
  20. #include "wx/wx.h"
  21. #include "wx/wxchar.h"
  22. #endif // WX_PRECOMP
  23. // NOTE: for more info about the specification of wxVsnprintf() behaviour you can
  24. // refer to the following page of the GNU libc manual:
  25. // http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html
  26. // ----------------------------------------------------------------------------
  27. // global utilities for testing
  28. // ----------------------------------------------------------------------------
  29. #define MAX_TEST_LEN 1024
  30. // temporary buffers
  31. static wxChar buf[MAX_TEST_LEN];
  32. int r;
  33. // these macros makes it possible to write all tests without repeating a lot
  34. // of times the wxT() macro
  35. // NOTE: you should use expected strings with these macros which do not exceed
  36. // MAX_TEST_LEN as these macro do check if the return value is == (int)wxStrlen(buf)
  37. #define ASSERT_STR_EQUAL( a, b ) \
  38. CPPUNIT_ASSERT_EQUAL( wxString(a), wxString(b) );
  39. #define CMP6(expected, fmt, y, z, w, t) \
  40. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w, t); \
  41. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  42. ASSERT_STR_EQUAL( wxT(expected), buf );
  43. #define CMP5(expected, fmt, y, z, w) \
  44. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z, w); \
  45. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  46. ASSERT_STR_EQUAL( wxT(expected), buf );
  47. #define CMP4(expected, fmt, y, z) \
  48. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y, z); \
  49. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  50. ASSERT_STR_EQUAL( wxT(expected), buf );
  51. #define CMP3(expected, fmt, y) \
  52. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
  53. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  54. ASSERT_STR_EQUAL( wxT(expected), buf );
  55. #define CMP3i(expected, fmt, y) \
  56. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt), y); \
  57. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  58. WX_ASSERT_MESSAGE( ("Expected \"%s\", got \"%s\"", expected, buf), \
  59. wxStricmp(expected, buf) == 0 );
  60. #define CMP2(expected, fmt) \
  61. r=wxSnprintf(buf, MAX_TEST_LEN, wxT(fmt)); \
  62. CPPUNIT_ASSERT_EQUAL( r, wxStrlen(buf) ); \
  63. ASSERT_STR_EQUAL( wxT(expected), buf );
  64. // NOTE: this macro is used also with too-small buffers (see Miscellaneous())
  65. // test function, thus the return value can be > size and thus we
  66. // cannot check if r == (int)wxStrlen(buf)
  67. #define CMPTOSIZE(buffer, size, failuremsg, expected, fmt, x, y, z, w) \
  68. r=wxSnprintf(buffer, size, wxT(fmt), x, y, z, w); \
  69. CPPUNIT_ASSERT( r > 0 ); \
  70. CPPUNIT_ASSERT_EQUAL_MESSAGE( \
  71. failuremsg, \
  72. wxString(wxT(expected)).Left(size - 1), \
  73. wxString(buffer))
  74. // this is the same as wxSnprintf() but it passes the format string to
  75. // wxVsnprintf() without using WX_ATTRIBUTE_PRINTF and thus suppresses the gcc
  76. // checks (and resulting warnings) for the format string
  77. //
  78. // use with extreme care and only when you're really sure the warnings must be
  79. // suppressed!
  80. template<typename T>
  81. static int
  82. wxUnsafeSnprintf(T *buf, size_t len, const wxChar *fmt, ...)
  83. {
  84. va_list args;
  85. va_start(args, fmt);
  86. int rc = wxVsnprintf(buf, len, fmt, args);
  87. va_end(args);
  88. return rc;
  89. }
  90. // ----------------------------------------------------------------------------
  91. // test class
  92. // ----------------------------------------------------------------------------
  93. class VsnprintfTestCase : public CppUnit::TestCase
  94. {
  95. public:
  96. VsnprintfTestCase() {}
  97. virtual void setUp();
  98. private:
  99. CPPUNIT_TEST_SUITE( VsnprintfTestCase );
  100. CPPUNIT_TEST( C );
  101. CPPUNIT_TEST( D );
  102. CPPUNIT_TEST( X );
  103. CPPUNIT_TEST( O );
  104. CPPUNIT_TEST( P );
  105. CPPUNIT_TEST( N );
  106. CPPUNIT_TEST( E );
  107. CPPUNIT_TEST( F );
  108. CPPUNIT_TEST( G );
  109. CPPUNIT_TEST( S );
  110. CPPUNIT_TEST( Asterisk );
  111. CPPUNIT_TEST( Percent );
  112. #ifdef wxLongLong_t
  113. CPPUNIT_TEST( LongLong );
  114. #endif
  115. CPPUNIT_TEST( BigToSmallBuffer );
  116. CPPUNIT_TEST( WrongFormatStrings );
  117. CPPUNIT_TEST( Miscellaneous );
  118. CPPUNIT_TEST( GlibcMisc1 );
  119. CPPUNIT_TEST( GlibcMisc2 );
  120. CPPUNIT_TEST_SUITE_END();
  121. void C();
  122. void D();
  123. void X();
  124. void O();
  125. void P();
  126. void N();
  127. void E();
  128. void F();
  129. void G();
  130. void S();
  131. void Asterisk();
  132. void Percent();
  133. #ifdef wxLongLong_t
  134. void LongLong();
  135. #endif
  136. void Unicode();
  137. template<typename T>
  138. void DoBigToSmallBuffer(T *buffer, int size);
  139. void BigToSmallBuffer();
  140. void WrongFormatStrings();
  141. // compares the expectedString and the result of wxVsnprintf() char by char
  142. // for all its lenght (not only for first expectedLen chars) and also
  143. // checks the return value
  144. void DoMisc(int expectedLen, const wxString& expectedString,
  145. size_t max, const wxChar *format, ...);
  146. void Miscellaneous();
  147. void GlibcMisc1();
  148. void GlibcMisc2();
  149. DECLARE_NO_COPY_CLASS(VsnprintfTestCase)
  150. };
  151. // register in the unnamed registry so that these tests are run by default
  152. CPPUNIT_TEST_SUITE_REGISTRATION( VsnprintfTestCase );
  153. // also include in its own registry so that these tests can be run alone
  154. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( VsnprintfTestCase, "VsnprintfTestCase" );
  155. void VsnprintfTestCase::setUp()
  156. {
  157. // this call is required to avoid check failures when running on machines
  158. // with a locale where the decimal point is not '.'
  159. wxSetlocale(LC_ALL, "C");
  160. }
  161. void VsnprintfTestCase::C()
  162. {
  163. CMP5("hi!", "%c%c%c", wxT('h'), wxT('i'), wxT('!'));
  164. // NOTE:
  165. // the NULL characters _can_ be passed to %c to e.g. create strings
  166. // with embedded NULs (because strings are not always supposed to be
  167. // NUL-terminated).
  168. DoMisc(14, wxT("Hello \0 World!"), 16, wxT("Hello %c World!"), wxT('\0'));
  169. }
  170. void VsnprintfTestCase::D()
  171. {
  172. CMP3("+123456", "%+d", 123456);
  173. CMP3("-123456", "%d", -123456);
  174. CMP3(" 123456", "% d", 123456);
  175. CMP3(" 123456", "%10d", 123456);
  176. CMP3("0000123456", "%010d", 123456);
  177. CMP3("-123456 ", "%-10d", -123456);
  178. }
  179. void VsnprintfTestCase::X()
  180. {
  181. CMP3("ABCD", "%X", 0xABCD);
  182. CMP3("0XABCD", "%#X", 0xABCD);
  183. CMP3("0xabcd", "%#x", 0xABCD);
  184. }
  185. void VsnprintfTestCase::O()
  186. {
  187. CMP3("1234567", "%o", 01234567);
  188. CMP3("01234567", "%#o", 01234567);
  189. }
  190. void VsnprintfTestCase::P()
  191. {
  192. // The exact format used for "%p" is not specified by the standard and so
  193. // varies among different platforms, so we need to expect different results
  194. // here (remember that while we test our own wxPrintf() code here, it uses
  195. // the system sprintf() for actual formatting so the results are still
  196. // different under different systems).
  197. #ifdef wxUSING_VC_CRT_IO
  198. // MSVC always prints pointers as %8X on 32 bit systems and as %16X on 64
  199. // bit systems.
  200. #if SIZEOF_VOID_P == 4
  201. CMP3i("00ABCDEF", "%p", (void*)0xABCDEF);
  202. CMP3("00000000", "%p", (void*)NULL);
  203. #elif SIZEOF_VOID_P == 8
  204. CMP3i("0000ABCDEFABCDEF", "%p", (void*)0xABCDEFABCDEF);
  205. CMP3("0000000000000000", "%p", (void*)NULL);
  206. #endif
  207. #elif defined(__MINGW32__)
  208. // mingw32 uses MSVC CRT in old versions but is own implementation now
  209. // which is somewhere in the middle as it uses %8x, so to catch both cases
  210. // we use case-insensitive comparison here.
  211. CMP3("0xabcdef", "%p", (void*)0xABCDEF);
  212. CMP3("0", "%p", (void*)NULL);
  213. #elif defined(__GNUG__)
  214. // glibc prints pointers as %#x except for NULL pointers which are printed
  215. // as '(nil)'.
  216. CMP3("0xabcdef", "%p", (void*)0xABCDEF);
  217. CMP3("(nil)", "%p", (void*)NULL);
  218. #endif
  219. }
  220. void VsnprintfTestCase::N()
  221. {
  222. int nchar;
  223. wxSnprintf(buf, MAX_TEST_LEN, wxT("%d %s%n\n"), 3, wxT("bears"), &nchar);
  224. CPPUNIT_ASSERT_EQUAL( 7, nchar );
  225. }
  226. void VsnprintfTestCase::E()
  227. {
  228. // NB: there are no standards about the minimum exponent width
  229. // (and the width of the %e conversion specifier refers to the
  230. // mantissa, not to the exponent).
  231. // Since newer MSVC versions use 3 digits as minimum exponent
  232. // width while GNU libc uses 2 digits as minimum width, here we
  233. // workaround this problem using for the exponent values with at
  234. // least three digits.
  235. // Some examples:
  236. // printf("%e",2.342E+02);
  237. // -> under MSVC7.1 prints: 2.342000e+002
  238. // -> under GNU libc 2.4 prints: 2.342000e+02
  239. CMP3("2.342000e+112", "%e",2.342E+112);
  240. CMP3("-2.3420e-112", "%10.4e",-2.342E-112);
  241. CMP3("-2.3420e-112", "%11.4e",-2.342E-112);
  242. CMP3(" -2.3420e-112", "%15.4e",-2.342E-112);
  243. CMP3("-0.02342", "%G",-2.342E-02);
  244. CMP3("3.1415E-116", "%G",3.1415e-116);
  245. CMP3("0003.141500e+103", "%016e", 3141.5e100);
  246. CMP3(" 3.141500e+103", "%16e", 3141.5e100);
  247. CMP3("3.141500e+103 ", "%-16e", 3141.5e100);
  248. CMP3("3.142e+103", "%010.3e", 3141.5e100);
  249. }
  250. void VsnprintfTestCase::F()
  251. {
  252. CMP3("3.300000", "%5f", 3.3);
  253. CMP3("3.000000", "%5f", 3.0);
  254. CMP3("0.000100", "%5f", .999999E-4);
  255. CMP3("0.000990", "%5f", .99E-3);
  256. CMP3("3333.000000", "%5f", 3333.0);
  257. }
  258. void VsnprintfTestCase::G()
  259. {
  260. // NOTE: the same about E() testcase applies here...
  261. CMP3(" 3.3", "%5g", 3.3);
  262. CMP3(" 3", "%5g", 3.0);
  263. CMP3("9.99999e-115", "%5g", .999999E-114);
  264. CMP3("0.00099", "%5g", .99E-3);
  265. CMP3(" 3333", "%5g", 3333.0);
  266. CMP3(" 0.01", "%5g", 0.01);
  267. CMP3(" 3", "%5.g", 3.3);
  268. CMP3(" 3", "%5.g", 3.0);
  269. CMP3("1e-114", "%5.g", .999999E-114);
  270. CMP3("0.0001", "%5.g", 1.0E-4);
  271. CMP3("0.001", "%5.g", .99E-3);
  272. CMP3("3e+103", "%5.g", 3333.0E100);
  273. CMP3(" 0.01", "%5.g", 0.01);
  274. CMP3(" 3.3", "%5.2g", 3.3);
  275. CMP3(" 3", "%5.2g", 3.0);
  276. CMP3("1e-114", "%5.2g", .999999E-114);
  277. CMP3("0.00099", "%5.2g", .99E-3);
  278. CMP3("3.3e+103", "%5.2g", 3333.0E100);
  279. CMP3(" 0.01", "%5.2g", 0.01);
  280. }
  281. void VsnprintfTestCase::S()
  282. {
  283. CMP3(" abc", "%5s", wxT("abc"));
  284. CMP3(" a", "%5s", wxT("a"));
  285. CMP3("abcdefghi", "%5s", wxT("abcdefghi"));
  286. CMP3("abc ", "%-5s", wxT("abc"));
  287. CMP3("abcdefghi", "%-5s", wxT("abcdefghi"));
  288. CMP3("abcde", "%.5s", wxT("abcdefghi"));
  289. // do the same tests but with Unicode characters:
  290. #if wxUSE_UNICODE
  291. // Unicode code points from U+03B1 to U+03B9 are the greek letters alpha-iota;
  292. // UTF8 encoding of such code points is 0xCEB1 to 0xCEB9
  293. #define ALPHA "\xCE\xB1"
  294. // alpha
  295. #define ABC "\xCE\xB1\xCE\xB2\xCE\xB3"
  296. // alpha+beta+gamma
  297. #define ABCDE "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5"
  298. // alpha+beta+gamma+delta+epsilon
  299. #define ABCDEFGHI "\xCE\xB1\xCE\xB2\xCE\xB3\xCE\xB4\xCE\xB5\xCE\xB6\xCE\xB7\xCE\xB8\xCE\xB9"
  300. // alpha+beta+gamma+delta+epsilon+zeta+eta+theta+iota
  301. // the 'expected' and 'arg' parameters of this macro are supposed to be
  302. // UTF-8 strings
  303. #define CMP3_UTF8(expected, fmt, arg) \
  304. CPPUNIT_ASSERT_EQUAL \
  305. ( \
  306. wxString::FromUTF8(expected).length(), \
  307. wxSnprintf(buf, MAX_TEST_LEN, fmt, wxString::FromUTF8(arg)) \
  308. ); \
  309. CPPUNIT_ASSERT_EQUAL \
  310. ( \
  311. wxString::FromUTF8(expected), \
  312. buf \
  313. )
  314. CMP3_UTF8(" " ABC, "%5s", ABC);
  315. CMP3_UTF8(" " ALPHA, "%5s", ALPHA);
  316. CMP3_UTF8(ABCDEFGHI, "%5s", ABCDEFGHI);
  317. CMP3_UTF8(ABC " ", "%-5s", ABC);
  318. CMP3_UTF8(ABCDEFGHI, "%-5s", ABCDEFGHI);
  319. CMP3_UTF8(ABCDE, "%.5s", ABCDEFGHI);
  320. #endif // wxUSE_UNICODE
  321. // test a string which has a NULL character after "ab";
  322. // obviously it should be handled exactly like just as "ab"
  323. CMP3(" ab", "%5s", wxT("ab\0cdefghi"));
  324. }
  325. void VsnprintfTestCase::Asterisk()
  326. {
  327. CMP5(" 0.1", "%*.*f", 10, 1, 0.123);
  328. CMP5(" 0.1230", "%*.*f", 10, 4, 0.123);
  329. CMP5("0.1", "%*.*f", 3, 1, 0.123);
  330. CMP4("%0.002", "%%%.*f", 3, 0.0023456789);
  331. CMP4(" a", "%*c", 8, 'a');
  332. CMP4(" four", "%*s", 8, "four");
  333. CMP6(" four four", "%*s %*s", 8, "four", 6, "four");
  334. }
  335. void VsnprintfTestCase::Percent()
  336. {
  337. // some tests without any argument passed through ...
  338. CMP2("%", "%%");
  339. CMP2("%%%", "%%%%%%");
  340. CMP3("% abc", "%%%5s", wxT("abc"));
  341. CMP3("% abc%", "%%%5s%%", wxT("abc"));
  342. // do not test odd number of '%' symbols as different implementations
  343. // of snprintf() give different outputs as this situation is not considered
  344. // by any standard (in fact, GCC will also warn you about a spurious % if
  345. // you write %%% as argument of some *printf function !)
  346. // Compare(wxT("%"), wxT("%%%"));
  347. }
  348. #ifdef wxLongLong_t
  349. void VsnprintfTestCase::LongLong()
  350. {
  351. CMP3("123456789", "%lld", (wxLongLong_t)123456789);
  352. CMP3("-123456789", "%lld", (wxLongLong_t)-123456789);
  353. CMP3("123456789", "%llu", (wxULongLong_t)123456789);
  354. #ifdef __WINDOWS__
  355. CMP3("123456789", "%I64d", (wxLongLong_t)123456789);
  356. CMP3("123456789abcdef", "%I64x", wxLL(0x123456789abcdef));
  357. #endif
  358. }
  359. #endif
  360. void VsnprintfTestCase::WrongFormatStrings()
  361. {
  362. // test how wxVsnprintf() behaves with wrong format string:
  363. #if 0
  364. // NB: the next 2 tests currently return an error but it would be nice
  365. // if they didn't (see ticket #9367)
  366. // two positionals with the same index:
  367. r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$s %1$s"), "hello");
  368. CPPUNIT_ASSERT(r != -1);
  369. // three positionals with the same index mixed with other pos args:
  370. r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%4$d %2$f %1$s %2$s %3$d"), "hello", "world", 3, 4);
  371. CPPUNIT_ASSERT(r != -1);
  372. #endif
  373. // a missing positional arg should result in an assert
  374. WX_ASSERT_FAILS_WITH_ASSERT(
  375. wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %3$d"), 1, 2, 3) );
  376. // positional and non-positionals in the same format string:
  377. r = wxSnprintf(buf, MAX_TEST_LEN, wxT("%1$d %d %3$d"), 1, 2, 3);
  378. CPPUNIT_ASSERT_EQUAL(-1, r);
  379. }
  380. // BigToSmallBuffer() test case helper:
  381. template<typename T>
  382. void VsnprintfTestCase::DoBigToSmallBuffer(T *buffer, int size)
  383. {
  384. // Remember that wx*printf could be mapped either to system
  385. // implementation or to wx implementation.
  386. // In the first case, when the output buffer is too small, the returned
  387. // value can be the number of characters required for the output buffer
  388. // (conforming to ISO C99; implemented in e.g. GNU libc >= 2.1), or
  389. // just a negative number, usually -1; (this is how e.g. MSVC's
  390. // *printf() behaves). Luckily, in all implementations, when the
  391. // output buffer is too small, it's nonetheless filled up to its max size.
  392. //
  393. // Note that in the second case (i.e. when we're using our own implementation),
  394. // wxVsnprintf() will return the number of characters written in the standard
  395. // output or
  396. // -1 if there was an error in the format string
  397. // maxSize+1 if the output buffer is too small
  398. wxString errStr;
  399. errStr << "The size of the buffer was " << size;
  400. std::string errMsg(errStr.mb_str());
  401. // test without positionals
  402. CMPTOSIZE(buffer, size, errMsg,
  403. "123456789012 - test - 123 -4.567",
  404. "%i%li - test - %d %.3f",
  405. 123, (long int)456789012, 123, -4.567);
  406. #if wxUSE_PRINTF_POS_PARAMS
  407. // test with positional
  408. CMPTOSIZE(buffer, size, errMsg,
  409. "-4.567 123 - test - 456789012 123",
  410. "%4$.3f %1$i - test - %2$li %3$d",
  411. 123, (long int)456789012, 123, -4.567);
  412. #endif
  413. // test unicode/ansi conversion specifiers
  414. //
  415. // NB: we use wxUnsafeSnprintf() as %hs and %hc are invalid in printf
  416. // format and gcc would warn about this otherwise
  417. r = wxUnsafeSnprintf(buffer, size,
  418. wxT("unicode string/char: %ls/%lc -- ansi string/char: %hs/%hc"),
  419. L"unicode", L'U', "ansi", 'A');
  420. wxString expected =
  421. wxString(wxT("unicode string/char: unicode/U -- ansi string/char: ansi/A")).Left(size - 1);
  422. CPPUNIT_ASSERT( r != -1 );
  423. CPPUNIT_ASSERT_EQUAL(
  424. expected,
  425. wxString(buffer)
  426. );
  427. }
  428. void VsnprintfTestCase::BigToSmallBuffer()
  429. {
  430. // VC6 can't compile this code
  431. #if !defined(__VISUALC__) || (__VISUALC__ >= 1310)
  432. #if wxUSE_UNICODE
  433. wchar_t bufw[1024], bufw2[16], bufw3[4], bufw4;
  434. DoBigToSmallBuffer(bufw, 1024);
  435. DoBigToSmallBuffer(bufw2, 16);
  436. DoBigToSmallBuffer(bufw3, 4);
  437. DoBigToSmallBuffer(&bufw4, 1);
  438. #endif // wxUSE_UNICODE
  439. char bufa[1024], bufa2[16], bufa3[4], bufa4;
  440. DoBigToSmallBuffer(bufa, 1024);
  441. DoBigToSmallBuffer(bufa2, 16);
  442. DoBigToSmallBuffer(bufa3, 4);
  443. DoBigToSmallBuffer(&bufa4, 1);
  444. #endif // !VC6
  445. }
  446. // Miscellaneous() test case helper:
  447. void VsnprintfTestCase::DoMisc(
  448. int expectedLen,
  449. const wxString& expectedString,
  450. size_t max,
  451. const wxChar *format, ...)
  452. {
  453. const size_t BUFSIZE = MAX_TEST_LEN - 1;
  454. size_t i;
  455. static int count = 0;
  456. wxASSERT(max <= BUFSIZE);
  457. for (i = 0; i < BUFSIZE; i++)
  458. buf[i] = '*';
  459. buf[BUFSIZE] = 0;
  460. va_list ap;
  461. va_start(ap, format);
  462. int n = wxVsnprintf(buf, max, format, ap);
  463. va_end(ap);
  464. // Prepare messages so that it is possible to see from the error which
  465. // test was running.
  466. wxString errStr, overflowStr;
  467. errStr << wxT("No.: ") << ++count << wxT(", expected: ") << expectedLen
  468. << wxT(" '") << expectedString << wxT("', result: ");
  469. overflowStr << errStr << wxT("buffer overflow");
  470. errStr << n << wxT(" '") << buf << wxT("'");
  471. // turn them into std::strings
  472. std::string errMsg(errStr.mb_str());
  473. std::string overflowMsg(overflowStr.mb_str());
  474. CPPUNIT_ASSERT_MESSAGE(errMsg,
  475. (expectedLen == -1 && size_t(n) >= max) || expectedLen == n);
  476. CPPUNIT_ASSERT_MESSAGE(errMsg, expectedString == buf);
  477. for (i = max; i < BUFSIZE; i++)
  478. CPPUNIT_ASSERT_MESSAGE(overflowMsg, buf[i] == '*');
  479. }
  480. void VsnprintfTestCase::Miscellaneous()
  481. {
  482. // expectedLen, expectedString, max, format, ...
  483. DoMisc(5, wxT("-1234"), 8, wxT("%d"), -1234);
  484. DoMisc(7, wxT("1234567"), 8, wxT("%d"), 1234567);
  485. DoMisc(-1, wxT("1234567"), 8, wxT("%d"), 12345678);
  486. DoMisc(-1, wxT("-123456"), 8, wxT("%d"), -1234567890);
  487. DoMisc(6, wxT("123456"), 8, wxT("123456"));
  488. DoMisc(7, wxT("1234567"), 8, wxT("1234567"));
  489. DoMisc(-1, wxT("1234567"), 8, wxT("12345678"));
  490. DoMisc(6, wxT("123450"), 8, wxT("12345%d"), 0);
  491. DoMisc(7, wxT("1234560"), 8, wxT("123456%d"), 0);
  492. DoMisc(-1, wxT("1234567"), 8, wxT("1234567%d"), 0);
  493. DoMisc(-1, wxT("1234567"), 8, wxT("12345678%d"), 0);
  494. DoMisc(6, wxT("12%45%"), 8, wxT("12%%45%%"));
  495. DoMisc(7, wxT("12%45%7"), 8, wxT("12%%45%%7"));
  496. DoMisc(-1, wxT("12%45%7"), 8, wxT("12%%45%%78"));
  497. DoMisc(5, wxT("%%%%%"), 6, wxT("%%%%%%%%%%"));
  498. DoMisc(6, wxT("%%%%12"), 7, wxT("%%%%%%%%%d"), 12);
  499. }
  500. /* (C) Copyright C E Chew
  501. *
  502. * Feel free to copy, use and distribute this software provided:
  503. *
  504. * 1. you do not pretend that you wrote it
  505. * 2. you leave this copyright notice intact.
  506. */
  507. void VsnprintfTestCase::GlibcMisc1()
  508. {
  509. CMP3(" ", "%5.s", "xyz");
  510. CMP3(" 33", "%5.f", 33.3);
  511. #ifdef wxUSING_VC_CRT_IO
  512. // see the previous notes about the minimum width of mantissa:
  513. CMP3(" 3e+008", "%8.e", 33.3e7);
  514. CMP3(" 3E+008", "%8.E", 33.3e7);
  515. CMP3("3e+001", "%.g", 33.3);
  516. CMP3("3E+001", "%.G", 33.3);
  517. #else
  518. CMP3(" 3e+08", "%8.e", 33.3e7);
  519. CMP3(" 3E+08", "%8.E", 33.3e7);
  520. CMP3("3e+01", "%.g", 33.3);
  521. CMP3("3E+01", "%.G", 33.3);
  522. #endif
  523. }
  524. void VsnprintfTestCase::GlibcMisc2()
  525. {
  526. int prec;
  527. wxString test_format;
  528. prec = 0;
  529. CMP4("3", "%.*g", prec, 3.3);
  530. prec = 0;
  531. CMP4("3", "%.*G", prec, 3.3);
  532. prec = 0;
  533. CMP4(" 3", "%7.*G", prec, 3.33);
  534. prec = 3;
  535. CMP4(" 041", "%04.*o", prec, 33);
  536. prec = 7;
  537. CMP4(" 0000033", "%09.*u", prec, 33);
  538. prec = 3;
  539. CMP4(" 021", "%04.*x", prec, 33);
  540. prec = 3;
  541. CMP4(" 021", "%04.*X", prec, 33);
  542. }
  543. #endif // wxUSE_WXVSNPRINTF