string_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2010-2016 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("StringView", "")
  34. {
  35. bx::StringView sv("test");
  36. REQUIRE(4 == sv.getLength() );
  37. bx::CrtAllocator crt;
  38. g_allocator = &crt;
  39. typedef bx::StringT<&g_allocator> String;
  40. String st(sv);
  41. REQUIRE(4 == st.getLength() );
  42. st.append("test");
  43. REQUIRE(8 == st.getLength() );
  44. st.append("test", 2);
  45. REQUIRE(10 == st.getLength() );
  46. REQUIRE(0 == strcmp(st.getPtr(), "testtestte") );
  47. st.clear();
  48. REQUIRE(0 == st.getLength() );
  49. REQUIRE(4 == sv.getLength() );
  50. st.append("test");
  51. REQUIRE(4 == st.getLength() );
  52. sv.clear();
  53. REQUIRE(0 == sv.getLength() );
  54. }