string.cpp 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. };
  122. } /* namespace lol */