StringTest.cpp 5.2 KB

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