head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC4:1.1.1.1
	netbsd-11-0-RC3:1.1.1.1
	netbsd-11-0-RC2:1.1.1.1
	netbsd-11-0-RC1:1.1.1.1
	perseant-exfatfs-base-20250801:1.1.1.1
	netbsd-11:1.1.1.1.0.44
	netbsd-11-base:1.1.1.1
	netbsd-10-1-RELEASE:1.1.1.1
	perseant-exfatfs-base-20240630:1.1.1.1
	perseant-exfatfs:1.1.1.1.0.42
	perseant-exfatfs-base:1.1.1.1
	netbsd-8-3-RELEASE:1.1.1.1
	netbsd-9-4-RELEASE:1.1.1.1
	netbsd-10-0-RELEASE:1.1.1.1
	netbsd-10-0-RC6:1.1.1.1
	netbsd-10-0-RC5:1.1.1.1
	netbsd-10-0-RC4:1.1.1.1
	netbsd-10-0-RC3:1.1.1.1
	netbsd-10-0-RC2:1.1.1.1
	netbsd-10-0-RC1:1.1.1.1
	netbsd-10:1.1.1.1.0.40
	netbsd-10-base:1.1.1.1
	netbsd-9-3-RELEASE:1.1.1.1
	cjep_sun2x-base1:1.1.1.1
	cjep_sun2x:1.1.1.1.0.38
	cjep_sun2x-base:1.1.1.1
	cjep_staticlib_x-base1:1.1.1.1
	netbsd-9-2-RELEASE:1.1.1.1
	cjep_staticlib_x:1.1.1.1.0.36
	cjep_staticlib_x-base:1.1.1.1
	netbsd-9-1-RELEASE:1.1.1.1
	phil-wifi-20200421:1.1.1.1
	phil-wifi-20200411:1.1.1.1
	is-mlppp:1.1.1.1.0.34
	is-mlppp-base:1.1.1.1
	phil-wifi-20200406:1.1.1.1
	netbsd-8-2-RELEASE:1.1.1.1
	netbsd-9-0-RELEASE:1.1.1.1
	netbsd-9-0-RC2:1.1.1.1
	netbsd-9-0-RC1:1.1.1.1
	phil-wifi-20191119:1.1.1.1
	netbsd-9:1.1.1.1.0.32
	netbsd-9-base:1.1.1.1
	phil-wifi-20190609:1.1.1.1
	netbsd-8-1-RELEASE:1.1.1.1
	netbsd-8-1-RC1:1.1.1.1
	pgoyette-compat-merge-20190127:1.1.1.1
	pgoyette-compat-20190127:1.1.1.1
	pgoyette-compat-20190118:1.1.1.1
	pgoyette-compat-1226:1.1.1.1
	pgoyette-compat-1126:1.1.1.1
	pgoyette-compat-1020:1.1.1.1
	pgoyette-compat-0930:1.1.1.1
	pgoyette-compat-0906:1.1.1.1
	netbsd-7-2-RELEASE:1.1.1.1
	pgoyette-compat-0728:1.1.1.1
	netbsd-8-0-RELEASE:1.1.1.1
	phil-wifi:1.1.1.1.0.30
	phil-wifi-base:1.1.1.1
	pgoyette-compat-0625:1.1.1.1
	netbsd-8-0-RC2:1.1.1.1
	pgoyette-compat-0521:1.1.1.1
	pgoyette-compat-0502:1.1.1.1
	pgoyette-compat-0422:1.1.1.1
	netbsd-8-0-RC1:1.1.1.1
	pgoyette-compat-0415:1.1.1.1
	pgoyette-compat-0407:1.1.1.1
	pgoyette-compat-0330:1.1.1.1
	pgoyette-compat-0322:1.1.1.1
	pgoyette-compat-0315:1.1.1.1
	netbsd-7-1-2-RELEASE:1.1.1.1
	pgoyette-compat:1.1.1.1.0.28
	pgoyette-compat-base:1.1.1.1
	netbsd-7-1-1-RELEASE:1.1.1.1
	matt-nb8-mediatek:1.1.1.1.0.26
	matt-nb8-mediatek-base:1.1.1.1
	perseant-stdc-iso10646:1.1.1.1.0.24
	perseant-stdc-iso10646-base:1.1.1.1
	netbsd-8:1.1.1.1.0.22
	netbsd-8-base:1.1.1.1
	prg-localcount2-base3:1.1.1.1
	prg-localcount2-base2:1.1.1.1
	prg-localcount2-base1:1.1.1.1
	prg-localcount2:1.1.1.1.0.20
	prg-localcount2-base:1.1.1.1
	pgoyette-localcount-20170426:1.1.1.1
	bouyer-socketcan-base1:1.1.1.1
	pgoyette-localcount-20170320:1.1.1.1
	netbsd-7-1:1.1.1.1.0.18
	netbsd-7-1-RELEASE:1.1.1.1
	netbsd-7-1-RC2:1.1.1.1
	netbsd-7-nhusb-base-20170116:1.1.1.1
	bouyer-socketcan:1.1.1.1.0.16
	bouyer-socketcan-base:1.1.1.1
	pgoyette-localcount-20170107:1.1.1.1
	netbsd-7-1-RC1:1.1.1.1
	pgoyette-localcount-20161104:1.1.1.1
	netbsd-7-0-2-RELEASE:1.1.1.1
	localcount-20160914:1.1.1.1
	netbsd-7-nhusb:1.1.1.1.0.14
	netbsd-7-nhusb-base:1.1.1.1
	pgoyette-localcount-20160806:1.1.1.1
	pgoyette-localcount-20160726:1.1.1.1
	pgoyette-localcount:1.1.1.1.0.12
	pgoyette-localcount-base:1.1.1.1
	netbsd-7-0-1-RELEASE:1.1.1.1
	netbsd-7-0:1.1.1.1.0.10
	netbsd-7-0-RELEASE:1.1.1.1
	netbsd-7-0-RC3:1.1.1.1
	netbsd-7-0-RC2:1.1.1.1
	netbsd-7-0-RC1:1.1.1.1
	tls-maxphys-base:1.1.1.1
	tls-maxphys:1.1.1.1.0.8
	netbsd-7:1.1.1.1.0.6
	netbsd-7-base:1.1.1.1
	yamt-pagecache:1.1.1.1.0.4
	yamt-pagecache-base9:1.1.1.1
	tls-earlyentropy:1.1.1.1.0.2
	tls-earlyentropy-base:1.1.1.1
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1.1.1
	riastradh-drm2-base3:1.1.1.1
	atf-0-19:1.1.1.1
	GITHUB_JMMV:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2014.02.08.19.11.34;	author jmmv;	state Exp;
