2 // Automated Testing Framework (atf)
4 // Copyright (c) 2007 The NetBSD Foundation, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
10 // 1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <atf-c++.hpp>
38 // ------------------------------------------------------------------------
39 // Test cases for the free functions.
40 // ------------------------------------------------------------------------
42 ATF_TEST_CASE(duplicate
);
43 ATF_TEST_CASE_HEAD(duplicate
)
45 set_md_var("descr", "Tests the duplicate function");
47 ATF_TEST_CASE_BODY(duplicate
)
49 using tools::text::duplicate
;
51 const char* orig
= "foo";
53 char* copy
= duplicate(orig
);
54 ATF_REQUIRE_EQ(std::strlen(copy
), 3);
55 ATF_REQUIRE(std::strcmp(copy
, "foo") == 0);
57 std::strcpy(copy
, "bar");
58 ATF_REQUIRE(std::strcmp(copy
, "bar") == 0);
59 ATF_REQUIRE(std::strcmp(orig
, "foo") == 0);
63 ATF_TEST_CASE_HEAD(join
)
65 set_md_var("descr", "Tests the join function");
67 ATF_TEST_CASE_BODY(join
)
69 using tools::text::join
;
71 // First set of tests using a non-sorted collection, std::vector.
73 std::vector
< std::string
> words
;
77 str
= join(words
, ",");
78 ATF_REQUIRE_EQ(str
, "");
82 str
= join(words
, ",");
83 ATF_REQUIRE_EQ(str
, "");
88 str
= join(words
, ",");
89 ATF_REQUIRE_EQ(str
, ",");
92 words
.push_back("foo");
94 words
.push_back("baz");
95 str
= join(words
, ",");
96 ATF_REQUIRE_EQ(str
, "foo,,baz");
99 words
.push_back("foo");
100 words
.push_back("bar");
101 words
.push_back("baz");
102 str
= join(words
, ",");
103 ATF_REQUIRE_EQ(str
, "foo,bar,baz");
106 // Second set of tests using a sorted collection, std::set.
108 std::set
< std::string
> words
;
112 str
= join(words
, ",");
113 ATF_REQUIRE_EQ(str
, "");
117 str
= join(words
, ",");
118 ATF_REQUIRE_EQ(str
, "");
124 str
= join(words
, ",");
125 ATF_REQUIRE_EQ(str
, ",baz,foo");
131 str
= join(words
, ",");
132 ATF_REQUIRE_EQ(str
, "bar,baz,foo");
136 ATF_TEST_CASE(match
);
137 ATF_TEST_CASE_HEAD(match
)
139 set_md_var("descr", "Tests the match function");
141 ATF_TEST_CASE_BODY(match
)
143 using tools::text::match
;
145 ATF_REQUIRE_THROW(std::runtime_error
, match("", "["));
147 ATF_REQUIRE(match("", ""));
148 ATF_REQUIRE(!match("foo", ""));
150 ATF_REQUIRE(match("", ".*"));
151 ATF_REQUIRE(match("", "[a-z]*"));
153 ATF_REQUIRE(match("hello", "hello"));
154 ATF_REQUIRE(match("hello", "[a-z]+"));
155 ATF_REQUIRE(match("hello", "^[a-z]+$"));
157 ATF_REQUIRE(!match("hello", "helooo"));
158 ATF_REQUIRE(!match("hello", "[a-z]+5"));
159 ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
162 ATF_TEST_CASE(split
);
163 ATF_TEST_CASE_HEAD(split
)
165 set_md_var("descr", "Tests the split function");
167 ATF_TEST_CASE_BODY(split
)
169 using tools::text::split
;
171 std::vector
< std::string
> words
;
173 words
= split("", " ");
174 ATF_REQUIRE_EQ(words
.size(), 0);
176 words
= split(" ", " ");
177 ATF_REQUIRE_EQ(words
.size(), 0);
179 words
= split(" ", " ");
180 ATF_REQUIRE_EQ(words
.size(), 0);
182 words
= split("a b", " ");
183 ATF_REQUIRE_EQ(words
.size(), 2);
184 ATF_REQUIRE_EQ(words
[0], "a");
185 ATF_REQUIRE_EQ(words
[1], "b");
187 words
= split("a b c d", " ");
188 ATF_REQUIRE_EQ(words
.size(), 4);
189 ATF_REQUIRE_EQ(words
[0], "a");
190 ATF_REQUIRE_EQ(words
[1], "b");
191 ATF_REQUIRE_EQ(words
[2], "c");
192 ATF_REQUIRE_EQ(words
[3], "d");
194 words
= split("foo bar", " ");
195 ATF_REQUIRE_EQ(words
.size(), 2);
196 ATF_REQUIRE_EQ(words
[0], "foo");
197 ATF_REQUIRE_EQ(words
[1], "bar");
199 words
= split("foo bar baz foobar", " ");
200 ATF_REQUIRE_EQ(words
.size(), 4);
201 ATF_REQUIRE_EQ(words
[0], "foo");
202 ATF_REQUIRE_EQ(words
[1], "bar");
203 ATF_REQUIRE_EQ(words
[2], "baz");
204 ATF_REQUIRE_EQ(words
[3], "foobar");
206 words
= split(" foo bar", " ");
207 ATF_REQUIRE_EQ(words
.size(), 2);
208 ATF_REQUIRE_EQ(words
[0], "foo");
209 ATF_REQUIRE_EQ(words
[1], "bar");
211 words
= split("foo bar", " ");
212 ATF_REQUIRE_EQ(words
.size(), 2);
213 ATF_REQUIRE_EQ(words
[0], "foo");
214 ATF_REQUIRE_EQ(words
[1], "bar");
216 words
= split("foo bar ", " ");
217 ATF_REQUIRE_EQ(words
.size(), 2);
218 ATF_REQUIRE_EQ(words
[0], "foo");
219 ATF_REQUIRE_EQ(words
[1], "bar");
221 words
= split(" foo bar ", " ");
222 ATF_REQUIRE_EQ(words
.size(), 2);
223 ATF_REQUIRE_EQ(words
[0], "foo");
224 ATF_REQUIRE_EQ(words
[1], "bar");
227 ATF_TEST_CASE(split_delims
);
228 ATF_TEST_CASE_HEAD(split_delims
)
230 set_md_var("descr", "Tests the split function using different delimiters");
232 ATF_TEST_CASE_BODY(split_delims
)
234 using tools::text::split
;
236 std::vector
< std::string
> words
;
238 words
= split("", "/");
239 ATF_REQUIRE_EQ(words
.size(), 0);
241 words
= split(" ", "/");
242 ATF_REQUIRE_EQ(words
.size(), 1);
243 ATF_REQUIRE_EQ(words
[0], " ");
245 words
= split(" ", "/");
246 ATF_REQUIRE_EQ(words
.size(), 1);
247 ATF_REQUIRE_EQ(words
[0], " ");
249 words
= split("a/b", "/");
250 ATF_REQUIRE_EQ(words
.size(), 2);
251 ATF_REQUIRE_EQ(words
[0], "a");
252 ATF_REQUIRE_EQ(words
[1], "b");
254 words
= split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
255 ATF_REQUIRE_EQ(words
.size(), 3);
256 ATF_REQUIRE_EQ(words
[0], "a");
257 ATF_REQUIRE_EQ(words
[1], "bcd");
258 ATF_REQUIRE_EQ(words
[2], "ef");
262 ATF_TEST_CASE_HEAD(trim
)
264 set_md_var("descr", "Tests the trim function");
266 ATF_TEST_CASE_BODY(trim
)
268 using tools::text::trim
;
270 ATF_REQUIRE_EQ(trim(""), "");
271 ATF_REQUIRE_EQ(trim(" "), "");
272 ATF_REQUIRE_EQ(trim("\t"), "");
274 ATF_REQUIRE_EQ(trim(" foo"), "foo");
275 ATF_REQUIRE_EQ(trim("\t foo"), "foo");
276 ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
277 ATF_REQUIRE_EQ(trim("foo\t "), "foo");
278 ATF_REQUIRE_EQ(trim("foo \t"), "foo");
280 ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
281 ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
282 ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
283 ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
284 ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
287 ATF_TEST_CASE(to_bool
);
288 ATF_TEST_CASE_HEAD(to_bool
)
290 set_md_var("descr", "Tests the to_string function");
292 ATF_TEST_CASE_BODY(to_bool
)
294 using tools::text::to_bool
;
296 ATF_REQUIRE(to_bool("true"));
297 ATF_REQUIRE(to_bool("TRUE"));
298 ATF_REQUIRE(to_bool("yes"));
299 ATF_REQUIRE(to_bool("YES"));
301 ATF_REQUIRE(!to_bool("false"));
302 ATF_REQUIRE(!to_bool("FALSE"));
303 ATF_REQUIRE(!to_bool("no"));
304 ATF_REQUIRE(!to_bool("NO"));
306 ATF_REQUIRE_THROW(std::runtime_error
, to_bool(""));
307 ATF_REQUIRE_THROW(std::runtime_error
, to_bool("tru"));
308 ATF_REQUIRE_THROW(std::runtime_error
, to_bool("true2"));
309 ATF_REQUIRE_THROW(std::runtime_error
, to_bool("fals"));
310 ATF_REQUIRE_THROW(std::runtime_error
, to_bool("false2"));
313 ATF_TEST_CASE(to_bytes
);
314 ATF_TEST_CASE_HEAD(to_bytes
)
316 set_md_var("descr", "Tests the to_bytes function");
318 ATF_TEST_CASE_BODY(to_bytes
)
320 using tools::text::to_bytes
;
322 ATF_REQUIRE_EQ(0, to_bytes("0"));
323 ATF_REQUIRE_EQ(12345, to_bytes("12345"));
324 ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
325 ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
326 ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
327 ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));
329 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Empty", to_bytes(""));
330 ATF_REQUIRE_THROW_RE(std::runtime_error
, "Unknown size unit 'd'",
332 ATF_REQUIRE_THROW(std::runtime_error
, to_bytes(" "));
333 ATF_REQUIRE_THROW(std::runtime_error
, to_bytes(" k"));
336 ATF_TEST_CASE(to_string
);
337 ATF_TEST_CASE_HEAD(to_string
)
339 set_md_var("descr", "Tests the to_string function");
341 ATF_TEST_CASE_BODY(to_string
)
343 using tools::text::to_string
;
345 ATF_REQUIRE_EQ(to_string('a'), "a");
346 ATF_REQUIRE_EQ(to_string("a"), "a");
347 ATF_REQUIRE_EQ(to_string(5), "5");
350 ATF_TEST_CASE(to_type
);
351 ATF_TEST_CASE_HEAD(to_type
)
353 set_md_var("descr", "Tests the to_type function");
355 ATF_TEST_CASE_BODY(to_type
)
357 using tools::text::to_type
;
359 ATF_REQUIRE_EQ(to_type
< int >("0"), 0);
360 ATF_REQUIRE_EQ(to_type
< int >("1234"), 1234);
361 ATF_REQUIRE_THROW(std::runtime_error
, to_type
< int >(" "));
362 ATF_REQUIRE_THROW(std::runtime_error
, to_type
< int >("0 a"));
363 ATF_REQUIRE_THROW(std::runtime_error
, to_type
< int >("a"));
365 ATF_REQUIRE_EQ(to_type
< float >("0.5"), 0.5);
366 ATF_REQUIRE_EQ(to_type
< float >("1234.5"), 1234.5);
367 ATF_REQUIRE_THROW(std::runtime_error
, to_type
< float >("0.5 a"));
368 ATF_REQUIRE_THROW(std::runtime_error
, to_type
< float >("a"));
370 ATF_REQUIRE_EQ(to_type
< std::string
>("a"), "a");
373 // ------------------------------------------------------------------------
375 // ------------------------------------------------------------------------
377 ATF_INIT_TEST_CASES(tcs
)
379 // Add the test cases for the free functions.
380 ATF_ADD_TEST_CASE(tcs
, duplicate
);
381 ATF_ADD_TEST_CASE(tcs
, join
);
382 ATF_ADD_TEST_CASE(tcs
, match
);
383 ATF_ADD_TEST_CASE(tcs
, split
);
384 ATF_ADD_TEST_CASE(tcs
, split_delims
);
385 ATF_ADD_TEST_CASE(tcs
, trim
);
386 ATF_ADD_TEST_CASE(tcs
, to_bool
);
387 ATF_ADD_TEST_CASE(tcs
, to_bytes
);
388 ATF_ADD_TEST_CASE(tcs
, to_string
);
389 ATF_ADD_TEST_CASE(tcs
, to_type
);