String.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "tests/framework/Framework.h"
  6. #include "anki/util/String.h"
  7. #include <string>
  8. namespace anki {
  9. //==============================================================================
  10. ANKI_TEST(Util, String)
  11. {
  12. HeapAllocator<U8> alloc(allocAligned, nullptr);
  13. // Copy
  14. {
  15. String a, b;
  16. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, "123"));
  17. ANKI_TEST_EXPECT_NO_ERR(b.create(alloc, a));
  18. ANKI_TEST_EXPECT_EQ(a, b);
  19. ANKI_TEST_EXPECT_EQ(b, "123");
  20. b.destroy(alloc);
  21. a.destroy(alloc);
  22. ANKI_TEST_EXPECT_NO_ERR(b.create(alloc, "321"));
  23. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, b));
  24. ANKI_TEST_EXPECT_EQ(a, b);
  25. ANKI_TEST_EXPECT_EQ(a, "321");
  26. b.destroy(alloc);
  27. a.destroy(alloc);
  28. }
  29. // Move
  30. {
  31. String a;
  32. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, "123"));
  33. String b(std::move(a));
  34. ANKI_TEST_EXPECT_EQ(a.isEmpty(), true);
  35. ANKI_TEST_EXPECT_EQ(b, "123");
  36. b.destroy(alloc);
  37. ANKI_TEST_EXPECT_NO_ERR(b.create(alloc, "321"));
  38. a = std::move(b);
  39. ANKI_TEST_EXPECT_EQ(a, "321");
  40. ANKI_TEST_EXPECT_EQ(b.isEmpty(), true);
  41. a.destroy(alloc);
  42. }
  43. // Accessors
  44. {
  45. const char* s = "123";
  46. String a;
  47. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, s));
  48. ANKI_TEST_EXPECT_EQ(a[0], '1');
  49. ANKI_TEST_EXPECT_EQ(a[1], '2');
  50. ANKI_TEST_EXPECT_EQ(a[2], '3');
  51. U count = 0;
  52. for(char& c : a)
  53. {
  54. ++c;
  55. ++count;
  56. }
  57. ANKI_TEST_EXPECT_EQ(a, "234");
  58. ANKI_TEST_EXPECT_EQ(count, 3);
  59. ANKI_TEST_EXPECT_EQ(a.begin(), &a[0]);
  60. ANKI_TEST_EXPECT_EQ(a.end(), &a[0] + 3);
  61. a.destroy(alloc);
  62. }
  63. // Append
  64. {
  65. String a, b;
  66. ANKI_TEST_EXPECT_NO_ERR(b.create(alloc, "123"));
  67. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, b));
  68. ANKI_TEST_EXPECT_EQ(a, "123");
  69. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, "456789"));
  70. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, String()));
  71. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, ""));
  72. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, "0"));
  73. ANKI_TEST_EXPECT_EQ(a, "1234567890");
  74. a.destroy(alloc);
  75. b.destroy(alloc);
  76. }
  77. // Compare
  78. {
  79. #define COMPARE(x_, y_, op_) \
  80. ANKI_TEST_EXPECT_NO_ERR(a.append(alloc, x_)); \
  81. ANKI_TEST_EXPECT_NO_ERR(b.append(alloc, y_)); \
  82. ANKI_TEST_EXPECT_EQ(a op_ b, \
  83. std::string(x_) op_ std::string(y_)) \
  84. a.destroy(alloc); \
  85. b.destroy(alloc);
  86. String a, b;
  87. COMPARE("123", "1233", <);
  88. COMPARE("0123", "1233", <=);
  89. COMPARE("ASDFA", "asdf90f", >);
  90. COMPARE(" %^*^^&", "aslkdfjb", >=);
  91. #undef COMPARE
  92. }
  93. // sprintf
  94. {
  95. String a;
  96. // Simple
  97. ANKI_TEST_EXPECT_NO_ERR(a.sprintf(alloc, "12%c %d", '3', 123));
  98. ANKI_TEST_EXPECT_EQ(a, "123 123");
  99. a.destroy(alloc);
  100. // Extreme
  101. const char* s = "1234567890ABCDEF!@#$%^&*()_+asfghjkl:,.;ljk\"><{}[]/";
  102. ANKI_TEST_EXPECT_NO_ERR(a.sprintf(alloc, "%s%s%s%s%s%s%s%s%s%s%s %d",
  103. s, s, s, s, s, s, s, s, s, s, s, 88));
  104. String b;
  105. for(U i = 0; i < 11; i++)
  106. {
  107. ANKI_TEST_EXPECT_NO_ERR(b.append(alloc, s));
  108. }
  109. ANKI_TEST_EXPECT_NO_ERR(b.append(alloc, " 88"));
  110. ANKI_TEST_EXPECT_EQ(a, b);
  111. ANKI_TEST_EXPECT_EQ(a.getLength(), b.getLength());
  112. a.destroy(alloc);
  113. b.destroy(alloc);
  114. }
  115. // sprintf #2: Smaller result (will trigger another path)
  116. {
  117. String a;
  118. // Simple
  119. ANKI_TEST_EXPECT_NO_ERR(a.sprintf(alloc, "12%c %d", '3', 123));
  120. ANKI_TEST_EXPECT_EQ(a, "123 123");
  121. a.destroy(alloc);
  122. // Extreme
  123. const char* s = "12345";
  124. ANKI_TEST_EXPECT_NO_ERR(a.sprintf(alloc, "%s%s %d", s, s, 88));
  125. String b;
  126. for(U i = 0; i < 2; i++)
  127. {
  128. ANKI_TEST_EXPECT_NO_ERR(b.append(alloc, s));
  129. }
  130. ANKI_TEST_EXPECT_NO_ERR(b.append(alloc, " 88"));
  131. ANKI_TEST_EXPECT_EQ(a, b);
  132. ANKI_TEST_EXPECT_EQ(a.getLength(), b.getLength());
  133. a.destroy(alloc);
  134. b.destroy(alloc);
  135. }
  136. // Other create
  137. {
  138. String a;
  139. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, '1', 3));
  140. ANKI_TEST_EXPECT_EQ(a, "111");
  141. ANKI_TEST_EXPECT_EQ(a.getLength(), 3);
  142. a.destroy(alloc);
  143. }
  144. // toString
  145. {
  146. String a;
  147. ANKI_TEST_EXPECT_NO_ERR(a.toString(alloc, 123));
  148. ANKI_TEST_EXPECT_EQ(a, "123");
  149. a.destroy(alloc);
  150. ANKI_TEST_EXPECT_NO_ERR(a.toString(alloc, 123.123));
  151. ANKI_TEST_EXPECT_EQ(a, "123.123000");
  152. a.destroy(alloc);
  153. }
  154. // To number
  155. {
  156. I64 i;
  157. String a;
  158. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, "123456789"));
  159. ANKI_TEST_EXPECT_NO_ERR(a.toI64(i));
  160. ANKI_TEST_EXPECT_EQ(i, 123456789);
  161. a.destroy(alloc);
  162. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, "-9223372036854775807"));
  163. ANKI_TEST_EXPECT_NO_ERR(a.toI64(i));
  164. ANKI_TEST_EXPECT_EQ(i, -9223372036854775807);
  165. a.destroy(alloc);
  166. F64 f;
  167. ANKI_TEST_EXPECT_NO_ERR(a.create(alloc, "123456789.145"));
  168. ANKI_TEST_EXPECT_NO_ERR(a.toF64(f));
  169. ANKI_TEST_EXPECT_EQ(f, 123456789.145);
  170. a.destroy(alloc);
  171. }
  172. }
  173. } // end namespace anki