branches
	1.1.1.1;
next	;
commitid	CKljpjViL2DYykox;

1.1.1.1
date	2014.02.08.19.11.34;	author jmmv;	state Exp;
branches
	1.1.1.1.4.1
	1.1.1.1.8.1;
next	;
commitid	CKljpjViL2DYykox;

1.1.1.1.4.1
date	2014.02.08.19.11.34;	author yamt;	state dead;
branches;
next	1.1.1.1.4.2;
commitid	TzMwNuE6CLSD5yBx;

1.1.1.1.4.2
date	2014.05.22.15.40.12;	author yamt;	state Exp;
branches;
next	;
commitid	TzMwNuE6CLSD5yBx;

1.1.1.1.8.1
date	2014.02.08.19.11.34;	author tls;	state dead;
branches;
next	1.1.1.1.8.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.1.8.2
date	2014.08.19.23.45.54;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;


desc
@@


1.1
log
@Initial revision
@
text
@//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include <cstring>
#include <set>
#include <vector>

#include <atf-c++.hpp>

#include "text.hpp"

// ------------------------------------------------------------------------
// Test cases for the free functions.
// ------------------------------------------------------------------------

ATF_TEST_CASE(duplicate);
ATF_TEST_CASE_HEAD(duplicate)
{
    set_md_var("descr", "Tests the duplicate function");
}
ATF_TEST_CASE_BODY(duplicate)
{
    using tools::text::duplicate;

    const char* orig = "foo";

    char* copy = duplicate(orig);
    ATF_REQUIRE_EQ(std::strlen(copy), 3);
    ATF_REQUIRE(std::strcmp(copy, "foo") == 0);

    std::strcpy(copy, "bar");
    ATF_REQUIRE(std::strcmp(copy, "bar") == 0);
    ATF_REQUIRE(std::strcmp(orig, "foo") == 0);
}

