StringToolsTest.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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("StringReplace")
  19. {
  20. JPH::String value = "Hello this si si a test";
  21. StringReplace(value, "si", "is");
  22. CHECK(value == "Hello this is is a test");
  23. StringReplace(value, "is is", "is");
  24. CHECK(value == "Hello this is a test");
  25. StringReplace(value, "Hello", "Bye");
  26. CHECK(value == "Bye this is a test");
  27. StringReplace(value, "a test", "complete");
  28. CHECK(value == "Bye this is complete");
  29. }
  30. TEST_CASE("StringToVector")
  31. {
  32. Array<JPH::String> value;
  33. StringToVector("", value);
  34. CHECK(value.empty());
  35. StringToVector("a,b,c", value);
  36. CHECK(value[0] == "a");
  37. CHECK(value[1] == "b");
  38. CHECK(value[2] == "c");
  39. StringToVector("a,.b,.c,", value, ".");
  40. CHECK(value[0] == "a,");
  41. CHECK(value[1] == "b,");
  42. CHECK(value[2] == "c,");
  43. }
  44. TEST_CASE("VectorToString")
  45. {
  46. Array<JPH::String> input;
  47. JPH::String value;
  48. VectorToString(input, value);
  49. CHECK(value.empty());
  50. input = { "a", "b", "c" };
  51. VectorToString(input, value);
  52. CHECK(value == "a,b,c");
  53. VectorToString(input, value, ", ");
  54. CHECK(value == "a, b, c");
  55. }
  56. TEST_CASE("ToLower")
  57. {
  58. CHECK(ToLower("123 HeLlO!") == "123 hello!");
  59. }
  60. TEST_CASE("NibbleToBinary")
  61. {
  62. CHECK(strcmp(NibbleToBinary(0b0000), "0000") == 0);
  63. CHECK(strcmp(NibbleToBinary(0b0001), "0001") == 0);
  64. CHECK(strcmp(NibbleToBinary(0b0010), "0010") == 0);
  65. CHECK(strcmp(NibbleToBinary(0b0011), "0011") == 0);
  66. CHECK(strcmp(NibbleToBinary(0b0100), "0100") == 0);
  67. CHECK(strcmp(NibbleToBinary(0b0101), "0101") == 0);
  68. CHECK(strcmp(NibbleToBinary(0b0110), "0110") == 0);
  69. CHECK(strcmp(NibbleToBinary(0b0111), "0111") == 0);
  70. CHECK(strcmp(NibbleToBinary(0b1000), "1000") == 0);
  71. CHECK(strcmp(NibbleToBinary(0b1001), "1001") == 0);
  72. CHECK(strcmp(NibbleToBinary(0b1010), "1010") == 0);
  73. CHECK(strcmp(NibbleToBinary(0b1011), "1011") == 0);
  74. CHECK(strcmp(NibbleToBinary(0b1100), "1100") == 0);
  75. CHECK(strcmp(NibbleToBinary(0b1101), "1101") == 0);
  76. CHECK(strcmp(NibbleToBinary(0b1110), "1110") == 0);
  77. CHECK(strcmp(NibbleToBinary(0b1111), "1111") == 0);
  78. CHECK(strcmp(NibbleToBinary(0xfffffff0), "0000") == 0);
  79. }
  80. }