From 57cc0523f600575feda09bd9fae13f685ce85b0f Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Fri, 13 Dec 2024 17:50:59 -0500 Subject: Add some tests for LM_NEXT_POWER_OF_2 --- libmisc/CMakeLists.txt | 2 +- libmisc/tests/test_macro.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 libmisc/tests/test_macro.c (limited to 'libmisc') diff --git a/libmisc/CMakeLists.txt b/libmisc/CMakeLists.txt index 18053dc..83e91fe 100644 --- a/libmisc/CMakeLists.txt +++ b/libmisc/CMakeLists.txt @@ -15,7 +15,7 @@ add_lib_test(libmisc test_assert) add_lib_test(libmisc test_endian) add_lib_test(libmisc test_hash) add_lib_test(libmisc test_log) -#add_lib_test(libmisc test_macro) # TODO +add_lib_test(libmisc test_macro) add_lib_test(libmisc test_private) add_lib_test(libmisc test_rand) add_lib_test(libmisc test_vcall) diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c new file mode 100644 index 0000000..7cbf9d3 --- /dev/null +++ b/libmisc/tests/test_macro.c @@ -0,0 +1,32 @@ +/* libmisc/tests/test_macro.c - Tests for + * + * Copyright (C) 2024 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include + +#include "test.h" + +int main() { + /* valid down to 0. */ + test_assert(LM_NEXT_POWER_OF_2(0) == 1); + test_assert(LM_NEXT_POWER_OF_2(1) == 2); + test_assert(LM_NEXT_POWER_OF_2(2) == 4); + test_assert(LM_NEXT_POWER_OF_2(3) == 4); + test_assert(LM_NEXT_POWER_OF_2(4) == 8); + test_assert(LM_NEXT_POWER_OF_2(5) == 8); + test_assert(LM_NEXT_POWER_OF_2(6) == 8); + test_assert(LM_NEXT_POWER_OF_2(7) == 8); + test_assert(LM_NEXT_POWER_OF_2(8) == 16); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(16) == 32); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(0x7000000000000000) == 0x8000000000000000); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000-1) == 0x8000000000000000); + /* Valid up to 0x8000000000000000-1 = (1<<63)-1 */ + test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000) == 0); /* :( */ + + return 0; +} -- cgit v1.2.3-2-g168b