StringTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-07-12
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <ls_std/ls_std.hpp>
  11. #include <TestHelper.hpp>
  12. namespace
  13. {
  14. class StringTest : public ::testing::Test
  15. {
  16. protected:
  17. StringTest() = default;
  18. ~StringTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. // assignment operators
  25. TEST_F(StringTest, operator_assignment)
  26. {
  27. ls_std::String text{};
  28. text = "Hi!";
  29. ASSERT_STREQ("Hi!", text.toString().c_str());
  30. }
  31. // arithmetic operators
  32. TEST_F(StringTest, operator_add)
  33. {
  34. ls_std::String greetings{"Hello! "};
  35. ls_std::String question{"How are you? "};
  36. const std::string& answer = "I'm good by the way!";
  37. greetings = greetings + question + answer;
  38. ASSERT_STREQ("Hello! How are you? I'm good by the way!", greetings.toString().c_str());
  39. }
  40. TEST_F(StringTest, operator_minus)
  41. {
  42. ls_std::String text{"abcdefghij"};
  43. text = text - 5;
  44. ASSERT_STREQ("abcde", text.toString().c_str());
  45. }
  46. // compound operators
  47. TEST_F(StringTest, operator_add_assign_with_reference)
  48. {
  49. ls_std::String text{};
  50. ls_std::String hello{"Hi!"};
  51. text += hello;
  52. ASSERT_STREQ("Hi!", text.toString().c_str());
  53. }
  54. TEST_F(StringTest, operator_add_assign_with_value)
  55. {
  56. ls_std::String text{};
  57. text += "Hi!";
  58. ASSERT_STREQ("Hi!", text.toString().c_str());
  59. }
  60. // comparison operators
  61. TEST_F(StringTest, operator_equals_with_reference)
  62. {
  63. ls_std::String text{"Hi!"};
  64. ls_std::String hello{"Hi!"};
  65. ASSERT_TRUE(text == hello);
  66. ASSERT_TRUE(hello == text);
  67. }
  68. TEST_F(StringTest, operator_equals_with_value)
  69. {
  70. ls_std::String hello{"Hi!"};
  71. ASSERT_TRUE(hello == "Hi!");
  72. }
  73. TEST_F(StringTest, operator_not_equals_with_reference)
  74. {
  75. ls_std::String text{"Hi!"};
  76. ls_std::String hello{"Hello!"};
  77. ASSERT_TRUE(text != hello);
  78. }
  79. TEST_F(StringTest, operator_not_equals_with_value)
  80. {
  81. ls_std::String text{"Hi!"};
  82. ASSERT_TRUE(text != "Hello!");
  83. }
  84. // implementation
  85. TEST_F(StringTest, parse)
  86. {
  87. ls_std::String text{};
  88. text.parse("Hello!");
  89. ASSERT_STREQ("Hello!", text.toString().c_str());
  90. }
  91. TEST_F(StringTest, toString)
  92. {
  93. ls_std::String text{"Hello!"};
  94. ASSERT_STREQ("Hello!", text.toString().c_str());
  95. }
  96. // additional functionality
  97. TEST_F(StringTest, contains)
  98. {
  99. ls_std::String text{};
  100. text = "Hey, I'm searching for the keyword 'cake'!";
  101. ASSERT_TRUE(text.contains("cake"));
  102. }
  103. TEST_F(StringTest, contains_does_not_contain_search_word)
  104. {
  105. ls_std::String text{};
  106. text = "Hey, I'm searching for the keyword 'cake'!";
  107. ASSERT_FALSE(text.contains("butter"));
  108. }
  109. TEST_F(StringTest, endsWith)
  110. {
  111. ls_std::String text{};
  112. text = "abcdef";
  113. ASSERT_TRUE(text.endsWith("ef"));
  114. }
  115. TEST_F(StringTest, endsWith_does_not_end_with_pattern)
  116. {
  117. ls_std::String text{};
  118. text = "abcdef";
  119. ASSERT_FALSE(text.endsWith("efg"));
  120. }
  121. TEST_F(StringTest, equalsIgnoreCase)
  122. {
  123. ls_std::String text{"Hello!"};
  124. ls_std::String hello{"HeLLo!"};
  125. ASSERT_TRUE(text.equalsIgnoreCase(hello));
  126. ASSERT_TRUE(text.equalsIgnoreCase("HeLLO!"));
  127. }
  128. TEST_F(StringTest, getByteData)
  129. {
  130. ls_std::String text{"Hallo!"};
  131. ASSERT_STREQ("Hallo!", text.getByteData().data());
  132. }
  133. TEST_F(StringTest, padLeft)
  134. {
  135. ls_std::String text{"abcdef"};
  136. ls_std::String anotherText{"ab"};
  137. ls_std::String emptyText{};
  138. ls_std::String longText{"This text is too long to fill!"};
  139. ASSERT_STREQ(" abcdef", text.padLeft(10, ' ').c_str());
  140. ASSERT_STREQ(" ab", anotherText.padLeft(10, ' ').c_str());
  141. ASSERT_STREQ(" ", emptyText.padLeft(10, ' ').c_str());
  142. ASSERT_STREQ("This text is too long to fill!", longText.padLeft(10, ' ').c_str());
  143. }
  144. TEST_F(StringTest, padRight)
  145. {
  146. ls_std::String text{"abcdef"};
  147. ls_std::String anotherText{"ab"};
  148. ls_std::String emptyText{};
  149. ls_std::String longText{"This text is too long to fill!"};
  150. ASSERT_STREQ("abcdef ", text.padRight(10, ' ').c_str());
  151. ASSERT_STREQ("ab ", anotherText.padRight(10, ' ').c_str());
  152. ASSERT_STREQ(" ", emptyText.padRight(10, ' ').c_str());
  153. ASSERT_STREQ("This text is too long to fill!", longText.padRight(10, ' ').c_str());
  154. }
  155. TEST_F(StringTest, reverse)
  156. {
  157. ls_std::String text{};
  158. text = "abcdef";
  159. ASSERT_STREQ("fedcba", text.reverse().c_str());
  160. ASSERT_STREQ("abcdef", text); // verify, that original string didn't change
  161. }
  162. TEST_F(StringTest, startsWith)
  163. {
  164. ls_std::String text{};
  165. text = "abcdef";
  166. ASSERT_TRUE(text.startsWith("abc"));
  167. }
  168. TEST_F(StringTest, startsWith_does_not_start_with_pattern)
  169. {
  170. ls_std::String text{};
  171. text = "abcdef";
  172. ASSERT_FALSE(text.startsWith("bc"));
  173. }
  174. TEST_F(StringTest, toLowerCase)
  175. {
  176. ls_std::String text{};
  177. text = "aBCdeFgHIJKLmn";
  178. ASSERT_STREQ("abcdefghijklmn", text.toLowerCase().c_str());
  179. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  180. }
  181. TEST_F(StringTest, toUpperCase)
  182. {
  183. ls_std::String text{};
  184. text = "aBCdeFgHIJKLmn";
  185. ASSERT_STREQ("ABCDEFGHIJKLMN", text.toUpperCase().c_str());
  186. ASSERT_STREQ("aBCdeFgHIJKLmn", text); // verify, that original String didn't change
  187. }
  188. }