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