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.
 
 
 

200 lines
4.8 KiB

  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 "core.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(StringPrintf)
  103. {
  104. String s1 = "3a";
  105. String s2 = String::Printf("%d%x", 3, 10);
  106. LOLUNIT_ASSERT(s1 == s2);
  107. String s3 = "abc 3";
  108. String s4 = String::Printf("abc %d", 3);
  109. LOLUNIT_ASSERT(s3 == s4);
  110. }
  111. LOLUNIT_TEST(SubString)
  112. {
  113. String s1 = "Hello World";
  114. String s2 = s1.Sub(0, 5);
  115. String s3 = "Hello";
  116. LOLUNIT_ASSERT(s2 == s3);
  117. String s4 = s1.Sub(6, 5);
  118. String s5 = "World";
  119. LOLUNIT_ASSERT(s4 == s5);
  120. }
  121. LOLUNIT_TEST(IndexOf)
  122. {
  123. String s1 = "Hello World";
  124. int i1 = s1.IndexOf('H');
  125. int i2 = s1.IndexOf('W');
  126. int i3 = s1.IndexOf('d');
  127. int i4 = s1.IndexOf("Hello");
  128. int i5 = s1.IndexOf("World");
  129. int i6 = s1.IndexOf("lo");
  130. int i7 = s1.IndexOf("Hello World");
  131. int i8 = s1.IndexOf("Sup' dude");
  132. LOLUNIT_ASSERT(i1 == 0);
  133. LOLUNIT_ASSERT(i2 == 6);
  134. LOLUNIT_ASSERT(i3 == 10);
  135. LOLUNIT_ASSERT(i4 == i1);
  136. LOLUNIT_ASSERT(i5 == i2);
  137. LOLUNIT_ASSERT(i6 == 3);
  138. LOLUNIT_ASSERT(i7 == 0);
  139. LOLUNIT_ASSERT(i8 == -1);
  140. }
  141. LOLUNIT_TEST(LastIndexOf)
  142. {
  143. String s1 = "Hello World";
  144. int i1 = s1.LastIndexOf('H');
  145. int i2 = s1.LastIndexOf('W');
  146. int i3 = s1.LastIndexOf('d');
  147. int i4 = s1.LastIndexOf("Hello");
  148. int i5 = s1.LastIndexOf("World");
  149. int i6 = s1.LastIndexOf("lo");
  150. int i7 = s1.LastIndexOf("Hello World");
  151. int i8 = s1.LastIndexOf("Sup' dude");
  152. int i9 = s1.LastIndexOf('l');
  153. LOLUNIT_ASSERT(i1 == 0);
  154. LOLUNIT_ASSERT(i2 == 6);
  155. LOLUNIT_ASSERT(i3 == 10);
  156. LOLUNIT_ASSERT(i4 == i1);
  157. LOLUNIT_ASSERT(i5 == i2);
  158. LOLUNIT_ASSERT(i6 == 3);
  159. LOLUNIT_ASSERT(i7 == 0);
  160. LOLUNIT_ASSERT(i8 == -1);
  161. LOLUNIT_ASSERT(i9 == 9);
  162. }};
  163. } /* namespace lol */