string_test.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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("strnlen", "")
  11. {
  12. const char* test = "test";
  13. REQUIRE(0 == bx::strnlen(test, 0) );
  14. REQUIRE(2 == bx::strnlen(test, 2) );
  15. REQUIRE(4 == bx::strnlen(test, UINT32_MAX) );
  16. }
  17. TEST_CASE("strlncpy", "")
  18. {
  19. char dst[128];
  20. size_t num;
  21. num = bx::strlncpy(dst, 1, "blah");
  22. REQUIRE(num == 0);
  23. num = bx::strlncpy(dst, 3, "blah", 3);
  24. REQUIRE(0 == strcmp(dst, "bl") );
  25. REQUIRE(num == 2);
  26. num = bx::strlncpy(dst, sizeof(dst), "blah", 3);
  27. REQUIRE(0 == strcmp(dst, "bla") );
  28. REQUIRE(num == 3);
  29. num = bx::strlncpy(dst, sizeof(dst), "blah");
  30. REQUIRE(0 == strcmp(dst, "blah") );
  31. REQUIRE(num == 4);
  32. }
  33. TEST_CASE("strincmp", "")
  34. {
  35. REQUIRE(0 == bx::strincmp("test", "test") );
  36. REQUIRE(0 == bx::strincmp("test", "testestes", 4) );
  37. REQUIRE(0 == bx::strincmp("testestes", "test", 4) );
  38. }
  39. TEST_CASE("strnchr", "")
  40. {
  41. const char* test = "test";
  42. REQUIRE(NULL == bx::strnchr(test, 's', 0) );
  43. REQUIRE(NULL == bx::strnchr(test, 's', 2) );
  44. REQUIRE(&test[2] == bx::strnchr(test, 's') );
  45. }
  46. TEST_CASE("strnrchr", "")
  47. {
  48. const char* test = "test";
  49. REQUIRE(NULL == bx::strnrchr(test, 's', 0) );
  50. REQUIRE(NULL == bx::strnrchr(test, 's', 1) );
  51. REQUIRE(&test[2] == bx::strnrchr(test, 's') );
  52. }
  53. TEST_CASE("StringView", "")
  54. {
  55. bx::StringView sv("test");
  56. REQUIRE(4 == sv.getLength() );
  57. bx::CrtAllocator crt;
  58. g_allocator = &crt;
  59. typedef bx::StringT<&g_allocator> String;
  60. String st(sv);
  61. REQUIRE(4 == st.getLength() );
  62. st.append("test");
  63. REQUIRE(8 == st.getLength() );
  64. st.append("test", 2);
  65. REQUIRE(10 == st.getLength() );
  66. REQUIRE(0 == strcmp(st.getPtr(), "testtestte") );
  67. st.clear();
  68. REQUIRE(0 == st.getLength() );
  69. REQUIRE(4 == sv.getLength() );
  70. st.append("test");
  71. REQUIRE(4 == st.getLength() );
  72. sv.clear();
  73. REQUIRE(0 == sv.getLength() );
  74. }