vsnprintf_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright 2010-2024 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include "test.h"
  6. #include <bx/string.h>
  7. #include <bx/readerwriter.h>
  8. #include <limits>
  9. #include <inttypes.h>
  10. TEST_CASE("No output buffer provided.", "[string][printf]")
  11. {
  12. REQUIRE(4 == bx::snprintf(NULL, 0, "test") );
  13. REQUIRE(1 == bx::snprintf(NULL, 0, "%d", 1) );
  14. }
  15. TEST_CASE("Truncated output buffer.", "[string][printf]")
  16. {
  17. REQUIRE(4 == bx::snprintf(NULL, 0, "abvg") );
  18. char buffer15[15]; // fit
  19. REQUIRE(4 == bx::snprintf(buffer15, BX_COUNTOF(buffer15), "abvg") );
  20. REQUIRE('\0' == buffer15[4]);
  21. REQUIRE(0 == bx::strCmp(buffer15, "abvg") );
  22. char buffer1[1]; // truncate
  23. REQUIRE(4 == bx::snprintf(buffer1, BX_COUNTOF(buffer1), "abvg") );
  24. REQUIRE('\0' == buffer1[BX_COUNTOF(buffer1)-1]);
  25. char buffer7[7]; // truncate
  26. REQUIRE(10 == bx::snprintf(NULL, 0, "Ten chars!") );
  27. REQUIRE(10 == bx::snprintf(buffer7, BX_COUNTOF(buffer7), "Ten chars!") );
  28. REQUIRE('\0' == buffer7[BX_COUNTOF(buffer7)-1]);
  29. REQUIRE(0 == bx::strCmp(buffer7, "Ten ch") );
  30. REQUIRE(7 == bx::snprintf(NULL, 0, "Seven67") );
  31. REQUIRE(7 == bx::snprintf(buffer7, BX_COUNTOF(buffer7), "Seven67") );
  32. REQUIRE('\0' == buffer7[BX_COUNTOF(buffer7)-1]);
  33. REQUIRE(0 == bx::strCmp(buffer7, "Seven6") );
  34. REQUIRE(11 == bx::snprintf(NULL, 0, "SevenEleven") );
  35. REQUIRE(11 == bx::snprintf(buffer7, BX_COUNTOF(buffer7), "SevenEleven") );
  36. REQUIRE('\0' == buffer7[BX_COUNTOF(buffer7)-1]);
  37. REQUIRE(0 == bx::strCmp(buffer7, "SevenE") );
  38. }
  39. template<bool StdCompliantT>
  40. static bool test(const char* _expected, const char* _format, va_list _argList)
  41. {
  42. int32_t max = (int32_t)bx::strLen(_expected) + 1;
  43. char* bxTemp = (char*)alloca(max);
  44. va_list argList;
  45. va_copy(argList, _argList);
  46. const int32_t bxLen = bx::vsnprintf(bxTemp, max, _format, argList);
  47. bool result = true
  48. && bxLen == max-1
  49. && 0 == bx::strCmp(_expected, bxTemp)
  50. ;
  51. char* crtTemp = NULL;
  52. int32_t crtLen = 0;
  53. if (!result
  54. || StdCompliantT)
  55. {
  56. BX_ASSERT(bx::strFind(_format, "%S").isEmpty()
  57. , "String format test is using '%%S' bx::StringView specific format specifier which is not standard compliant. "
  58. "Use `testNotStdCompliant` string testing method."
  59. );
  60. crtTemp = (char*)alloca(max);
  61. va_copy(argList, _argList);
  62. crtLen = ::vsnprintf(crtTemp, max, _format, argList);
  63. result &= true
  64. && crtLen == bxLen
  65. && 0 == bx::strCmp(bx::StringView(bxTemp, bxLen), bx::StringView(crtTemp, crtLen) )
  66. ;
  67. }
  68. if (!result)
  69. {
  70. printf("---\n");
  71. printf("printf format '%s'\n", _format);
  72. printf(" result (%4d) '%s'\n", bxLen, bxTemp);
  73. printf(" expected (%4d) '%s'\n", max-1, _expected);
  74. printf("CRT vsnprintf (%4d) '%s'\n", crtLen, crtTemp);
  75. }
  76. return result;
  77. }
  78. // Test against CRT's vsnprintf implementation.
  79. static bool test(const char* _expected, const char* _format, ...)
  80. {
  81. va_list argList;
  82. va_start(argList, _format);
  83. const bool result = test<true>(_expected, _format, argList);
  84. va_end(argList);
  85. return result;
  86. }
  87. // Skip test against CRT's vsnprintf implementation.
  88. static bool testNotStdCompliant(const char* _expected, const char* _format, ...)
  89. {
  90. va_list argList;
  91. va_start(argList, _format);
  92. const bool result = test<false>(_expected, _format, argList);
  93. va_end(argList);
  94. return result;
  95. }
  96. TEST_CASE("Format %f", "[string][printf]")
  97. {
  98. REQUIRE(test("1.337", "%0.3f", 1.337) );
  99. REQUIRE(test(" 13.370", "%8.3f", 13.37) );
  100. REQUIRE(test(" 13.370", "%*.*f", 8, 3, 13.37) );
  101. REQUIRE(test("13.370 ", "%-8.3f", 13.37) );
  102. REQUIRE(test("13.370 ", "%*.*f", -8, 3, 13.37) );
  103. REQUIRE(test("nan ", "%-8f", std::numeric_limits<double>::quiet_NaN() ) );
  104. REQUIRE(test(" nan", "%8f", std::numeric_limits<double>::quiet_NaN() ) );
  105. REQUIRE(test("-NAN ", "%-8F", -std::numeric_limits<double>::quiet_NaN() ) );
  106. REQUIRE(test(" inf", "%8f", std::numeric_limits<double>::infinity() ) );
  107. REQUIRE(test("inf ", "%-8f", std::numeric_limits<double>::infinity() ) );
  108. REQUIRE(test(" -INF", "%8F", -std::numeric_limits<double>::infinity() ) );
  109. REQUIRE(test(" 1.0", "%4.1f", 1.0) );
  110. REQUIRE(test(" 1.500", "%6.3f", 1.5) );
  111. REQUIRE(test("0001.500", "%08.3f", 1.5) );
  112. REQUIRE(test("+001.500", "%+08.3f", 1.5) );
  113. REQUIRE(test("-001.500", "%+08.3f", -1.5) );
  114. REQUIRE(test("0.0039", "%.4f", 0.00390625) );
  115. REQUIRE(test("0.003906", "%f", 0.00390625) );
  116. REQUIRE(testNotStdCompliant("-1.234567e-9", "%f", -1.234567e-9) );
  117. REQUIRE(testNotStdCompliant("-1e-9", "%.0f", -1.234567e-9) );
  118. REQUIRE(testNotStdCompliant("-1.2e-9", "%.1f", -1.234567e-9) );
  119. REQUIRE(testNotStdCompliant("-1.23e-9", "%.2f", -1.234567e-9) );
  120. REQUIRE(testNotStdCompliant("-1.234e-9", "%.3f", -1.234567e-9) );
  121. REQUIRE(testNotStdCompliant("-1.2345e-9", "%.4f", -1.234567e-9) );
  122. REQUIRE(testNotStdCompliant("-1.23456e-9", "%.5f", -1.234567e-9) );
  123. REQUIRE(testNotStdCompliant("-1.234567e-9", "%.6f", -1.234567e-9) );
  124. REQUIRE(testNotStdCompliant("-1.2345670e-9", "%.7f", -1.234567e-9) );
  125. REQUIRE(testNotStdCompliant("-1.23456700e-9", "%.8f", -1.234567e-9) );
  126. REQUIRE(testNotStdCompliant("-1.234567000e-9", "%.9f", -1.234567e-9) );
  127. REQUIRE(testNotStdCompliant("-1.2345670000e-9", "%.10f", -1.234567e-9) );
  128. REQUIRE(testNotStdCompliant("3.141592", "%f", 3.1415926535897932) );
  129. REQUIRE(testNotStdCompliant("3.141592", "%F", 3.1415926535897932) );
  130. REQUIRE(testNotStdCompliant("3", "%.0f", 3.1415926535897932) );
  131. REQUIRE(testNotStdCompliant("3.1", "%.1f", 3.1415926535897932) );
  132. REQUIRE(testNotStdCompliant("3.14", "%.2f", 3.1415926535897932) );
  133. REQUIRE(testNotStdCompliant("3.141", "%.3f", 3.1415926535897932) );
  134. REQUIRE(testNotStdCompliant("3.1415", "%.4f", 3.1415926535897932) );
  135. REQUIRE(testNotStdCompliant("3.14159", "%.5f", 3.1415926535897932) );
  136. REQUIRE(testNotStdCompliant("3.141592", "%.6f", 3.1415926535897932) );
  137. REQUIRE(testNotStdCompliant("3.1415926", "%.7f", 3.1415926535897932) );
  138. REQUIRE(testNotStdCompliant("3.14159265", "%.8f", 3.1415926535897932) );
  139. REQUIRE(testNotStdCompliant("3.141592653", "%.9f", 3.1415926535897932) );
  140. REQUIRE(testNotStdCompliant("3.1415926535", "%.10f", 3.1415926535897932) );
  141. REQUIRE(testNotStdCompliant("3.14159265358", "%.11f", 3.1415926535897932) );
  142. REQUIRE(testNotStdCompliant("3.141592653589", "%.12f", 3.1415926535897932) );
  143. REQUIRE(testNotStdCompliant("3.1415926535897", "%.13f", 3.1415926535897932) );
  144. REQUIRE(testNotStdCompliant("3.14159265358979", "%.14f", 3.1415926535897932) );
  145. REQUIRE(testNotStdCompliant("3.141592653589793", "%.15f", 3.1415926535897932) );
  146. REQUIRE(testNotStdCompliant("3.1415926535897930", "%.16f", 3.1415926535897932) );
  147. REQUIRE(testNotStdCompliant("3.1415926535897930", "%.16F", 3.1415926535897932) );
  148. REQUIRE(testNotStdCompliant("-3.141592e-9", "%f", -3.1415926535897932e-9) );
  149. REQUIRE(testNotStdCompliant("-3.141592E-9", "%F", -3.1415926535897932e-9) );
  150. REQUIRE(testNotStdCompliant("-3e-9", "%.0f", -3.1415926535897932e-9) );
  151. REQUIRE(testNotStdCompliant("-3.1e-9", "%.1f", -3.1415926535897932e-9) );
  152. REQUIRE(testNotStdCompliant("-3.14e-9", "%.2f", -3.1415926535897932e-9) );
  153. REQUIRE(testNotStdCompliant("-3.141e-9", "%.3f", -3.1415926535897932e-9) );
  154. REQUIRE(testNotStdCompliant("-3.1415e-9", "%.4f", -3.1415926535897932e-9) );
  155. REQUIRE(testNotStdCompliant("-3.14159e-9", "%.5f", -3.1415926535897932e-9) );
  156. REQUIRE(testNotStdCompliant("-3.141592e-9", "%.6f", -3.1415926535897932e-9) );
  157. REQUIRE(testNotStdCompliant("-3.1415926e-9", "%.7f", -3.1415926535897932e-9) );
  158. REQUIRE(testNotStdCompliant("-3.14159265e-9", "%.8f", -3.1415926535897932e-9) );
  159. REQUIRE(testNotStdCompliant("-3.141592653e-9", "%.9f", -3.1415926535897932e-9) );
  160. REQUIRE(testNotStdCompliant("-3.1415926535e-9", "%.10f", -3.1415926535897932e-9) );
  161. REQUIRE(testNotStdCompliant("-3.14159265358e-9", "%.11f", -3.1415926535897932e-9) );
  162. REQUIRE(testNotStdCompliant("-3.141592653589e-9", "%.12f", -3.1415926535897932e-9) );
  163. REQUIRE(testNotStdCompliant("-3.1415926535897e-9", "%.13f", -3.1415926535897932e-9) );
  164. REQUIRE(testNotStdCompliant("-3.14159265358979e-9", "%.14f", -3.1415926535897932e-9) );
  165. REQUIRE(testNotStdCompliant("-3.141592653589793e-9", "%.15f", -3.1415926535897932e-9) );
  166. REQUIRE(testNotStdCompliant("-3.1415926535897930e-9", "%.16f", -3.1415926535897932e-9) );
  167. REQUIRE(testNotStdCompliant("-3.1415926535897930E-9", "%.16F", -3.1415926535897932e-9) );
  168. REQUIRE(testNotStdCompliant("1e-12", "%f", 1e-12));
  169. REQUIRE(test("0.00390625", "%.8f", 0.00390625) );
  170. REQUIRE(test("-0.00390625", "%.8f", -0.00390625) );
  171. REQUIRE(test("1.50000000000000000", "%.17f", 1.5) );
  172. }
  173. TEST_CASE("Format %d, %i, %o, %u, %x", "[string][printf]")
  174. {
  175. REQUIRE(test("1337", "%d", 1337) );
  176. REQUIRE(test("1337 ", "%-20d", 1337) );
  177. REQUIRE(test("-1337 ", "%-20d", -1337) );
  178. REQUIRE(test("1337", "%i", 1337) );
  179. REQUIRE(test("1337 ", "%-20i", 1337) );
  180. REQUIRE(test("-1337 ", "%-20i", -1337) );
  181. REQUIRE(test("1337", "%o", 01337) );
  182. REQUIRE(test("2471", "%o", 1337) );
  183. REQUIRE(test("1337 ", "%-20o", 01337) );
  184. REQUIRE(test("37777776441 ", "%-20o", -01337) );
  185. REQUIRE(test(" 2471", "%20o", 1337) );
  186. REQUIRE(test("00000000000000002471", "%020o", 1337) );
  187. REQUIRE(test("1337", "%u", 1337) );
  188. REQUIRE(test("1337 ", "%-20u", 1337) );
  189. REQUIRE(test("4294965959 ", "%-20u", -1337) );
  190. REQUIRE(test("1337", "%x", 0x1337) );
  191. REQUIRE(test("1234abcd ", "%-20x", 0x1234abcd) );
  192. REQUIRE(test("1234ABCD ", "%-20X", 0x1234abcd) );
  193. REQUIRE(test("edcb5433 ", "%-20x", -0x1234abcd) );
  194. REQUIRE(test("EDCB5433 ", "%-20X", -0x1234abcd) );
  195. REQUIRE(test(" 1234abcd", "% 20x", 0x1234abcd) );
  196. REQUIRE(test(" 1234ABCD", "% 20X", 0x1234abcd) );
  197. REQUIRE(test(" edcb5433", "% 20x", -0x1234abcd) );
  198. REQUIRE(test(" EDCB5433", "% 20X", -0x1234abcd) );
  199. REQUIRE(test("0000000000001234abcd", "%020x", 0x1234abcd) );
  200. REQUIRE(test("0000000000001234ABCD", "%020X", 0x1234abcd) );
  201. REQUIRE(test("000000000000edcb5433", "%020x", -0x1234abcd) );
  202. REQUIRE(test("000000000000EDCB5433", "%020X", -0x1234abcd) );
  203. REQUIRE(testNotStdCompliant("0xf", "0x%01x", -1) );
  204. REQUIRE(testNotStdCompliant("0xff", "0x%02x", -1) );
  205. REQUIRE(testNotStdCompliant("0xfff", "0x%03x", -1) );
  206. REQUIRE(testNotStdCompliant("0xffff", "0x%04x", -1) );
  207. REQUIRE(testNotStdCompliant("0xfffff", "0x%05x", -1) );
  208. REQUIRE(testNotStdCompliant("0xffffff", "0x%06x", -1) );
  209. REQUIRE(testNotStdCompliant("0xfffffff", "0x%07x", -1) );
  210. REQUIRE(testNotStdCompliant("0xffffffff", "0x%08x", -1) );
  211. REQUIRE(test(" -1", "% 4i", -1) );
  212. REQUIRE(test(" -1", "% 4i", -1) );
  213. REQUIRE(test(" 0", "% 4i", 0) );
  214. REQUIRE(test(" 1", "% 4i", 1) );
  215. REQUIRE(test(" 1", "% 4o", 1) );
  216. REQUIRE(test(" +1", "%+4i", 1) );
  217. REQUIRE(testNotStdCompliant(" +1", "%+4o", 1) );
  218. REQUIRE(test(" +0", "%+4i", 0) );
  219. REQUIRE(test(" -1", "%+4i", -1) );
  220. REQUIRE(test("0001", "%04i", 1) );
  221. REQUIRE(test("0001", "%04o", 1) );
  222. REQUIRE(test("0000", "%04i", 0) );
  223. REQUIRE(test("0000", "%04o", 0) );
  224. REQUIRE(test("-001", "%04i", -1) );
  225. REQUIRE(test("+001", "%+04i", 1) );
  226. if (BX_ENABLED(BX_ARCH_32BIT) )
  227. {
  228. REQUIRE(test("2147483647", "%jd", INTMAX_MAX) );
  229. }
  230. else
  231. {
  232. REQUIRE(test("9223372036854775807", "%jd", INTMAX_MAX) );
  233. }
  234. REQUIRE(test("18446744073709551615", "%" PRIu64, UINT64_MAX) );
  235. REQUIRE(test("ffffffffffffffff", "%016" PRIx64, UINT64_MAX) );
  236. }
  237. TEST_CASE("Format modifiers", "[string][printf]")
  238. {
  239. REQUIRE(test("| 1.000000|", "|%10f|", 1.0f) );
  240. REQUIRE(test("|1.000000 |", "|%-10f|", 1.0f) );
  241. REQUIRE(test("|001.000000|", "|%010f|", 1.0f) );
  242. REQUIRE(test("|0000000001|", "|%010.0f|", 1.0f) );
  243. REQUIRE(test("|000000001.|", "|%#010.0f|", 1.0f) );
  244. REQUIRE(test("| 1|", "|%10.0f|", 1.0f) );
  245. REQUIRE(test("| 1.|", "|%#10.0f|", 1.0f) );
  246. REQUIRE(test("| +1.|", "|%#+10.0f|", 1.0f) );
  247. REQUIRE(test("|1 |", "|%-10.0f|", 1.0f) );
  248. REQUIRE(test("|1. |", "|%#-10.0f|", 1.0f) );
  249. REQUIRE(test("|+1. |", "|%+#-10.0f|", 1.0f) );
  250. REQUIRE(test("| 00013: -00089|", "|%10.5d:%10.5d|", 13, -89) );
  251. REQUIRE(test("| -00013: +00089|", "|%10.5d:%+10.5d|", -13, 89) );
  252. REQUIRE(test("| -00013: -00089|", "|%10.5d:%10.5d|", -13, -89) );
  253. }
  254. TEST_CASE("Format %p", "[string][printf]")
  255. {
  256. REQUIRE(test("0xbadc0de", "%p", (void*)0xbadc0de) );
  257. REQUIRE(test("0xbadc0de ", "%-20p", (void*)0xbadc0de) );
  258. }
  259. TEST_CASE("Format %s", "[string][printf]")
  260. {
  261. REQUIRE(test("(null)", "%s", NULL) );
  262. }
  263. TEST_CASE("Format %td", "[string][printf]")
  264. {
  265. size_t size = size_t(-1);
  266. REQUIRE(test("-1", "%td", size) );
  267. REQUIRE(test("3221225472", "%td", size_t(3221225472) ) );
  268. }
  269. TEST_CASE("Format %n", "[string][printf]")
  270. {
  271. char temp[64];
  272. int32_t p0, p1, p2;
  273. bx::snprintf(temp, sizeof(temp), "%n", &p0);
  274. REQUIRE(0 == p0);
  275. bx::snprintf(temp, sizeof(temp), "01%n23%n45%n", &p0, &p1, &p2);
  276. REQUIRE(2 == p0);
  277. REQUIRE(4 == p1);
  278. REQUIRE(6 == p2);
  279. }
  280. TEST_CASE("Format %g", "[string][printf]")
  281. {
  282. REQUIRE(test(" 0.01", "%7.2g", .01) );
  283. REQUIRE(test(" 0.0123", "%7.4G", .0123) );
  284. // REQUIRE(test("1.23e+05", "%.3g", 123000.25) );
  285. // REQUIRE(test("1e+05", "%.0g", 123000.25) );
  286. }
  287. TEST_CASE("Format %c, %s, %S", "[string][printf]")
  288. {
  289. REQUIRE(test("x", "%c", 'x') );
  290. REQUIRE(test("x ", "%-20c", 'x') );
  291. REQUIRE(test("hello ", "%-20s", "hello") );
  292. REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") );
  293. REQUIRE(testNotStdCompliant("h", "%1s", "hello") );
  294. REQUIRE(testNotStdCompliant("he", "%2s", "hello") );
  295. REQUIRE(testNotStdCompliant("hel", "%3s", "hello") );
  296. REQUIRE(testNotStdCompliant("hell", "%4s", "hello") );
  297. REQUIRE(testNotStdCompliant("hello", "%5s", "hello") );
  298. bx::StringView str("0hello1world2");
  299. bx::StringView hello(str, 1, 5);
  300. bx::StringView world(str, 7, 5);
  301. REQUIRE(test("hello, world!", "%.*s, %.*s!"
  302. , hello.getLength(), hello.getPtr()
  303. , world.getLength(), world.getPtr()
  304. ) );
  305. REQUIRE(testNotStdCompliant("hello, world!", "%S, %S!"
  306. , &hello
  307. , &world
  308. ) );
  309. }
  310. TEST_CASE("WriterI", "[string][printf]")
  311. {
  312. char tmp[64];
  313. bx::StaticMemoryBlock mb(tmp, sizeof(tmp));
  314. bx::MemoryWriter writer(&mb);
  315. bx::Error err;
  316. int32_t len = bx::write(&writer, &err, "%d", 1389);
  317. REQUIRE(err.isOk());
  318. REQUIRE(len == 4);
  319. bx::StringView str(tmp, len);
  320. REQUIRE(0 == bx::strCmp(str, "1389") );
  321. }
  322. TEST_CASE("Invalid", "[string][printf]")
  323. {
  324. char temp[64];
  325. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%", 1) );
  326. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-", 1) );
  327. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-0", 1) );
  328. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-03", 1) );
  329. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-03.", 1) );
  330. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-03.0", 1) );
  331. REQUIRE(0 == bx::snprintf(temp, sizeof(temp), "%-03.0t", 1) );
  332. }