ATF_TEST_CASE(join);
ATF_TEST_CASE_HEAD(join)
{
    set_md_var("descr", "Tests the join function");
}
ATF_TEST_CASE_BODY(join)
{
    using tools::text::join;

    // First set of tests using a non-sorted collection, std::vector.
    {
        std::vector< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",");

        words.clear();
        words.push_back("foo");
        words.push_back("");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,,baz");

        words.clear();
        words.push_back("foo");
        words.push_back("bar");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,bar,baz");
    }

    // Second set of tests using a sorted collection, std::set.
    {
        std::set< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("foo");
        words.insert("");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",baz,foo");

        words.clear();
        words.insert("foo");
        words.insert("bar");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "bar,baz,foo");
    }
}

ATF_TEST_CASE(match);
ATF_TEST_CASE_HEAD(match)
{
    set_md_var("descr", "Tests the match function");
}
ATF_TEST_CASE_BODY(match)
{
    using tools::text::match;

    ATF_REQUIRE_THROW(std::runtime_error, match("", "["));

    ATF_REQUIRE(match("", ""));
    ATF_REQUIRE(!match("foo", ""));

    ATF_REQUIRE(match("", ".*"));
    ATF_REQUIRE(match("", "[a-z]*"));

    ATF_REQUIRE(match("hello", "hello"));
    ATF_REQUIRE(match("hello", "[a-z]+"));
    ATF_REQUIRE(match("hello", "^[a-z]+$"));

    ATF_REQUIRE(!match("hello", "helooo"));
    ATF_REQUIRE(!match("hello", "[a-z]+5"));
    ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
}

ATF_TEST_CASE(split);
ATF_TEST_CASE_HEAD(split)
{
    set_md_var("descr", "Tests the split function");
}
ATF_TEST_CASE_BODY(split)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("    ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("a b", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("a b c d", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");
    ATF_REQUIRE_EQ(words[2], "c");
    ATF_REQUIRE_EQ(words[3], "d");

    words = split("foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar baz foobar", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
    ATF_REQUIRE_EQ(words[2], "baz");
    ATF_REQUIRE_EQ(words[3], "foobar");

    words = split(" foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo  bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("  foo  bar  ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
}

ATF_TEST_CASE(split_delims);
ATF_TEST_CASE_HEAD(split_delims)
{
    set_md_var("descr", "Tests the split function using different delimiters");
}
ATF_TEST_CASE_BODY(split_delims)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", "/");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], " ");

    words = split("    ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], "    ");

    words = split("a/b", "/");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
    ATF_REQUIRE_EQ(words.size(), 3);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "bcd");
    ATF_REQUIRE_EQ(words[2], "ef");
}

ATF_TEST_CASE(trim);
ATF_TEST_CASE_HEAD(trim)
{
    set_md_var("descr", "Tests the trim function");
}
ATF_TEST_CASE_BODY(trim)
{
    using tools::text::trim;

    ATF_REQUIRE_EQ(trim(""), "");
    ATF_REQUIRE_EQ(trim(" "), "");
    ATF_REQUIRE_EQ(trim("\t"), "");

    ATF_REQUIRE_EQ(trim(" foo"), "foo");
    ATF_REQUIRE_EQ(trim("\t foo"), "foo");
    ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
    ATF_REQUIRE_EQ(trim("foo\t "), "foo");
    ATF_REQUIRE_EQ(trim("foo \t"), "foo");

    ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
}

ATF_TEST_CASE(to_bool);
ATF_TEST_CASE_HEAD(to_bool)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_bool)
{
    using tools::text::to_bool;

    ATF_REQUIRE(to_bool("true"));
    ATF_REQUIRE(to_bool("TRUE"));
    ATF_REQUIRE(to_bool("yes"));
    ATF_REQUIRE(to_bool("YES"));

    ATF_REQUIRE(!to_bool("false"));
    ATF_REQUIRE(!to_bool("FALSE"));
    ATF_REQUIRE(!to_bool("no"));
    ATF_REQUIRE(!to_bool("NO"));

    ATF_REQUIRE_THROW(std::runtime_error, to_bool(""));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("tru"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("true2"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("fals"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("false2"));
}

ATF_TEST_CASE(to_bytes);
ATF_TEST_CASE_HEAD(to_bytes)
{
    set_md_var("descr", "Tests the to_bytes function");
}
ATF_TEST_CASE_BODY(to_bytes)
{
    using tools::text::to_bytes;

    ATF_REQUIRE_EQ(0, to_bytes("0"));
    ATF_REQUIRE_EQ(12345, to_bytes("12345"));
    ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
    ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
    ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
    ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));

    ATF_REQUIRE_THROW_RE(std::runtime_error, "Empty", to_bytes(""));
    ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown size unit 'd'",
                         to_bytes("12d"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" "));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" k"));
}

