BLI_string_test.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* Apache License, Version 2.0 */
  2. #include "testing/testing.h"
  3. #include <initializer_list>
  4. #include <ostream> // NOLINT
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. extern "C" {
  9. #include "BLI_utildefines.h"
  10. #include "BLI_string.h"
  11. #include "BLI_string_utf8.h"
  12. }
  13. using std::initializer_list;
  14. using std::pair;
  15. using std::string;
  16. using std::vector;
  17. /* -------------------------------------------------------------------- */
  18. /* stubs */
  19. extern "C" {
  20. int mk_wcwidth(wchar_t ucs);
  21. int mk_wcswidth(const wchar_t *pwcs, size_t n);
  22. int mk_wcwidth(wchar_t ucs)
  23. {
  24. return 0;
  25. }
  26. int mk_wcswidth(const wchar_t *pwcs, size_t n)
  27. {
  28. return 0;
  29. }
  30. }
  31. /* -------------------------------------------------------------------- */
  32. /* tests */
  33. /* BLI_str_partition */
  34. TEST(string, StrPartition)
  35. {
  36. const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
  37. const char *sep, *suf;
  38. size_t pre_ln;
  39. {
  40. const char *str = "mat.e-r_ial";
  41. /* "mat.e-r_ial" -> "mat", '.', "e-r_ial", 3 */
  42. pre_ln = BLI_str_partition(str, delim, &sep, &suf);
  43. EXPECT_EQ(pre_ln, 3);
  44. EXPECT_EQ(&str[3], sep);
  45. EXPECT_STREQ("e-r_ial", suf);
  46. }
  47. /* Corner cases. */
  48. {
  49. const char *str = ".mate-rial--";
  50. /* ".mate-rial--" -> "", '.', "mate-rial--", 0 */
  51. pre_ln = BLI_str_partition(str, delim, &sep, &suf);
  52. EXPECT_EQ(pre_ln, 0);
  53. EXPECT_EQ(&str[0], sep);
  54. EXPECT_STREQ("mate-rial--", suf);
  55. }
  56. {
  57. const char *str = ".__.--_";
  58. /* ".__.--_" -> "", '.', "__.--_", 0 */
  59. pre_ln = BLI_str_partition(str, delim, &sep, &suf);
  60. EXPECT_EQ(pre_ln, 0);
  61. EXPECT_EQ(&str[0], sep);
  62. EXPECT_STREQ("__.--_", suf);
  63. }
  64. {
  65. const char *str = "";
  66. /* "" -> "", NULL, NULL, 0 */
  67. pre_ln = BLI_str_partition(str, delim, &sep, &suf);
  68. EXPECT_EQ(pre_ln, 0);
  69. EXPECT_EQ(sep, (void *)NULL);
  70. EXPECT_EQ(suf, (void *)NULL);
  71. }
  72. {
  73. const char *str = "material";
  74. /* "material" -> "material", NULL, NULL, 8 */
  75. pre_ln = BLI_str_partition(str, delim, &sep, &suf);
  76. EXPECT_EQ(pre_ln, 8);
  77. EXPECT_EQ(sep, (void *)NULL);
  78. EXPECT_EQ(suf, (void *)NULL);
  79. }
  80. }
  81. /* BLI_str_rpartition */
  82. TEST(string, StrRPartition)
  83. {
  84. const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
  85. const char *sep, *suf;
  86. size_t pre_ln;
  87. {
  88. const char *str = "mat.e-r_ial";
  89. /* "mat.e-r_ial" -> "mat.e-r", '_', "ial", 7 */
  90. pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
  91. EXPECT_EQ(pre_ln, 7);
  92. EXPECT_EQ(&str[7], sep);
  93. EXPECT_STREQ("ial", suf);
  94. }
  95. /* Corner cases. */
  96. {
  97. const char *str = ".mate-rial--";
  98. /* ".mate-rial--" -> ".mate-rial-", '-', "", 11 */
  99. pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
  100. EXPECT_EQ(pre_ln, 11);
  101. EXPECT_EQ(&str[11], sep);
  102. EXPECT_STREQ("", suf);
  103. }
  104. {
  105. const char *str = ".__.--_";
  106. /* ".__.--_" -> ".__.--", '_', "", 6 */
  107. pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
  108. EXPECT_EQ(pre_ln, 6);
  109. EXPECT_EQ(&str[6], sep);
  110. EXPECT_STREQ("", suf);
  111. }
  112. {
  113. const char *str = "";
  114. /* "" -> "", NULL, NULL, 0 */
  115. pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
  116. EXPECT_EQ(pre_ln, 0);
  117. EXPECT_EQ(sep, (void *)NULL);
  118. EXPECT_EQ(suf, (void *)NULL);
  119. }
  120. {
  121. const char *str = "material";
  122. /* "material" -> "material", NULL, NULL, 8 */
  123. pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
  124. EXPECT_EQ(pre_ln, 8);
  125. EXPECT_EQ(sep, (void *)NULL);
  126. EXPECT_EQ(suf, (void *)NULL);
  127. }
  128. }
  129. /* BLI_str_partition_ex */
  130. TEST(string, StrPartitionEx)
  131. {
  132. const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
  133. const char *sep, *suf;
  134. size_t pre_ln;
  135. /* Only considering 'from_right' cases here. */
  136. {
  137. const char *str = "mat.e-r_ia.l";
  138. /* "mat.e-r_ia.l" over "mat.e-r" -> "mat.e", '.', "r_ia.l", 3 */
  139. pre_ln = BLI_str_partition_ex(str, str + 6, delim, &sep, &suf, true);
  140. EXPECT_EQ(pre_ln, 5);
  141. EXPECT_EQ(&str[5], sep);
  142. EXPECT_STREQ("r_ia.l", suf);
  143. }
  144. /* Corner cases. */
  145. {
  146. const char *str = "mate.rial";
  147. /* "mate.rial" over "mate" -> "mate.rial", NULL, NULL, 4 */
  148. pre_ln = BLI_str_partition_ex(str, str + 4, delim, &sep, &suf, true);
  149. EXPECT_EQ(pre_ln, 4);
  150. EXPECT_EQ(sep, (void *)NULL);
  151. EXPECT_EQ(suf, (void *)NULL);
  152. }
  153. }
  154. /* BLI_str_partition_utf8 */
  155. TEST(string, StrPartitionUtf8)
  156. {
  157. const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
  158. const char *sep, *suf;
  159. size_t pre_ln;
  160. {
  161. const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
  162. /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "ma", '\xc3\xb1', "te-r\xe2\x98\xafial", 2 */
  163. pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
  164. EXPECT_EQ(pre_ln, 2);
  165. EXPECT_EQ(&str[2], sep);
  166. EXPECT_STREQ("te-r\xe2\x98\xafial", suf);
  167. }
  168. /* Corner cases. */
  169. {
  170. const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
  171. /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "", '\xe2\x98\xaf', "mate-rial-\xc3\xb1", 0 */
  172. pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
  173. EXPECT_EQ(pre_ln, 0);
  174. EXPECT_EQ(&str[0], sep);
  175. EXPECT_STREQ("mate-rial-\xc3\xb1", suf);
  176. }
  177. {
  178. const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
  179. /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "", '\xe2\x98\xaf', ".\xc3\xb1_.--\xc3\xb1", 0 */
  180. pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
  181. EXPECT_EQ(pre_ln, 0);
  182. EXPECT_EQ(&str[0], sep);
  183. EXPECT_STREQ(".\xc3\xb1_.--\xc3\xb1", suf);
  184. }
  185. {
  186. const char *str = "";
  187. /* "" -> "", NULL, NULL, 0 */
  188. pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
  189. EXPECT_EQ(pre_ln, 0);
  190. EXPECT_EQ(sep, (void *)NULL);
  191. EXPECT_EQ(suf, (void *)NULL);
  192. }
  193. {
  194. const char *str = "material";
  195. /* "material" -> "material", NULL, NULL, 8 */
  196. pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
  197. EXPECT_EQ(pre_ln, 8);
  198. EXPECT_EQ(sep, (void *)NULL);
  199. EXPECT_EQ(suf, (void *)NULL);
  200. }
  201. }
  202. /* BLI_str_rpartition_utf8 */
  203. TEST(string, StrRPartitionUtf8)
  204. {
  205. const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
  206. const char *sep, *suf;
  207. size_t pre_ln;
  208. {
  209. const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
  210. /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "mat\xc3\xb1te-r", '\xe2\x98\xaf', "ial", 8 */
  211. pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
  212. EXPECT_EQ(pre_ln, 8);
  213. EXPECT_EQ(&str[8], sep);
  214. EXPECT_STREQ("ial", suf);
  215. }
  216. /* Corner cases. */
  217. {
  218. const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
  219. /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "\xe2\x98\xafmate-rial-", '\xc3\xb1', "", 13 */
  220. pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
  221. EXPECT_EQ(pre_ln, 13);
  222. EXPECT_EQ(&str[13], sep);
  223. EXPECT_STREQ("", suf);
  224. }
  225. {
  226. const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
  227. /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "\xe2\x98\xaf.\xc3\xb1_.--", '\xc3\xb1', "", 10 */
  228. pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
  229. EXPECT_EQ(pre_ln, 10);
  230. EXPECT_EQ(&str[10], sep);
  231. EXPECT_STREQ("", suf);
  232. }
  233. {
  234. const char *str = "";
  235. /* "" -> "", NULL, NULL, 0 */
  236. pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
  237. EXPECT_EQ(pre_ln, 0);
  238. EXPECT_EQ(sep, (void *)NULL);
  239. EXPECT_EQ(suf, (void *)NULL);
  240. }
  241. {
  242. const char *str = "material";
  243. /* "material" -> "material", NULL, NULL, 8 */
  244. pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
  245. EXPECT_EQ(pre_ln, 8);
  246. EXPECT_EQ(sep, (void *)NULL);
  247. EXPECT_EQ(suf, (void *)NULL);
  248. }
  249. }
  250. /* BLI_str_partition_ex_utf8 */
  251. TEST(string, StrPartitionExUtf8)
  252. {
  253. const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
  254. const char *sep, *suf;
  255. size_t pre_ln;
  256. /* Only considering 'from_right' cases here. */
  257. {
  258. const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
  259. /* "ma\xc3\xb1te-r\xe2\x98\xafial" over
  260. * "ma\xc3\xb1te" -> "ma", '\xc3\xb1', "te-r\xe2\x98\xafial", 2 */
  261. pre_ln = BLI_str_partition_ex_utf8(str, str + 6, delim, &sep, &suf, true);
  262. EXPECT_EQ(pre_ln, 2);
  263. EXPECT_EQ(&str[2], sep);
  264. EXPECT_STREQ("te-r\xe2\x98\xafial", suf);
  265. }
  266. /* Corner cases. */
  267. {
  268. const char *str = "mate\xe2\x98\xafrial";
  269. /* "mate\xe2\x98\xafrial" over "mate" -> "mate\xe2\x98\xafrial", NULL, NULL, 4 */
  270. pre_ln = BLI_str_partition_ex_utf8(str, str + 4, delim, &sep, &suf, true);
  271. EXPECT_EQ(pre_ln, 4);
  272. EXPECT_EQ(sep, (void *)NULL);
  273. EXPECT_EQ(suf, (void *)NULL);
  274. }
  275. }
  276. /* BLI_str_format_int_grouped */
  277. TEST(string, StrFormatIntGrouped)
  278. {
  279. char num_str[16];
  280. int num;
  281. BLI_str_format_int_grouped(num_str, num = 0);
  282. EXPECT_STREQ("0", num_str);
  283. BLI_str_format_int_grouped(num_str, num = 1);
  284. EXPECT_STREQ("1", num_str);
  285. BLI_str_format_int_grouped(num_str, num = -1);
  286. EXPECT_STREQ("-1", num_str);
  287. BLI_str_format_int_grouped(num_str, num = -2147483648);
  288. EXPECT_STREQ("-2,147,483,648", num_str);
  289. BLI_str_format_int_grouped(num_str, num = 2147483647);
  290. EXPECT_STREQ("2,147,483,647", num_str);
  291. BLI_str_format_int_grouped(num_str, num = 1000);
  292. EXPECT_STREQ("1,000", num_str);
  293. BLI_str_format_int_grouped(num_str, num = -1000);
  294. EXPECT_STREQ("-1,000", num_str);
  295. BLI_str_format_int_grouped(num_str, num = 999);
  296. EXPECT_STREQ("999", num_str);
  297. BLI_str_format_int_grouped(num_str, num = -999);
  298. EXPECT_STREQ("-999", num_str);
  299. }
  300. /* BLI_str_format_byte_unit */
  301. TEST(string, StrFormatByteUnits)
  302. {
  303. char size_str[15];
  304. long long int size;
  305. /* Base 10 */
  306. BLI_str_format_byte_unit(size_str, size = 0, true);
  307. EXPECT_STREQ("0 B", size_str);
  308. BLI_str_format_byte_unit(size_str, size = -0, true);
  309. EXPECT_STREQ("0 B", size_str);
  310. BLI_str_format_byte_unit(size_str, size = 1, true);
  311. EXPECT_STREQ("1 B", size_str);
  312. BLI_str_format_byte_unit(size_str, size = -1, true);
  313. EXPECT_STREQ("-1 B", size_str);
  314. BLI_str_format_byte_unit(size_str, size = 1000, true);
  315. EXPECT_STREQ("1 KB", size_str);
  316. BLI_str_format_byte_unit(size_str, size = -1000, true);
  317. EXPECT_STREQ("-1 KB", size_str);
  318. BLI_str_format_byte_unit(size_str, size = 1024, true);
  319. EXPECT_STREQ("1 KB", size_str);
  320. BLI_str_format_byte_unit(size_str, size = -1024, true);
  321. EXPECT_STREQ("-1 KB", size_str);
  322. BLI_str_format_byte_unit(
  323. size_str, size = 9223372036854775807, true); /* LLONG_MAX - largest possible value */
  324. EXPECT_STREQ("9223.372 PB", size_str);
  325. BLI_str_format_byte_unit(size_str, size = -9223372036854775807, true);
  326. EXPECT_STREQ("-9223.372 PB", size_str);
  327. /* Base 2 */
  328. BLI_str_format_byte_unit(size_str, size = 0, false);
  329. EXPECT_STREQ("0 B", size_str);
  330. BLI_str_format_byte_unit(size_str, size = -0, false);
  331. EXPECT_STREQ("0 B", size_str);
  332. BLI_str_format_byte_unit(size_str, size = 1, false);
  333. EXPECT_STREQ("1 B", size_str);
  334. BLI_str_format_byte_unit(size_str, size = -1, false);
  335. EXPECT_STREQ("-1 B", size_str);
  336. BLI_str_format_byte_unit(size_str, size = 1000, false);
  337. EXPECT_STREQ("1000 B", size_str);
  338. BLI_str_format_byte_unit(size_str, size = -1000, false);
  339. EXPECT_STREQ("-1000 B", size_str);
  340. BLI_str_format_byte_unit(size_str, size = 1024, false);
  341. EXPECT_STREQ("1 KiB", size_str);
  342. BLI_str_format_byte_unit(size_str, size = -1024, false);
  343. EXPECT_STREQ("-1 KiB", size_str);
  344. BLI_str_format_byte_unit(
  345. size_str, size = 9223372036854775807, false); /* LLONG_MAX - largest possible value */
  346. EXPECT_STREQ("8192.0 PiB", size_str);
  347. BLI_str_format_byte_unit(size_str, size = -9223372036854775807, false);
  348. EXPECT_STREQ("-8192.0 PiB", size_str);
  349. /* Test maximum string length. */
  350. BLI_str_format_byte_unit(size_str, size = -9223200000000000000, false);
  351. EXPECT_STREQ("-8191.8472 PiB", size_str);
  352. }
  353. struct WordInfo {
  354. WordInfo()
  355. {
  356. }
  357. WordInfo(int start, int end) : start(start), end(end)
  358. {
  359. }
  360. bool operator==(const WordInfo &other) const
  361. {
  362. return start == other.start && end == other.end;
  363. }
  364. int start, end;
  365. };
  366. std::ostream &operator<<(std::ostream &os, const WordInfo &word_info)
  367. {
  368. os << "start: " << word_info.start << ", end: " << word_info.end;
  369. return os;
  370. }
  371. class StringFindSplitWords : public testing::Test {
  372. protected:
  373. StringFindSplitWords()
  374. {
  375. }
  376. /* If max_words is -1 it will be initialized from the number of expected
  377. * words +1. This way there is no need to pass an explicit number of words,
  378. * but is also making it possible to catch situation when too many words
  379. * are being returned. */
  380. void testStringFindSplitWords(const string &str,
  381. const size_t max_length,
  382. initializer_list<WordInfo> expected_words_info_init,
  383. int max_words = -1)
  384. {
  385. const vector<WordInfo> expected_words_info = expected_words_info_init;
  386. if (max_words != -1) {
  387. CHECK_LE(max_words, expected_words_info.size() - 1);
  388. }
  389. /* Since number of word info is used here, this makes it so we allow one
  390. * extra word to be collected from the input. This allows to catch possible
  391. * issues with word splitting not doing a correct thing. */
  392. const int effective_max_words = (max_words == -1) ? expected_words_info.size() : max_words;
  393. /* One extra element for the {-1, -1}. */
  394. vector<WordInfo> actual_word_info(effective_max_words + 1, WordInfo(-1, -1));
  395. const int actual_word_num = BLI_string_find_split_words(
  396. str.c_str(),
  397. max_length,
  398. ' ',
  399. reinterpret_cast<int(*)[2]>(actual_word_info.data()),
  400. effective_max_words);
  401. /* Schrink actual array to an actual number of words, so we can compare
  402. * vectors as-is. */
  403. EXPECT_LE(actual_word_num, actual_word_info.size() - 1);
  404. actual_word_info.resize(actual_word_num + 1);
  405. /* Perform actual comparison. */
  406. EXPECT_EQ_VECTOR(actual_word_info, expected_words_info);
  407. }
  408. void testStringFindSplitWords(const string &str,
  409. initializer_list<WordInfo> expected_words_info_init)
  410. {
  411. testStringFindSplitWords(str, str.length(), expected_words_info_init);
  412. }
  413. };
  414. /* BLI_string_find_split_words */
  415. TEST_F(StringFindSplitWords, Simple)
  416. {
  417. testStringFindSplitWords("t", {{0, 1}, {-1, -1}});
  418. testStringFindSplitWords("test", {{0, 4}, {-1, -1}});
  419. }
  420. TEST_F(StringFindSplitWords, Triple)
  421. {
  422. testStringFindSplitWords("f t w", {{0, 1}, {2, 1}, {4, 1}, {-1, -1}});
  423. testStringFindSplitWords("find three words", {{0, 4}, {5, 5}, {11, 5}, {-1, -1}});
  424. }
  425. TEST_F(StringFindSplitWords, Spacing)
  426. {
  427. testStringFindSplitWords("# ## ### ####", {{0, 1}, {2, 2}, {5, 3}, {9, 4}, {-1, -1}});
  428. testStringFindSplitWords("# # # #", {{0, 1}, {3, 1}, {7, 1}, {12, 1}, {-1, -1}});
  429. }
  430. TEST_F(StringFindSplitWords, Trailing_Left)
  431. {
  432. testStringFindSplitWords(" t", {{3, 1}, {-1, -1}});
  433. testStringFindSplitWords(" test", {{3, 4}, {-1, -1}});
  434. }
  435. TEST_F(StringFindSplitWords, Trailing_Right)
  436. {
  437. testStringFindSplitWords("t ", {{0, 1}, {-1, -1}});
  438. testStringFindSplitWords("test ", {{0, 4}, {-1, -1}});
  439. }
  440. TEST_F(StringFindSplitWords, Trailing_LeftRight)
  441. {
  442. testStringFindSplitWords(" surrounding space test 123 ",
  443. {{3, 11}, {15, 5}, {21, 4}, {28, 3}, {-1, -1}});
  444. }
  445. TEST_F(StringFindSplitWords, Blank)
  446. {
  447. testStringFindSplitWords("", {{-1, -1}});
  448. }
  449. TEST_F(StringFindSplitWords, Whitespace)
  450. {
  451. testStringFindSplitWords(" ", {{-1, -1}});
  452. testStringFindSplitWords(" ", {{-1, -1}});
  453. }
  454. TEST_F(StringFindSplitWords, LimitWords)
  455. {
  456. const string words = "too many chars";
  457. const int words_len = words.length();
  458. testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {9, 5}, {-1, -1}}, 3);
  459. testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {-1, -1}}, 2);
  460. testStringFindSplitWords(words, words_len, {{0, 3}, {-1, -1}}, 1);
  461. testStringFindSplitWords(words, words_len, {{-1, -1}}, 0);
  462. }
  463. TEST_F(StringFindSplitWords, LimitChars)
  464. {
  465. const string words = "too many chars";
  466. const int words_len = words.length();
  467. testStringFindSplitWords(words, words_len, {{0, 3}, {4, 4}, {9, 5}, {-1, -1}});
  468. testStringFindSplitWords(words, words_len - 1, {{0, 3}, {4, 4}, {9, 4}, {-1, -1}});
  469. testStringFindSplitWords(words, words_len - 5, {{0, 3}, {4, 4}, {-1, -1}});
  470. testStringFindSplitWords(words, 1, {{0, 1}, {-1, -1}});
  471. testStringFindSplitWords(words, 0, {{-1, -1}});
  472. }
  473. /* BLI_strncasestr */
  474. TEST(string, StringStrncasestr)
  475. {
  476. const char *str_test0 = "search here";
  477. const char *res;
  478. res = BLI_strncasestr(str_test0, "", 0);
  479. EXPECT_EQ(res, str_test0);
  480. res = BLI_strncasestr(str_test0, " ", 1);
  481. EXPECT_EQ(res, str_test0 + 6);
  482. res = BLI_strncasestr(str_test0, "her", 3);
  483. EXPECT_EQ(res, str_test0 + 7);
  484. res = BLI_strncasestr(str_test0, "ARCh", 4);
  485. EXPECT_EQ(res, str_test0 + 2);
  486. res = BLI_strncasestr(str_test0, "earcq", 4);
  487. EXPECT_EQ(res, str_test0 + 1);
  488. res = BLI_strncasestr(str_test0, "not there", 9);
  489. EXPECT_EQ(res, (void *)NULL);
  490. }