vsnprintf_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2010-2020 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 buffer5[5]; // fit
  18. REQUIRE(4 == bx::snprintf(buffer5, BX_COUNTOF(buffer5), "abvg") );
  19. REQUIRE(0 == bx::strCmp(buffer5, "abvg") );
  20. char buffer7[7]; // truncate
  21. REQUIRE(10 == bx::snprintf(buffer7, BX_COUNTOF(buffer7), "Ten chars!") );
  22. REQUIRE(0 == bx::strCmp(buffer7, "Ten ch") );
  23. }
  24. static bool test(const char* _expected, const char* _format, ...)
  25. {
  26. int32_t max = (int32_t)bx::strLen(_expected) + 1;
  27. char* temp = (char*)alloca(max);
  28. va_list argList;
  29. va_start(argList, _format);
  30. int32_t len = bx::vsnprintf(temp, max, _format, argList);
  31. va_end(argList);
  32. bool result = true
  33. && len == max-1
  34. && 0 == bx::strCmp(_expected, temp)
  35. ;
  36. if (!result)
  37. {
  38. printf("result (%d) '%s', expected (%d) '%s'\n", len, temp, max-1, _expected);
  39. }
  40. return result;
  41. }
  42. TEST_CASE("vsnprintf f")
  43. {
  44. REQUIRE(test("1.337", "%0.3f", 1.337) );
  45. REQUIRE(test(" 13.370", "%8.3f", 13.37) );
  46. REQUIRE(test(" 13.370", "%*.*f", 8, 3, 13.37) );
  47. REQUIRE(test("13.370 ", "%-8.3f", 13.37) );
  48. REQUIRE(test("13.370 ", "%*.*f", -8, 3, 13.37) );
  49. REQUIRE(test("nan ", "%-8f", std::numeric_limits<double>::quiet_NaN() ) );
  50. REQUIRE(test(" nan", "%8f", std::numeric_limits<double>::quiet_NaN() ) );
  51. REQUIRE(test("-NAN ", "%-8F", -std::numeric_limits<double>::quiet_NaN() ) );
  52. REQUIRE(test(" inf", "%8f", std::numeric_limits<double>::infinity() ) );
  53. REQUIRE(test("inf ", "%-8f", std::numeric_limits<double>::infinity() ) );
  54. REQUIRE(test(" -INF", "%8F", -std::numeric_limits<double>::infinity() ) );
  55. }
  56. TEST_CASE("vsnprintf d/i/o/u/x")
  57. {
  58. REQUIRE(test("1337", "%d", 1337) );
  59. REQUIRE(test("1337 ", "%-20d", 1337) );
  60. REQUIRE(test("-1337 ", "%-20d", -1337) );
  61. REQUIRE(test("1337", "%i", 1337) );
  62. REQUIRE(test("1337 ", "%-20i", 1337) );
  63. REQUIRE(test("-1337 ", "%-20i", -1337) );
  64. REQUIRE(test("1337", "%o", 01337) );
  65. REQUIRE(test("2471", "%o", 1337) );
  66. REQUIRE(test("1337 ", "%-20o", 01337) );
  67. REQUIRE(test("37777776441 ", "%-20o", -01337) );
  68. REQUIRE(test("1337", "%u", 1337) );
  69. REQUIRE(test("1337 ", "%-20u", 1337) );
  70. REQUIRE(test("4294965959 ", "%-20u", -1337) );
  71. REQUIRE(test("1337", "%x", 0x1337) );
  72. REQUIRE(test("1234abcd ", "%-20x", 0x1234abcd) );
  73. REQUIRE(test("1234ABCD ", "%-20X", 0x1234abcd) );
  74. REQUIRE(test("edcb5433 ", "%-20x", -0x1234abcd) );
  75. REQUIRE(test("EDCB5433 ", "%-20X", -0x1234abcd) );
  76. REQUIRE(test("0000000000001234abcd", "%020x", 0x1234abcd) );
  77. REQUIRE(test("0000000000001234ABCD", "%020X", 0x1234abcd) );
  78. REQUIRE(test("000000000000edcb5433", "%020x", -0x1234abcd) );
  79. REQUIRE(test("000000000000EDCB5433", "%020X", -0x1234abcd) );
  80. if (BX_ENABLED(BX_ARCH_32BIT) )
  81. {
  82. REQUIRE(test("2147483647", "%jd", INTMAX_MAX) );
  83. }
  84. else
  85. {
  86. REQUIRE(test("9223372036854775807", "%jd", INTMAX_MAX) );
  87. }
  88. REQUIRE(test("18446744073709551615", "%" PRIu64, UINT64_MAX) );
  89. REQUIRE(test("ffffffffffffffff", "%016" PRIx64, UINT64_MAX) );
  90. }
  91. TEST_CASE("vsnprintf modifiers")
  92. {
  93. REQUIRE(test("| 1.000000|", "|%10f|", 1.0f) );
  94. REQUIRE(test("|1.000000 |", "|%-10f|", 1.0f) );
  95. REQUIRE(test("|001.000000|", "|%010f|", 1.0f) );
  96. REQUIRE(test("|0000000001|", "|%010.0f|", 1.0f) );
  97. REQUIRE(test("|000000001.|", "|%#010.0f|", 1.0f) );
  98. REQUIRE(test("| 1|", "|%10.0f|", 1.0f) );
  99. REQUIRE(test("| 1.|", "|%#10.0f|", 1.0f) );
  100. REQUIRE(test("| +1.|", "|%#+10.0f|", 1.0f) );
  101. REQUIRE(test("|1 |", "|%-10.0f|", 1.0f) );
  102. REQUIRE(test("|1. |", "|%#-10.0f|", 1.0f) );
  103. REQUIRE(test("|+1. |", "|%+#-10.0f|", 1.0f) );
  104. }
  105. TEST_CASE("vsnprintf p")
  106. {
  107. REQUIRE(test("0xbadc0de", "%p", (void*)0xbadc0de) );
  108. REQUIRE(test("0xbadc0de ", "%-20p", (void*)0xbadc0de) );
  109. }
  110. TEST_CASE("vsnprintf s")
  111. {
  112. REQUIRE(test("(null)", "%s", NULL) );
  113. }
  114. TEST_CASE("vsnprintf g")
  115. {
  116. REQUIRE(test(" 0.01", "%7.2g", .01) );
  117. REQUIRE(test(" 0.0123", "%7.4G", .0123) );
  118. // REQUIRE(test("1.23e+05", "%.3g", 123000.25) );
  119. // REQUIRE(test("1e+05", "%.0g", 123000.25) );
  120. }
  121. TEST_CASE("vsnprintf")
  122. {
  123. REQUIRE(test("x", "%c", 'x') );
  124. REQUIRE(test("x ", "%-20c", 'x') );
  125. REQUIRE(test("hello ", "%-20s", "hello") );
  126. REQUIRE(test("hello, world!", "%s, %s!", "hello", "world") );
  127. bx::StringView str("0hello1world2");
  128. bx::StringView hello(str, 1, 5);
  129. bx::StringView world(str, 7, 5);
  130. REQUIRE(test("hello, world!", "%.*s, %.*s!"
  131. , hello.getLength(), hello.getPtr()
  132. , world.getLength(), world.getPtr()
  133. ) );
  134. }
  135. TEST_CASE("vsnprintf write")
  136. {
  137. char tmp[64];
  138. bx::StaticMemoryBlock mb(tmp, sizeof(tmp));
  139. bx::MemoryWriter writer(&mb);
  140. bx::Error err;
  141. int32_t len = bx::write(&writer, &err, "%d", 1389);
  142. REQUIRE(err.isOk());
  143. REQUIRE(len == 4);
  144. bx::StringView str(tmp, len);
  145. REQUIRE(0 == bx::strCmp(str, "1389") );
  146. }