ATF_TEST_CASE(to_string);
ATF_TEST_CASE_HEAD(to_string)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_string)
{
    using tools::text::to_string;

    ATF_REQUIRE_EQ(to_string('a'), "a");
    ATF_REQUIRE_EQ(to_string("a"), "a");
    ATF_REQUIRE_EQ(to_string(5), "5");
}

ATF_TEST_CASE(to_type);
ATF_TEST_CASE_HEAD(to_type)
{
    set_md_var("descr", "Tests the to_type function");
}
ATF_TEST_CASE_BODY(to_type)
{
    using tools::text::to_type;

    ATF_REQUIRE_EQ(to_type< int >("0"), 0);
    ATF_REQUIRE_EQ(to_type< int >("1234"), 1234);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("   "));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("0 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("a"));

    ATF_REQUIRE_EQ(to_type< float >("0.5"), 0.5);
    ATF_REQUIRE_EQ(to_type< float >("1234.5"), 1234.5);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("0.5 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("a"));

    ATF_REQUIRE_EQ(to_type< std::string >("a"), "a");
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the test cases for the free functions.
    ATF_ADD_TEST_CASE(tcs, duplicate);
    ATF_ADD_TEST_CASE(tcs, join);
    ATF_ADD_TEST_CASE(tcs, match);
    ATF_ADD_TEST_CASE(tcs, split);
    ATF_ADD_TEST_CASE(tcs, split_delims);
    ATF_ADD_TEST_CASE(tcs, trim);
    ATF_ADD_TEST_CASE(tcs, to_bool);
    ATF_ADD_TEST_CASE(tcs, to_bytes);
    ATF_ADD_TEST_CASE(tcs, to_string);
    ATF_ADD_TEST_CASE(tcs, to_type);
}
@


1.1.1.1
log
@Import atf 0.19:

Changes in version 0.19
***********************

Experimental version released on February 7th, 2014.

This is the last release to bundle the code for the deprecated tools.
The next release will drop their code and will stop worrying about
backwards compatibility between the ATF libraries and what the old tools
may or may not support.

If you still require the old tools for some reason, grab a copy of the
'tools' directory now.  The code in this directory is standalone and
does not depend on any internal details of atf-c++ any longer.

* Various fixes and improvements to support running as part of the FreeBSD
  test suite.

* Project hosting moved from Google Code (as a subproject of Kyua) to
  GitHub (as a first-class project).  The main reason for the change is
  the suppression of binary downloads in Google Code on Jan 15th, 2014.
  See https://github.com/jmmv/atf/

* Removed builtin help from atf-sh(1) and atf-check(1) for simplicity
  reasons.  In other words, their -h option is gone.

* Moved the code of the deprecated tools into a 'tools' directory and
  completely decoupled their code from the internals of atf-c++.  The
  reason for this is to painlessly allow a third-party to maintain a
  copy of these tools after we delete them because upcoming changes to
  atf-c++ would break the stale tools.


Changes in version 0.18
***********************

Experimental version released on November 16th, 2013.

* Issue 45: Added require.memory support in atf-run for FreeBSD.

* Fixed an issue with the handling of cin with libc++.

* Issue 64: Fixed various mandoc formatting warnings.

* NetBSD PR bin/48284: Made atf-check flush its progress message to
  stdout so that an interrupted test case always shows the last message
  being executed.

* NetBSD PR bin/48285: Fixed atf_check examples in atf-sh-api(3).
@
text
@@


1.1.1.1.8.1
log
@file text_test.cpp was added on branch tls-maxphys on 2014-08-19 23:45:54 +0000
@
text
@d1 390
@


1.1.1.1.8.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 390
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include <cstring>
#include <set>
#include <vector>

#include <atf-c++.hpp>

#include "text.hpp"

// ------------------------------------------------------------------------
// Test cases for the free functions.
// ------------------------------------------------------------------------

ATF_TEST_CASE(duplicate);
ATF_TEST_CASE_HEAD(duplicate)
{
    set_md_var("descr", "Tests the duplicate function");
}
ATF_TEST_CASE_BODY(duplicate)
{
    using tools::text::duplicate;

    const char* orig = "foo";

    char* copy = duplicate(orig);
    ATF_REQUIRE_EQ(std::strlen(copy), 3);
    ATF_REQUIRE(std::strcmp(copy, "foo") == 0);

    std::strcpy(copy, "bar");
    ATF_REQUIRE(std::strcmp(copy, "bar") == 0);
    ATF_REQUIRE(std::strcmp(orig, "foo") == 0);
}

ATF_TEST_CASE(join);
ATF_TEST_CASE_HEAD(join)
{
    set_md_var("descr", "Tests the join function");
}
ATF_TEST_CASE_BODY(join)
{
    using tools::text::join;

    // First set of tests using a non-sorted collection, std::vector.
    {
        std::vector< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",");

        words.clear();
        words.push_back("foo");
        words.push_back("");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,,baz");

        words.clear();
        words.push_back("foo");
        words.push_back("bar");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,bar,baz");
    }

    // Second set of tests using a sorted collection, std::set.
    {
        std::set< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("foo");
        words.insert("");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",baz,foo");

        words.clear();
        words.insert("foo");
        words.insert("bar");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "bar,baz,foo");
    }
}

