From 554e755ea1a876c7f37aab42bdd61888ba3b0442 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Daisuke Date: Sun, 29 Mar 2020 21:44:28 +0900 Subject: [PATCH] clamp: Add. --- clamp.h | 50 ++++++++++++++++++++++++++++++++++++++++++++ test/clamp_test.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 clamp.h create mode 100644 test/clamp_test.cpp diff --git a/clamp.h b/clamp.h new file mode 100644 index 0000000..f05bff0 --- /dev/null +++ b/clamp.h @@ -0,0 +1,50 @@ +/*! + * \file + */ +/* + * zlib License + * + * Copyright (C) 2020 KOBAYASHI Daisuke + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef CLAMP_H_ +#define CLAMP_H_ + +#ifndef CLAMP_T +#define CLAMP_T int +#endif + +#define CLAMP_MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define CLAMP_MAX(a, b) (((a) > (b)) ? (a) : (b)) + +/** Clamps a variable to a given range. + * + * @param value value to clamp + * @param min lower limit + * @param max upper limit + */ +static inline CLAMP_T clamp(CLAMP_T value, CLAMP_T min, CLAMP_T max) +{ + return CLAMP_MIN(CLAMP_MAX(value, min), max); +} + +#undef CLAMP_MIN +#undef CLAMP_MAX + +#endif /* CLAMP_H_ */ diff --git a/test/clamp_test.cpp b/test/clamp_test.cpp new file mode 100644 index 0000000..898dc8a --- /dev/null +++ b/test/clamp_test.cpp @@ -0,0 +1,51 @@ +/* + * zlib License + * + * Copyright (C) 2029 KOBAYASHI Daisuke + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#include + +extern "C" { +#include "clamp.h" +} + +TEST(Clamp, Lower) +{ + EXPECT_EQ(-10, clamp(-20, -10, 10)); + EXPECT_EQ(0, clamp(-10, 0, 10)); + EXPECT_EQ(200, clamp(100, 200, 300)); +} + +TEST(Clamp, Value) +{ + EXPECT_EQ(-300, clamp(-300, -500, 100)); + EXPECT_EQ(0, clamp(0, 0, 10)); + EXPECT_EQ(1, clamp(1, 0, 10)); + EXPECT_EQ(5, clamp(5, 0, 10)); + EXPECT_EQ(10, clamp(10, 0, 10)); + EXPECT_EQ(50, clamp(50, 0, 500)); + EXPECT_EQ(1001, clamp(1001, 1000, 10000)); +} +TEST(Clamp, Upper) +{ + EXPECT_EQ(-20, clamp(10, -300, -20)); + EXPECT_EQ(10, clamp(11, 0, 10)); + EXPECT_EQ(1000, clamp(2000, 0, 1000)); +}