diff --git a/src/lib/util.hpp b/src/lib/util.hpp index ce3755c52..b80468cce 100644 --- a/src/lib/util.hpp +++ b/src/lib/util.hpp @@ -59,7 +59,16 @@ namespace util { /** helper to treat int or long division uniformly */ template - struct IDiv; + struct IDiv + { + I quot; + I rem; + + IDiv (I num, I den) + : quot(num/den) + , rem(num - quot*den) + { } + }; template<> struct IDiv diff --git a/tests/lib/util-floordiv-test.cpp b/tests/lib/util-floordiv-test.cpp index 015faaf64..dad2a40b7 100644 --- a/tests/lib/util-floordiv-test.cpp +++ b/tests/lib/util-floordiv-test.cpp @@ -90,6 +90,7 @@ namespace test { /********************************************************************** * @test Evaluate a custom built integer floor function. + * Also known as Knuth's floor division. * This function is crucial for Lumiera's rule of quantisation * of time values into frame intervals. This rule requires time * points to be rounded towards the next lower frame border always, @@ -111,6 +112,12 @@ namespace test { { verifyBehaviour (); + verifyIntegerTypes(); + verifyIntegerTypes(); + verifyIntegerTypes(); + verifyIntegerTypes(); + verifyIntegerTypes(); + if (!isnil (arg)) runPerformanceTest(); } @@ -147,6 +154,22 @@ namespace test { } + template + void + verifyIntegerTypes () + { + I n,d,expectedRes; + + for (int i=-12; i <= 12; ++i) + { + n = i; + d = 4; + expectedRes = floordiv (i,4); + CHECK (floordiv(n,d) == expectedRes); + } + } + + /** @test timing measurements to compare implementation details. * This test uses a sequence of random integers, where the values