view tests/libcommon/util/main.cpp @ 33:d4f5f7231b84

Misc: switch to .hpp, dos2unix everything while here, #478
author David Demelier <markand@malikania.fr>
date Fri, 17 Jun 2016 13:12:35 +0200
parents a1e80d991968
children fabbe1759cec
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.
 */

#include <gtest/gtest.h>

#include <malikania/util.hpp>

using namespace malikania;

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

TEST(Clamp, normal)
{
    ASSERT_EQ(5, util::clamp(5, 0, 10));
}

TEST(Clamp, minimum)
{
    ASSERT_EQ(0, util::clamp(0, 0, 10));
}

TEST(Clamp, maximum)
{
    ASSERT_EQ(10, util::clamp(10, 0, 10));
}

TEST(Clamp, less)
{
    ASSERT_EQ(0, util::clamp(-10, 0, 10));
}

TEST(Clamp, higher)
{
    ASSERT_EQ(10, util::clamp(20, 0, 10));
}

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

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

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

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

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

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

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

TEST(Netsplit, 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);

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

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}