a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt// Copyright (c) 2007 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt//
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// are met:
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt//
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c++/detail/text.hpp"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <cstring>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <set>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <vector>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include <atf-c++.hpp>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(duplicate);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(duplicate)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the duplicate function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(duplicate)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::duplicate;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt const char* orig = "foo";
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char* copy = duplicate(orig);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(std::strlen(copy), 3);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(std::strcmp(copy, "foo") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::strcpy(copy, "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(std::strcmp(copy, "bar") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(std::strcmp(orig, "foo") == 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(join);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(join)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the join function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(join)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::join;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt // First set of tests using a non-sorted collection, std::vector.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::vector< std::string > words;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::string str;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "foo,,baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.push_back("baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "foo,bar,baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt // Second set of tests using a sorted collection, std::set.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::set< std::string > words;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::string str;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, ",baz,foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.clear();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words.insert("baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt str = join(words, ",");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(str, "bar,baz,foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(match);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(match)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the match function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(match)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::match;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, match("", "["));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("", ""));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!match("foo", ""));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("", ".*"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("", "[a-z]*"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("hello", "hello"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("hello", "[a-z]+"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(match("hello", "^[a-z]+$"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!match("hello", "helooo"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!match("hello", "[a-z]+5"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(split);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(split)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the split function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(split)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::split;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::vector< std::string > words;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" ", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" ", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("a b", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "b");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("a b c d", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 4);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "b");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[2], "c");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[3], "d");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("foo bar", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("foo bar baz foobar", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 4);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[2], "baz");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[3], "foobar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" foo bar", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("foo bar", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("foo bar ", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" foo bar ", " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(split_delims);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(split_delims)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the split function using different delimiters");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(split_delims)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::split;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt std::vector< std::string > words;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("", "/");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" ", "/");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split(" ", "/");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], " ");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("a/b", "/");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 2);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "b");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt words = split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words.size(), 3);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[0], "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[1], "bcd");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(words[2], "ef");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(trim);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(trim)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the trim function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(trim)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::trim;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim(""), "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim(" "), "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("\t"), "");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim(" foo"), "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("\t foo"), "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("foo\t "), "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("foo \t"), "foo");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(to_bool);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(to_bool)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the to_string function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(to_bool)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::to_bool;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(to_bool("true"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(to_bool("TRUE"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(to_bool("yes"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(to_bool("YES"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!to_bool("false"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!to_bool("FALSE"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!to_bool("no"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE(!to_bool("NO"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bool(""));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bool("tru"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bool("true2"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bool("fals"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bool("false2"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TEST_CASE(to_bytes);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TEST_CASE_HEAD(to_bytes)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt set_md_var("descr", "Tests the to_bytes function");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TEST_CASE_BODY(to_bytes)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt using atf::text::to_bytes;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(0, to_bytes("0"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(12345, to_bytes("12345"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_THROW_RE(std::runtime_error, "Empty", to_bytes(""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown size unit 'd'",
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt to_bytes("12d"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" "));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" k"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(to_string);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(to_string)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the to_string function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(to_string)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::to_string;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_string('a'), "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_string("a"), "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_string(5), "5");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE(to_type);
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_HEAD(to_type)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt set_md_var("descr", "Tests the to_type function");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TEST_CASE_BODY(to_type)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt using atf::text::to_type;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_type< int >("0"), 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_type< int >("1234"), 1234);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_type< int >(" "));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("0 a"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("a"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_type< float >("0.5"), 0.5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_type< float >("1234.5"), 1234.5);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("0.5 a"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("a"));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_REQUIRE_EQ(to_type< std::string >("a"), "a");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// Main.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt// ------------------------------------------------------------------------
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_INIT_TEST_CASES(tcs)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt // Add the test cases for the free functions.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, duplicate);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, join);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, match);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, split);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, split_delims);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, trim);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, to_bool);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_ADD_TEST_CASE(tcs, to_bytes);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, to_string);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ATF_ADD_TEST_CASE(tcs, to_type);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}