2
0

StringToolsTest.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Jolt/Core/StringTools.h>
  5. TEST_SUITE("StringToolsTest")
  6. {
  7. TEST_CASE("TestStringFormat")
  8. {
  9. CHECK(StringFormat("Test: %d", 1234) == "Test: 1234");
  10. }
  11. TEST_CASE("TestConvertToString")
  12. {
  13. CHECK(ConvertToString(1234) == "1234");
  14. CHECK(ConvertToString(-1) == "-1");
  15. CHECK(ConvertToString(0x7fffffffffffffffUL) == "9223372036854775807");
  16. }
  17. TEST_CASE("TestStringHash")
  18. {
  19. CHECK(HashString("This is a test") == 2733878766136413408UL);
  20. }
  21. TEST_CASE("StringReplace")
  22. {
  23. JPH::String value = "Hello this si si a test";
  24. StringReplace(value, "si", "is");
  25. CHECK(value == "Hello this is is a test");
  26. StringReplace(value, "is is", "is");
  27. CHECK(value == "Hello this is a test");
  28. StringReplace(value, "Hello", "Bye");
  29. CHECK(value == "Bye this is a test");
  30. StringReplace(value, "a test", "complete");
  31. CHECK(value == "Bye this is complete");
  32. }
  33. TEST_CASE("StringToVector")
  34. {
  35. Array<JPH::String> value;
  36. StringToVector("", value);
  37. CHECK(value.empty());
  38. StringToVector("a,b,c", value);
  39. CHECK(value[0] == "a");
  40. CHECK(value[1] == "b");
  41. CHECK(value[2] == "c");
  42. StringToVector("a,.b,.c,", value, ".");
  43. CHECK(value[0] == "a,");
  44. CHECK(value[1] == "b,");
  45. CHECK(value[2] == "c,");
  46. }
  47. TEST_CASE("VectorToString")
  48. {
  49. Array<JPH::String> input;
  50. JPH::String value;
  51. VectorToString(input, value);
  52. CHECK(value.empty());
  53. input = { "a", "b", "c" };
  54. VectorToString(input, value);
  55. CHECK(value == "a,b,c");
  56. VectorToString(input, value, ", ");
  57. CHECK(value == "a, b, c");
  58. }
  59. TEST_CASE("ToLower")
  60. {
  61. CHECK(ToLower("123 HeLlO!") == "123 hello!");
  62. }
  63. TEST_CASE("NibbleToBinary")
  64. {
  65. CHECK(strcmp(NibbleToBinary(0b0000), "0000") == 0);
  66. CHECK(strcmp(NibbleToBinary(0b0001), "0001") == 0);
  67. CHECK(strcmp(NibbleToBinary(0b0010), "0010") == 0);
  68. CHECK(strcmp(NibbleToBinary(0b0011), "0011") == 0);
  69. CHECK(strcmp(NibbleToBinary(0b0100), "0100") == 0);
  70. CHECK(strcmp(NibbleToBinary(0b0101), "0101") == 0);
  71. CHECK(strcmp(NibbleToBinary(0b0110), "0110") == 0);
  72. CHECK(strcmp(NibbleToBinary(0b0111), "0111") == 0);
  73. CHECK(strcmp(NibbleToBinary(0b1000), "1000") == 0);
  74. CHECK(strcmp(NibbleToBinary(0b1001), "1001") == 0);
  75. CHECK(strcmp(NibbleToBinary(0b1010), "1010") == 0);
  76. CHECK(strcmp(NibbleToBinary(0b1011), "1011") == 0);
  77. CHECK(strcmp(NibbleToBinary(0b1100), "1100") == 0);
  78. CHECK(strcmp(NibbleToBinary(0b1101), "1101") == 0);
  79. CHECK(strcmp(NibbleToBinary(0b1110), "1110") == 0);
  80. CHECK(strcmp(NibbleToBinary(0b1111), "1111") == 0);
  81. CHECK(strcmp(NibbleToBinary(0xfffffff0), "0000") == 0);
  82. }
  83. }