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