Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

140 rader
3.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL 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[0], 'a');
  73. LOLUNIT_ASSERT_EQUAL(s1[1], 'b');
  74. LOLUNIT_ASSERT_EQUAL(s1[2], 'c');
  75. LOLUNIT_ASSERT_EQUAL(s1[3], 'd');
  76. LOLUNIT_ASSERT_EQUAL(s1[4], '\0');
  77. s2 += s2;
  78. LOLUNIT_ASSERT_EQUAL(s2[0], 'c');
  79. LOLUNIT_ASSERT_EQUAL(s2[1], 'd');
  80. LOLUNIT_ASSERT_EQUAL(s2[2], 'c');
  81. LOLUNIT_ASSERT_EQUAL(s2[3], 'd');
  82. LOLUNIT_ASSERT_EQUAL(s2[4], '\0');
  83. }
  84. LOLUNIT_TEST(StringEqual)
  85. {
  86. String s1("abc");
  87. String s2("abc");
  88. String s3("ab");
  89. LOLUNIT_ASSERT(s1 == s2);
  90. LOLUNIT_ASSERT(!(s1 == s3));
  91. }
  92. LOLUNIT_TEST(StringDifferent)
  93. {
  94. String s1("abc");
  95. String s2("ab");
  96. String s3("abc");
  97. LOLUNIT_ASSERT(s1 != s2);
  98. LOLUNIT_ASSERT(!(s1 != s3));
  99. }
  100. LOLUNIT_TEST(StringPrintf)
  101. {
  102. String s1 = "3a";
  103. String s2 = String::Printf("%d%x", 3, 10);
  104. LOLUNIT_ASSERT(s1 == s2);
  105. String s3 = "abc 3";
  106. String s4 = String::Printf("abc %d", 3);
  107. LOLUNIT_ASSERT(s3 == s4);
  108. }
  109. };
  110. } /* namespace lol */