String.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (C) 2009-2016, 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. //==============================================================================
  11. ANKI_TEST(Util, String)
  12. {
  13. HeapAllocator<U8> alloc(allocAligned, nullptr);
  14. // Copy
  15. {
  16. String a, b;
  17. a.create(alloc, "123");
  18. b.create(alloc, a);
  19. ANKI_TEST_EXPECT_EQ(a, b);
  20. ANKI_TEST_EXPECT_EQ(b, "123");
  21. b.destroy(alloc);
  22. a.destroy(alloc);
  23. b.create(alloc, "321");
  24. a.create(alloc, b);
  25. ANKI_TEST_EXPECT_EQ(a, b);
  26. ANKI_TEST_EXPECT_EQ(a, "321");
  27. b.destroy(alloc);
  28. a.destroy(alloc);
  29. }
  30. // Move
  31. {
  32. String a;
  33. a.create(alloc, "123");
  34. String b(std::move(a));
  35. ANKI_TEST_EXPECT_EQ(a.isEmpty(), true);
  36. ANKI_TEST_EXPECT_EQ(b, "123");
  37. b.destroy(alloc);
  38. b.create(alloc, "321");
  39. a = std::move(b);
  40. ANKI_TEST_EXPECT_EQ(a, "321");
  41. ANKI_TEST_EXPECT_EQ(b.isEmpty(), true);
  42. a.destroy(alloc);
  43. }
  44. // Accessors
  45. {
  46. const char* s = "123";
  47. String a;
  48. a.create(alloc, s);
  49. ANKI_TEST_EXPECT_EQ(a[0], '1');
  50. ANKI_TEST_EXPECT_EQ(a[1], '2');
  51. ANKI_TEST_EXPECT_EQ(a[2], '3');
  52. U count = 0;
  53. for(char& c : a)
  54. {
  55. ++c;
  56. ++count;
  57. }
  58. ANKI_TEST_EXPECT_EQ(a, "234");
  59. ANKI_TEST_EXPECT_EQ(count, 3);
  60. ANKI_TEST_EXPECT_EQ(a.begin(), &a[0]);
  61. ANKI_TEST_EXPECT_EQ(a.end(), &a[0] + 3);
  62. a.destroy(alloc);
  63. }
  64. // Append
  65. {
  66. String a, b;
  67. b.create(alloc, "123");
  68. a.append(alloc, b);
  69. ANKI_TEST_EXPECT_EQ(a, "123");
  70. a.append(alloc, "456789");
  71. a.append(alloc, String());
  72. a.append(alloc, "");
  73. a.append(alloc, "0");
  74. ANKI_TEST_EXPECT_EQ(a, "1234567890");
  75. a.destroy(alloc);
  76. b.destroy(alloc);
  77. }
  78. // Compare
  79. {
  80. #define COMPARE(x_, y_, op_) \
  81. a.append(alloc, x_); \
  82. b.append(alloc, y_); \
  83. ANKI_TEST_EXPECT_EQ(a op_ b, 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. 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. a.sprintf(alloc,
  103. "%s%s%s%s%s%s%s%s%s%s%s %d",
  104. s,
  105. s,
  106. s,
  107. s,
  108. s,
  109. s,
  110. s,
  111. s,
  112. s,
  113. s,
  114. s,
  115. 88);
  116. String b;
  117. for(U i = 0; i < 11; i++)
  118. {
  119. b.append(alloc, s);
  120. }
  121. b.append(alloc, " 88");
  122. ANKI_TEST_EXPECT_EQ(a, b);
  123. ANKI_TEST_EXPECT_EQ(a.getLength(), b.getLength());
  124. a.destroy(alloc);
  125. b.destroy(alloc);
  126. }
  127. // sprintf #2: Smaller result (will trigger another path)
  128. {
  129. String a;
  130. // Simple
  131. a.sprintf(alloc, "12%c %d", '3', 123);
  132. ANKI_TEST_EXPECT_EQ(a, "123 123");
  133. a.destroy(alloc);
  134. // Extreme
  135. const char* s = "12345";
  136. a.sprintf(alloc, "%s%s %d", s, s, 88);
  137. String b;
  138. for(U i = 0; i < 2; i++)
  139. {
  140. b.append(alloc, s);
  141. }
  142. b.append(alloc, " 88");
  143. ANKI_TEST_EXPECT_EQ(a, b);
  144. ANKI_TEST_EXPECT_EQ(a.getLength(), b.getLength());
  145. a.destroy(alloc);
  146. b.destroy(alloc);
  147. }
  148. // Other create
  149. {
  150. String a;
  151. a.create(alloc, '1', 3);
  152. ANKI_TEST_EXPECT_EQ(a, "111");
  153. ANKI_TEST_EXPECT_EQ(a.getLength(), 3);
  154. a.destroy(alloc);
  155. }
  156. // toString
  157. {
  158. String a;
  159. a.toString(alloc, 123);
  160. ANKI_TEST_EXPECT_EQ(a, "123");
  161. a.destroy(alloc);
  162. a.toString(alloc, 123.123);
  163. ANKI_TEST_EXPECT_EQ(a, "123.123000");
  164. a.destroy(alloc);
  165. }
  166. // To number
  167. {
  168. I64 i;
  169. String a;
  170. a.create(alloc, "123456789");
  171. ANKI_TEST_EXPECT_NO_ERR(a.toI64(i));
  172. ANKI_TEST_EXPECT_EQ(i, 123456789);
  173. a.destroy(alloc);
  174. a.create(alloc, "-9223372036854775807");
  175. ANKI_TEST_EXPECT_NO_ERR(a.toI64(i));
  176. ANKI_TEST_EXPECT_EQ(i, -9223372036854775807);
  177. a.destroy(alloc);
  178. F64 f;
  179. a.create(alloc, "123456789.145");
  180. ANKI_TEST_EXPECT_NO_ERR(a.toF64(f));
  181. ANKI_TEST_EXPECT_EQ(f, 123456789.145);
  182. a.destroy(alloc);
  183. }
  184. }
  185. } // end namespace anki