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