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);