string_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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("strincmp", "")
  46. {
  47. REQUIRE(0 == bx::strincmp("test", "test") );
  48. REQUIRE(0 == bx::strincmp("test", "testestes", 4) );
  49. REQUIRE(0 == bx::strincmp("testestes", "test", 4) );
  50. REQUIRE(0 != bx::strincmp("preprocess", "platform") );
  51. }
  52. TEST_CASE("strnchr", "")
  53. {
  54. const char* test = "test";
  55. REQUIRE(NULL == bx::strnchr(test, 's', 0) );
  56. REQUIRE(NULL == bx::strnchr(test, 's', 2) );
  57. REQUIRE(&test[2] == bx::strnchr(test, 's') );
  58. }
  59. TEST_CASE("strnrchr", "")
  60. {
  61. const char* test = "test";
  62. REQUIRE(NULL == bx::strnrchr(test, 's', 0) );
  63. REQUIRE(NULL == bx::strnrchr(test, 's', 1) );
  64. REQUIRE(&test[2] == bx::strnrchr(test, 's') );
  65. }
  66. TEST_CASE("stristr", "")
  67. {
  68. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  69. REQUIRE(NULL == bx::stristr(test, "quick", 8) );
  70. REQUIRE(NULL == bx::stristr(test, "quick1") );
  71. REQUIRE(&test[4] == bx::stristr(test, "quick", 9) );
  72. REQUIRE(&test[4] == bx::stristr(test, "quick") );
  73. }
  74. TEST_CASE("strnstr", "")
  75. {
  76. const char* test = "The Quick Brown Fox Jumps Over The Lazy Dog.";
  77. REQUIRE(NULL == bx::strnstr(test, "quick", 8) );
  78. REQUIRE(NULL == bx::strnstr(test, "quick1") );
  79. REQUIRE(NULL == bx::strnstr(test, "quick", 9) );
  80. REQUIRE(NULL == bx::strnstr(test, "quick") );
  81. REQUIRE(NULL == bx::strnstr(test, "Quick", 8) );
  82. REQUIRE(NULL == bx::strnstr(test, "Quick1") );
  83. REQUIRE(&test[4] == bx::strnstr(test, "Quick", 9) );
  84. REQUIRE(&test[4] == bx::strnstr(test, "Quick") );
  85. }
  86. TEST_CASE("StringView", "")
  87. {
  88. bx::StringView sv("test");
  89. REQUIRE(4 == sv.getLength() );
  90. bx::CrtAllocator crt;
  91. g_allocator = &crt;
  92. typedef bx::StringT<&g_allocator> String;
  93. String st(sv);
  94. REQUIRE(4 == st.getLength() );
  95. st.append("test");
  96. REQUIRE(8 == st.getLength() );
  97. st.append("test", 2);
  98. REQUIRE(10 == st.getLength() );
  99. REQUIRE(0 == strcmp(st.getPtr(), "testtestte") );
  100. st.clear();
  101. REQUIRE(0 == st.getLength() );
  102. REQUIRE(4 == sv.getLength() );
  103. st.append("test");
  104. REQUIRE(4 == st.getLength() );
  105. sv.clear();
  106. REQUIRE(0 == sv.getLength() );
  107. }