feat(math_util): Templated min/max

This commit is contained in:
Michael Carlberg 2016-11-26 13:09:29 +01:00
parent 12ff82e913
commit c5bc338ae3
2 changed files with 28 additions and 0 deletions

View File

@ -7,6 +7,22 @@
POLYBAR_NS POLYBAR_NS
namespace math_util { namespace math_util {
/**
* Get the min value
*/
template <typename ValueType>
ValueType min(ValueType one, ValueType two) {
return one < two ? one : two;
}
/**
* Get the max value
*/
template <typename ValueType>
ValueType max(ValueType one, ValueType two) {
return one > two ? one : two;
}
/** /**
* Limit value T by min and max bounds * Limit value T by min and max bounds
*/ */

View File

@ -3,6 +3,18 @@
int main() { int main() {
using namespace polybar; using namespace polybar;
"min"_test = [] {
expect(math_util::min<int>(2, 5) == 2);
expect(math_util::min<int>(-8, -50) == -50);
expect(math_util::min<uint8_t>(0, -5) == 0);
};
"min"_test = [] {
expect(math_util::max<int>(2, 5) == 5);
expect(math_util::max<int>(-8, -50) == -8);
expect(math_util::max<uint8_t>(0, (1 << 8) - 5));
};
"cap"_test = [] { "cap"_test = [] {
expect(math_util::cap<int>(8, 0, 10) == 8); expect(math_util::cap<int>(8, 0, 10) == 8);
expect(math_util::cap<int>(-8, 0, 10) == 0); expect(math_util::cap<int>(-8, 0, 10) == 0);