aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-13 11:20:42 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commit39819593f45c29982182c740c5ba6669473ebfc3 (patch)
tree311b0fa20aff0afe115122875d1a469924bc276c /tests
parent2901a3669de67025f4daa47e5c452a4ffbec1476 (diff)
downloadserd-39819593f45c29982182c740c5ba6669473ebfc3.tar.gz
serd-39819593f45c29982182c740c5ba6669473ebfc3.tar.bz2
serd-39819593f45c29982182c740c5ba6669473ebfc3.zip
Add integer and bit math utilities
Diffstat (limited to 'tests')
-rw-r--r--tests/int_math_test.c64
1 files changed, 64 insertions, 0 deletions
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 <http://drobilla.net>
+
+ 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 <assert.h>
+
+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;
+}