ATF_TEST_CASE(match);
ATF_TEST_CASE_HEAD(match)
{
    set_md_var("descr", "Tests the match function");
}
ATF_TEST_CASE_BODY(match)
{
    using tools::text::match;

    ATF_REQUIRE_THROW(std::runtime_error, match("", "["));

    ATF_REQUIRE(match("", ""));
    ATF_REQUIRE(!match("foo", ""));

    ATF_REQUIRE(match("", ".*"));
    ATF_REQUIRE(match("", "[a-z]*"));

    ATF_REQUIRE(match("hello", "hello"));
    ATF_REQUIRE(match("hello", "[a-z]+"));
    ATF_REQUIRE(match("hello", "^[a-z]+$"));

    ATF_REQUIRE(!match("hello", "helooo"));
    ATF_REQUIRE(!match("hello", "[a-z]+5"));
    ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
}

ATF_TEST_CASE(split);
ATF_TEST_CASE_HEAD(split)
{
    set_md_var("descr", "Tests the split function");
}
ATF_TEST_CASE_BODY(split)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("    ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("a b", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("a b c d", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");
    ATF_REQUIRE_EQ(words[2], "c");
    ATF_REQUIRE_EQ(words[3], "d");

    words = split("foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar baz foobar", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
    ATF_REQUIRE_EQ(words[2], "baz");
    ATF_REQUIRE_EQ(words[3], "foobar");

    words = split(" foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo  bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("  foo  bar  ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
}

ATF_TEST_CASE(split_delims);
ATF_TEST_CASE_HEAD(split_delims)
{
    set_md_var("descr", "Tests the split function using different delimiters");
}
ATF_TEST_CASE_BODY(split_delims)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", "/");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], " ");

    words = split("    ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], "    ");

    words = split("a/b", "/");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
    ATF_REQUIRE_EQ(words.size(), 3);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "bcd");
    ATF_REQUIRE_EQ(words[2], "ef");
}

ATF_TEST_CASE(trim);
ATF_TEST_CASE_HEAD(trim)
{
    set_md_var("descr", "Tests the trim function");
}
ATF_TEST_CASE_BODY(trim)
{
    using tools::text::trim;

    ATF_REQUIRE_EQ(trim(""), "");
    ATF_REQUIRE_EQ(trim(" "), "");
    ATF_REQUIRE_EQ(trim("\t"), "");

    ATF_REQUIRE_EQ(trim(" foo"), "foo");
    ATF_REQUIRE_EQ(trim("\t foo"), "foo");
    ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
    ATF_REQUIRE_EQ(trim("foo\t "), "foo");
    ATF_REQUIRE_EQ(trim("foo \t"), "foo");

    ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
}

