tests: unit: test for Power-of-Two utilities
Tests for Power-of-Two (PoT) utilities. Signed-off-by: Chris Friedt <cfriedt@meta.com>
This commit is contained in:
parent
c26fc959e6
commit
edb5ee1575
16
tests/unit/pot/CMakeLists.txt
Normal file
16
tests/unit/pot/CMakeLists.txt
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Copyright 2023 Meta
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
|
||||
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(pot_test)
|
||||
|
||||
FILE(GLOB app_sources *.c *.cpp)
|
||||
target_sources(testbinary PRIVATE ${app_sources})
|
||||
|
||||
# gcc
|
||||
target_compile_options(testbinary PRIVATE -Wno-overflow)
|
||||
|
||||
# clang
|
||||
target_compile_options(testbinary PRIVATE -Wno-shift-count-overflow -Wno-integer-overflow)
|
27
tests/unit/pot/is_power_of_two.c
Normal file
27
tests/unit/pot/is_power_of_two.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
ZTEST(pot, test_IS_POWER_OF_TWO)
|
||||
{
|
||||
zassert_false(IS_POWER_OF_TWO(-1));
|
||||
zassert_false(IS_POWER_OF_TWO(0));
|
||||
zassert_true(IS_POWER_OF_TWO(1));
|
||||
zassert_true(IS_POWER_OF_TWO(2));
|
||||
zassert_false(IS_POWER_OF_TWO(3));
|
||||
zassert_true(IS_POWER_OF_TWO(4));
|
||||
zassert_true(IS_POWER_OF_TWO(BIT(30)));
|
||||
zassert_false(IS_POWER_OF_TWO(BIT(30) + 1));
|
||||
zassert_true(IS_POWER_OF_TWO(BIT64(32)));
|
||||
zassert_false(IS_POWER_OF_TWO(BIT64(32) + 1));
|
||||
zassert_true(IS_POWER_OF_TWO(BIT64(63)));
|
||||
zassert_false(IS_POWER_OF_TWO(BIT64(63) + 1));
|
||||
zassert_false(IS_POWER_OF_TWO(UINT64_MAX));
|
||||
}
|
36
tests/unit/pot/log2.c
Normal file
36
tests/unit/pot/log2.c
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static const uint8_t val = LOG2(42);
|
||||
static const uint8_t val64 = LOG2(42 + BIT64(32));
|
||||
|
||||
ZTEST(pot, test_LOG2)
|
||||
{
|
||||
zassert_equal(-1, LOG2(LLONG_MIN));
|
||||
zassert_equal(-1, LOG2(LONG_MIN));
|
||||
zassert_equal(-1, LOG2(INT_MIN));
|
||||
zassert_equal(-1, LOG2(-1));
|
||||
zassert_equal(-1, LOG2(0));
|
||||
zassert_equal(0, LOG2(1));
|
||||
zassert_equal(1, LOG2(2));
|
||||
zassert_equal(1, LOG2(3));
|
||||
zassert_equal(2, LOG2(4));
|
||||
zassert_equal(2, LOG2(5));
|
||||
zassert_equal(31, LOG2(BIT(31)));
|
||||
zassert_equal(31, LOG2(BIT(31) + 1));
|
||||
zassert_equal(31, LOG2(UINT32_MAX));
|
||||
zassert_equal(32, LOG2(BIT64(32)));
|
||||
zassert_equal(62, LOG2(BIT64(63) - 1));
|
||||
zassert_equal(63, LOG2(BIT64(63)));
|
||||
zassert_equal(63, LOG2(BIT64(63) + 1));
|
||||
zassert_equal(63, LOG2(UINT64_MAX));
|
||||
|
||||
zassert_equal(5, val);
|
||||
zassert_equal(32, val64);
|
||||
}
|
39
tests/unit/pot/log2.cpp
Normal file
39
tests/unit/pot/log2.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static constexpr uint8_t val = LOG2(42);
|
||||
static constexpr uint8_t val64 = LOG2(42 + BIT64(32));
|
||||
|
||||
template <typename T> static inline constexpr uint8_t log2(T x)
|
||||
{
|
||||
return LOG2(x);
|
||||
}
|
||||
|
||||
ZTEST(pot, test_constexpr_LOG2)
|
||||
{
|
||||
zassert_equal(-1, LOG2(0));
|
||||
zassert_equal(0, LOG2(1));
|
||||
zassert_equal(1, LOG2(2));
|
||||
zassert_equal(1, LOG2(3));
|
||||
zassert_equal(2, LOG2(4));
|
||||
zassert_equal(2, LOG2(5));
|
||||
zassert_equal(31, LOG2(BIT(31)));
|
||||
zassert_equal(31, LOG2(BIT(31) + 1));
|
||||
zassert_equal(31, LOG2(UINT32_MAX));
|
||||
zassert_equal(32, LOG2(BIT64(32)));
|
||||
zassert_equal(62, LOG2(BIT64(63) - 1));
|
||||
zassert_equal(63, LOG2(BIT64(63)));
|
||||
zassert_equal(63, LOG2(BIT64(63) + 1));
|
||||
zassert_equal(63, LOG2(UINT64_MAX));
|
||||
|
||||
zassert_equal(5, val);
|
||||
zassert_equal(5, log2(42));
|
||||
zassert_equal(32, val64);
|
||||
zassert_equal(32, log2(42 + BIT64(32)));
|
||||
}
|
37
tests/unit/pot/log2ceil.c
Normal file
37
tests/unit/pot/log2ceil.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static const uint8_t val = LOG2CEIL(42);
|
||||
static const uint8_t val64 = LOG2CEIL(42 + BIT64(32));
|
||||
|
||||
ZTEST(pot, test_LOG2CEIL)
|
||||
{
|
||||
zassert_equal(0, LOG2CEIL(LLONG_MIN));
|
||||
zassert_equal(0, LOG2CEIL(LONG_MIN));
|
||||
zassert_equal(0, LOG2CEIL(INT_MIN));
|
||||
zassert_equal(0, LOG2CEIL(-1));
|
||||
zassert_equal(0, LOG2CEIL(0));
|
||||
zassert_equal(0, LOG2CEIL(1));
|
||||
zassert_equal(1, LOG2CEIL(2));
|
||||
zassert_equal(2, LOG2CEIL(3));
|
||||
zassert_equal(2, LOG2CEIL(4));
|
||||
zassert_equal(3, LOG2CEIL(5));
|
||||
zassert_equal(31, LOG2CEIL(BIT(31)));
|
||||
zassert_equal(32, LOG2CEIL(BIT(31) + 1));
|
||||
zassert_equal(32, LOG2CEIL(UINT32_MAX));
|
||||
zassert_equal(32, LOG2CEIL(BIT64(32)));
|
||||
zassert_equal(33, LOG2CEIL(BIT64(32) + 1));
|
||||
zassert_equal(63, LOG2CEIL(BIT64(63) - 1));
|
||||
zassert_equal(63, LOG2CEIL(BIT64(63)));
|
||||
zassert_equal(64, LOG2CEIL(BIT64(63) + 1));
|
||||
zassert_equal(64, LOG2CEIL(UINT64_MAX));
|
||||
|
||||
zassert_equal(6, val);
|
||||
zassert_equal(33, val64);
|
||||
}
|
44
tests/unit/pot/log2ceil.cpp
Normal file
44
tests/unit/pot/log2ceil.cpp
Normal file
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static constexpr uint8_t val = LOG2CEIL(32);
|
||||
static constexpr uint8_t val64 = LOG2CEIL(42 + BIT64(32));
|
||||
|
||||
template <typename T> static inline constexpr uint8_t log2ceil(T x)
|
||||
{
|
||||
return LOG2CEIL(x);
|
||||
}
|
||||
|
||||
ZTEST(pot, test_constexpr_LOG2CEIL)
|
||||
{
|
||||
zassert_equal(0, LOG2CEIL(LLONG_MIN));
|
||||
zassert_equal(0, LOG2CEIL(LONG_MIN));
|
||||
zassert_equal(0, LOG2CEIL(INT_MIN));
|
||||
zassert_equal(0, LOG2CEIL(-1));
|
||||
zassert_equal(0, LOG2CEIL(0));
|
||||
zassert_equal(0, LOG2CEIL(1));
|
||||
zassert_equal(1, LOG2CEIL(2));
|
||||
zassert_equal(2, LOG2CEIL(3));
|
||||
zassert_equal(2, LOG2CEIL(4));
|
||||
zassert_equal(3, LOG2CEIL(5));
|
||||
zassert_equal(31, LOG2CEIL(BIT(31)));
|
||||
zassert_equal(32, LOG2CEIL(BIT(31) + 1));
|
||||
zassert_equal(32, LOG2CEIL(UINT32_MAX));
|
||||
zassert_equal(32, LOG2CEIL(BIT64(32)));
|
||||
zassert_equal(33, LOG2CEIL(BIT64(32) + 1));
|
||||
zassert_equal(63, LOG2CEIL(BIT64(63) - 1));
|
||||
zassert_equal(63, LOG2CEIL(BIT64(63)));
|
||||
zassert_equal(64, LOG2CEIL(BIT64(63) + 1));
|
||||
zassert_equal(64, LOG2CEIL(UINT64_MAX));
|
||||
|
||||
zassert_equal(5, val);
|
||||
zassert_equal(5, log2ceil(32));
|
||||
zassert_equal(33, val64);
|
||||
zassert_equal(33, log2ceil(42 + BIT64(32)));
|
||||
}
|
9
tests/unit/pot/main.c
Normal file
9
tests/unit/pot/main.c
Normal file
|
@ -0,0 +1,9 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
ZTEST_SUITE(pot, NULL, NULL, NULL, NULL, NULL);
|
40
tests/unit/pot/nhpot.c
Normal file
40
tests/unit/pot/nhpot.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static const uint32_t val = NHPOT(42);
|
||||
static const uint64_t val64 = NHPOT(42 + BIT64(32));
|
||||
|
||||
ZTEST(pot, test_NHPOT)
|
||||
{
|
||||
zassert_equal(1, NHPOT(LLONG_MIN));
|
||||
zassert_equal(1, NHPOT(LONG_MIN));
|
||||
zassert_equal(1, NHPOT(INT_MIN));
|
||||
zassert_equal(1, NHPOT(-1));
|
||||
zassert_equal(1, NHPOT(0));
|
||||
zassert_equal(1, NHPOT(1));
|
||||
zassert_equal(2, NHPOT(2));
|
||||
zassert_equal(4, NHPOT(3));
|
||||
zassert_equal(4, NHPOT(4));
|
||||
zassert_equal(8, NHPOT(5));
|
||||
zassert_equal(BIT(31), NHPOT(BIT(31)));
|
||||
zassert_equal(BIT64(32), NHPOT(BIT(31) + 1));
|
||||
zassert_equal(BIT64(32), NHPOT(UINT32_MAX));
|
||||
zassert_equal(BIT64(32), NHPOT(BIT64(32)));
|
||||
zassert_equal(0, (uint32_t)NHPOT(BIT64(32)));
|
||||
zassert_equal(BIT64(33), NHPOT(BIT64(32) + 1));
|
||||
zassert_equal(BIT64(63), NHPOT(BIT64(63) - 1));
|
||||
zassert_equal(BIT64(63), NHPOT(BIT64(63)));
|
||||
zassert_equal(0, NHPOT(BIT64(63) + 1));
|
||||
zassert_equal(0, NHPOT(UINT64_MAX));
|
||||
|
||||
zassert_equal(64, val);
|
||||
zassert_equal(BIT64(33), val64);
|
||||
}
|
45
tests/unit/pot/nhpot.cpp
Normal file
45
tests/unit/pot/nhpot.cpp
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright 2023 Meta
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/ztest.h>
|
||||
|
||||
static constexpr uint32_t val = NHPOT(42);
|
||||
static constexpr uint64_t val64 = NHPOT(42 + BIT64(32));
|
||||
|
||||
template <typename T> static inline constexpr uint64_t nhpot(T x)
|
||||
{
|
||||
return NHPOT(x);
|
||||
}
|
||||
|
||||
ZTEST(pot, test_constexpr_NHPOT)
|
||||
{
|
||||
zassert_equal(1, NHPOT(LLONG_MIN));
|
||||
zassert_equal(1, NHPOT(LONG_MIN));
|
||||
zassert_equal(1, NHPOT(INT_MIN));
|
||||
zassert_equal(1, NHPOT(-1));
|
||||
zassert_equal(1, NHPOT(0));
|
||||
zassert_equal(1, NHPOT(1));
|
||||
zassert_equal(2, NHPOT(2));
|
||||
zassert_equal(4, NHPOT(3));
|
||||
zassert_equal(4, NHPOT(4));
|
||||
zassert_equal(8, NHPOT(5));
|
||||
zassert_equal(BIT(31), NHPOT(BIT(31)));
|
||||
zassert_equal(BIT64(32), NHPOT(BIT(31) + 1));
|
||||
zassert_equal(BIT64(32), NHPOT(UINT32_MAX));
|
||||
zassert_equal(BIT64(32), NHPOT(BIT64(32)));
|
||||
zassert_equal(0, (uint32_t)NHPOT(BIT64(32)));
|
||||
zassert_equal(BIT64(33), NHPOT(BIT64(32) + 1));
|
||||
zassert_equal(BIT64(63), NHPOT(BIT64(63) - 1));
|
||||
zassert_equal(BIT64(63), NHPOT(BIT64(63)));
|
||||
zassert_equal(0, NHPOT(BIT64(63) + 1));
|
||||
zassert_equal(0, NHPOT(UINT64_MAX));
|
||||
|
||||
zassert_equal(64, val);
|
||||
zassert_equal(64, nhpot(42));
|
||||
zassert_equal(BIT64(33), val64);
|
||||
zassert_equal(BIT64(33), nhpot(42 + BIT64(32)));
|
||||
}
|
5
tests/unit/pot/prj.conf
Normal file
5
tests/unit/pot/prj.conf
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Copyright 2023 Meta
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
CONFIG_ZTEST=y
|
||||
CONFIG_ZTEST_NEW_API=y
|
7
tests/unit/pot/testcase.yaml
Normal file
7
tests/unit/pot/testcase.yaml
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Copyright 2023 Meta
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
tests:
|
||||
utilities.powers_of_two:
|
||||
tags: pot
|
||||
type: unit
|
Loading…
Reference in a new issue