StringTest.cpp 5.2 KB

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