StringTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. 
  2. #include "../testTools.h"
  3. // TODO: Cannot use casting to parent type at the same time as automatic conversion from const char*
  4. // Cover everything using a single dsr::String type?
  5. // Use "" operand as only constructor?
  6. void fooInPlace(dsr::String& target, const dsr::ReadableString& a, const dsr::ReadableString& b) {
  7. string_clear(target);
  8. string_append(target, U"Foo(");
  9. string_append(target, a);
  10. string_appendChar(target, U',');
  11. string_append(target, b);
  12. string_appendChar(target, U')');
  13. }
  14. dsr::String foo(const dsr::ReadableString& a, const dsr::ReadableString& b) {
  15. dsr::String result;
  16. string_reserve(result, string_length(a) + string_length(b) + 6);
  17. fooInPlace(result, a, b);
  18. return result;
  19. }
  20. START_TEST(String)
  21. { // Length
  22. ASSERT_EQUAL(string_length(dsr::String()), 0);
  23. ASSERT_EQUAL(string_length(U""), 0);
  24. ASSERT_EQUAL(string_length(U"a"), 1);
  25. ASSERT_EQUAL(string_length(U"ab"), 2);
  26. ASSERT_EQUAL(string_length(U"abc"), 3);
  27. ASSERT_EQUAL(string_length(U"0123456789"), 10);
  28. }
  29. { // Reading characters
  30. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[0], U'A');
  31. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[1], U'B');
  32. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[2], U'C');
  33. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[3], U'\0');
  34. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[10], U'\0');
  35. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[1000000], U'\0');
  36. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[-1], U'\0');
  37. ASSERT_EQUAL(dsr::ReadableString(U"ABC")[-1000000], U'\0');
  38. ASSERT_EQUAL(dsr::ReadableString(U"")[-1], U'\0');
  39. ASSERT_EQUAL(dsr::ReadableString(U"")[0], U'\0');
  40. ASSERT_EQUAL(dsr::ReadableString(U"")[1], U'\0');
  41. }
  42. { // Comparison
  43. dsr::ReadableString litA = U"Testing \u0444";
  44. dsr::ReadableString litB = U"Testing ф";
  45. ASSERT(string_match(litA, litB));
  46. ASSERT(!string_match(litA, U"wrong"));
  47. ASSERT(!string_match(U"wrong", litB));
  48. ASSERT(dsr::string_caseInsensitiveMatch(U"abc 123!", U"ABC 123!"));
  49. ASSERT(!dsr::string_caseInsensitiveMatch(U"abc 123!", U"ABD 123!"));
  50. ASSERT(dsr::string_match(U"aBc 123!", U"aBc 123!"));
  51. ASSERT(!dsr::string_match(U"abc 123!", U"ABC 123!"));
  52. }
  53. { // Concatenation
  54. dsr::String ab = dsr::string_combine(U"a", U"b");
  55. ASSERT_EQUAL(ab, U"ab");
  56. dsr::String cd = dsr::string_combine(U"c", U"d");
  57. ASSERT_EQUAL(cd, U"cd");
  58. cd = dsr::string_combine(U"c", U"d");
  59. ASSERT_EQUAL(cd, U"cd");
  60. auto abcd = ab + cd;
  61. ASSERT_EQUAL(abcd, U"abcd");
  62. ASSERT_EQUAL(dsr::string_combine(U"a", U"b", U"c", "d"), U"abcd");
  63. }
  64. { // Sub-strings
  65. dsr::ReadableString abcd = U"abcd";
  66. dsr::String efgh = U"efgh";
  67. ASSERT_EQUAL(dsr::string_inclusiveRange(abcd, 0, 3), U"abcd");
  68. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, 1, 2), U"b");
  69. ASSERT_EQUAL(dsr::string_inclusiveRange(efgh, 2, 3), U"gh");
  70. ASSERT_EQUAL(dsr::string_exclusiveRange(efgh, 3, 4), U"h");
  71. ASSERT_EQUAL(dsr::string_combine(string_from(abcd, 2), string_before(efgh, 2)), U"cdef");
  72. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, 0, 0), U""); // No size returns nothing
  73. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, -670214452, 2), U"ab"); // Reading out of bound is clamped
  74. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, 2, 985034841), U"cd"); // Reading out of bound is clamped
  75. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, 4, 764), U""); // Completely ous of bound returns nothing
  76. ASSERT_EQUAL(dsr::string_exclusiveRange(abcd, -631, 0), U""); // Completely ous of bound returns nothing
  77. }
  78. { // Processing
  79. dsr::String buffer = U"Garbage";
  80. ASSERT_EQUAL(buffer, U"Garbage");
  81. buffer = foo(U"Ball", U"åäöÅÄÖ");
  82. ASSERT_EQUAL(buffer, U"Foo(Ball,åäöÅÄÖ)");
  83. fooInPlace(buffer, U"Å", U"ф");
  84. ASSERT_EQUAL(buffer, U"Foo(Å,ф)");
  85. }
  86. { // Numbers
  87. uint32_t x = 0;
  88. int32_t y = -123456;
  89. int64_t z = 100200300400500600ULL;
  90. dsr::String values = dsr::string_combine(U"x = ", x, U", y = ", y, U", z = ", z);
  91. ASSERT_EQUAL(values, U"x = 0, y = -123456, z = 100200300400500600");
  92. }
  93. { // Identifying numbers
  94. ASSERT_EQUAL(dsr::character_isDigit(U'0' - 1), false);
  95. ASSERT_EQUAL(dsr::character_isDigit(U'0'), true);
  96. ASSERT_EQUAL(dsr::character_isDigit(U'1'), true);
  97. ASSERT_EQUAL(dsr::character_isDigit(U'2'), true);
  98. ASSERT_EQUAL(dsr::character_isDigit(U'3'), true);
  99. ASSERT_EQUAL(dsr::character_isDigit(U'4'), true);
  100. ASSERT_EQUAL(dsr::character_isDigit(U'5'), true);
  101. ASSERT_EQUAL(dsr::character_isDigit(U'6'), true);
  102. ASSERT_EQUAL(dsr::character_isDigit(U'7'), true);
  103. ASSERT_EQUAL(dsr::character_isDigit(U'8'), true);
  104. ASSERT_EQUAL(dsr::character_isDigit(U'9'), true);
  105. ASSERT_EQUAL(dsr::character_isDigit(U'9' + 1), false);
  106. ASSERT_EQUAL(dsr::character_isDigit(U'a'), false);
  107. ASSERT_EQUAL(dsr::character_isDigit(U' '), false);
  108. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'-'), true);
  109. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'0' - 1), false);
  110. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'0'), true);
  111. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'9'), true);
  112. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'9' + 1), false);
  113. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U'a'), false);
  114. ASSERT_EQUAL(dsr::character_isIntegerCharacter(U' '), false);
  115. ASSERT_EQUAL(dsr::character_isValueCharacter(U'-'), true);
  116. ASSERT_EQUAL(dsr::character_isValueCharacter(U'.'), true);
  117. ASSERT_EQUAL(dsr::character_isValueCharacter(U'0' - 1), false);
  118. ASSERT_EQUAL(dsr::character_isValueCharacter(U'0'), true);
  119. ASSERT_EQUAL(dsr::character_isValueCharacter(U'9'), true);
  120. ASSERT_EQUAL(dsr::character_isValueCharacter(U'9' + 1), false);
  121. ASSERT_EQUAL(dsr::character_isValueCharacter(U'a'), false);
  122. ASSERT_EQUAL(dsr::character_isValueCharacter(U' '), false);
  123. ASSERT_EQUAL(dsr::character_isWhiteSpace(U' '), true);
  124. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'\t'), true);
  125. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'\r'), true);
  126. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'\0'), false);
  127. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'a'), false);
  128. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'1'), false);
  129. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'('), false);
  130. ASSERT_EQUAL(dsr::character_isWhiteSpace(U')'), false);
  131. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'.'), false);
  132. ASSERT_EQUAL(dsr::character_isWhiteSpace(U','), false);
  133. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'-'), false);
  134. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'_'), false);
  135. ASSERT_EQUAL(dsr::character_isWhiteSpace(U'|'), false);
  136. ASSERT_EQUAL(dsr::string_isInteger(U"0"), true);
  137. ASSERT_EQUAL(dsr::string_isInteger(U"1"), true);
  138. ASSERT_EQUAL(dsr::string_isInteger(U"-0"), true);
  139. ASSERT_EQUAL(dsr::string_isInteger(U"-1"), true);
  140. ASSERT_EQUAL(dsr::string_isInteger(U"0", false), true);
  141. ASSERT_EQUAL(dsr::string_isInteger(U" 0 "), true);
  142. ASSERT_EQUAL(dsr::string_isInteger(U" 0 ", false), false);
  143. ASSERT_EQUAL(dsr::string_isInteger(U" 123"), true);
  144. ASSERT_EQUAL(dsr::string_isInteger(U"-123"), true);
  145. ASSERT_EQUAL(dsr::string_isInteger(U""), false);
  146. ASSERT_EQUAL(dsr::string_isInteger(U"85x"), false);
  147. ASSERT_EQUAL(dsr::string_isInteger(U"F15"), false);
  148. ASSERT_EQUAL(dsr::string_isInteger(U" 14"), true);
  149. ASSERT_EQUAL(dsr::string_isInteger(U"8 "), true);
  150. ASSERT_EQUAL(dsr::string_isInteger(U" 100"), true);
  151. ASSERT_EQUAL(dsr::string_isInteger(U"100 "), true);
  152. ASSERT_EQUAL(dsr::string_isInteger(U"10 10"), false);
  153. ASSERT_EQUAL(dsr::string_isInteger(U"10 10"), false);
  154. ASSERT_EQUAL(dsr::string_isInteger(U" 10 10 "), false);
  155. ASSERT_EQUAL(dsr::string_isDouble(U"0"), true);
  156. ASSERT_EQUAL(dsr::string_isDouble(U"-0"), true);
  157. ASSERT_EQUAL(dsr::string_isDouble(U"1"), true);
  158. ASSERT_EQUAL(dsr::string_isDouble(U"-1"), true);
  159. ASSERT_EQUAL(dsr::string_isDouble(U"1.1"), true);
  160. ASSERT_EQUAL(dsr::string_isDouble(U"-1.1"), true);
  161. ASSERT_EQUAL(dsr::string_isDouble(U".1"), true);
  162. ASSERT_EQUAL(dsr::string_isDouble(U"-.1"), true);
  163. ASSERT_EQUAL(dsr::string_isDouble(U"0", false), true);
  164. ASSERT_EQUAL(dsr::string_isDouble(U" 0 "), true);
  165. ASSERT_EQUAL(dsr::string_isDouble(U" 0 ", false), false);
  166. ASSERT_EQUAL(dsr::string_isDouble(U" 123"), true);
  167. ASSERT_EQUAL(dsr::string_isDouble(U"-123"), true);
  168. ASSERT_EQUAL(dsr::string_isDouble(U"0.5"), true);
  169. ASSERT_EQUAL(dsr::string_isDouble(U"-0.5"), true);
  170. ASSERT_EQUAL(dsr::string_isDouble(U".5"), true);
  171. ASSERT_EQUAL(dsr::string_isDouble(U"-.5"), true);
  172. ASSERT_EQUAL(dsr::string_isDouble(U"0.54321"), true);
  173. ASSERT_EQUAL(dsr::string_isDouble(U"-0.54321"), true);
  174. ASSERT_EQUAL(dsr::string_isDouble(U""), false);
  175. ASSERT_EQUAL(dsr::string_isDouble(U"0..0"), false);
  176. ASSERT_EQUAL(dsr::string_isDouble(U"M0.0"), false);
  177. ASSERT_EQUAL(dsr::string_isDouble(U"0.0x"), false);
  178. ASSERT_EQUAL(dsr::string_isDouble(U"T0.0q"), false);
  179. }
  180. // Upper case
  181. ASSERT_EQUAL(dsr::string_upperCase(U"a"), U"A");
  182. ASSERT_EQUAL(dsr::string_upperCase(U"aB"), U"AB");
  183. ASSERT_EQUAL(dsr::string_upperCase(U"abc"), U"ABC");
  184. ASSERT_EQUAL(dsr::string_upperCase(U"abc1"), U"ABC1");
  185. ASSERT_EQUAL(dsr::string_upperCase(U"Abc12"), U"ABC12");
  186. ASSERT_EQUAL(dsr::string_upperCase(U"ABC123"), U"ABC123");
  187. // Lower case
  188. ASSERT_EQUAL(dsr::string_lowerCase(U"a"), U"a");
  189. ASSERT_EQUAL(dsr::string_lowerCase(U"aB"), U"ab");
  190. ASSERT_EQUAL(dsr::string_lowerCase(U"abc"), U"abc");
  191. ASSERT_EQUAL(dsr::string_lowerCase(U"abc1"), U"abc1");
  192. ASSERT_EQUAL(dsr::string_lowerCase(U"Abc12"), U"abc12");
  193. ASSERT_EQUAL(dsr::string_lowerCase(U"ABC123"), U"abc123");
  194. // White space removal by pointing to a section of the original input
  195. ASSERT_EQUAL(dsr::string_removeOuterWhiteSpace(U" "), U"");
  196. ASSERT_EQUAL(dsr::string_removeOuterWhiteSpace(U" abc "), U"abc");
  197. ASSERT_EQUAL(dsr::string_removeOuterWhiteSpace(U" two words "), U"two words");
  198. ASSERT_EQUAL(dsr::string_removeOuterWhiteSpace(U" \" something quoted \" "), U"\" something quoted \"");
  199. // Quote mangling
  200. ASSERT_EQUAL(dsr::string_mangleQuote(U""), U"\"\"");
  201. ASSERT_EQUAL(dsr::string_mangleQuote(U"1"), U"\"1\"");
  202. ASSERT_EQUAL(dsr::string_mangleQuote(U"12"), U"\"12\"");
  203. ASSERT_EQUAL(dsr::string_mangleQuote(U"123"), U"\"123\"");
  204. ASSERT_EQUAL(dsr::string_mangleQuote(U"abc"), U"\"abc\"");
  205. // Not enough quote signs
  206. ASSERT_CRASH(dsr::string_unmangleQuote(U""));
  207. ASSERT_CRASH(dsr::string_unmangleQuote(U" "));
  208. ASSERT_CRASH(dsr::string_unmangleQuote(U"ab\"cd"));
  209. // Too many quote signs
  210. ASSERT_CRASH(dsr::string_unmangleQuote(U"ab\"cd\"ef\"gh"));
  211. // Basic quote
  212. ASSERT_EQUAL(dsr::string_unmangleQuote(U"\"ab\""), U"ab");
  213. // Surrounded quote
  214. ASSERT_EQUAL(dsr::string_unmangleQuote(U"\"ab\"cd"), U"ab");
  215. ASSERT_EQUAL(dsr::string_unmangleQuote(U"ab\"cd\""), U"cd");
  216. ASSERT_EQUAL(dsr::string_unmangleQuote(U"ab\"cd\"ef"), U"cd");
  217. // Mangled quote inside of quote
  218. ASSERT_EQUAL(dsr::string_unmangleQuote(U"ab\"c\\\"d\"ef"), U"c\"d");
  219. ASSERT_EQUAL(dsr::string_unmangleQuote(dsr::string_mangleQuote(U"c\"d")), U"c\"d");
  220. // Mangle things
  221. dsr::String randomText;
  222. string_reserve(randomText, 100);
  223. for (int i = 1; i < 100; i++) {
  224. // Randomize previous characters
  225. for (int j = 1; j < i - 1; j++) {
  226. string_appendChar(randomText, (DsrChar)((i * 21 + j * 49 + 136) % 1024));
  227. }
  228. // Add a new random character
  229. string_appendChar(randomText, (i * 21 + 136) % 256);
  230. ASSERT_EQUAL(dsr::string_unmangleQuote(dsr::string_mangleQuote(randomText)), randomText);
  231. }
  232. // Number serialization
  233. ASSERT_EQUAL(dsr::string_combine(0, U" ", 1), U"0 1");
  234. ASSERT_EQUAL(dsr::string_combine(14, U"x", 135), U"14x135");
  235. ASSERT_EQUAL(dsr::string_combine(-135), U"-135");
  236. ASSERT_EQUAL(dsr::string_combine(-14), U"-14");
  237. ASSERT_EQUAL(dsr::string_combine(-1), U"-1");
  238. ASSERT_EQUAL(dsr::string_combine(0u), U"0");
  239. ASSERT_EQUAL(dsr::string_combine(1u), U"1");
  240. ASSERT_EQUAL(dsr::string_combine(14u), U"14");
  241. ASSERT_EQUAL(dsr::string_combine(135u), U"135");
  242. ASSERT_EQUAL(dsr::string_combine(0.0), U"0.0");
  243. ASSERT_EQUAL(dsr::string_combine(-0.0), U"0.0");
  244. ASSERT_EQUAL(dsr::string_combine(1.0), U"1.0");
  245. ASSERT_EQUAL(dsr::string_combine(10.0), U"10.0");
  246. ASSERT_EQUAL(dsr::string_combine(100.0), U"100.0");
  247. ASSERT_EQUAL(dsr::string_combine(1000.0), U"1000.0");
  248. ASSERT_EQUAL(dsr::string_combine(10000.0), U"10000.0");
  249. ASSERT_EQUAL(dsr::string_combine(100000.0), U"100000.0");
  250. ASSERT_EQUAL(dsr::string_combine(1000000.0), U"1000000.0");
  251. ASSERT_EQUAL(dsr::string_combine(-1.0), U"-1.0");
  252. ASSERT_EQUAL(dsr::string_combine(-10.0), U"-10.0");
  253. ASSERT_EQUAL(dsr::string_combine(-100.0), U"-100.0");
  254. ASSERT_EQUAL(dsr::string_combine(-1000.0), U"-1000.0");
  255. ASSERT_EQUAL(dsr::string_combine(-10000.0), U"-10000.0");
  256. ASSERT_EQUAL(dsr::string_combine(-100000.0), U"-100000.0");
  257. ASSERT_EQUAL(dsr::string_combine(-1000000.0), U"-1000000.0");
  258. ASSERT_EQUAL(dsr::string_combine(0.5), U"0.5");
  259. ASSERT_EQUAL(dsr::string_combine(-0.5), U"-0.5");
  260. ASSERT_EQUAL(dsr::string_combine(789.123456), U"789.123456");
  261. ASSERT_EQUAL(dsr::string_combine(-789.123456), U"-789.123456");
  262. // Number parsing
  263. ASSERT_EQUAL(string_toInteger(U"0"), 0);
  264. ASSERT_EQUAL(string_toInteger(U"-0"), 0);
  265. ASSERT_EQUAL(string_toInteger(U"No digits here."), 0);
  266. ASSERT_EQUAL(string_toInteger(U" (12 garbage 34) "), 1234); // You are supposed to catch these errors before converting to an integer
  267. ASSERT_EQUAL(string_toInteger(U""), 0);
  268. ASSERT_EQUAL(string_toInteger(U"1"), 1);
  269. ASSERT_EQUAL(string_toInteger(U"-1"), -1);
  270. ASSERT_EQUAL(string_toInteger(U"1024"), 1024);
  271. ASSERT_EQUAL(string_toInteger(U"-1024"), -1024);
  272. ASSERT_EQUAL(string_toInteger(U"1000000"), 1000000);
  273. ASSERT_EQUAL(string_toInteger(U"-1000000"), -1000000);
  274. ASSERT_EQUAL(string_toInteger(U"123"), 123);
  275. ASSERT_EQUAL(string_toDouble(U"123"), 123.0);
  276. ASSERT_EQUAL(string_toDouble(U"123.456"), 123.456);
  277. { // Assigning strings using reference counting
  278. String a = U"Some text";
  279. ASSERT_EQUAL(string_getBufferUseCount(a), 1);
  280. String b = a;
  281. ASSERT_EQUAL(string_getBufferUseCount(a), 2);
  282. ASSERT_EQUAL(string_getBufferUseCount(b), 2);
  283. String c = b;
  284. ASSERT_EQUAL(string_getBufferUseCount(a), 3);
  285. ASSERT_EQUAL(string_getBufferUseCount(b), 3);
  286. ASSERT_EQUAL(string_getBufferUseCount(c), 3);
  287. }
  288. { // String splitting by shared reference counted buffer
  289. String source = U" a . b . c . d ";
  290. String source2 = U" a . b .\tc ";
  291. ASSERT_EQUAL(string_getBufferUseCount(source), 1);
  292. ASSERT_EQUAL(string_getBufferUseCount(source2), 1);
  293. List<String> result;
  294. result = string_split(source, U'.', false);
  295. ASSERT_EQUAL(result.length(), 4);
  296. ASSERT_EQUAL(result[0], U" a ");
  297. ASSERT_EQUAL(result[1], U" b ");
  298. ASSERT_EQUAL(result[2], U" c ");
  299. ASSERT_EQUAL(result[3], U" d ");
  300. ASSERT_EQUAL(string_getBufferUseCount(source), 5);
  301. ASSERT_EQUAL(string_getBufferUseCount(source2), 1);
  302. result = string_split(source2, U'.', true);
  303. ASSERT_EQUAL(result.length(), 3);
  304. ASSERT_EQUAL(result[0], U"a");
  305. ASSERT_EQUAL(result[1], U"b");
  306. ASSERT_EQUAL(result[2], U"c");
  307. ASSERT_EQUAL(string_getBufferUseCount(source), 1);
  308. ASSERT_EQUAL(string_getBufferUseCount(source2), 4);
  309. }
  310. { // Using buffer remembered in ReadableString to reuse memory for splitting
  311. String original = U" a . b . c . d ";
  312. ReadableString borrowsTheBuffer = string_after(original, 3);
  313. ASSERT_EQUAL(borrowsTheBuffer, U" b . c . d ");
  314. List<String> result = string_split(borrowsTheBuffer, U'.', true);
  315. ASSERT_EQUAL(result.length(), 3);
  316. ASSERT_EQUAL(result[0], U"b");
  317. ASSERT_EQUAL(result[1], U"c");
  318. ASSERT_EQUAL(result[2], U"d");
  319. ASSERT_EQUAL(string_getBufferUseCount(original), 5);
  320. ASSERT_EQUAL(string_getBufferUseCount(borrowsTheBuffer), 5);
  321. ASSERT_EQUAL(string_getBufferUseCount(result[0]), 5);
  322. ASSERT_EQUAL(string_getBufferUseCount(result[1]), 5);
  323. ASSERT_EQUAL(string_getBufferUseCount(result[2]), 5);
  324. }
  325. { // Automatically allocating a shared buffer for many elements
  326. List<String> result = string_split(U" a . b . c . d ", U'.', true);
  327. ASSERT_EQUAL(result[0], U"a");
  328. ASSERT_EQUAL(result[1], U"b");
  329. ASSERT_EQUAL(result[2], U"c");
  330. ASSERT_EQUAL(result[3], U"d");
  331. ASSERT_EQUAL(string_getBufferUseCount(result[0]), 4);
  332. ASSERT_EQUAL(string_getBufferUseCount(result[1]), 4);
  333. ASSERT_EQUAL(string_getBufferUseCount(result[2]), 4);
  334. ASSERT_EQUAL(string_getBufferUseCount(result[3]), 4);
  335. result = string_split(U" a . b . c ", U'.', false);
  336. ASSERT_EQUAL(result[0], U" a ");
  337. ASSERT_EQUAL(result[1], U" b ");
  338. ASSERT_EQUAL(result[2], U" c ");
  339. ASSERT_EQUAL(string_getBufferUseCount(result[0]), 3);
  340. ASSERT_EQUAL(string_getBufferUseCount(result[1]), 3);
  341. ASSERT_EQUAL(string_getBufferUseCount(result[2]), 3);
  342. }
  343. { // Callback splitting
  344. String numbers = U"1, 3, 5, 7, 9";
  345. List<int> result;
  346. string_split_callback([&numbers, &result](ReadableString section) {
  347. result.push(string_toInteger(section));
  348. }, numbers, U',');
  349. ASSERT_EQUAL(result.length(), 5);
  350. ASSERT_EQUAL(result[0], 1);
  351. ASSERT_EQUAL(result[1], 3);
  352. ASSERT_EQUAL(result[2], 5);
  353. ASSERT_EQUAL(result[3], 7);
  354. ASSERT_EQUAL(result[4], 9);
  355. }
  356. // TODO: Test taking a part of a parent string with a start offset, leaving the parent scope,
  357. // and expanding with append while the buffer isn't shared but has an offset from buffer start.
  358. // TODO: Test sharing the same buffer between strings, then clear and append more text without overwriting other strings.
  359. // TODO: Assert that buffers are shared when they should, but prevents side-effects when one is being written to.
  360. END_TEST