string_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2010-2017 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/crtimpl.h>
  8. #include <bx/handlealloc.h>
  9. bx::AllocatorI* g_allocator;
  10. TEST_CASE("chars", "")
  11. {
  12. for (char ch = 'A'; ch <= 'Z'; ++ch)
  13. {
  14. REQUIRE(!bx::isLower(ch) );
  15. REQUIRE(!bx::isNumeric(ch) );
  16. REQUIRE(bx::isUpper(ch) );
  17. REQUIRE(bx::isAlpha(ch) );
  18. REQUIRE(bx::isAlphaNum(ch) );
  19. REQUIRE(bx::isLower(bx::toLower(ch) ) );
  20. }
  21. }
  22. TEST_CASE("strnlen", "")
  23. {
  24. const char* test = "test";
  25. REQUIRE(0 == bx::strnlen(test, 0) );
  26. REQUIRE(2 == bx::strnlen(test, 2) );
  27. REQUIRE(4 == bx::strnlen(test, UINT32_MAX) );
  28. }
  29. TEST_CASE("strlncpy", "")
  30. {
  31. char dst[128];
  32. size_t num;
  33. num = bx::strlncpy(dst, 1, "blah");
  34. REQUIRE(num == 0);
  35. num = bx::strlncpy(dst, 3, "blah", 3);
  36. REQUIRE(0 == strcmp(dst, "bl") );
  37. REQUIRE(num == 2);
  38. num = bx::strlncpy(dst, sizeof(dst), "blah", 3);
  39. REQUIRE(0 == strcmp(dst, "bla") );
  40. REQUIRE(num == 3);
  41. num = bx::strlncpy(dst, sizeof(dst), "blah");
  42. REQUIRE(0 == strcmp(dst, "blah") );
  43. REQUIRE(num == 4);
  44. }
  45. TEST_CASE("strlncat", "")
  46. {
  47. char dst[128] = { '\0' };
  48. REQUIRE(0 == bx::strlncat(dst, 1, "cat") );
  49. REQUIRE(4 == bx::strlncpy(dst, 5, "copy") );
  50. REQUIRE(3 == bx::strlncat(dst, 8, "cat") );
  51. REQUIRE(0 == bx::strncmp(dst, "copycat") );
  52. REQUIRE(1 == bx::strlncat(dst, BX_COUNTOF(dst), "------", 1) );
  53. REQUIRE(3 == bx::strlncat(dst, BX_COUNTOF(dst), "cat") );
  54. REQUIRE(0 == bx::strncmp(dst, "copycat-cat") );
  55. }
  56. TEST_CASE("strincmp", "")
  57. {
  58. REQUIRE(0 == bx::strincmp("test", "test") );
  59. REQUIRE(0 == bx::strincmp("test", "testestes", 4) );
  60. REQUIRE(0 == bx::strincmp("testestes", "test", 4) );
  61. REQUIRE(0 != bx::strincmp("preprocess", "platform") );
  62. const char* abvgd = "abvgd";
  63. const char* abvgx = "abvgx";
  64. const char* empty = "";
  65. REQUIRE(0 == bx::strincmp(abvgd, abvgd) );
  66. REQUIRE(0 == bx::strincmp(abvgd, abvgx, 4) );
  67. REQUIRE(0 > bx::strincmp(abvgd, abvgx) );
  68. REQUIRE(0 > bx::strincmp(empty, abvgd) );
  69. REQUIRE(0 < bx::strincmp(abvgx, abvgd) );
  70. REQUIRE(0 < bx::strincmp(abvgd, empty) );
  71. }
  72. TEST_CASE("strnchr", "")
  73. {
  74. const char* test = "test";
  75. REQUIRE(NULL == bx::strnchr(test, 's', 0) );
  76. REQUIRE(NULL == bx::strnchr(test, 's', 2) );
  77. REQUIRE(&test[2] == bx::strnchr(test, 's') );
  78. }
  79. TEST_CASE("strnrchr", "")
  80. {
  81. const char* test = "test";
  82. REQUIRE(NULL == bx::strnrchr(test, 's', 0) );
  83. REQUIRE(NULL == bx::strnrchr(test, 's', 1) );
  84. REQUIRE(&test[2] == bx::strnrchr(test, 's') );
  85. }
  86. TEST_CASE("stristr", "")
  87. {
  88. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  89. REQUIRE(NULL == bx::stristr(test, "quick", 8) );
  90. REQUIRE(NULL == bx::stristr(test, "quick1") );
  91. REQUIRE(&test[4] == bx::stristr(test, "quick", 9) );
  92. REQUIRE(&test[4] == bx::stristr(test, "quick") );
  93. }
  94. TEST_CASE("strnstr", "")
  95. {
  96. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  97. REQUIRE(NULL == bx::strnstr(test, "quick", 8) );
  98. REQUIRE(NULL == bx::strnstr(test, "quick1") );
  99. REQUIRE(NULL == bx::strnstr(test, "quick", 9) );
  100. REQUIRE(NULL == bx::strnstr(test, "quick") );
  101. REQUIRE(NULL == bx::strnstr(test, "Quick", 8) );
  102. REQUIRE(NULL == bx::strnstr(test, "Quick1") );
  103. REQUIRE(&test[4] == bx::strnstr(test, "Quick", 9) );
  104. REQUIRE(&test[4] == bx::strnstr(test, "Quick") );
  105. }
  106. TEST_CASE("StringView", "")
  107. {
  108. bx::StringView sv("test");
  109. REQUIRE(4 == sv.getLength() );
  110. bx::CrtAllocator crt;
  111. g_allocator = &crt;
  112. typedef bx::StringT<&g_allocator> String;
  113. String st(sv);
  114. REQUIRE(4 == st.getLength() );
  115. st.append("test");
  116. REQUIRE(8 == st.getLength() );
  117. st.append("test", 2);
  118. REQUIRE(10 == st.getLength() );
  119. REQUIRE(0 == strcmp(st.getPtr(), "testtestte") );
  120. st.clear();
  121. REQUIRE(0 == st.getLength() );
  122. REQUIRE(4 == sv.getLength() );
  123. st.append("test");
  124. REQUIRE(4 == st.getLength() );
  125. sv.clear();
  126. REQUIRE(0 == sv.getLength() );
  127. }