# HG changeset patch # User David Demelier # Date 1459510881 -7200 # Node ID cff1d99eff116cf9cc3ddb1a4185ac051013d3f2 # Parent 35852c7d422a17d97ab7d1d52a737954abd674bc Common: add util::clamp - Add util::clamp to clamp value between a range, - While here, put back tests on util::netsplit. diff -r 35852c7d422a -r cff1d99eff11 libcommon/malikania/util.h --- 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 #include #include @@ -40,6 +41,20 @@ */ std::vector 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 +constexpr T clamp(T value, T low, T high) noexcept +{ + return (value < high) ? std::max(value, low) : std::min(value, high); +} + } // !util } // !malikania diff -r 35852c7d422a -r cff1d99eff11 tests/libcommon/util/main.cpp --- 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 -#if 0 - -#include +#include 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 messages = NetworkUtil::split(input); + std::vector 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 messages = NetworkUtil::split(input); + std::vector 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 messages = NetworkUtil::split(input); + std::vector 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 messages = NetworkUtil::split(input); + std::vector 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);