StringToolsTest.cpp 2.7 KB

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