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