xref: /freebsd/contrib/atf/atf-c++/detail/text_test.cpp (revision dece5b05f577fc4c6b89af7ad48dfbdf7cc4945c)
1 // Copyright (c) 2007 The NetBSD Foundation, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 // 1. Redistributions of source code must retain the above copyright
8 //    notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 //    notice, this list of conditions and the following disclaimer in the
11 //    documentation and/or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "atf-c++/detail/text.hpp"
27 
28 #include <cstring>
29 #include <set>
30 #include <vector>
31 
32 #include <atf-c++.hpp>
33 
34 // ------------------------------------------------------------------------
35 // Test cases for the free functions.
36 // ------------------------------------------------------------------------
37 
38 ATF_TEST_CASE(join);
39 ATF_TEST_CASE_HEAD(join)
40 {
41     set_md_var("descr", "Tests the join function");
42 }
43 ATF_TEST_CASE_BODY(join)
44 {
45     using atf::text::join;
46 
47     // First set of tests using a non-sorted collection, std::vector.
48     {
49         std::vector< std::string > words;
50         std::string str;
51 
52         words.clear();
53         str = join(words, ",");
54         ATF_REQUIRE_EQ(str, "");
55 
56         words.clear();
57         words.push_back("");
58         str = join(words, ",");
59         ATF_REQUIRE_EQ(str, "");
60 
61         words.clear();
62         words.push_back("");
63         words.push_back("");
64         str = join(words, ",");
65         ATF_REQUIRE_EQ(str, ",");
66 
67         words.clear();
68         words.push_back("foo");
69         words.push_back("");
70         words.push_back("baz");
71         str = join(words, ",");
72         ATF_REQUIRE_EQ(str, "foo,,baz");
73 
74         words.clear();
75         words.push_back("foo");
76         words.push_back("bar");
77         words.push_back("baz");
78         str = join(words, ",");
79         ATF_REQUIRE_EQ(str, "foo,bar,baz");
80     }
81 
82     // Second set of tests using a sorted collection, std::set.
83     {
84         std::set< std::string > words;
85         std::string str;
86 
87         words.clear();
88         str = join(words, ",");
89         ATF_REQUIRE_EQ(str, "");
90 
91         words.clear();
92         words.insert("");
93         str = join(words, ",");
94         ATF_REQUIRE_EQ(str, "");
95 
96         words.clear();
97         words.insert("foo");
98         words.insert("");
99         words.insert("baz");
100         str = join(words, ",");
101         ATF_REQUIRE_EQ(str, ",baz,foo");
102 
103         words.clear();
104         words.insert("foo");
105         words.insert("bar");
106         words.insert("baz");
107         str = join(words, ",");
108         ATF_REQUIRE_EQ(str, "bar,baz,foo");
109     }
110 }
111 
112 ATF_TEST_CASE(match);
113 ATF_TEST_CASE_HEAD(match)
114 {
115     set_md_var("descr", "Tests the match function");
116 }
117 ATF_TEST_CASE_BODY(match)
118 {
119     using atf::text::match;
120 
121     ATF_REQUIRE_THROW(std::runtime_error, match("", "["));
122 
123     ATF_REQUIRE(match("", ""));
124     ATF_REQUIRE(!match("foo", ""));
125 
126     ATF_REQUIRE(match("", ".*"));
127     ATF_REQUIRE(match("", "[a-z]*"));
128 
129     ATF_REQUIRE(match("hello", "hello"));
130     ATF_REQUIRE(match("hello", "[a-z]+"));
131     ATF_REQUIRE(match("hello", "^[a-z]+$"));
132 
133     ATF_REQUIRE(!match("hello", "helooo"));
134     ATF_REQUIRE(!match("hello", "[a-z]+5"));
135     ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
136 }
137 
138 ATF_TEST_CASE(split);
139 ATF_TEST_CASE_HEAD(split)
140 {
141     set_md_var("descr", "Tests the split function");
142 }
143 ATF_TEST_CASE_BODY(split)
144 {
145     using atf::text::split;
146 
147     std::vector< std::string > words;
148 
149     words = split("", " ");
150     ATF_REQUIRE_EQ(words.size(), 0);
151 
152     words = split(" ", " ");
153     ATF_REQUIRE_EQ(words.size(), 0);
154 
155     words = split("    ", " ");
156     ATF_REQUIRE_EQ(words.size(), 0);
157 
158     words = split("a b", " ");
159     ATF_REQUIRE_EQ(words.size(), 2);
160     ATF_REQUIRE_EQ(words[0], "a");
161     ATF_REQUIRE_EQ(words[1], "b");
162 
163     words = split("a b c d", " ");
164     ATF_REQUIRE_EQ(words.size(), 4);
165     ATF_REQUIRE_EQ(words[0], "a");
166     ATF_REQUIRE_EQ(words[1], "b");
167     ATF_REQUIRE_EQ(words[2], "c");
168     ATF_REQUIRE_EQ(words[3], "d");
169 
170     words = split("foo bar", " ");
171     ATF_REQUIRE_EQ(words.size(), 2);
172     ATF_REQUIRE_EQ(words[0], "foo");
173     ATF_REQUIRE_EQ(words[1], "bar");
174 
175     words = split("foo bar baz foobar", " ");
176     ATF_REQUIRE_EQ(words.size(), 4);
177     ATF_REQUIRE_EQ(words[0], "foo");
178     ATF_REQUIRE_EQ(words[1], "bar");
179     ATF_REQUIRE_EQ(words[2], "baz");
180     ATF_REQUIRE_EQ(words[3], "foobar");
181 
182     words = split(" foo bar", " ");
183     ATF_REQUIRE_EQ(words.size(), 2);
184     ATF_REQUIRE_EQ(words[0], "foo");
185     ATF_REQUIRE_EQ(words[1], "bar");
186 
187     words = split("foo  bar", " ");
188     ATF_REQUIRE_EQ(words.size(), 2);
189     ATF_REQUIRE_EQ(words[0], "foo");
190     ATF_REQUIRE_EQ(words[1], "bar");
191 
192     words = split("foo bar ", " ");
193     ATF_REQUIRE_EQ(words.size(), 2);
194     ATF_REQUIRE_EQ(words[0], "foo");
195     ATF_REQUIRE_EQ(words[1], "bar");
196 
197     words = split("  foo  bar  ", " ");
198     ATF_REQUIRE_EQ(words.size(), 2);
199     ATF_REQUIRE_EQ(words[0], "foo");
200     ATF_REQUIRE_EQ(words[1], "bar");
201 }
202 
203 ATF_TEST_CASE(split_delims);
204 ATF_TEST_CASE_HEAD(split_delims)
205 {
206     set_md_var("descr", "Tests the split function using different delimiters");
207 }
208 ATF_TEST_CASE_BODY(split_delims)
209 {
210     using atf::text::split;
211 
212     std::vector< std::string > words;
213 
214     words = split("", "/");
215     ATF_REQUIRE_EQ(words.size(), 0);
216 
217     words = split(" ", "/");
218     ATF_REQUIRE_EQ(words.size(), 1);
219     ATF_REQUIRE_EQ(words[0], " ");
220 
221     words = split("    ", "/");
222     ATF_REQUIRE_EQ(words.size(), 1);
223     ATF_REQUIRE_EQ(words[0], "    ");
224 
225     words = split("a/b", "/");
226     ATF_REQUIRE_EQ(words.size(), 2);
227     ATF_REQUIRE_EQ(words[0], "a");
228     ATF_REQUIRE_EQ(words[1], "b");
229 
230     words = split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
231     ATF_REQUIRE_EQ(words.size(), 3);
232     ATF_REQUIRE_EQ(words[0], "a");
233     ATF_REQUIRE_EQ(words[1], "bcd");
234     ATF_REQUIRE_EQ(words[2], "ef");
235 }
236 
237 ATF_TEST_CASE(trim);
238 ATF_TEST_CASE_HEAD(trim)
239 {
240     set_md_var("descr", "Tests the trim function");
241 }
242 ATF_TEST_CASE_BODY(trim)
243 {
244     using atf::text::trim;
245 
246     ATF_REQUIRE_EQ(trim(""), "");
247     ATF_REQUIRE_EQ(trim(" "), "");
248     ATF_REQUIRE_EQ(trim("\t"), "");
249 
250     ATF_REQUIRE_EQ(trim(" foo"), "foo");
251     ATF_REQUIRE_EQ(trim("\t foo"), "foo");
252     ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
253     ATF_REQUIRE_EQ(trim("foo\t "), "foo");
254     ATF_REQUIRE_EQ(trim("foo \t"), "foo");
255 
256     ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
257     ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
258     ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
259     ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
260     ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
261 }
262 
263 ATF_TEST_CASE(to_bool);
264 ATF_TEST_CASE_HEAD(to_bool)
265 {
266     set_md_var("descr", "Tests the to_string function");
267 }
268 ATF_TEST_CASE_BODY(to_bool)
269 {
270     using atf::text::to_bool;
271 
272     ATF_REQUIRE(to_bool("true"));
273     ATF_REQUIRE(to_bool("TRUE"));
274     ATF_REQUIRE(to_bool("yes"));
275     ATF_REQUIRE(to_bool("YES"));
276 
277     ATF_REQUIRE(!to_bool("false"));
278     ATF_REQUIRE(!to_bool("FALSE"));
279     ATF_REQUIRE(!to_bool("no"));
280     ATF_REQUIRE(!to_bool("NO"));
281 
282     ATF_REQUIRE_THROW(std::runtime_error, to_bool(""));
283     ATF_REQUIRE_THROW(std::runtime_error, to_bool("tru"));
284     ATF_REQUIRE_THROW(std::runtime_error, to_bool("true2"));
285     ATF_REQUIRE_THROW(std::runtime_error, to_bool("fals"));
286     ATF_REQUIRE_THROW(std::runtime_error, to_bool("false2"));
287 }
288 
289 ATF_TEST_CASE(to_bytes);
290 ATF_TEST_CASE_HEAD(to_bytes)
291 {
292     set_md_var("descr", "Tests the to_bytes function");
293 }
294 ATF_TEST_CASE_BODY(to_bytes)
295 {
296     using atf::text::to_bytes;
297 
298     ATF_REQUIRE_EQ(0, to_bytes("0"));
299     ATF_REQUIRE_EQ(12345, to_bytes("12345"));
300     ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
301     ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
302     ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
303     ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));
304 
305     ATF_REQUIRE_THROW_RE(std::runtime_error, "Empty", to_bytes(""));
306     ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown size unit 'd'",
307                          to_bytes("12d"));
308     ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" "));
309     ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" k"));
310 }
311 
312 ATF_TEST_CASE(to_string);
313 ATF_TEST_CASE_HEAD(to_string)
314 {
315     set_md_var("descr", "Tests the to_string function");
316 }
317 ATF_TEST_CASE_BODY(to_string)
318 {
319     using atf::text::to_string;
320 
321     ATF_REQUIRE_EQ(to_string('a'), "a");
322     ATF_REQUIRE_EQ(to_string("a"), "a");
323     ATF_REQUIRE_EQ(to_string(5), "5");
324 }
325 
326 ATF_TEST_CASE(to_type);
327 ATF_TEST_CASE_HEAD(to_type)
328 {
329     set_md_var("descr", "Tests the to_type function");
330 }
331 ATF_TEST_CASE_BODY(to_type)
332 {
333     using atf::text::to_type;
334 
335     ATF_REQUIRE_EQ(to_type< int >("0"), 0);
336     ATF_REQUIRE_EQ(to_type< int >("1234"), 1234);
337     ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("   "));
338     ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("0 a"));
339     ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("a"));
340 
341     ATF_REQUIRE_EQ(to_type< float >("0.5"), 0.5);
342     ATF_REQUIRE_EQ(to_type< float >("1234.5"), 1234.5);
343     ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("0.5 a"));
344     ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("a"));
345 
346     ATF_REQUIRE_EQ(to_type< std::string >("a"), "a");
347 }
348 
349 // ------------------------------------------------------------------------
350 // Main.
351 // ------------------------------------------------------------------------
352 
353 ATF_INIT_TEST_CASES(tcs)
354 {
355     // Add the test cases for the free functions.
356     ATF_ADD_TEST_CASE(tcs, join);
357     ATF_ADD_TEST_CASE(tcs, match);
358     ATF_ADD_TEST_CASE(tcs, split);
359     ATF_ADD_TEST_CASE(tcs, split_delims);
360     ATF_ADD_TEST_CASE(tcs, trim);
361     ATF_ADD_TEST_CASE(tcs, to_bool);
362     ATF_ADD_TEST_CASE(tcs, to_bytes);
363     ATF_ADD_TEST_CASE(tcs, to_string);
364     ATF_ADD_TEST_CASE(tcs, to_type);
365 }
366