Mercurial > malikania
changeset 10:cff1d99eff11
Common: add util::clamp
- Add util::clamp to clamp value between a range,
- While here, put back tests on util::netsplit.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Apr 2016 13:41:21 +0200 |
parents | 35852c7d422a |
children | fe95a8db2970 |
files | libcommon/malikania/util.h tests/libcommon/util/main.cpp |
diffstat | 2 files changed, 60 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/libcommon/malikania/util.h Tue Mar 29 20:18:32 2016 +0200 +++ b/libcommon/malikania/util.h Fri Apr 01 13:41:21 2016 +0200 @@ -24,6 +24,7 @@ * @brief Some utilities */ +#include <algorithm> #include <string> #include <vector> @@ -40,6 +41,20 @@ */ std::vector<std::string> netsplit(std::string &input); +/** + * Clamp the value between low and high. + * + * @param value the value + * @param low the minimum value + * @param high the maximum value + * @return the value between minimum and maximum + */ +template <typename T> +constexpr T clamp(T value, T low, T high) noexcept +{ + return (value < high) ? std::max(value, low) : std::min(value, high); +} + } // !util } // !malikania
--- a/tests/libcommon/util/main.cpp Tue Mar 29 20:18:32 2016 +0200 +++ b/tests/libcommon/util/main.cpp Fri Apr 01 13:41:21 2016 +0200 @@ -1,5 +1,5 @@ /* - * main.cpp -- test NetworkUtil + * main.cpp -- test util * * Copyright (c) 2013-2016 Malikania Authors * @@ -18,26 +18,59 @@ #include <gtest/gtest.h> -#if 0 - -#include <malikania/NetworkUtil.h> +#include <malikania/util.h> using namespace malikania; -TEST(Basic, simple) +/* + * 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 = NetworkUtil::split(input); + std::vector<std::string> messages = util::netsplit(input); ASSERT_EQ(1U, messages.size()); ASSERT_EQ("hello world", messages[0]); ASSERT_TRUE(input.empty()); } -TEST(Basic, two) +TEST(Netsplit, two) { std::string input = "hello world\r\n\r\nhow are you?\r\n\r\n"; - std::vector<std::string> messages = NetworkUtil::split(input); + std::vector<std::string> messages = util::netsplit(input); ASSERT_EQ(2U, messages.size()); ASSERT_EQ("hello world", messages[0]); @@ -45,19 +78,19 @@ ASSERT_TRUE(input.empty()); } -TEST(Basic, imcomplete) +TEST(Netsplit, imcomplete) { std::string input = "hello world\r\n"; - std::vector<std::string> messages = NetworkUtil::split(input); + std::vector<std::string> messages = util::netsplit(input); ASSERT_EQ(0U, messages.size()); ASSERT_EQ("hello world\r\n", input); } -TEST(Basic, empty) +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 = NetworkUtil::split(input); + std::vector<std::string> messages = util::netsplit(input); ASSERT_EQ(3U, messages.size()); ASSERT_EQ("hello world", messages[0]); @@ -66,8 +99,6 @@ ASSERT_TRUE(input.empty()); } -#endif - int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);