From 967d2ac6c2034d1374fc603e64212fc06b5f6133 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Aug 2018 23:11:28 +0200 Subject: Add tests for left and right rotation --- test/test_bitvec.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/test_bitvec.cpp b/test/test_bitvec.cpp index 78a8272..2fa62ca 100644 --- a/test/test_bitvec.cpp +++ b/test/test_bitvec.cpp @@ -195,6 +195,40 @@ test_right_shift(Context& ctx) } } +template +void +test_left_rotate(Context& ctx) +{ + const T v = make_random_bitvec(ctx); + for (size_t width = 0; width < std::min(N, size_t(128)); ++width) { + for (size_t bits = 0; bits <= width; ++bits) { + T r = v; + r.rotl(bits, width); + + for (size_t i = 0; i < width; ++i) { + assert(r.test((i + bits) % width) == v.test(i)); + } + } + } +} + +template +void +test_right_rotate(Context& ctx) +{ + const T v = make_random_bitvec(ctx); + for (size_t width = 0; width < std::min(N, size_t(128)); ++width) { + for (size_t bits = 0; bits <= width; ++bits) { + T r = v; + r.rotr(bits, width); + + for (size_t i = 0; i < width; ++i) { + assert(r.test(i) == v.test((i + bits) % width)); + } + } + } +} + template void test_find_first(Context&) @@ -283,6 +317,8 @@ test(Context& ctx) test_reset_all(ctx); test_left_shift(ctx); test_right_shift(ctx); + test_left_rotate(ctx); + test_right_rotate(ctx); test_find_first(ctx); test_gray_code(ctx); test_comparison(ctx); -- cgit v1.2.1