From c5bc338ae33d38198e404b8a11edd0d6408cbd3f Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sat, 26 Nov 2016 13:09:29 +0100 Subject: [PATCH] feat(math_util): Templated min/max --- include/utils/math.hpp | 16 ++++++++++++++++ tests/unit_tests/utils/math.cpp | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/utils/math.hpp b/include/utils/math.hpp index 4d6dbed4..d65767cd 100644 --- a/include/utils/math.hpp +++ b/include/utils/math.hpp @@ -7,6 +7,22 @@ POLYBAR_NS namespace math_util { + /** + * Get the min value + */ + template + ValueType min(ValueType one, ValueType two) { + return one < two ? one : two; + } + + /** + * Get the max value + */ + template + ValueType max(ValueType one, ValueType two) { + return one > two ? one : two; + } + /** * Limit value T by min and max bounds */ diff --git a/tests/unit_tests/utils/math.cpp b/tests/unit_tests/utils/math.cpp index c7f12ba3..605a362c 100644 --- a/tests/unit_tests/utils/math.cpp +++ b/tests/unit_tests/utils/math.cpp @@ -3,6 +3,18 @@ int main() { using namespace polybar; + "min"_test = [] { + expect(math_util::min(2, 5) == 2); + expect(math_util::min(-8, -50) == -50); + expect(math_util::min(0, -5) == 0); + }; + + "min"_test = [] { + expect(math_util::max(2, 5) == 5); + expect(math_util::max(-8, -50) == -8); + expect(math_util::max(0, (1 << 8) - 5)); + }; + "cap"_test = [] { expect(math_util::cap(8, 0, 10) == 8); expect(math_util::cap(-8, 0, 10) == 0);