From 39819593f45c29982182c740c5ba6669473ebfc3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 13 Oct 2019 11:20:42 +0200 Subject: Add integer and bit math utilities --- tests/int_math_test.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/int_math_test.c (limited to 'tests/int_math_test.c') diff --git a/tests/int_math_test.c b/tests/int_math_test.c new file mode 100644 index 00000000..188559eb --- /dev/null +++ b/tests/int_math_test.c @@ -0,0 +1,64 @@ +/* + Copyright 2019-2020 David Robillard + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ + +#undef NDEBUG + +#include "../src/int_math.h" + +#include + +static void +test_clz32(void) +{ + for (unsigned i = 0; i < 32; ++i) { + assert(serd_clz32(1u << i) == 32u - i - 1u); + } +} + +static void +test_clz64(void) +{ + for (unsigned i = 0; i < 64; ++i) { + assert(serd_clz64(1ull << i) == 64u - i - 1u); + } +} + +static void +test_ilog2(void) +{ + for (unsigned i = 0; i < 64; ++i) { + assert(serd_ilog2(1ull << i) == i); + } +} + +static void +test_ilog10(void) +{ + uint64_t power = 1; + for (unsigned i = 0; i < 20; ++i, power *= 10) { + assert(serd_ilog10(power) == i); + } +} + +int +main(void) +{ + test_clz32(); + test_clz64(); + test_ilog2(); + test_ilog10(); + return 0; +} -- cgit v1.2.1