string_test.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.clear();
  43. REQUIRE(0 == st.getLength() );
  44. REQUIRE(4 == sv.getLength() );
  45. sv.clear();
  46. REQUIRE(0 == sv.getLength() );
  47. }