Blender  V3.3
util_string_test.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "testing/testing.h"
5 
6 #include "util/string.h"
7 
9 
10 /* ******** Tests for string_printf() ******** */
11 
12 TEST(util_string_printf, no_format)
13 {
14  string str = string_printf("foo bar");
15  EXPECT_EQ("foo bar", str);
16 }
17 
18 TEST(util_string_printf, int_number)
19 {
20  string str = string_printf("foo %d bar", 314);
21  EXPECT_EQ("foo 314 bar", str);
22 }
23 
24 TEST(util_string_printf, float_number_default_precision)
25 {
26  string str = string_printf("foo %f bar", 3.1415);
27  EXPECT_EQ("foo 3.141500 bar", str);
28 }
29 
30 TEST(util_string_printf, float_number_custom_precision)
31 {
32  string str = string_printf("foo %.1f bar", 3.1415);
33  EXPECT_EQ("foo 3.1 bar", str);
34 }
35 
36 /* ******** Tests for string_printf() ******** */
37 
38 TEST(util_string_iequals, empty_a)
39 {
40  bool equals = string_iequals("", "foo");
41  EXPECT_FALSE(equals);
42 }
43 
44 TEST(util_string_iequals, empty_b)
45 {
46  bool equals = string_iequals("foo", "");
47  EXPECT_FALSE(equals);
48 }
49 
50 TEST(util_string_iequals, same_register)
51 {
52  bool equals = string_iequals("foo", "foo");
53  EXPECT_TRUE(equals);
54 }
55 
56 TEST(util_string_iequals, different_register)
57 {
58  bool equals = string_iequals("XFoo", "XfoO");
59  EXPECT_TRUE(equals);
60 }
61 
62 /* ******** Tests for string_split() ******** */
63 
64 TEST(util_string_split, empty)
65 {
66  vector<string> tokens;
67  string_split(tokens, "");
68  EXPECT_EQ(tokens.size(), 0);
69 }
70 
71 TEST(util_string_split, only_spaces)
72 {
73  vector<string> tokens;
74  string_split(tokens, " \t\t \t");
75  EXPECT_EQ(tokens.size(), 0);
76 }
77 
78 TEST(util_string_split, single)
79 {
80  vector<string> tokens;
81  string_split(tokens, "foo");
82  EXPECT_EQ(tokens.size(), 1);
83  EXPECT_EQ(tokens[0], "foo");
84 }
85 
86 TEST(util_string_split, simple)
87 {
88  vector<string> tokens;
89  string_split(tokens, "foo a bar b");
90  EXPECT_EQ(tokens.size(), 4);
91  EXPECT_EQ(tokens[0], "foo");
92  EXPECT_EQ(tokens[1], "a");
93  EXPECT_EQ(tokens[2], "bar");
94  EXPECT_EQ(tokens[3], "b");
95 }
96 
97 TEST(util_string_split, multiple_spaces)
98 {
99  vector<string> tokens;
100  string_split(tokens, " \t foo \ta bar b\t ");
101  EXPECT_EQ(tokens.size(), 4);
102  EXPECT_EQ(tokens[0], "foo");
103  EXPECT_EQ(tokens[1], "a");
104  EXPECT_EQ(tokens[2], "bar");
105  EXPECT_EQ(tokens[3], "b");
106 }
107 
108 /* ******** Tests for string_replace() ******** */
109 
110 TEST(util_string_replace, empty_haystack_and_other)
111 {
112  string str = "";
113  string_replace(str, "x", "");
114  EXPECT_EQ(str, "");
115 }
116 
117 TEST(util_string_replace, empty_haystack)
118 {
119  string str = "";
120  string_replace(str, "x", "y");
121  EXPECT_EQ(str, "");
122 }
123 
124 TEST(util_string_replace, empty_other)
125 {
126  string str = "x";
127  string_replace(str, "x", "");
128  EXPECT_EQ(str, "");
129 }
130 
131 TEST(util_string_replace, long_haystack_empty_other)
132 {
133  string str = "a x b xxc";
134  string_replace(str, "x", "");
135  EXPECT_EQ(str, "a b c");
136 }
137 
138 TEST(util_string_replace, long_haystack)
139 {
140  string str = "a x b xxc";
141  string_replace(str, "x", "FOO");
142  EXPECT_EQ(str, "a FOO b FOOFOOc");
143 }
144 
145 /* ******** Tests for string_endswith() ******** */
146 
147 TEST(util_string_endswith, empty_both)
148 {
149  bool endswith = string_endswith("", "");
150  EXPECT_TRUE(endswith);
151 }
152 
153 TEST(util_string_endswith, empty_string)
154 {
155  bool endswith = string_endswith("", "foo");
156  EXPECT_FALSE(endswith);
157 }
158 
159 TEST(util_string_endswith, empty_end)
160 {
161  bool endswith = string_endswith("foo", "");
162  EXPECT_TRUE(endswith);
163 }
164 
165 TEST(util_string_endswith, simple_true)
166 {
167  bool endswith = string_endswith("foo bar", "bar");
168  EXPECT_TRUE(endswith);
169 }
170 
171 TEST(util_string_endswith, simple_false)
172 {
173  bool endswith = string_endswith("foo bar", "foo");
174  EXPECT_FALSE(endswith);
175 }
176 
177 /* ******** Tests for string_strip() ******** */
178 
179 TEST(util_string_strip, empty)
180 {
181  string str = string_strip("");
182  EXPECT_EQ(str, "");
183 }
184 
185 TEST(util_string_strip, only_spaces)
186 {
187  string str = string_strip(" ");
188  EXPECT_EQ(str, "");
189 }
190 
191 TEST(util_string_strip, no_spaces)
192 {
193  string str = string_strip("foo bar");
194  EXPECT_EQ(str, "foo bar");
195 }
196 
197 TEST(util_string_strip, with_spaces)
198 {
199  string str = string_strip(" foo bar ");
200  EXPECT_EQ(str, "foo bar");
201 }
202 
203 /* ******** Tests for string_remove_trademark() ******** */
204 
205 TEST(util_string_remove_trademark, empty)
206 {
207  string str = string_remove_trademark("");
208  EXPECT_EQ(str, "");
209 }
210 
211 TEST(util_string_remove_trademark, no_trademark)
212 {
213  string str = string_remove_trademark("foo bar");
214  EXPECT_EQ(str, "foo bar");
215 }
216 
217 TEST(util_string_remove_trademark, only_tm)
218 {
219  string str = string_remove_trademark("foo bar(TM) zzz");
220  EXPECT_EQ(str, "foo bar zzz");
221 }
222 
223 TEST(util_string_remove_trademark, only_r)
224 {
225  string str = string_remove_trademark("foo bar(R) zzz");
226  EXPECT_EQ(str, "foo bar zzz");
227 }
228 
229 TEST(util_string_remove_trademark, both)
230 {
231  string str = string_remove_trademark("foo bar(TM)(R) zzz");
232  EXPECT_EQ(str, "foo bar zzz");
233 }
234 
235 TEST(util_string_remove_trademark, both_space)
236 {
237  string str = string_remove_trademark("foo bar(TM) (R) zzz");
238  EXPECT_EQ(str, "foo bar zzz");
239 }
240 
241 TEST(util_string_remove_trademark, both_space_around)
242 {
243  string str = string_remove_trademark("foo bar (TM) (R) zzz");
244  EXPECT_EQ(str, "foo bar zzz");
245 }
246 
247 TEST(util_string_remove_trademark, trademark_space_suffix)
248 {
249  string str = string_remove_trademark("foo bar (TM)");
250  EXPECT_EQ(str, "foo bar");
251 }
252 
253 TEST(util_string_remove_trademark, trademark_space_middle)
254 {
255  string str = string_remove_trademark("foo bar (TM) baz");
256  EXPECT_EQ(str, "foo bar baz");
257 }
258 
259 TEST(util_string_remove_trademark, r_space_suffix)
260 {
261  string str = string_remove_trademark("foo bar (R)");
262  EXPECT_EQ(str, "foo bar");
263 }
264 
265 TEST(util_string_remove_trademark, r_space_middle)
266 {
267  string str = string_remove_trademark("foo bar (R) baz");
268  EXPECT_EQ(str, "foo bar baz");
269 }
270 
271 /* ******** Tests for string_startswith() ******** */
272 
274 {
275  EXPECT_TRUE(string_startswith("", ""));
276 
277  EXPECT_FALSE(string_startswith("", "World"));
278  EXPECT_TRUE(string_startswith("Hello", ""));
279 
280  EXPECT_FALSE(string_startswith("Hello", "World"));
281 
282  EXPECT_TRUE(string_startswith("Hello", "Hello"));
283  EXPECT_TRUE(string_startswith("Hello", "He"));
284  EXPECT_TRUE(string_startswith("Hello", "H"));
285 
286  EXPECT_FALSE(string_startswith("Hello", "e"));
287  EXPECT_FALSE(string_startswith("Hello", "HelloWorld"));
288 }
289 
291 {
292  EXPECT_TRUE(string_endswith("", ""));
293 
294  EXPECT_FALSE(string_endswith("", "World"));
295  EXPECT_TRUE(string_endswith("Hello", ""));
296 
297  EXPECT_FALSE(string_endswith("Hello", "World"));
298 
299  EXPECT_TRUE(string_endswith("Hello", "Hello"));
300  EXPECT_TRUE(string_endswith("Hello", "lo"));
301  EXPECT_TRUE(string_endswith("Hello", "o"));
302 
303  EXPECT_FALSE(string_endswith("Hello", "e"));
304  EXPECT_FALSE(string_endswith("Hello", "WorldHello"));
305 }
306 
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
#define str(s)
string string_remove_trademark(const string &s)
Definition: string.cpp:152
bool string_iequals(const string &a, const string &b)
Definition: string.cpp:54
string string_strip(const string &s)
Definition: string.cpp:122
bool string_startswith(const string_view s, const string_view start)
Definition: string.cpp:100
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition: string.cpp:22
void string_split(vector< string > &tokens, const string &str, const string &separators, bool skip_empty_tokens)
Definition: string.cpp:67
void string_replace(string &haystack, const string &needle, const string &other)
Definition: string.cpp:130
bool string_endswith(const string_view s, const string_view end)
Definition: string.cpp:111
CCL_NAMESPACE_BEGIN TEST(util_string_printf, no_format)