ATF_TEST_CASE(to_bool);
ATF_TEST_CASE_HEAD(to_bool)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_bool)
{
    using tools::text::to_bool;

    ATF_REQUIRE(to_bool("true"));
    ATF_REQUIRE(to_bool("TRUE"));
    ATF_REQUIRE(to_bool("yes"));
    ATF_REQUIRE(to_bool("YES"));

    ATF_REQUIRE(!to_bool("false"));
    ATF_REQUIRE(!to_bool("FALSE"));
    ATF_REQUIRE(!to_bool("no"));
    ATF_REQUIRE(!to_bool("NO"));

    ATF_REQUIRE_THROW(std::runtime_error, to_bool(""));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("tru"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("true2"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("fals"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("false2"));
}

ATF_TEST_CASE(to_bytes);
ATF_TEST_CASE_HEAD(to_bytes)
{
    set_md_var("descr", "Tests the to_bytes function");
}
ATF_TEST_CASE_BODY(to_bytes)
{
    using tools::text::to_bytes;

    ATF_REQUIRE_EQ(0, to_bytes("0"));
    ATF_REQUIRE_EQ(12345, to_bytes("12345"));
    ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
    ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
    ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
    ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));

    ATF_REQUIRE_THROW_RE(std::runtime_error, "Empty", to_bytes(""));
    ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown size unit 'd'",
                         to_bytes("12d"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" "));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" k"));
}

ATF_TEST_CASE(to_string);
ATF_TEST_CASE_HEAD(to_string)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_string)
{
    using tools::text::to_string;

    ATF_REQUIRE_EQ(to_string('a'), "a");
    ATF_REQUIRE_EQ(to_string("a"), "a");
    ATF_REQUIRE_EQ(to_string(5), "5");
}

