StringTest.cpp 5.2 KB

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