vsnprintf_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  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("vsnprintf NULL buffer", "No output buffer provided.")
  11. {
  12. REQUIRE(4 == bx::snprintf(NULL, 0, "test") );
  13. REQUIRE(1 == bx::snprintf(NULL, 0, "%d", 1) );
  14. }
  15. TEST_CASE("vsnprintf truncated", "Truncated output buffer.")
  16. {
  17. char buffer[7];
  18. REQUIRE(10 == bx::snprintf(buffer, BX_COUNTOF(buffer), "Ten chars!") );
  19. REQUIRE(0 == bx::strCmp(buffer, "Ten ch") );
  20. }
  21. static bool test(const char* _expected, const char* _format, ...)
  22. {
  23. int32_t max = (int32_t)bx::strLen(_expected) + 1;
  24. char* temp = (char*)alloca(max);
  25. va_list argList;
  26. va_start(argList, _format);
  27. int32_t len = bx::vsnprintf(temp, max, _format, argList);
  28. va_end(argList);
  29. bool result = true
  30. && len == max-1
  31. && 0 == bx::strCmp(_expected, temp)
  32. ;
  33. if (!result)
  34. {
  35. printf("result (%d) '%s', expected (%d) '%s'\n", len, temp, max-1, _expected);
  36. }
  37. return result;
  38. }
  39. TEST_CASE("vsnprintf f", "")
  40. {
  41. REQUIRE(test("1.337", "%0.3f", 1.337) );
  42. REQUIRE(test(" 13.370", "%8.3f", 13.37) );
  43. REQUIRE(test(" 13.370", "%*.*f", 8, 3, 13.37) );
  44. REQUIRE(test("13.370 ", "%-8.3f", 13.37) );
  45. REQUIRE(test("13.370 ", "%*.*f", -8, 3, 13.37) );
  46. REQUIRE(test("nan ", "%-8f", std::numeric_limits<double>::quiet_NaN() ) );
  47. REQUIRE(test(" nan", "%8f", std::numeric_limits<double>::quiet_NaN() ) );
  48. #if !BX_CRT_MSVC
  49. // BK - VS2015 CRT vsnprintf returns '-NAN(IND'.
  50. # if BX_CRT_LIBCXX
  51. // BK - Clang LibC vsnprintf returns 'NAN '.
  52. REQUIRE(test("NAN ", "%-8F", -std::numeric_limits<double>::quiet_NaN() ) );
  53. # else
  54. REQUIRE(test("-NAN ", "%-8F", -std::numeric_limits<double>::quiet_NaN() ) );
  55. # endif // BX_CRT_LIBCXX
  56. #endif // !BX_CRT_MSVC
  57. REQUIRE(test(" inf", "%8f", std::numeric_limits<double>::infinity() ) );
  58. REQUIRE(test("inf ", "%-8f", std::numeric_limits<double>::infinity() ) );
  59. REQUIRE(test(" -INF", "%8F", -std::numeric_limits<double>::infinity() ) );
  60. }
  61. TEST_CASE("vsnprintf d/i/o/u/x", "")
  62. {
  63. REQUIRE(test("1337", "%d", 1337) );
  64. REQUIRE(test("1337 ", "%-20d", 1337) );
  65. REQUIRE(test("-1337 ", "%-20d", -1337) );
  66. REQUIRE(test("1337", "%i", 1337) );
  67. REQUIRE(test("1337 ", "%-20i", 1337) );
  68. REQUIRE(test("-1337 ", "%-20i", -1337) );
  69. REQUIRE(test("1337", "%o", 01337) );
  70. REQUIRE(test("2471", "%o", 1337) );
  71. REQUIRE(test("1337 ", "%-20o", 01337) );
  72. REQUIRE(test("37777776441 ", "%-20o", -01337) );
  73. REQUIRE(test("1337", "%u", 1337) );
  74. REQUIRE(test("1337 ", "%-20u", 1337) );
  75. REQUIRE(test("4294965959 ", "%-20u", -1337) );
  76. REQUIRE(test("1337", "%x", 0x1337) );
  77. REQUIRE(test("1234abcd ", "%-20x", 0x1234abcd) );
  78. REQUIRE(test("1234ABCD ", "%-20X", 0x1234abcd) );
  79. REQUIRE(test("edcb5433 ", "%-20x", -0x1234abcd) );
  80. REQUIRE(test("EDCB5433 ", "%-20X", -0x1234abcd) );
  81. REQUIRE(test("0000000000001234abcd", "%020x", 0x1234abcd) );
  82. REQUIRE(test("0000000000001234ABCD", "%020X", 0x1234abcd) );
  83. REQUIRE(test("000000000000edcb5433", "%020x", -0x1234abcd) );
  84. REQUIRE(test("000000000000EDCB5433", "%020X", -0x1234abcd) );
  85. #if !BX_CRT_MSVC
  86. // BK - VS2015 CRT vsnprintf doesn't support 'j' length sub-specifier?
  87. if (BX_ENABLED(BX_ARCH_32BIT) )
  88. {
  89. REQUIRE(test("2147483647", "%jd", INTMAX_MAX) );
  90. }
  91. else
  92. {
  93. REQUIRE(test("9223372036854775807", "%jd", INTMAX_MAX) );
  94. }
  95. #endif // !BX_CRT_MSVC
  96. REQUIRE(test("18446744073709551615", "%" PRIu64, UINT64_MAX) );
  97. REQUIRE(test("ffffffffffffffff", "%016" PRIx64, UINT64_MAX) );
  98. }
  99. TEST_CASE("vsnprintf modifiers", "")
  100. {
  101. REQUIRE(test("| 1.000000|", "|%10f|", 1.0f) );
  102. REQUIRE(test("|1.000000 |", "|%-10f|", 1.0f) );
  103. REQUIRE(test("|001.000000|", "|%010f|", 1.0f) );
  104. REQUIRE(test("|0000000001|", "|%010.0f|", 1.0f) );
  105. REQUIRE(test("|000000001.|", "|%#010.0f|", 1.0f) );
  106. REQUIRE(test("| 1|", "|%10.0f|", 1.0f) );
  107. REQUIRE(test("| 1.|", "|%#10.0f|", 1.0f) );
  108. REQUIRE(test("| +1.|", "|%#+10.0f|", 1.0f) );
  109. REQUIRE(test("|1 |", "|%-10.0f|", 1.0f) );
  110. REQUIRE(test("|1. |", "|%#-10.0f|", 1.0f) );
  111. REQUIRE(test("|+1. |", "|%+#-10.0f|", 1.0f) );
  112. }
  113. TEST_CASE("vsnprintf p", "")
  114. {
  115. #if BX_CRT_MSVC
  116. // BK - VS2015 CRT vsnprintf has different output for 'p' pointer specifier.
  117. REQUIRE(test("0BADC0DE", "%p", (void*)0xbadc0de));
  118. REQUIRE(test("0BADC0DE ", "%-20p", (void*)0xbadc0de));
  119. #else
  120. REQUIRE(test("0xbadc0de", "%p", (void*)0xbadc0de) );
  121. REQUIRE(test("0xbadc0de ", "%-20p", (void*)0xbadc0de) );
  122. #endif // BX_CRT_MSVC
  123. }
  124. TEST_CASE("vsnprintf s", "")
  125. {
  126. REQUIRE(test("(null)", "%s", NULL) );
  127. }
  128. TEST_CASE("vsnprintf g", "")
  129. {
  130. REQUIRE(test(" 0.01", "%7.3g", .01) );
  131. REQUIRE(test(" 0.0123", "%7.3G", .0123) );
  132. REQUIRE(test("1.23e+05", "%.3g", 123000.25) );
  133. REQUIRE(test("1e+05", "%.0g", 123000.25) );
  134. }
  135. TEST_CASE("vsnprintf", "")
  136. {
  137. REQUIRE(test("x", "%c", 'x') );
  138. REQUIRE(test("x ", "%-20c", 'x') );
  139. REQUIRE(test("hello ", "%-20s", "hello") );
  140. REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") );
  141. bx::StringView str("0hello1world2");
  142. bx::StringView hello(str, 1, 5);
  143. bx::StringView world(str, 7, 5);
  144. REQUIRE(test("hello, world!", "%.*s, %.*s!"
  145. , hello.getLength(), hello.getPtr()
  146. , world.getLength(), world.getPtr()
  147. ) );
  148. }
  149. TEST_CASE("vsnprintf write")
  150. {
  151. char tmp[64];
  152. bx::StaticMemoryBlock mb(tmp, sizeof(tmp));
  153. bx::MemoryWriter writer(&mb);
  154. bx::Error err;
  155. int32_t len = bx::write(&writer, &err, "%d", 1389);
  156. REQUIRE(err.isOk());
  157. REQUIRE(len == 4);
  158. bx::StringView str(tmp, len);
  159. REQUIRE(0 == bx::strCmp(str, "1389") );
  160. }