You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string.cpp 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "lol/unit.h"
  15. namespace lol
  16. {
  17. LOLUNIT_FIXTURE(StringTest)
  18. {
  19. void SetUp() {}
  20. void TearDown() {}
  21. LOLUNIT_TEST(StringBuild)
  22. {
  23. String s1;
  24. LOLUNIT_ASSERT_EQUAL(s1.Count(), 0);
  25. LOLUNIT_ASSERT_EQUAL(s1[0], '\0');
  26. String s2("");
  27. LOLUNIT_ASSERT_EQUAL(s2.Count(), 0);
  28. LOLUNIT_ASSERT_EQUAL(s2[0], '\0');
  29. String s3("a");
  30. LOLUNIT_ASSERT_EQUAL(s3.Count(), 1);
  31. LOLUNIT_ASSERT_EQUAL(s3[0], 'a');
  32. LOLUNIT_ASSERT_EQUAL(s3[1], '\0');
  33. String s4(s3);
  34. LOLUNIT_ASSERT_EQUAL(s4.Count(), 1);
  35. LOLUNIT_ASSERT_EQUAL(s4[0], 'a');
  36. LOLUNIT_ASSERT_EQUAL(s4[1], '\0');
  37. }
  38. LOLUNIT_TEST(StringAppendChar)
  39. {
  40. String s;
  41. s += 'a';
  42. s += 'b';
  43. s += 'c';
  44. LOLUNIT_ASSERT_EQUAL(s[0], 'a');
  45. LOLUNIT_ASSERT_EQUAL(s[1], 'b');
  46. LOLUNIT_ASSERT_EQUAL(s[2], 'c');
  47. LOLUNIT_ASSERT_EQUAL(s[3], '\0');
  48. }
  49. LOLUNIT_TEST(StringCopy)
  50. {
  51. String s1 = "abc";
  52. String s2 = s1;
  53. LOLUNIT_ASSERT_EQUAL(s1[0], s2[0]);
  54. LOLUNIT_ASSERT_EQUAL(s1[1], s2[1]);
  55. LOLUNIT_ASSERT_EQUAL(s1[2], s2[2]);
  56. LOLUNIT_ASSERT_EQUAL(s1[3], s2[3]);
  57. }
  58. LOLUNIT_TEST(StringConcat)
  59. {
  60. String s1("ab"), s2("cd");
  61. String s3 = s1 + s2;
  62. LOLUNIT_ASSERT_EQUAL(s3[0], 'a');
  63. LOLUNIT_ASSERT_EQUAL(s3[1], 'b');
  64. LOLUNIT_ASSERT_EQUAL(s3[2], 'c');
  65. LOLUNIT_ASSERT_EQUAL(s3[3], 'd');
  66. LOLUNIT_ASSERT_EQUAL(s3[4], '\0');
  67. }
  68. LOLUNIT_TEST(StringAppendString)
  69. {
  70. String s1("ab"), s2("cd");
  71. s1 += s2;
  72. LOLUNIT_ASSERT_EQUAL(s1.Count(), 4);
  73. LOLUNIT_ASSERT_EQUAL(s1[0], 'a');
  74. LOLUNIT_ASSERT_EQUAL(s1[1], 'b');
  75. LOLUNIT_ASSERT_EQUAL(s1[2], 'c');
  76. LOLUNIT_ASSERT_EQUAL(s1[3], 'd');
  77. LOLUNIT_ASSERT_EQUAL(s1[4], '\0');
  78. s2 += s2;
  79. LOLUNIT_ASSERT_EQUAL(s2.Count(), 4);
  80. LOLUNIT_ASSERT_EQUAL(s2[0], 'c');
  81. LOLUNIT_ASSERT_EQUAL(s2[1], 'd');
  82. LOLUNIT_ASSERT_EQUAL(s2[2], 'c');
  83. LOLUNIT_ASSERT_EQUAL(s2[3], 'd');
  84. LOLUNIT_ASSERT_EQUAL(s2[4], '\0');
  85. }
  86. LOLUNIT_TEST(StringEqual)
  87. {
  88. String s1("abc");
  89. String s2("abc");
  90. String s3("ab");
  91. LOLUNIT_ASSERT(s1 == s2);
  92. LOLUNIT_ASSERT(!(s1 == s3));
  93. }
  94. LOLUNIT_TEST(StringDifferent)
  95. {
  96. String s1("abc");
  97. String s2("ab");
  98. String s3("abc");
  99. LOLUNIT_ASSERT(s1 != s2);
  100. LOLUNIT_ASSERT(!(s1 != s3));
  101. }
  102. LOLUNIT_TEST(StringCharsEqual)
  103. {
  104. char const* sz = "abc";
  105. String s1("abc");
  106. String s2("ab");
  107. LOLUNIT_ASSERT(s1 == sz);
  108. LOLUNIT_ASSERT(sz == s1);
  109. LOLUNIT_ASSERT(!(s2 == sz));
  110. LOLUNIT_ASSERT(!(sz == s2));
  111. }
  112. LOLUNIT_TEST(StringCharsDifferent)
  113. {
  114. char const* sz = "abc";
  115. String s1("ab");
  116. String s2("abc");
  117. LOLUNIT_ASSERT(s1 != sz);
  118. LOLUNIT_ASSERT(sz != s1);
  119. LOLUNIT_ASSERT(!(s2 != sz));
  120. LOLUNIT_ASSERT(!(sz != s2));
  121. }
  122. LOLUNIT_TEST(StringPrintf)
  123. {
  124. String s1 = "3a";
  125. String s2 = String::Printf("%d%x", 3, 10);
  126. LOLUNIT_ASSERT(s1 == s2);
  127. String s3 = "abc 3";
  128. String s4 = String::Printf("abc %d", 3);
  129. LOLUNIT_ASSERT(s3 == s4);
  130. }
  131. LOLUNIT_TEST(SubString)
  132. {
  133. String s1 = "Hello World";
  134. String s2 = s1.Sub(0, 5);
  135. String s3 = "Hello";
  136. LOLUNIT_ASSERT(s2 == s3);
  137. String s4 = s1.Sub(6, 5);
  138. String s5 = "World";
  139. LOLUNIT_ASSERT(s4 == s5);
  140. }
  141. LOLUNIT_TEST(IndexOf)
  142. {
  143. String s1 = "Hello World";
  144. ptrdiff_t i1 = s1.IndexOf('H');
  145. ptrdiff_t i2 = s1.IndexOf('W');
  146. ptrdiff_t i3 = s1.IndexOf('d');
  147. ptrdiff_t i4 = s1.IndexOf("Hello");
  148. ptrdiff_t i5 = s1.IndexOf("World");
  149. ptrdiff_t i6 = s1.IndexOf("lo");
  150. ptrdiff_t i7 = s1.IndexOf("Hello World");
  151. ptrdiff_t i8 = s1.IndexOf("Sup' dude");
  152. LOLUNIT_ASSERT(i1 == 0);
  153. LOLUNIT_ASSERT(i2 == 6);
  154. LOLUNIT_ASSERT(i3 == 10);
  155. LOLUNIT_ASSERT(i4 == i1);
  156. LOLUNIT_ASSERT(i5 == i2);
  157. LOLUNIT_ASSERT(i6 == 3);
  158. LOLUNIT_ASSERT(i7 == 0);
  159. LOLUNIT_ASSERT(i8 == -1);
  160. }
  161. LOLUNIT_TEST(LastIndexOf)
  162. {
  163. String s1 = "Hello World";
  164. ptrdiff_t i1 = s1.LastIndexOf('H');
  165. ptrdiff_t i2 = s1.LastIndexOf('W');
  166. ptrdiff_t i3 = s1.LastIndexOf('d');
  167. ptrdiff_t i4 = s1.LastIndexOf("Hello");
  168. ptrdiff_t i5 = s1.LastIndexOf("World");
  169. ptrdiff_t i6 = s1.LastIndexOf("lo");
  170. ptrdiff_t i7 = s1.LastIndexOf("Hello World");
  171. ptrdiff_t i8 = s1.LastIndexOf("Sup' dude");
  172. ptrdiff_t i9 = s1.LastIndexOf('l');
  173. LOLUNIT_ASSERT(i1 == 0);
  174. LOLUNIT_ASSERT(i2 == 6);
  175. LOLUNIT_ASSERT(i3 == 10);
  176. LOLUNIT_ASSERT(i4 == i1);
  177. LOLUNIT_ASSERT(i5 == i2);
  178. LOLUNIT_ASSERT(i6 == 3);
  179. LOLUNIT_ASSERT(i7 == 0);
  180. LOLUNIT_ASSERT(i8 == -1);
  181. LOLUNIT_ASSERT(i9 == 9);
  182. }
  183. LOLUNIT_TEST(StartsEndsWith)
  184. {
  185. String s = "lolilol";
  186. LOLUNIT_ASSERT(s.StartsWith("loli"));
  187. LOLUNIT_ASSERT(!s.StartsWith("lolo"));
  188. LOLUNIT_ASSERT(!s.StartsWith("lolilolilol"));
  189. LOLUNIT_ASSERT(s.EndsWith("ilol"));
  190. LOLUNIT_ASSERT(!s.EndsWith("olol"));
  191. LOLUNIT_ASSERT(!s.EndsWith("lolilolilol"));
  192. }
  193. };
  194. } /* namespace lol */