/ceammc/ext/tests/cells/test_string.cpp

https://github.com/uliss/pure-data · C++ · 362 lines · 296 code · 49 blank · 17 comment · 165 complexity · 532a35e590954638ef1321a2b3638c73 MD5 · raw file

  1. /*****************************************************************************
  2. * Copyright 2017 Serge Poltavsky. All rights reserved.
  3. *
  4. * This file may be distributed under the terms of GNU Public License version
  5. * 3 (GPL v3) as defined by the Free Software Foundation (FSF). A copy of the
  6. * license should have been included with this file, or the project in which
  7. * this file belongs to. You may also find the details of GPL v3 at:
  8. * http://www.gnu.org/licenses/gpl-3.0.txt
  9. *
  10. * If you have any questions regarding the use of this file, feel free to
  11. * contact the author of this file, or the owner of the project in which
  12. * this file belongs to.
  13. *****************************************************************************/
  14. #include "catch.hpp"
  15. #include "ceammc_string.h"
  16. #include <cstring>
  17. using namespace ceammc::string;
  18. TEST_CASE("ceammc_string", "[PureData]")
  19. {
  20. SECTION("utf8_strlen")
  21. {
  22. REQUIRE(utf8_strlen("") == 0);
  23. REQUIRE(utf8_strlen("a") == 1);
  24. REQUIRE(utf8_strlen("abcd") == 4);
  25. REQUIRE(utf8_strlen("ф") == 1);
  26. REQUIRE(utf8_strlen("jй") == 2);
  27. REQUIRE(utf8_strlen("моцарт") == 6);
  28. REQUIRE(strlen("的") == 3);
  29. REQUIRE(utf8_strlen("的") == 1);
  30. REQUIRE(utf8_strlen("的的的 的的") == 6);
  31. std::string s;
  32. for (size_t i = 0; i < 200; i++)
  33. s += "jй";
  34. REQUIRE(utf8_strlen(s.c_str()) == 400);
  35. REQUIRE(s.length() == 600);
  36. }
  37. SECTION("utf8_substr")
  38. {
  39. REQUIRE(utf8_substr("", 0, 0) == "");
  40. REQUIRE(utf8_substr("TEST", 4, 0) == "");
  41. REQUIRE(utf8_substr("ТЕСТ", 4, 0) == "");
  42. REQUIRE(utf8_substr("TEST", 5, 0) == "");
  43. REQUIRE(utf8_substr("ТЕСТ", 5, 0) == "");
  44. REQUIRE(utf8_substr("TEST", 0, 1) == "T");
  45. REQUIRE(utf8_substr("TEST", 1, 1) == "E");
  46. REQUIRE(utf8_substr("TEST", 2, 1) == "S");
  47. REQUIRE(utf8_substr("TEST", 3, 1) == "T");
  48. REQUIRE(utf8_substr("TEST", 0, 2) == "TE");
  49. REQUIRE(utf8_substr("TEST", 1, 2) == "ES");
  50. REQUIRE(utf8_substr("TEST", 2, 2) == "ST");
  51. REQUIRE(utf8_substr("TEST", 3, 2) == "T");
  52. REQUIRE(utf8_substr("TEST", 0, 3) == "TES");
  53. REQUIRE(utf8_substr("TEST", 1, 3) == "EST");
  54. REQUIRE(utf8_substr("TEST", 2, 3) == "ST");
  55. REQUIRE(utf8_substr("TEST", 3, 3) == "T");
  56. REQUIRE(utf8_substr("TEST", 0, 4) == "TEST");
  57. REQUIRE(utf8_substr("TEST", 1, 4) == "EST");
  58. REQUIRE(utf8_substr("TEST", 2, 4) == "ST");
  59. REQUIRE(utf8_substr("TEST", 3, 4) == "T");
  60. REQUIRE(utf8_substr("TEST", 0, 5) == "TEST");
  61. REQUIRE(utf8_substr("TEST", 1, 5) == "EST");
  62. REQUIRE(utf8_substr("TEST", 2, 5) == "ST");
  63. REQUIRE(utf8_substr("TEST", 3, 5) == "T");
  64. REQUIRE(utf8_substr("TEST", -4, 0) == "");
  65. REQUIRE(utf8_substr("TEST", -3, 0) == "");
  66. REQUIRE(utf8_substr("TEST", -2, 0) == "");
  67. REQUIRE(utf8_substr("TEST", -1, 0) == "");
  68. REQUIRE(utf8_substr("TEST", -4, 1) == "T");
  69. REQUIRE(utf8_substr("TEST", -3, 1) == "E");
  70. REQUIRE(utf8_substr("TEST", -2, 1) == "S");
  71. REQUIRE(utf8_substr("TEST", -1, 1) == "T");
  72. REQUIRE(utf8_substr("TEST", -4, 2) == "TE");
  73. REQUIRE(utf8_substr("TEST", -3, 2) == "ES");
  74. REQUIRE(utf8_substr("TEST", -2, 2) == "ST");
  75. REQUIRE(utf8_substr("TEST", -1, 2) == "T");
  76. REQUIRE(utf8_substr("TEST", -4, 3) == "TES");
  77. REQUIRE(utf8_substr("TEST", -3, 3) == "EST");
  78. REQUIRE(utf8_substr("TEST", -2, 3) == "ST");
  79. REQUIRE(utf8_substr("TEST", -1, 3) == "T");
  80. REQUIRE(utf8_substr("TEST", -4, 4) == "TEST");
  81. REQUIRE(utf8_substr("TEST", -3, 4) == "EST");
  82. REQUIRE(utf8_substr("TEST", -2, 4) == "ST");
  83. REQUIRE(utf8_substr("TEST", -1, 4) == "T");
  84. REQUIRE(utf8_substr("TEST", -4, 5) == "TEST");
  85. REQUIRE(utf8_substr("TEST", -3, 5) == "EST");
  86. REQUIRE(utf8_substr("TEST", -2, 5) == "ST");
  87. REQUIRE(utf8_substr("TEST", -1, 5) == "T");
  88. SECTION("utf8")
  89. {
  90. REQUIRE(utf8_substr("ТЕСТ", 0, 1) == "Т");
  91. REQUIRE(utf8_substr("ТЕСТ", 1, 1) == "Е");
  92. REQUIRE(utf8_substr("ТЕСТ", 2, 1) == "С");
  93. REQUIRE(utf8_substr("ТЕСТ", 3, 1) == "Т");
  94. REQUIRE(utf8_substr("ТЕСТ", 0, 2) == "ТЕ");
  95. REQUIRE(utf8_substr("ТЕСТ", 1, 2) == "ЕС");
  96. REQUIRE(utf8_substr("ТЕСТ", 2, 2) == "СТ");
  97. REQUIRE(utf8_substr("ТЕСТ", 3, 2) == "Т");
  98. REQUIRE(utf8_substr("ТЕСТ", 0, 3) == "ТЕС");
  99. REQUIRE(utf8_substr("ТЕСТ", 1, 3) == "ЕСТ");
  100. REQUIRE(utf8_substr("ТЕСТ", 2, 3) == "СТ");
  101. REQUIRE(utf8_substr("ТЕСТ", 3, 3) == "Т");
  102. REQUIRE(utf8_substr("ТЕСТ", 0, 4) == "ТЕСТ");
  103. REQUIRE(utf8_substr("ТЕСТ", 1, 4) == "ЕСТ");
  104. REQUIRE(utf8_substr("ТЕСТ", 2, 4) == "СТ");
  105. REQUIRE(utf8_substr("ТЕСТ", 3, 4) == "Т");
  106. REQUIRE(utf8_substr("ТЕСТ", 0, 5) == "ТЕСТ");
  107. REQUIRE(utf8_substr("ТЕСТ", 1, 5) == "ЕСТ");
  108. REQUIRE(utf8_substr("ТЕСТ", 2, 5) == "СТ");
  109. REQUIRE(utf8_substr("ТЕСТ", 3, 5) == "Т");
  110. REQUIRE(utf8_substr("ТЕСТ", -4, 0) == "");
  111. REQUIRE(utf8_substr("ТЕСТ", -3, 0) == "");
  112. REQUIRE(utf8_substr("ТЕСТ", -2, 0) == "");
  113. REQUIRE(utf8_substr("ТЕСТ", -1, 0) == "");
  114. REQUIRE(utf8_substr("ТЕСТ", -4, 1) == "Т");
  115. REQUIRE(utf8_substr("ТЕСТ", -3, 1) == "Е");
  116. REQUIRE(utf8_substr("ТЕСТ", -2, 1) == "С");
  117. REQUIRE(utf8_substr("ТЕСТ", -1, 1) == "Т");
  118. REQUIRE(utf8_substr("ТЕСТ", -4, 2) == "ТЕ");
  119. REQUIRE(utf8_substr("ТЕСТ", -3, 2) == "ЕС");
  120. REQUIRE(utf8_substr("ТЕСТ", -2, 2) == "СТ");
  121. REQUIRE(utf8_substr("ТЕСТ", -1, 2) == "Т");
  122. REQUIRE(utf8_substr("ТЕСТ", -4, 3) == "ТЕС");
  123. REQUIRE(utf8_substr("ТЕСТ", -3, 3) == "ЕСТ");
  124. REQUIRE(utf8_substr("ТЕСТ", -2, 3) == "СТ");
  125. REQUIRE(utf8_substr("ТЕСТ", -1, 3) == "Т");
  126. REQUIRE(utf8_substr("ТЕСТ", -4, 4) == "ТЕСТ");
  127. REQUIRE(utf8_substr("ТЕСТ", -3, 4) == "ЕСТ");
  128. REQUIRE(utf8_substr("ТЕСТ", -2, 4) == "СТ");
  129. REQUIRE(utf8_substr("ТЕСТ", -1, 4) == "Т");
  130. REQUIRE(utf8_substr("ТЕСТ", -4, 5) == "ТЕСТ");
  131. REQUIRE(utf8_substr("ТЕСТ", -3, 5) == "ЕСТ");
  132. REQUIRE(utf8_substr("ТЕСТ", -2, 5) == "СТ");
  133. REQUIRE(utf8_substr("ТЕСТ", -1, 5) == "Т");
  134. }
  135. }
  136. SECTION("splitByChar")
  137. {
  138. std::vector<std::string> chars;
  139. utf8_split_by_char(chars, "");
  140. REQUIRE(chars.empty());
  141. utf8_split_by_char(chars, "abc");
  142. REQUIRE(chars.size() == 3);
  143. REQUIRE(chars[0] == "a");
  144. REQUIRE(chars[1] == "b");
  145. REQUIRE(chars[2] == "c");
  146. utf8_split_by_char(chars, "абв 123");
  147. REQUIRE(chars.size() == 7);
  148. REQUIRE(chars[0] == "а");
  149. REQUIRE(chars[1] == "б");
  150. REQUIRE(chars[2] == "в");
  151. REQUIRE(chars[3] == " ");
  152. REQUIRE(chars[4] == "1");
  153. REQUIRE(chars[5] == "2");
  154. REQUIRE(chars[6] == "3");
  155. utf8_split_by_char(chars, "1的Ж");
  156. REQUIRE(chars.size() == 3);
  157. REQUIRE(chars[0] == "1");
  158. REQUIRE(chars[1] == "的");
  159. REQUIRE(chars[2] == "Ж");
  160. }
  161. SECTION("utf8_to_upper/lower")
  162. {
  163. REQUIRE(utf8_to_upper("abcde 12345 çå") == "ABCDE 12345 ÇÅ");
  164. REQUIRE(utf8_to_upper("абвгд") == "АБВГД");
  165. REQUIRE(utf8_to_upper("ß") == "SS");
  166. REQUIRE(utf8_to_lower(utf8_to_upper("абвгд").c_str()) == "абвгд");
  167. REQUIRE(utf8_to_lower(utf8_to_upper("abcde 12345 çå").c_str()) == "abcde 12345 çå");
  168. }
  169. SECTION("utf8_insert")
  170. {
  171. REQUIRE(utf8_insert("", 0, "") == "");
  172. REQUIRE(utf8_insert("a", 0, "") == "a");
  173. REQUIRE(utf8_insert("", 0, "Ф") == "Ф");
  174. REQUIRE(utf8_insert("абвгд", 0, "АБВ") == "АБВабвгд");
  175. REQUIRE(utf8_insert("абвгд", 1, "АБВ") == "аАБВбвгд");
  176. REQUIRE(utf8_insert("абвгд", 2, "АБВ") == "абАБВвгд");
  177. REQUIRE(utf8_insert("абвгд", 3, "АБВ") == "абвАБВгд");
  178. REQUIRE(utf8_insert("абвгд", 4, "АБВ") == "абвгАБВд");
  179. REQUIRE(utf8_insert("абвгд", 5, "АБВ") == "абвгдАБВ");
  180. REQUIRE(utf8_insert("абвгд", -1, "АБВ") == "абвгдАБВ");
  181. REQUIRE(utf8_insert("абвгд", -2, "АБВ") == "абвгАБВд");
  182. REQUIRE(utf8_insert("абвгд", -3, "АБВ") == "абвАБВгд");
  183. REQUIRE(utf8_insert("абвгд", -4, "АБВ") == "абАБВвгд");
  184. REQUIRE(utf8_insert("абвгд", -5, "АБВ") == "аАБВбвгд");
  185. REQUIRE(utf8_insert("абвгд", -6, "АБВ") == "АБВабвгд");
  186. REQUIRE_THROWS(utf8_insert("абвгд", 6, "АБВ"), std::exception());
  187. REQUIRE_THROWS(utf8_insert("абвгд", -7, "АБВ"), std::exception());
  188. }
  189. SECTION("starts_with")
  190. {
  191. REQUIRE(starts_with("abc", ""));
  192. REQUIRE(starts_with("abc", "a"));
  193. REQUIRE(starts_with("abc", "ab"));
  194. REQUIRE(starts_with("abc", "abc"));
  195. REQUIRE_FALSE(starts_with("abc", "abcd"));
  196. REQUIRE_FALSE(starts_with("abc", "b"));
  197. // utf8
  198. REQUIRE(starts_with("абв", ""));
  199. REQUIRE(starts_with("абв", "а"));
  200. REQUIRE(starts_with("абв", "аб"));
  201. REQUIRE(starts_with("абв", "абв"));
  202. REQUIRE_FALSE(starts_with("абв", "абвг"));
  203. REQUIRE_FALSE(starts_with("абв", "б"));
  204. }
  205. SECTION("ends_with")
  206. {
  207. REQUIRE(ends_with("file.wav", ".wav"));
  208. REQUIRE(ends_with("abc", ""));
  209. REQUIRE(ends_with("abc", "c"));
  210. REQUIRE(ends_with("abc", "bc"));
  211. REQUIRE(ends_with("abc", "abc"));
  212. REQUIRE_FALSE(ends_with("abc", "ab"));
  213. REQUIRE_FALSE(ends_with("abc", "b"));
  214. // utf8
  215. REQUIRE(ends_with("береза", ""));
  216. REQUIRE(ends_with("береза", "еза"));
  217. REQUIRE(ends_with("береза", "береза"));
  218. REQUIRE_FALSE(ends_with("береза", "абв"));
  219. }
  220. SECTION("contains")
  221. {
  222. REQUIRE(contains("abc", ""));
  223. REQUIRE(contains("abc", "a"));
  224. REQUIRE(contains("abc", "b"));
  225. REQUIRE(contains("abcd", "bc"));
  226. REQUIRE(contains("", ""));
  227. REQUIRE_FALSE(contains("", "abc"));
  228. REQUIRE_FALSE(contains("asdab", "abc"));
  229. // utf8
  230. REQUIRE(contains("абв", ""));
  231. REQUIRE(contains("абв", "а"));
  232. REQUIRE(contains("абв", "аб"));
  233. REQUIRE(contains("абвг", "бв"));
  234. REQUIRE_FALSE(contains("", "абв"));
  235. REQUIRE_FALSE(contains("вапьты", "абв"));
  236. }
  237. SECTION("pd_string_match")
  238. {
  239. std::string str;
  240. REQUIRE(pd_string_match("\"\"", str));
  241. REQUIRE(str == "");
  242. REQUIRE(pd_string_match("\" \"", str));
  243. REQUIRE(str == " ");
  244. REQUIRE(pd_string_match("\"wasn`\"t\"", str));
  245. REQUIRE(str == "wasn`\"t");
  246. REQUIRE(pd_string_match("\"`\"a b c`\"\"", str));
  247. REQUIRE(str == "`\"a b c`\"");
  248. REQUIRE_FALSE(pd_string_match(R"("""")", str));
  249. REQUIRE(pd_string_match(R"("`"`"")", str));
  250. REQUIRE(pd_string_match(R"("`.")", str));
  251. REQUIRE(pd_string_match(R"("`:")", str));
  252. REQUIRE(pd_string_match(R"("`(")", str));
  253. REQUIRE(pd_string_match(R"("`/")", str));
  254. REQUIRE(pd_string_match(R"("``")", str));
  255. REQUIRE_FALSE(pd_string_match(R"("```")", str));
  256. REQUIRE_FALSE(pd_string_match(R"("`n")", str));
  257. REQUIRE_FALSE(pd_string_match(R"("`"``"")", str));
  258. }
  259. SECTION("pd_string_unescape")
  260. {
  261. REQUIRE(pd_string_unescape("") == "");
  262. REQUIRE(pd_string_unescape(" ") == " ");
  263. REQUIRE(pd_string_unescape("wasn't") == "wasn't");
  264. REQUIRE(pd_string_unescape("`\"") == "\"");
  265. REQUIRE(pd_string_unescape("`'") == "'");
  266. REQUIRE(pd_string_unescape("``'") == "`'");
  267. REQUIRE(pd_string_unescape("``") == "`");
  268. REQUIRE(pd_string_unescape("````") == "``");
  269. REQUIRE(pd_string_unescape("`(") == "{");
  270. REQUIRE(pd_string_unescape("`)") == "}");
  271. REQUIRE(pd_string_unescape("`.") == ",");
  272. REQUIRE(pd_string_unescape("`:") == ";");
  273. REQUIRE(pd_string_unescape("'") == "'");
  274. REQUIRE(pd_string_unescape("'") == "'");
  275. REQUIRE(pd_string_unescape(" `\" ") == " \" ");
  276. }
  277. SECTION("pd_string_parse")
  278. {
  279. std::string str;
  280. REQUIRE_FALSE(pd_string_parse("''", str));
  281. REQUIRE(pd_string_parse("\"\"", str));
  282. REQUIRE(str == "");
  283. REQUIRE_FALSE(pd_string_parse("' '", str));
  284. REQUIRE(pd_string_parse("\" \"", str));
  285. REQUIRE(str == " ");
  286. REQUIRE_FALSE(pd_string_parse("wasn't", str));
  287. REQUIRE(pd_string_parse("\"wasn't\"", str));
  288. REQUIRE(str == "wasn't");
  289. REQUIRE(pd_string_parse("\"a`.b`.c\"", str));
  290. REQUIRE(str == "a,b,c");
  291. // check self-reference passing
  292. std::string sp("\" \"");
  293. REQUIRE(pd_string_parse(sp, sp));
  294. REQUIRE(sp == " ");
  295. }
  296. SECTION("is_pd_string")
  297. {
  298. REQUIRE_FALSE(is_pd_string(""));
  299. REQUIRE_FALSE(is_pd_string("\""));
  300. REQUIRE_FALSE(is_pd_string("abc"));
  301. REQUIRE_FALSE(is_pd_string("123"));
  302. REQUIRE_FALSE(is_pd_string("\"123"));
  303. REQUIRE_FALSE(is_pd_string("123\""));
  304. REQUIRE_FALSE(is_pd_string("\"`\""));
  305. REQUIRE(is_pd_string("\"\""));
  306. REQUIRE(is_pd_string("\" \""));
  307. REQUIRE(is_pd_string("\"123\""));
  308. REQUIRE(is_pd_string("\"a b\""));
  309. REQUIRE(is_pd_string("\"a``\""));
  310. REQUIRE(is_pd_string("\"`\" asb `\"\""));
  311. REQUIRE(is_pd_string("\"```\"`/`:`.\""));
  312. }
  313. }