BLI_string, dont pass unicode to ascii BLI_str_partition functions
[blender.git] / tests / gtests / blenlib / BLI_string_test.cc
1 /* Apache License, Version 2.0 */
2
3 #include "testing/testing.h"
4
5 extern "C" {
6 #include "BLI_utildefines.h"
7 #include "BLI_string.h"
8 #include "BLI_string_utf8.h"
9 }
10
11 /* -------------------------------------------------------------------- */
12 /* stubs */
13
14 extern "C" {
15
16 int mk_wcwidth(wchar_t ucs);
17 int mk_wcswidth(const wchar_t *pwcs, size_t n);
18
19 int mk_wcwidth(wchar_t ucs)
20 {
21         return 0;
22 }
23
24 int mk_wcswidth(const wchar_t *pwcs, size_t n)
25 {
26         return 0;
27 }
28
29 }
30
31
32 /* -------------------------------------------------------------------- */
33 /* tests */
34
35 /* BLI_str_partition */
36 TEST(string, StrPartition)
37 {
38         const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
39         char *sep, *suf;
40         size_t pre_ln;
41
42         {
43                 const char *str = "mat.e-r_ial";
44
45                 /* "mat.e-r_ial" -> "mat", '.', "e-r_ial", 3 */
46                 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
47                 EXPECT_EQ(3, pre_ln);
48                 EXPECT_EQ(&str[3], sep);
49                 EXPECT_STREQ("e-r_ial", suf);
50         }
51
52         /* Corner cases. */
53         {
54                 const char *str = ".mate-rial--";
55
56                 /* ".mate-rial--" -> "", '.', "mate-rial--", 0 */
57                 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
58                 EXPECT_EQ(0, pre_ln);
59                 EXPECT_EQ(&str[0], sep);
60                 EXPECT_STREQ("mate-rial--", suf);
61         }
62
63         {
64                 const char *str = ".__.--_";
65
66                 /* ".__.--_" -> "", '.', "__.--_", 0 */
67                 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
68                 EXPECT_EQ(0, pre_ln);
69                 EXPECT_EQ(&str[0], sep);
70                 EXPECT_STREQ("__.--_", suf);
71         }
72
73         {
74                 const char *str = "";
75
76                 /* "" -> "", NULL, NULL, 0 */
77                 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
78                 EXPECT_EQ(0, pre_ln);
79                 EXPECT_EQ(NULL, sep);
80                 EXPECT_EQ(NULL, suf);
81         }
82
83         {
84                 const char *str = "material";
85
86                 /* "material" -> "material", NULL, NULL, 8 */
87                 pre_ln = BLI_str_partition(str, delim, &sep, &suf);
88                 EXPECT_EQ(8, pre_ln);
89                 EXPECT_EQ(NULL, sep);
90                 EXPECT_EQ(NULL, suf);
91         }
92 }
93
94 /* BLI_str_rpartition */
95 TEST(string, StrRPartition)
96 {
97         const char delim[] = {'-', '.', '_', '~', '\\', '\0'};
98         char *sep, *suf;
99         size_t pre_ln;
100
101         {
102                 const char *str = "mat.e-r_ial";
103
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);
109         }
110
111         /* Corner cases. */
112         {
113                 const char *str = ".mate-rial--";
114
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);
120         }
121
122         {
123                 const char *str = ".__.--_";
124
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);
130         }
131
132         {
133                 const char *str = "";
134
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);
140         }
141
142         {
143                 const char *str = "material";
144
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);
150         }
151 }
152
153 /* BLI_str_partition_utf8 */
154 TEST(string, StrPartitionUtf8)
155 {
156         const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
157         char *sep, *suf;
158         size_t pre_ln;
159
160         {
161                 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
162
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);
168         }
169
170         /* Corner cases. */
171         {
172                 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
173
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);
179         }
180
181         {
182                 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
183
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);
189         }
190
191         {
192                 const char *str = "";
193
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);
199         }
200
201         {
202                 const char *str = "material";
203
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);
209         }
210 }
211
212 /* BLI_str_rpartition_utf8 */
213 TEST(string, StrRPartitionUtf8)
214 {
215         const unsigned int delim[] = {'-', '.', '_', 0x00F1 /* n tilde */, 0x262F /* ying-yang */, '\0'};
216         char *sep, *suf;
217         size_t pre_ln;
218
219         {
220                 const char *str = "ma\xc3\xb1te-r\xe2\x98\xafial";
221
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);
227         }
228
229         /* Corner cases. */
230         {
231                 const char *str = "\xe2\x98\xafmate-rial-\xc3\xb1";
232
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);
238         }
239
240         {
241                 const char *str = "\xe2\x98\xaf.\xc3\xb1_.--\xc3\xb1";
242
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);
248         }
249
250         {
251                 const char *str = "";
252
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);
258         }
259
260         {
261                 const char *str = "material";
262
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);
268         }
269 }