view tests/libcommon/util/main.cpp @ 44:f30c84b4b9ed

Tests: switch from GoogleTest to Boost.Unit, closes #588
author David Demelier <markand@malikania.fr>
date Wed, 30 Nov 2016 21:15:53 +0100
parents fabbe1759cec
children b0593a3e2ca8
line wrap: on
line source

/*
 * main.cpp -- test util
 *
 * Copyright (c) 2013-2016 Malikania Authors
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define BOOST_TEST_MODULE "Util"
#include <boost/test/unit_test.hpp>

#include <malikania/util.hpp>

using namespace mlk;

/*
 * util::clamp
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(clamp)

BOOST_AUTO_TEST_CASE(normal)
{
    BOOST_REQUIRE_EQUAL(5, util::clamp(5, 0, 10));
}

BOOST_AUTO_TEST_CASE(minimum)
{
    BOOST_REQUIRE_EQUAL(0, util::clamp(0, 0, 10));
}

BOOST_AUTO_TEST_CASE(maximum)
{
    BOOST_REQUIRE_EQUAL(10, util::clamp(10, 0, 10));
}

BOOST_AUTO_TEST_CASE(less)
{
    BOOST_REQUIRE_EQUAL(0, util::clamp(-10, 0, 10));
}

BOOST_AUTO_TEST_CASE(higher)
{
    BOOST_REQUIRE_EQUAL(10, util::clamp(20, 0, 10));
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::netsplit
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(netsplit)

BOOST_AUTO_TEST_CASE(simple)
{
    std::string input = "hello world\r\n\r\n";
    std::vector<std::string> messages = util::netsplit(input);

    BOOST_REQUIRE_EQUAL(1U, messages.size());
    BOOST_REQUIRE_EQUAL("hello world", messages[0]);
    BOOST_REQUIRE(input.empty());
}

BOOST_AUTO_TEST_CASE(two)
{
    std::string input = "hello world\r\n\r\nhow are you?\r\n\r\n";
    std::vector<std::string> messages = util::netsplit(input);

    BOOST_REQUIRE_EQUAL(2U, messages.size());
    BOOST_REQUIRE_EQUAL("hello world", messages[0]);
    BOOST_REQUIRE_EQUAL("how are you?", messages[1]);
    BOOST_REQUIRE(input.empty());
}

BOOST_AUTO_TEST_CASE(imcomplete)
{
    std::string input = "hello world\r\n";
    std::vector<std::string> messages = util::netsplit(input);

    BOOST_REQUIRE_EQUAL(0U, messages.size());
    BOOST_REQUIRE_EQUAL("hello world\r\n", input);
}

BOOST_AUTO_TEST_CASE(empty)
{
    std::string input = "hello world\r\n\r\n\r\n\r\nhow are you?\r\n\r\n";
    std::vector<std::string> messages = util::netsplit(input);

    BOOST_REQUIRE_EQUAL(3U, messages.size());
    BOOST_REQUIRE_EQUAL("hello world", messages[0]);
    BOOST_REQUIRE(messages[1].empty());
    BOOST_REQUIRE_EQUAL("how are you?", messages[2]);
    BOOST_REQUIRE(input.empty());
}

BOOST_AUTO_TEST_SUITE_END()