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