1 /* Apache License, Version 2.0 */
3 #include "testing/testing.h"
6 #include "BLI_utildefines.h"
7 #include "BLI_string.h"
8 #include "BLI_string_utf8.h"
11 /* -------------------------------------------------------------------- */
16 int mk_wcwidth(wchar_t ucs);
17 int mk_wcswidth(const wchar_t *pwcs, size_t n);
19 int mk_wcwidth(wchar_t ucs)
24 int mk_wcswidth(const wchar_t *pwcs, size_t n)
32 /* -------------------------------------------------------------------- */
35 /* BLI_str_partition */
36 TEST(string, StrPartition)
38 const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
43 const char *str = "mat.e-r_ial";
45 /* "mat.e-r_ial" -> "mat", '.', "e-r_ial", 3 */
46 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
48 EXPECT_EQ(&str[3], sep);
49 EXPECT_STREQ("e-r_ial", suf);
54 const char *str = ".mate-rial--";
56 /* ".mate-rial--" -> "", '.', "mate-rial--", 0 */
57 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
59 EXPECT_EQ(&str[0], sep);
60 EXPECT_STREQ("mate-rial--", suf);
64 const char *str = ".__.--_";
66 /* ".__.--_" -> "", '.', "__.--_", 0 */
67 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
69 EXPECT_EQ(&str[0], sep);
70 EXPECT_STREQ("__.--_", suf);
76 /* "" -> "", NULL, NULL, 0 */
77 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
84 const char *str = "material";
86 /* "material" -> "material", NULL, NULL, 8 */
87 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
94 /* BLI_str_rpartition */
95 TEST(string, StrRPartition)
97 const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
102 const char *str = "mat.e-r_ial";
104 /* "mat.e-r_ial" -> "mat.e-r", '_', "ial", 7 */
105 pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
106 EXPECT_EQ(7, pre_ln);
107 EXPECT_EQ(&str[7], sep);
108 EXPECT_STREQ("ial", suf);
113 const char *str = ".mate-rial--";
115 /* ".mate-rial--" -> ".mate-rial-", '-', "", 11 */
116 pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
117 EXPECT_EQ(11, pre_ln);
118 EXPECT_EQ(&str[11], sep);
119 EXPECT_STREQ("", suf);
123 const char *str = ".__.--_";
125 /* ".__.--_" -> ".__.--", '_', "", 6 */
126 pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
127 EXPECT_EQ(6, pre_ln);
128 EXPECT_EQ(&str[6], sep);
129 EXPECT_STREQ("", suf);
133 const char *str = "";
135 /* "" -> "", NULL, NULL, 0 */
136 pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
137 EXPECT_EQ(0, pre_ln);
138 EXPECT_EQ(NULL, sep);
139 EXPECT_EQ(NULL, suf);
143 const char *str = "material";
145 /* "material" -> "material", NULL, NULL, 8 */
146 pre_ln = BLI_str_rpartition(str, delim, &sep, &suf);
147 EXPECT_EQ(8, pre_ln);
148 EXPECT_EQ(NULL, sep);
149 EXPECT_EQ(NULL, suf);
153 /* BLI_str_partition_utf8 */
154 TEST(string, StrPartitionUtf8)
156 const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
161 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
163 /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "ma", '\xc3\xb1', "te-r\xe2\x98\xafial", 2 */
164 pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
165 EXPECT_EQ(2, pre_ln);
166 EXPECT_EQ(&str[2], sep);
167 EXPECT_STREQ("te-r\xe2\x98\xafial", suf);
172 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
174 /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "", '\xe2\x98\xaf', "mate-rial-\xc3\xb1", 0 */
175 pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
176 EXPECT_EQ(0, pre_ln);
177 EXPECT_EQ(&str[0], sep);
178 EXPECT_STREQ("mate-rial-\xc3\xb1", suf);
182 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
184 /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "", '\xe2\x98\xaf', ".\xc3\xb1_.--\xc3\xb1", 0 */
185 pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
186 EXPECT_EQ(0, pre_ln);
187 EXPECT_EQ(&str[0], sep);
188 EXPECT_STREQ(".\xc3\xb1_.--\xc3\xb1", suf);
192 const char *str = "";
194 /* "" -> "", NULL, NULL, 0 */
195 pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
196 EXPECT_EQ(0, pre_ln);
197 EXPECT_EQ(NULL, sep);
198 EXPECT_EQ(NULL, suf);
202 const char *str = "material";
204 /* "material" -> "material", NULL, NULL, 8 */
205 pre_ln = BLI_str_partition_utf8(str, delim, &sep, &suf);
206 EXPECT_EQ(8, pre_ln);
207 EXPECT_EQ(NULL, sep);
208 EXPECT_EQ(NULL, suf);
212 /* BLI_str_rpartition_utf8 */
213 TEST(string, StrRPartitionUtf8)
215 const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
220 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
222 /* "ma\xc3\xb1te-r\xe2\x98\xafial" -> "mat\xc3\xb1te-r", '\xe2\x98\xaf', "ial", 8 */
223 pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
224 EXPECT_EQ(8, pre_ln);
225 EXPECT_EQ(&str[8], sep);
226 EXPECT_STREQ("ial", suf);
231 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
233 /* "\xe2\x98\xafmate-rial-\xc3\xb1" -> "\xe2\x98\xafmate-rial-", '\xc3\xb1', "", 13 */
234 pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
235 EXPECT_EQ(13, pre_ln);
236 EXPECT_EQ(&str[13], sep);
237 EXPECT_STREQ("", suf);
241 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
243 /* "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1" -> "\xe2\x98\xaf.\xc3\xb1_.--", '\xc3\xb1', "", 10 */
244 pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
245 EXPECT_EQ(10, pre_ln);
246 EXPECT_EQ(&str[10], sep);
247 EXPECT_STREQ("", suf);
251 const char *str = "";
253 /* "" -> "", NULL, NULL, 0 */
254 pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
255 EXPECT_EQ(0, pre_ln);
256 EXPECT_EQ(NULL, sep);
257 EXPECT_EQ(NULL, suf);
261 const char *str = "material";
263 /* "material" -> "material", NULL, NULL, 8 */
264 pre_ln = BLI_str_rpartition_utf8(str, delim, &sep, &suf);
265 EXPECT_EQ(8, pre_ln);
266 EXPECT_EQ(NULL, sep);
267 EXPECT_EQ(NULL, suf);
271 /* BLI_str_format_int_grouped */
272 TEST(string, StrFormatIntGrouped)
277 BLI_str_format_int_grouped(num_str, num = 0);
278 EXPECT_STREQ("0", num_str);
280 BLI_str_format_int_grouped(num_str, num = 1);
281 EXPECT_STREQ("1", num_str);
283 BLI_str_format_int_grouped(num_str, num = -1);
284 EXPECT_STREQ("-1", num_str);
286 BLI_str_format_int_grouped(num_str, num = -2147483648);
287 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);
292 BLI_str_format_int_grouped(num_str, num = 1000);
293 EXPECT_STREQ("1,000", num_str);
295 BLI_str_format_int_grouped(num_str, num = -1000);
296 EXPECT_STREQ("-1,000", num_str);
298 BLI_str_format_int_grouped(num_str, num = 999);
299 EXPECT_STREQ("999", num_str);
301 BLI_str_format_int_grouped(num_str, num = -999);
302 EXPECT_STREQ("-999", num_str);