ATF_TEST_CASE(to_type);
ATF_TEST_CASE_HEAD(to_type)
{
    set_md_var("descr", "Tests the to_type function");
}
ATF_TEST_CASE_BODY(to_type)
{
    using tools::text::to_type;

    ATF_REQUIRE_EQ(to_type< int >("0"), 0);
    ATF_REQUIRE_EQ(to_type< int >("1234"), 1234);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("   "));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("0 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("a"));

    ATF_REQUIRE_EQ(to_type< float >("0.5"), 0.5);
    ATF_REQUIRE_EQ(to_type< float >("1234.5"), 1234.5);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("0.5 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("a"));

    ATF_REQUIRE_EQ(to_type< std::string >("a"), "a");
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the test cases for the free functions.
    ATF_ADD_TEST_CASE(tcs, duplicate);
    ATF_ADD_TEST_CASE(tcs, join);
    ATF_ADD_TEST_CASE(tcs, match);
    ATF_ADD_TEST_CASE(tcs, split);
    ATF_ADD_TEST_CASE(tcs, split_delims);
    ATF_ADD_TEST_CASE(tcs, trim);
    ATF_ADD_TEST_CASE(tcs, to_bool);
    ATF_ADD_TEST_CASE(tcs, to_bytes);
    ATF_ADD_TEST_CASE(tcs, to_string);
    ATF_ADD_TEST_CASE(tcs, to_type);
}
@


1.1.1.1.4.1
log
@file text_test.cpp was added on branch yamt-pagecache on 2014-05-22 15:40:12 +0000
@
text
@d1 390
@


1.1.1.1.4.2
log
@sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs.  ("Protocol error: too many arguments")
@
text
@a0 390
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include <cstring>
#include <set>
#include <vector>

#include <atf-c++.hpp>

#include "text.hpp"

// ------------------------------------------------------------------------
// Test cases for the free functions.
// ------------------------------------------------------------------------

ATF_TEST_CASE(duplicate);
ATF_TEST_CASE_HEAD(duplicate)
{
    set_md_var("descr", "Tests the duplicate function");
}
ATF_TEST_CASE_BODY(duplicate)
{
    using tools::text::duplicate;

    const char* orig = "foo";

    char* copy = duplicate(orig);
    ATF_REQUIRE_EQ(std::strlen(copy), 3);
    ATF_REQUIRE(std::strcmp(copy, "foo") == 0);

    std::strcpy(copy, "bar");
    ATF_REQUIRE(std::strcmp(copy, "bar") == 0);
    ATF_REQUIRE(std::strcmp(orig, "foo") == 0);
}

ATF_TEST_CASE(join);
ATF_TEST_CASE_HEAD(join)
{
    set_md_var("descr", "Tests the join function");
}
ATF_TEST_CASE_BODY(join)
{
    using tools::text::join;

    // First set of tests using a non-sorted collection, std::vector.
    {
        std::vector< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.push_back("");
        words.push_back("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",");

        words.clear();
        words.push_back("foo");
        words.push_back("");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,,baz");

        words.clear();
        words.push_back("foo");
        words.push_back("bar");
        words.push_back("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "foo,bar,baz");
    }

    // Second set of tests using a sorted collection, std::set.
    {
        std::set< std::string > words;
        std::string str;

        words.clear();
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "");

        words.clear();
        words.insert("foo");
        words.insert("");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, ",baz,foo");

        words.clear();
        words.insert("foo");
        words.insert("bar");
        words.insert("baz");
        str = join(words, ",");
        ATF_REQUIRE_EQ(str, "bar,baz,foo");
    }
}

ATF_TEST_CASE(match);
ATF_TEST_CASE_HEAD(match)
{
    set_md_var("descr", "Tests the match function");
}
ATF_TEST_CASE_BODY(match)
{
    using tools::text::match;

    ATF_REQUIRE_THROW(std::runtime_error, match("", "["));

    ATF_REQUIRE(match("", ""));
    ATF_REQUIRE(!match("foo", ""));

    ATF_REQUIRE(match("", ".*"));
    ATF_REQUIRE(match("", "[a-z]*"));

    ATF_REQUIRE(match("hello", "hello"));
    ATF_REQUIRE(match("hello", "[a-z]+"));
    ATF_REQUIRE(match("hello", "^[a-z]+$"));

    ATF_REQUIRE(!match("hello", "helooo"));
    ATF_REQUIRE(!match("hello", "[a-z]+5"));
    ATF_REQUIRE(!match("hello", "^ [a-z]+$"));
}

ATF_TEST_CASE(split);
ATF_TEST_CASE_HEAD(split)
{
    set_md_var("descr", "Tests the split function");
}
ATF_TEST_CASE_BODY(split)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("    ", " ");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split("a b", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("a b c d", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");
    ATF_REQUIRE_EQ(words[2], "c");
    ATF_REQUIRE_EQ(words[3], "d");

    words = split("foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar baz foobar", " ");
    ATF_REQUIRE_EQ(words.size(), 4);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
    ATF_REQUIRE_EQ(words[2], "baz");
    ATF_REQUIRE_EQ(words[3], "foobar");

    words = split(" foo bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo  bar", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("foo bar ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");

    words = split("  foo  bar  ", " ");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "foo");
    ATF_REQUIRE_EQ(words[1], "bar");
}

ATF_TEST_CASE(split_delims);
ATF_TEST_CASE_HEAD(split_delims)
{
    set_md_var("descr", "Tests the split function using different delimiters");
}
ATF_TEST_CASE_BODY(split_delims)
{
    using tools::text::split;

    std::vector< std::string > words;

    words = split("", "/");
    ATF_REQUIRE_EQ(words.size(), 0);

    words = split(" ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], " ");

    words = split("    ", "/");
    ATF_REQUIRE_EQ(words.size(), 1);
    ATF_REQUIRE_EQ(words[0], "    ");

    words = split("a/b", "/");
    ATF_REQUIRE_EQ(words.size(), 2);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "b");

    words = split("aLONGDELIMbcdLONGDELIMef", "LONGDELIM");
    ATF_REQUIRE_EQ(words.size(), 3);
    ATF_REQUIRE_EQ(words[0], "a");
    ATF_REQUIRE_EQ(words[1], "bcd");
    ATF_REQUIRE_EQ(words[2], "ef");
}

ATF_TEST_CASE(trim);
ATF_TEST_CASE_HEAD(trim)
{
    set_md_var("descr", "Tests the trim function");
}
ATF_TEST_CASE_BODY(trim)
{
    using tools::text::trim;

    ATF_REQUIRE_EQ(trim(""), "");
    ATF_REQUIRE_EQ(trim(" "), "");
    ATF_REQUIRE_EQ(trim("\t"), "");

    ATF_REQUIRE_EQ(trim(" foo"), "foo");
    ATF_REQUIRE_EQ(trim("\t foo"), "foo");
    ATF_REQUIRE_EQ(trim(" \tfoo"), "foo");
    ATF_REQUIRE_EQ(trim("foo\t "), "foo");
    ATF_REQUIRE_EQ(trim("foo \t"), "foo");

    ATF_REQUIRE_EQ(trim("foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("\t foo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim(" \tfoo bar"), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar\t "), "foo bar");
    ATF_REQUIRE_EQ(trim("foo bar \t"), "foo bar");
}

ATF_TEST_CASE(to_bool);
ATF_TEST_CASE_HEAD(to_bool)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_bool)
{
    using tools::text::to_bool;

    ATF_REQUIRE(to_bool("true"));
    ATF_REQUIRE(to_bool("TRUE"));
    ATF_REQUIRE(to_bool("yes"));
    ATF_REQUIRE(to_bool("YES"));

    ATF_REQUIRE(!to_bool("false"));
    ATF_REQUIRE(!to_bool("FALSE"));
    ATF_REQUIRE(!to_bool("no"));
    ATF_REQUIRE(!to_bool("NO"));

    ATF_REQUIRE_THROW(std::runtime_error, to_bool(""));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("tru"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("true2"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("fals"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bool("false2"));
}

ATF_TEST_CASE(to_bytes);
ATF_TEST_CASE_HEAD(to_bytes)
{
    set_md_var("descr", "Tests the to_bytes function");
}
ATF_TEST_CASE_BODY(to_bytes)
{
    using tools::text::to_bytes;

    ATF_REQUIRE_EQ(0, to_bytes("0"));
    ATF_REQUIRE_EQ(12345, to_bytes("12345"));
    ATF_REQUIRE_EQ(2 * 1024, to_bytes("2k"));
    ATF_REQUIRE_EQ(4 * 1024 * 1024, to_bytes("4m"));
    ATF_REQUIRE_EQ(int64_t(8) * 1024 * 1024 * 1024, to_bytes("8g"));
    ATF_REQUIRE_EQ(int64_t(16) * 1024 * 1024 * 1024 * 1024, to_bytes("16t"));

    ATF_REQUIRE_THROW_RE(std::runtime_error, "Empty", to_bytes(""));
    ATF_REQUIRE_THROW_RE(std::runtime_error, "Unknown size unit 'd'",
                         to_bytes("12d"));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" "));
    ATF_REQUIRE_THROW(std::runtime_error, to_bytes(" k"));
}

ATF_TEST_CASE(to_string);
ATF_TEST_CASE_HEAD(to_string)
{
    set_md_var("descr", "Tests the to_string function");
}
ATF_TEST_CASE_BODY(to_string)
{
    using tools::text::to_string;

    ATF_REQUIRE_EQ(to_string('a'), "a");
    ATF_REQUIRE_EQ(to_string("a"), "a");
    ATF_REQUIRE_EQ(to_string(5), "5");
}

ATF_TEST_CASE(to_type);
ATF_TEST_CASE_HEAD(to_type)
{
    set_md_var("descr", "Tests the to_type function");
}
ATF_TEST_CASE_BODY(to_type)
{
    using tools::text::to_type;

    ATF_REQUIRE_EQ(to_type< int >("0"), 0);
    ATF_REQUIRE_EQ(to_type< int >("1234"), 1234);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("   "));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("0 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< int >("a"));

    ATF_REQUIRE_EQ(to_type< float >("0.5"), 0.5);
    ATF_REQUIRE_EQ(to_type< float >("1234.5"), 1234.5);
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("0.5 a"));
    ATF_REQUIRE_THROW(std::runtime_error, to_type< float >("a"));

    ATF_REQUIRE_EQ(to_type< std::string >("a"), "a");
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the test cases for the free functions.
    ATF_ADD_TEST_CASE(tcs, duplicate);
    ATF_ADD_TEST_CASE(tcs, join);
    ATF_ADD_TEST_CASE(tcs, match);
    ATF_ADD_TEST_CASE(tcs, split);
    ATF_ADD_TEST_CASE(tcs, split_delims);
    ATF_ADD_TEST_CASE(tcs, trim);
    ATF_ADD_TEST_CASE(tcs, to_bool);
    ATF_ADD_TEST_CASE(tcs, to_bytes);
    ATF_ADD_TEST_CASE(tcs, to_string);
    ATF_ADD_TEST_CASE(tcs, to_type);
}
@


