string_ut.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. #include "deque.h"
  2. #include "strbuf.h"
  3. #include "string_ut.h"
  4. #include "vector.h"
  5. #include "yexception.h"
  6. #include <util/charset/wide.h>
  7. #include <util/str_stl.h>
  8. #include <util/stream/output.h>
  9. #include <util/string/subst.h>
  10. #include <string>
  11. #include <sstream>
  12. #include <algorithm>
  13. #include <stdexcept>
  14. #ifdef TSTRING_IS_STD_STRING
  15. static_assert(sizeof(TString) == sizeof(std::string), "expect sizeof(TString) == sizeof(std::string)");
  16. #else
  17. static_assert(sizeof(TString) == sizeof(const char*), "expect sizeof(TString) == sizeof(const char*)");
  18. #endif
  19. class TStringTestZero: public TTestBase {
  20. UNIT_TEST_SUITE(TStringTestZero);
  21. UNIT_TEST(TestZero);
  22. UNIT_TEST_SUITE_END();
  23. public:
  24. void TestZero() {
  25. const char data[] = "abc\0def\0";
  26. TString s(data, sizeof(data));
  27. UNIT_ASSERT(s.size() == sizeof(data));
  28. UNIT_ASSERT(s.StartsWith(s));
  29. UNIT_ASSERT(s.EndsWith(s));
  30. UNIT_ASSERT(s.Contains('\0'));
  31. const char raw_def[] = "def";
  32. const char raw_zero[] = "\0";
  33. TString def(raw_def, sizeof(raw_def) - 1);
  34. TString zero(raw_zero, sizeof(raw_zero) - 1);
  35. UNIT_ASSERT_EQUAL(4, s.find(raw_def));
  36. UNIT_ASSERT_EQUAL(4, s.find(def));
  37. UNIT_ASSERT_EQUAL(4, s.find_first_of(raw_def));
  38. UNIT_ASSERT_EQUAL(3, s.find_first_of(zero));
  39. UNIT_ASSERT_EQUAL(7, s.find_first_not_of(def, 4));
  40. const char nonSubstring[] = "def\0ghi";
  41. UNIT_ASSERT_EQUAL(TString::npos, s.find(TString(nonSubstring, sizeof(nonSubstring))));
  42. TString copy = s;
  43. copy.replace(copy.size() - 1, 1, "z");
  44. UNIT_ASSERT(s != copy);
  45. copy.replace(copy.size() - 1, 1, "\0", 0, 1);
  46. UNIT_ASSERT(s == copy);
  47. TString prefix(data, 5);
  48. UNIT_ASSERT(s.StartsWith(prefix));
  49. UNIT_ASSERT(s != prefix);
  50. UNIT_ASSERT(s > prefix);
  51. UNIT_ASSERT(s > s.data());
  52. UNIT_ASSERT(s == TString(s.data(), s.size()));
  53. UNIT_ASSERT(data < s);
  54. s.remove(5);
  55. UNIT_ASSERT(s == prefix);
  56. }
  57. };
  58. UNIT_TEST_SUITE_REGISTRATION(TStringTestZero);
  59. template <typename TStringType, typename TTestData>
  60. class TStringStdTestImpl {
  61. using TChar = typename TStringType::char_type;
  62. using TTraits = typename TStringType::traits_type;
  63. using TView = std::basic_string_view<TChar, TTraits>;
  64. TTestData Data_;
  65. protected:
  66. void Constructor() {
  67. UNIT_ASSERT_EXCEPTION(TStringType((size_t)-1, *Data_.a()), std::length_error);
  68. }
  69. void reserve() {
  70. #if 0
  71. TStringType s;
  72. UNIT_ASSERT_EXCEPTION(s.reserve(s.max_size() + 1), std::length_error);
  73. // Non-shared behaviour - never shrink
  74. s.reserve(256);
  75. #ifndef TSTRING_IS_STD_STRING
  76. const auto* data = s.data();
  77. UNIT_ASSERT(s.capacity() >= 256);
  78. s.reserve(128);
  79. UNIT_ASSERT(s.capacity() >= 256 && s.data() == data);
  80. #endif
  81. s.resize(64, 'x');
  82. s.reserve(10);
  83. #ifdef TSTRING_IS_STD_STRING
  84. UNIT_ASSERT(s.capacity() >= 64);
  85. #else
  86. UNIT_ASSERT(s.capacity() >= 256 && s.data() == data);
  87. #endif
  88. #ifndef TSTRING_IS_STD_STRING
  89. // Shared behaviour - always reallocate, just as much as requisted
  90. TStringType holder = s;
  91. UNIT_ASSERT(s.capacity() >= 256);
  92. s.reserve(128);
  93. UNIT_ASSERT(s.capacity() >= 128 && s.capacity() < 256 && s.data() != data);
  94. UNIT_ASSERT(s.IsDetached());
  95. s.resize(64, 'x');
  96. data = s.data();
  97. holder = s;
  98. s.reserve(10);
  99. UNIT_ASSERT(s.capacity() >= 64 && s.capacity() < 128 && s.data() != data);
  100. UNIT_ASSERT(s.IsDetached());
  101. #endif
  102. #endif
  103. }
  104. void short_string() {
  105. TStringType const ref_short_str1(Data_.str1()), ref_short_str2(Data_.str2());
  106. TStringType short_str1(ref_short_str1), short_str2(ref_short_str2);
  107. TStringType const ref_long_str1(Data_.str__________________________________________________1());
  108. TStringType const ref_long_str2(Data_.str__________________________________________________2());
  109. TStringType long_str1(ref_long_str1), long_str2(ref_long_str2);
  110. UNIT_ASSERT(short_str1 == ref_short_str1);
  111. UNIT_ASSERT(long_str1 == ref_long_str1);
  112. {
  113. TStringType str1(short_str1);
  114. str1 = long_str1;
  115. UNIT_ASSERT(str1 == ref_long_str1);
  116. }
  117. {
  118. TStringType str1(long_str1);
  119. str1 = short_str1;
  120. UNIT_ASSERT(str1 == ref_short_str1);
  121. }
  122. {
  123. short_str1.swap(short_str2);
  124. UNIT_ASSERT((short_str1 == ref_short_str2) && (short_str2 == ref_short_str1));
  125. short_str1.swap(short_str2);
  126. }
  127. {
  128. long_str1.swap(long_str2);
  129. UNIT_ASSERT((long_str1 == ref_long_str2) && (long_str2 == ref_long_str1));
  130. long_str1.swap(long_str2);
  131. }
  132. {
  133. short_str1.swap(long_str1);
  134. UNIT_ASSERT((short_str1 == ref_long_str1) && (long_str1 == ref_short_str1));
  135. short_str1.swap(long_str1);
  136. }
  137. {
  138. long_str1.swap(short_str1);
  139. UNIT_ASSERT((short_str1 == ref_long_str1) && (long_str1 == ref_short_str1));
  140. long_str1.swap(short_str1);
  141. }
  142. {
  143. //This is to test move constructor
  144. TVector<TStringType> str_vect;
  145. str_vect.push_back(short_str1);
  146. str_vect.push_back(long_str1);
  147. str_vect.push_back(short_str2);
  148. str_vect.push_back(long_str2);
  149. UNIT_ASSERT(str_vect[0] == ref_short_str1);
  150. UNIT_ASSERT(str_vect[1] == ref_long_str1);
  151. UNIT_ASSERT(str_vect[2] == ref_short_str2);
  152. UNIT_ASSERT(str_vect[3] == ref_long_str2);
  153. }
  154. }
  155. void erase() {
  156. TChar const* c_str = Data_.Hello_World();
  157. TStringType str(c_str);
  158. UNIT_ASSERT(str == c_str);
  159. str.erase(str.begin() + 1, str.end() - 1); // Erase all but first and last.
  160. size_t i;
  161. for (i = 0; i < str.size(); ++i) {
  162. switch (i) {
  163. case 0:
  164. UNIT_ASSERT(str[i] == *Data_.H());
  165. break;
  166. case 1:
  167. UNIT_ASSERT(str[i] == *Data_.d());
  168. break;
  169. default:
  170. UNIT_ASSERT(false);
  171. }
  172. }
  173. str.insert(1, c_str);
  174. str.erase(str.begin()); // Erase first element.
  175. str.erase(str.end() - 1); // Erase last element.
  176. UNIT_ASSERT(str == c_str);
  177. str.clear(); // Erase all.
  178. UNIT_ASSERT(str.empty());
  179. str = c_str;
  180. UNIT_ASSERT(str == c_str);
  181. str.erase(1, str.size() - 1); // Erase all but first and last.
  182. for (i = 0; i < str.size(); i++) {
  183. switch (i) {
  184. case 0:
  185. UNIT_ASSERT(str[i] == *Data_.H());
  186. break;
  187. case 1:
  188. UNIT_ASSERT(str[i] == *Data_.d());
  189. break;
  190. default:
  191. UNIT_ASSERT(false);
  192. }
  193. }
  194. str.erase(1);
  195. UNIT_ASSERT(str == Data_.H());
  196. }
  197. void data() {
  198. TStringType xx;
  199. // ISO-IEC-14882:1998(E), 21.3.6, paragraph 3
  200. UNIT_ASSERT(xx.data() != nullptr);
  201. }
  202. void c_str() {
  203. TStringType low(Data_._2004_01_01());
  204. TStringType xx;
  205. TStringType yy;
  206. // ISO-IEC-14882:1998(E), 21.3.6, paragraph 1
  207. UNIT_ASSERT(*(yy.c_str()) == 0);
  208. // Blocks A and B should follow each other.
  209. // Block A:
  210. xx = Data_._123456();
  211. xx += low;
  212. UNIT_ASSERT(xx.c_str() == TView(Data_._1234562004_01_01()));
  213. // End of block A
  214. // Block B:
  215. xx = Data_._1234();
  216. xx += Data_._5();
  217. UNIT_ASSERT(xx.c_str() == TView(Data_._12345()));
  218. // End of block B
  219. }
  220. void null_char_of_empty() {
  221. const TStringType s;
  222. //NOTE: https://a.yandex-team.ru/arcadia/junk/grechnik/test_string?rev=r12602052
  223. i64 i = s[s.size()];
  224. UNIT_ASSERT_VALUES_EQUAL(i, 0);
  225. }
  226. void null_char() {
  227. // ISO/IEC 14882:1998(E), ISO/IEC 14882:2003(E), 21.3.4 ('... the const version')
  228. const TStringType s(Data_._123456());
  229. UNIT_ASSERT(s[s.size()] == 0);
  230. }
  231. // Allowed since C++17, see http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2475
  232. void null_char_assignment_to_subscript_of_empty() {
  233. TStringType s;
  234. #ifdef TSTRING_IS_STD_STRING
  235. using reference = volatile typename TStringType::value_type&;
  236. #else
  237. using reference = typename TStringType::reference;
  238. #endif
  239. reference trailing_zero = s[s.size()];
  240. trailing_zero = 0;
  241. UNIT_ASSERT(trailing_zero == 0);
  242. }
  243. // Allowed since C++17, see http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#2475
  244. void null_char_assignment_to_subscript_of_nonempty() {
  245. TStringType s(Data_._123456());
  246. #ifdef TSTRING_IS_STD_STRING
  247. using reference = volatile typename TStringType::value_type&;
  248. #else
  249. using reference = typename TStringType::reference;
  250. #endif
  251. reference trailing_zero = s[s.size()];
  252. trailing_zero = 0;
  253. UNIT_ASSERT(trailing_zero == 0);
  254. }
  255. #ifndef TSTRING_IS_STD_STRING
  256. // Dereferencing string end() is not allowed by C++ standard as of C++20, avoid using in real code.
  257. void null_char_assignment_to_end_of_empty() {
  258. TStringType s;
  259. volatile auto& trailing_zero = *(s.begin() + s.size());
  260. trailing_zero = 0;
  261. UNIT_ASSERT(trailing_zero == 0);
  262. }
  263. // Dereferencing string end() is not allowed by C++ standard as of C++20, avoid using in real code.
  264. void null_char_assignment_to_end_of_nonempty() {
  265. TStringType s(Data_._123456());
  266. volatile auto& trailing_zero = *(s.begin() + s.size());
  267. trailing_zero = 0;
  268. UNIT_ASSERT(trailing_zero == 0);
  269. }
  270. #endif
  271. void insert() {
  272. TStringType strorg = Data_.This_is_test_string_for_string_calls();
  273. TStringType str;
  274. // In case of reallocation there is no auto reference problem
  275. // so we reserve a big enough TStringType to be sure to test this
  276. // particular point.
  277. str.reserve(100);
  278. str = strorg;
  279. //test self insertion:
  280. str.insert(10, str.c_str() + 5, 15);
  281. UNIT_ASSERT(str == Data_.This_is_teis_test_string_st_string_for_string_calls());
  282. str = strorg;
  283. str.insert(15, str.c_str() + 5, 25);
  284. UNIT_ASSERT(str == Data_.This_is_test_stis_test_string_for_stringring_for_string_calls());
  285. str = strorg;
  286. str.insert(0, str.c_str() + str.size() - 4, 4);
  287. UNIT_ASSERT(str == Data_.allsThis_is_test_string_for_string_calls());
  288. str = strorg;
  289. str.insert(0, str.c_str() + str.size() / 2 - 1, str.size() / 2 + 1);
  290. UNIT_ASSERT(str == Data_.ng_for_string_callsThis_is_test_string_for_string_calls());
  291. str = strorg;
  292. typename TStringType::iterator b = str.begin();
  293. typename TStringType::const_iterator s = str.begin() + str.size() / 2 - 1;
  294. typename TStringType::const_iterator e = str.end();
  295. str.insert(b, s, e);
  296. UNIT_ASSERT(str == Data_.ng_for_string_callsThis_is_test_string_for_string_calls());
  297. #if 0
  298. // AV
  299. str = strorg;
  300. str.insert(str.begin(), str.begin() + str.size() / 2 - 1, str.end());
  301. UNIT_ASSERT(str == Data.ng_for_string_callsThis_is_test_string_for_string_calls());
  302. #endif
  303. TStringType str0;
  304. str0.insert(str0.begin(), 5, *Data_._0());
  305. UNIT_ASSERT(str0 == Data_._00000());
  306. TStringType str1;
  307. {
  308. typename TStringType::size_type pos = 0, nb = 2;
  309. str1.insert(pos, nb, *Data_._1());
  310. }
  311. UNIT_ASSERT(str1 == Data_._11());
  312. str0.insert(0, str1);
  313. UNIT_ASSERT(str0 == Data_._1100000());
  314. TStringType str2(Data_._2345());
  315. str0.insert(str0.size(), str2, 1, 2);
  316. UNIT_ASSERT(str0 == Data_._110000034());
  317. str1.insert(str1.begin() + 1, 2, *Data_._2());
  318. UNIT_ASSERT(str1 == Data_._1221());
  319. str1.insert(2, Data_._333333(), 3);
  320. UNIT_ASSERT(str1 == Data_._1233321());
  321. str1.insert(4, Data_._4444());
  322. UNIT_ASSERT(str1 == Data_._12334444321());
  323. str1.insert(str1.begin() + 6, *Data_._5());
  324. UNIT_ASSERT(str1 == Data_._123344544321());
  325. }
  326. void resize() {
  327. TStringType s;
  328. s.resize(0);
  329. UNIT_ASSERT(*s.c_str() == 0);
  330. s = Data_._1234567();
  331. s.resize(0);
  332. UNIT_ASSERT(*s.c_str() == 0);
  333. s = Data_._1234567();
  334. s.resize(1);
  335. UNIT_ASSERT(s.size() == 1);
  336. UNIT_ASSERT(*s.c_str() == *Data_._1());
  337. UNIT_ASSERT(*(s.c_str() + 1) == 0);
  338. s = Data_._1234567();
  339. #if 0
  340. s.resize(10);
  341. #else
  342. s.resize(10, 0);
  343. #endif
  344. UNIT_ASSERT(s.size() == 10);
  345. UNIT_ASSERT(s[6] == *Data_._7());
  346. UNIT_ASSERT(s[7] == 0);
  347. UNIT_ASSERT(s[8] == 0);
  348. UNIT_ASSERT(s[9] == 0);
  349. }
  350. void find() {
  351. TStringType s(Data_.one_two_three_one_two_three());
  352. UNIT_ASSERT(s.find(Data_.one()) == 0);
  353. UNIT_ASSERT(s.find(*Data_.t()) == 4);
  354. UNIT_ASSERT(s.find(*Data_.t(), 5) == 8);
  355. UNIT_ASSERT(s.find(Data_.four()) == TStringType::npos);
  356. UNIT_ASSERT(s.find(Data_.one(), TStringType::npos) == TStringType::npos);
  357. UNIT_ASSERT(s.find_first_of(Data_.abcde()) == 2);
  358. UNIT_ASSERT(s.find_first_not_of(Data_.enotw_()) == 9);
  359. }
  360. void capacity() {
  361. TStringType s;
  362. UNIT_ASSERT(s.capacity() < s.max_size());
  363. UNIT_ASSERT(s.capacity() >= s.size());
  364. for (int i = 0; i < 18; ++i) {
  365. s += ' ';
  366. UNIT_ASSERT(s.capacity() > 0);
  367. UNIT_ASSERT(s.capacity() < s.max_size());
  368. UNIT_ASSERT(s.capacity() >= s.size());
  369. }
  370. }
  371. void assign() {
  372. TStringType s;
  373. TChar const* cstr = Data_.test_string_for_assign();
  374. s.assign(cstr, cstr + 22);
  375. UNIT_ASSERT(s == Data_.test_string_for_assign());
  376. TStringType s2(Data_.other_test_string());
  377. s.assign(s2);
  378. UNIT_ASSERT(s == s2);
  379. static TStringType str1;
  380. static TStringType str2;
  381. // short TStringType optim:
  382. str1 = Data_._123456();
  383. // longer than short TStringType:
  384. str2 = Data_._1234567890123456789012345678901234567890();
  385. UNIT_ASSERT(str1[5] == *Data_._6());
  386. UNIT_ASSERT(str2[29] == *Data_._0());
  387. }
  388. void copy() {
  389. TStringType s(Data_.foo());
  390. TChar dest[4];
  391. dest[0] = dest[1] = dest[2] = dest[3] = 1;
  392. s.copy(dest, 4);
  393. int pos = 0;
  394. UNIT_ASSERT(dest[pos++] == *Data_.f());
  395. UNIT_ASSERT(dest[pos++] == *Data_.o());
  396. UNIT_ASSERT(dest[pos++] == *Data_.o());
  397. UNIT_ASSERT(dest[pos++] == 1);
  398. dest[0] = dest[1] = dest[2] = dest[3] = 1;
  399. s.copy(dest, 4, 2);
  400. pos = 0;
  401. UNIT_ASSERT(dest[pos++] == *Data_.o());
  402. UNIT_ASSERT(dest[pos++] == 1);
  403. UNIT_ASSERT_EXCEPTION(s.copy(dest, 4, 5), std::out_of_range);
  404. }
  405. void cbegin_cend() {
  406. const char helloThere[] = "Hello there";
  407. TString s = helloThere;
  408. size_t index = 0;
  409. for (auto it = s.cbegin(); s.cend() != it; ++it, ++index) {
  410. UNIT_ASSERT_VALUES_EQUAL(helloThere[index], *it);
  411. }
  412. }
  413. void compare() {
  414. TStringType str1(Data_.abcdef());
  415. TStringType str2;
  416. str2 = Data_.abcdef();
  417. UNIT_ASSERT(str1.compare(str2) == 0);
  418. UNIT_ASSERT(str1.compare(str2.data(), str2.size()) == 0);
  419. str2 = Data_.abcde();
  420. UNIT_ASSERT(str1.compare(str2) > 0);
  421. UNIT_ASSERT(str1.compare(str2.data(), str2.size()) > 0);
  422. str2 = Data_.abcdefg();
  423. UNIT_ASSERT(str1.compare(str2) < 0);
  424. UNIT_ASSERT(str1.compare(str2.data(), str2.size()) < 0);
  425. UNIT_ASSERT(str1.compare(Data_.abcdef()) == 0);
  426. UNIT_ASSERT(str1.compare(Data_.abcde()) > 0);
  427. UNIT_ASSERT(str1.compare(Data_.abcdefg()) < 0);
  428. str2 = Data_.cde();
  429. UNIT_ASSERT(str1.compare(2, 3, str2) == 0);
  430. str2 = Data_.cd();
  431. UNIT_ASSERT(str1.compare(2, 3, str2) > 0);
  432. str2 = Data_.cdef();
  433. UNIT_ASSERT(str1.compare(2, 3, str2) < 0);
  434. str2 = Data_.abcdef();
  435. UNIT_ASSERT(str1.compare(2, 3, str2, 2, 3) == 0);
  436. UNIT_ASSERT(str1.compare(2, 3, str2, 2, 2) > 0);
  437. UNIT_ASSERT(str1.compare(2, 3, str2, 2, 4) < 0);
  438. UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 3) == 0);
  439. UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 2) > 0);
  440. UNIT_ASSERT(str1.compare(2, 3, Data_.cdefgh(), 4) < 0);
  441. }
  442. void find_last_of() {
  443. // 21.3.6.4
  444. TStringType s(Data_.one_two_three_one_two_three());
  445. UNIT_ASSERT(s.find_last_of(Data_.abcde()) == 26);
  446. UNIT_ASSERT(s.find_last_of(TStringType(Data_.abcde())) == 26);
  447. TStringType test(Data_.aba());
  448. UNIT_ASSERT(test.find_last_of(Data_.a(), 2, 1) == 2);
  449. UNIT_ASSERT(test.find_last_of(Data_.a(), 1, 1) == 0);
  450. UNIT_ASSERT(test.find_last_of(Data_.a(), 0, 1) == 0);
  451. UNIT_ASSERT(test.find_last_of(*Data_.a(), 2) == 2);
  452. UNIT_ASSERT(test.find_last_of(*Data_.a(), 1) == 0);
  453. UNIT_ASSERT(test.find_last_of(*Data_.a(), 0) == 0);
  454. }
  455. #if 0
  456. void rfind() {
  457. // 21.3.6.2
  458. TStringType s(Data.one_two_three_one_two_three());
  459. UNIT_ASSERT(s.rfind(Data.two()) == 18);
  460. UNIT_ASSERT(s.rfind(Data.two(), 0) == TStringType::npos);
  461. UNIT_ASSERT(s.rfind(Data.two(), 11) == 4);
  462. UNIT_ASSERT(s.rfind(*Data.w()) == 19);
  463. TStringType test(Data.aba());
  464. UNIT_ASSERT(test.rfind(Data.a(), 2, 1) == 2);
  465. UNIT_ASSERT(test.rfind(Data.a(), 1, 1) == 0);
  466. UNIT_ASSERT(test.rfind(Data.a(), 0, 1) == 0);
  467. UNIT_ASSERT(test.rfind(*Data.a(), 2) == 2);
  468. UNIT_ASSERT(test.rfind(*Data.a(), 1) == 0);
  469. UNIT_ASSERT(test.rfind(*Data.a(), 0) == 0);
  470. }
  471. #endif
  472. void find_last_not_of() {
  473. // 21.3.6.6
  474. TStringType s(Data_.one_two_three_one_two_three());
  475. UNIT_ASSERT(s.find_last_not_of(Data_.ehortw_()) == 15);
  476. TStringType test(Data_.aba());
  477. UNIT_ASSERT(test.find_last_not_of(Data_.a(), 2, 1) == 1);
  478. UNIT_ASSERT(test.find_last_not_of(Data_.b(), 2, 1) == 2);
  479. UNIT_ASSERT(test.find_last_not_of(Data_.a(), 1, 1) == 1);
  480. UNIT_ASSERT(test.find_last_not_of(Data_.b(), 1, 1) == 0);
  481. UNIT_ASSERT(test.find_last_not_of(Data_.a(), 0, 1) == TStringType::npos);
  482. UNIT_ASSERT(test.find_last_not_of(Data_.b(), 0, 1) == 0);
  483. UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 2) == 1);
  484. UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 2) == 2);
  485. UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 1) == 1);
  486. UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 1) == 0);
  487. UNIT_ASSERT(test.find_last_not_of(*Data_.a(), 0) == TStringType::npos);
  488. UNIT_ASSERT(test.find_last_not_of(*Data_.b(), 0) == 0);
  489. }
  490. #if 0
  491. void replace() {
  492. // This test case is for the non template basic_TString::replace method,
  493. // this is why we play with the const iterators and reference to guaranty
  494. // that the right method is called.
  495. const TStringType v(Data._78());
  496. TStringType s(Data._123456());
  497. TStringType const& cs = s;
  498. typename TStringType::iterator i = s.begin() + 1;
  499. s.replace(i, i + 3, v.begin(), v.end());
  500. UNIT_ASSERT(s == Data._17856());
  501. s = Data._123456();
  502. i = s.begin() + 1;
  503. s.replace(i, i + 1, v.begin(), v.end());
  504. UNIT_ASSERT(s == Data._1783456());
  505. s = Data._123456();
  506. i = s.begin() + 1;
  507. typename TStringType::const_iterator ci = s.begin() + 1;
  508. s.replace(i, i + 3, ci + 3, cs.end());
  509. UNIT_ASSERT(s == Data._15656());
  510. s = Data._123456();
  511. i = s.begin() + 1;
  512. ci = s.begin() + 1;
  513. s.replace(i, i + 3, ci, ci + 2);
  514. UNIT_ASSERT(s == Data._12356());
  515. s = Data._123456();
  516. i = s.begin() + 1;
  517. ci = s.begin() + 1;
  518. s.replace(i, i + 3, ci + 1, cs.end());
  519. UNIT_ASSERT(s == Data._1345656());
  520. s = Data._123456();
  521. i = s.begin();
  522. ci = s.begin() + 1;
  523. s.replace(i, i, ci, ci + 1);
  524. UNIT_ASSERT(s == Data._2123456());
  525. s = Data._123456();
  526. s.replace(s.begin() + 4, s.end(), cs.begin(), cs.end());
  527. UNIT_ASSERT(s == Data._1234123456());
  528. // This is the test for the template replace method.
  529. s = Data._123456();
  530. typename TStringType::iterator b = s.begin() + 4;
  531. typename TStringType::iterator e = s.end();
  532. typename TStringType::const_iterator rb = s.begin();
  533. typename TStringType::const_iterator re = s.end();
  534. s.replace(b, e, rb, re);
  535. UNIT_ASSERT(s == Data._1234123456());
  536. s = Data._123456();
  537. s.replace(s.begin() + 4, s.end(), s.begin(), s.end());
  538. UNIT_ASSERT(s == Data._1234123456());
  539. TStringType strorg(Data.This_is_test_StringT_for_StringT_calls());
  540. TStringType str = strorg;
  541. str.replace(5, 15, str.c_str(), 10);
  542. UNIT_ASSERT(str == Data.This_This_is_tefor_StringT_calls());
  543. str = strorg;
  544. str.replace(5, 5, str.c_str(), 10);
  545. UNIT_ASSERT(str == Data.This_This_is_test_StringT_for_StringT_calls());
  546. #if !defined(STLPORT) || defined(_STLP_MEMBER_TEMPLATES)
  547. deque<TChar> cdeque;
  548. cdeque.push_back(*Data.I());
  549. str.replace(str.begin(), str.begin() + 11, cdeque.begin(), cdeque.end());
  550. UNIT_ASSERT(str == Data.Is_test_StringT_for_StringT_calls());
  551. #endif
  552. }
  553. #endif
  554. }; // TStringStdTestImpl
  555. class TStringTest: public TTestBase, private TStringTestImpl<TString, TTestData<char>> {
  556. public:
  557. UNIT_TEST_SUITE(TStringTest);
  558. UNIT_TEST(TestMaxSize);
  559. UNIT_TEST(TestConstructors);
  560. UNIT_TEST(TestReplace);
  561. #ifndef TSTRING_IS_STD_STRING
  562. UNIT_TEST(TestRefCount);
  563. #endif
  564. UNIT_TEST(TestFind);
  565. UNIT_TEST(TestContains);
  566. UNIT_TEST(TestOperators);
  567. UNIT_TEST(TestMulOperators);
  568. UNIT_TEST(TestFuncs);
  569. UNIT_TEST(TestUtils);
  570. UNIT_TEST(TestEmpty);
  571. UNIT_TEST(TestJoin);
  572. UNIT_TEST(TestCopy);
  573. UNIT_TEST(TestStrCpy);
  574. UNIT_TEST(TestPrefixSuffix);
  575. #ifndef TSTRING_IS_STD_STRING
  576. UNIT_TEST(TestCharRef);
  577. #endif
  578. UNIT_TEST(TestBack)
  579. UNIT_TEST(TestFront)
  580. UNIT_TEST(TestIterators);
  581. UNIT_TEST(TestReverseIterators);
  582. UNIT_TEST(TestAppendUtf16)
  583. UNIT_TEST(TestFillingAssign)
  584. UNIT_TEST(TestStdStreamApi)
  585. //UNIT_TEST(TestOperatorsCI); must fail
  586. UNIT_TEST_SUITE_END();
  587. void TestAppendUtf16() {
  588. TString appended = TString("А роза упала").AppendUtf16(u" на лапу Азора");
  589. UNIT_ASSERT(appended == "А роза упала на лапу Азора");
  590. }
  591. void TestFillingAssign() {
  592. TString s("abc");
  593. s.assign(5, 'a');
  594. UNIT_ASSERT_VALUES_EQUAL(s, "aaaaa");
  595. }
  596. void TestStdStreamApi() {
  597. const TString data = "abracadabra";
  598. std::stringstream ss;
  599. ss << data;
  600. UNIT_ASSERT_VALUES_EQUAL(data, ss.str());
  601. ss << '\n'
  602. << data << std::endl;
  603. TString read = "xxx";
  604. ss >> read;
  605. UNIT_ASSERT_VALUES_EQUAL(read, data);
  606. }
  607. };
  608. UNIT_TEST_SUITE_REGISTRATION(TStringTest);
  609. class TWideStringTest: public TTestBase, private TStringTestImpl<TUtf16String, TTestData<wchar16>> {
  610. public:
  611. UNIT_TEST_SUITE(TWideStringTest);
  612. UNIT_TEST(TestConstructors);
  613. UNIT_TEST(TestReplace);
  614. #ifndef TSTRING_IS_STD_STRING
  615. UNIT_TEST(TestRefCount);
  616. #endif
  617. UNIT_TEST(TestFind);
  618. UNIT_TEST(TestContains);
  619. UNIT_TEST(TestOperators);
  620. UNIT_TEST(TestLetOperator)
  621. UNIT_TEST(TestMulOperators);
  622. UNIT_TEST(TestFuncs);
  623. UNIT_TEST(TestUtils);
  624. UNIT_TEST(TestEmpty);
  625. UNIT_TEST(TestJoin);
  626. UNIT_TEST(TestCopy);
  627. UNIT_TEST(TestStrCpy);
  628. UNIT_TEST(TestPrefixSuffix);
  629. #ifndef TSTRING_IS_STD_STRING
  630. UNIT_TEST(TestCharRef);
  631. #endif
  632. UNIT_TEST(TestBack);
  633. UNIT_TEST(TestFront)
  634. UNIT_TEST(TestDecodingMethods);
  635. UNIT_TEST(TestIterators);
  636. UNIT_TEST(TestReverseIterators);
  637. UNIT_TEST(TestStringLiterals);
  638. UNIT_TEST_SUITE_END();
  639. private:
  640. void TestDecodingMethods() {
  641. UNIT_ASSERT(TUtf16String::FromAscii("").empty());
  642. UNIT_ASSERT(TUtf16String::FromAscii("abc") == ASCIIToWide("abc"));
  643. const char* text = "123kx83abcd ej)#$%ddja&%J&";
  644. TUtf16String wtext = ASCIIToWide(text);
  645. UNIT_ASSERT(wtext == TUtf16String::FromAscii(text));
  646. TString strtext(text);
  647. UNIT_ASSERT(wtext == TUtf16String::FromAscii(strtext));
  648. TStringBuf strbuftext(text);
  649. UNIT_ASSERT(wtext == TUtf16String::FromAscii(strbuftext));
  650. UNIT_ASSERT(wtext.substr(5) == TUtf16String::FromAscii(text + 5));
  651. const wchar16 wideCyrillicAlphabet[] = {
  652. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  653. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  654. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  655. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  656. 0x00};
  657. TUtf16String strWide(wideCyrillicAlphabet);
  658. TString strUtf8 = WideToUTF8(strWide);
  659. UNIT_ASSERT(strWide == TUtf16String::FromUtf8(strUtf8.c_str()));
  660. UNIT_ASSERT(strWide == TUtf16String::FromUtf8(strUtf8));
  661. UNIT_ASSERT(strWide == TUtf16String::FromUtf8(TStringBuf(strUtf8)));
  662. // assign
  663. TUtf16String s1;
  664. s1.AssignAscii("1234");
  665. UNIT_ASSERT(s1 == ASCIIToWide("1234"));
  666. s1.AssignUtf8(strUtf8);
  667. UNIT_ASSERT(s1 == strWide);
  668. s1.AssignAscii(text);
  669. UNIT_ASSERT(s1 == wtext);
  670. // append
  671. TUtf16String s2;
  672. TUtf16String testAppend = strWide;
  673. s2.AppendUtf8(strUtf8);
  674. UNIT_ASSERT(testAppend == s2);
  675. testAppend += ' ';
  676. s2.AppendAscii(" ");
  677. UNIT_ASSERT(testAppend == s2);
  678. testAppend += '_';
  679. s2.AppendUtf8("_");
  680. UNIT_ASSERT(testAppend == s2);
  681. testAppend += wtext;
  682. s2.AppendAscii(text);
  683. UNIT_ASSERT(testAppend == s2);
  684. testAppend += wtext;
  685. s2.AppendUtf8(text);
  686. UNIT_ASSERT(testAppend == s2);
  687. }
  688. void TestLetOperator() {
  689. TUtf16String str;
  690. str = wchar16('X');
  691. UNIT_ASSERT(str == TUtf16String::FromAscii("X"));
  692. const TUtf16String hello = TUtf16String::FromAscii("hello");
  693. str = hello.data();
  694. UNIT_ASSERT(str == hello);
  695. str = hello;
  696. UNIT_ASSERT(str == hello);
  697. }
  698. void TestStringLiterals() {
  699. TUtf16String s1 = u"hello";
  700. UNIT_ASSERT_VALUES_EQUAL(s1, TUtf16String::FromAscii("hello"));
  701. TUtf16String s2 = u"привет";
  702. UNIT_ASSERT_VALUES_EQUAL(s2, TUtf16String::FromUtf8("привет"));
  703. }
  704. };
  705. UNIT_TEST_SUITE_REGISTRATION(TWideStringTest);
  706. class TUtf32StringTest: public TTestBase, private TStringTestImpl<TUtf32String, TTestData<wchar32>> {
  707. public:
  708. UNIT_TEST_SUITE(TUtf32StringTest);
  709. UNIT_TEST(TestConstructors);
  710. UNIT_TEST(TestReplace);
  711. #ifndef TSTRING_IS_STD_STRING
  712. UNIT_TEST(TestRefCount);
  713. #endif
  714. UNIT_TEST(TestFind);
  715. UNIT_TEST(TestContains);
  716. UNIT_TEST(TestOperators);
  717. UNIT_TEST(TestLetOperator)
  718. UNIT_TEST(TestMulOperators);
  719. UNIT_TEST(TestFuncs);
  720. UNIT_TEST(TestUtils);
  721. UNIT_TEST(TestEmpty);
  722. UNIT_TEST(TestJoin);
  723. UNIT_TEST(TestCopy);
  724. UNIT_TEST(TestStrCpy);
  725. UNIT_TEST(TestPrefixSuffix);
  726. #ifndef TSTRING_IS_STD_STRING
  727. UNIT_TEST(TestCharRef);
  728. #endif
  729. UNIT_TEST(TestBack);
  730. UNIT_TEST(TestFront)
  731. UNIT_TEST(TestDecodingMethods);
  732. UNIT_TEST(TestDecodingMethodsMixedStr);
  733. UNIT_TEST(TestIterators);
  734. UNIT_TEST(TestReverseIterators);
  735. UNIT_TEST(TestStringLiterals);
  736. UNIT_TEST_SUITE_END();
  737. private:
  738. void TestDecodingMethods() {
  739. UNIT_ASSERT(TUtf32String::FromAscii("").empty());
  740. UNIT_ASSERT(TUtf32String::FromAscii("abc") == ASCIIToUTF32("abc"));
  741. const char* text = "123kx83abcd ej)#$%ddja&%J&";
  742. TUtf32String wtext = ASCIIToUTF32(text);
  743. UNIT_ASSERT(wtext == TUtf32String::FromAscii(text));
  744. TString strtext(text);
  745. UNIT_ASSERT(wtext == TUtf32String::FromAscii(strtext));
  746. TStringBuf strbuftext(text);
  747. UNIT_ASSERT(wtext == TUtf32String::FromAscii(strbuftext));
  748. UNIT_ASSERT(wtext.substr(5) == TUtf32String::FromAscii(text + 5));
  749. const wchar32 wideCyrillicAlphabet[] = {
  750. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  751. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  752. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  753. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  754. 0x00};
  755. TUtf32String strWide(wideCyrillicAlphabet);
  756. TString strUtf8 = WideToUTF8(strWide);
  757. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8.c_str()));
  758. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8));
  759. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(TStringBuf(strUtf8)));
  760. // assign
  761. TUtf32String s1;
  762. s1.AssignAscii("1234");
  763. UNIT_ASSERT(s1 == ASCIIToUTF32("1234"));
  764. s1.AssignUtf8(strUtf8);
  765. UNIT_ASSERT(s1 == strWide);
  766. s1.AssignAscii(text);
  767. UNIT_ASSERT(s1 == wtext);
  768. // append
  769. TUtf32String s2;
  770. TUtf32String testAppend = strWide;
  771. s2.AppendUtf8(strUtf8);
  772. UNIT_ASSERT(testAppend == s2);
  773. testAppend += ' ';
  774. s2.AppendAscii(" ");
  775. UNIT_ASSERT(testAppend == s2);
  776. testAppend += '_';
  777. s2.AppendUtf8("_");
  778. UNIT_ASSERT(testAppend == s2);
  779. testAppend += wtext;
  780. s2.AppendAscii(text);
  781. UNIT_ASSERT(testAppend == s2);
  782. testAppend += wtext;
  783. s2.AppendUtf8(text);
  784. UNIT_ASSERT(testAppend == s2);
  785. }
  786. void TestDecodingMethodsMixedStr() {
  787. UNIT_ASSERT(TUtf32String::FromAscii("").empty());
  788. UNIT_ASSERT(TUtf32String::FromAscii("abc") == ASCIIToUTF32("abc"));
  789. const char* text = "123kx83abcd ej)#$%ddja&%J&";
  790. TUtf32String wtext = ASCIIToUTF32(text);
  791. UNIT_ASSERT(wtext == TUtf32String::FromAscii(text));
  792. TString strtext(text);
  793. UNIT_ASSERT(wtext == TUtf32String::FromAscii(strtext));
  794. TStringBuf strbuftext(text);
  795. UNIT_ASSERT(wtext == TUtf32String::FromAscii(strbuftext));
  796. UNIT_ASSERT(wtext.substr(5) == TUtf32String::FromAscii(text + 5));
  797. const wchar32 cyrilicAndLatinWide[] = {
  798. 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
  799. 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
  800. 0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
  801. wchar32('z'),
  802. 0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
  803. wchar32('z'),
  804. 0x00};
  805. TUtf32String strWide(cyrilicAndLatinWide);
  806. TString strUtf8 = WideToUTF8(strWide);
  807. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8.c_str()));
  808. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(strUtf8));
  809. UNIT_ASSERT(strWide == UTF8ToUTF32<true>(strUtf8));
  810. UNIT_ASSERT(strWide == UTF8ToUTF32<false>(strUtf8));
  811. UNIT_ASSERT(strWide == TUtf32String::FromUtf8(TStringBuf(strUtf8)));
  812. // assign
  813. TUtf32String s1;
  814. s1.AssignAscii("1234");
  815. UNIT_ASSERT(s1 == ASCIIToUTF32("1234"));
  816. s1.AssignUtf8(strUtf8);
  817. UNIT_ASSERT(s1 == strWide);
  818. s1.AssignAscii(text);
  819. UNIT_ASSERT(s1 == wtext);
  820. // append
  821. TUtf32String s2;
  822. TUtf32String testAppend = strWide;
  823. s2.AppendUtf16(UTF8ToWide(strUtf8));
  824. UNIT_ASSERT(testAppend == s2);
  825. testAppend += ' ';
  826. s2.AppendAscii(" ");
  827. UNIT_ASSERT(testAppend == s2);
  828. testAppend += '_';
  829. s2.AppendUtf8("_");
  830. UNIT_ASSERT(testAppend == s2);
  831. testAppend += wtext;
  832. s2.AppendAscii(text);
  833. UNIT_ASSERT(testAppend == s2);
  834. testAppend += wtext;
  835. s2.AppendUtf8(text);
  836. UNIT_ASSERT(testAppend == s2);
  837. }
  838. void TestLetOperator() {
  839. TUtf32String str;
  840. str = wchar32('X');
  841. UNIT_ASSERT(str == TUtf32String::FromAscii("X"));
  842. const TUtf32String hello = TUtf32String::FromAscii("hello");
  843. str = hello.data();
  844. UNIT_ASSERT(str == hello);
  845. str = hello;
  846. UNIT_ASSERT(str == hello);
  847. }
  848. void TestStringLiterals() {
  849. TUtf32String s1 = U"hello";
  850. UNIT_ASSERT_VALUES_EQUAL(s1, TUtf32String::FromAscii("hello"));
  851. TUtf32String s2 = U"привет";
  852. UNIT_ASSERT_VALUES_EQUAL(s2, TUtf32String::FromUtf8("привет"));
  853. }
  854. };
  855. UNIT_TEST_SUITE_REGISTRATION(TUtf32StringTest);
  856. class TStringStdTest: public TTestBase, private TStringStdTestImpl<TString, TTestData<char>> {
  857. public:
  858. UNIT_TEST_SUITE(TStringStdTest);
  859. UNIT_TEST(Constructor);
  860. UNIT_TEST(reserve);
  861. UNIT_TEST(short_string);
  862. UNIT_TEST(erase);
  863. UNIT_TEST(data);
  864. UNIT_TEST(c_str);
  865. UNIT_TEST(null_char_of_empty);
  866. UNIT_TEST(null_char);
  867. UNIT_TEST(null_char_assignment_to_subscript_of_empty);
  868. UNIT_TEST(null_char_assignment_to_subscript_of_nonempty);
  869. #ifndef TSTRING_IS_STD_STRING
  870. UNIT_TEST(null_char_assignment_to_end_of_empty);
  871. UNIT_TEST(null_char_assignment_to_end_of_nonempty);
  872. #endif
  873. UNIT_TEST(insert);
  874. UNIT_TEST(resize);
  875. UNIT_TEST(find);
  876. UNIT_TEST(capacity);
  877. UNIT_TEST(assign);
  878. UNIT_TEST(copy);
  879. UNIT_TEST(cbegin_cend);
  880. UNIT_TEST(compare);
  881. UNIT_TEST(find_last_of);
  882. #if 0
  883. UNIT_TEST(rfind);
  884. UNIT_TEST(replace);
  885. #endif
  886. UNIT_TEST(find_last_not_of);
  887. UNIT_TEST_SUITE_END();
  888. };
  889. UNIT_TEST_SUITE_REGISTRATION(TStringStdTest);
  890. class TWideStringStdTest: public TTestBase, private TStringStdTestImpl<TUtf16String, TTestData<wchar16>> {
  891. public:
  892. UNIT_TEST_SUITE(TWideStringStdTest);
  893. UNIT_TEST(Constructor);
  894. UNIT_TEST(reserve);
  895. UNIT_TEST(short_string);
  896. UNIT_TEST(erase);
  897. UNIT_TEST(data);
  898. UNIT_TEST(c_str);
  899. UNIT_TEST(null_char_of_empty);
  900. UNIT_TEST(null_char);
  901. UNIT_TEST(null_char_assignment_to_subscript_of_empty);
  902. UNIT_TEST(null_char_assignment_to_subscript_of_nonempty);
  903. #ifndef TSTRING_IS_STD_STRING
  904. UNIT_TEST(null_char_assignment_to_end_of_empty);
  905. UNIT_TEST(null_char_assignment_to_end_of_nonempty);
  906. #endif
  907. UNIT_TEST(insert);
  908. UNIT_TEST(resize);
  909. UNIT_TEST(find);
  910. UNIT_TEST(capacity);
  911. UNIT_TEST(assign);
  912. UNIT_TEST(copy);
  913. UNIT_TEST(cbegin_cend);
  914. UNIT_TEST(compare);
  915. UNIT_TEST(find_last_of);
  916. #if 0
  917. UNIT_TEST(rfind);
  918. UNIT_TEST(replace);
  919. #endif
  920. UNIT_TEST(find_last_not_of);
  921. UNIT_TEST_SUITE_END();
  922. };
  923. UNIT_TEST_SUITE_REGISTRATION(TWideStringStdTest);
  924. Y_UNIT_TEST_SUITE(TStringConversionTest) {
  925. Y_UNIT_TEST(ConversionToStdStringTest) {
  926. TString abra = "cadabra";
  927. std::string stdAbra = abra;
  928. UNIT_ASSERT_VALUES_EQUAL(stdAbra, "cadabra");
  929. }
  930. Y_UNIT_TEST(ConversionToStdStringViewTest) {
  931. TString abra = "cadabra";
  932. std::string_view stdAbra = abra;
  933. UNIT_ASSERT_VALUES_EQUAL(stdAbra, "cadabra");
  934. }
  935. }
  936. Y_UNIT_TEST_SUITE(HashFunctorTests) {
  937. Y_UNIT_TEST(TestTransparency) {
  938. THash<TString> h;
  939. const char* ptr = "a";
  940. const TStringBuf strbuf = ptr;
  941. const TString str = ptr;
  942. const std::string stdStr = ptr;
  943. UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(strbuf));
  944. UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(str));
  945. UNIT_ASSERT_VALUES_EQUAL(h(ptr), h(stdStr));
  946. }
  947. }
  948. #if !defined(TSTRING_IS_STD_STRING)
  949. Y_UNIT_TEST_SUITE(StdNonConformant) {
  950. Y_UNIT_TEST(TestEraseNoThrow) {
  951. TString x;
  952. LegacyErase(x, 10);
  953. }
  954. Y_UNIT_TEST(TestReplaceNoThrow) {
  955. TString x;
  956. LegacyReplace(x, 0, 0, "1");
  957. UNIT_ASSERT_VALUES_EQUAL(x, "1");
  958. LegacyReplace(x, 10, 0, "1");
  959. UNIT_ASSERT_VALUES_EQUAL(x, "1");
  960. }
  961. Y_UNIT_TEST(TestNoAlias) {
  962. TString s = "x";
  963. s.AppendNoAlias("abc", 3);
  964. UNIT_ASSERT_VALUES_EQUAL(s, "xabc");
  965. UNIT_ASSERT_VALUES_EQUAL(TString(s.c_str()), "xabc");
  966. }
  967. }
  968. #endif
  969. Y_UNIT_TEST_SUITE(Interop) {
  970. static void Mutate(std::string& s) {
  971. s += "y";
  972. }
  973. static void Mutate(TString& s) {
  974. Mutate(MutRef(s));
  975. }
  976. Y_UNIT_TEST(TestMutate) {
  977. TString x = "x";
  978. Mutate(x);
  979. UNIT_ASSERT_VALUES_EQUAL(x, "xy");
  980. }
  981. static std::string TransformStd(const std::string& s) {
  982. return s + "y";
  983. }
  984. static TString Transform(const TString& s) {
  985. return TransformStd(s);
  986. }
  987. Y_UNIT_TEST(TestTransform) {
  988. UNIT_ASSERT_VALUES_EQUAL(Transform(TString("x")), "xy");
  989. }
  990. Y_UNIT_TEST(TestTemp) {
  991. UNIT_ASSERT_VALUES_EQUAL("x" + ConstRef(TString("y")), "xy");
  992. }
  993. }