displayLayout and Rendering TUI library |
git clone git://git.dimitrijedobrota.com/display.git |
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | c451c9d592719287e5fe70b35cc303c4a233dce0 |
parent | 6d3a184649746770fac3ca971257c3ee1b326c04 |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Sun, 9 Feb 2025 10:47:11 +0100 |
Add tests and improve consistency of utility
Diffstat:M | CMakeLists.txt | | | +- |
M | cmake/dev-mode.cmake | | | +++++ |
M | include/display/utility.hpp | | | ++++++++++++++++---------- |
A | test/CMakeLists.txt | | | +++++++++++++++++++++++++ |
A | test/source/utility_test.cpp | | | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 99 insertions(+), 11 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
display
VERSION 0.1.9
VERSION 0.1.10
DESCRIPTION "TUI library"
HOMEPAGE_URL "https://example.com/"
LANGUAGES CXX
diff --git a/cmake/dev-mode.cmake b/cmake/dev-mode.cmake
@@ -1,5 +1,10 @@
include(cmake/folders.cmake)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(test)
endif()
option(ENABLE_COVERAGE "Enable coverage support separate from CTest's" OFF)
if(ENABLE_COVERAGE)
include(cmake/coverage.cmake)
diff --git a/include/display/utility.hpp b/include/display/utility.hpp
@@ -6,55 +6,61 @@ namespace display
{
template<typename T>
inline bool is_oveflow_lim(T val, T add, T lim)
requires std::is_unsigned_v<T>
constexpr bool is_overflow_lim(T val, T add, T lim)
{
return val > lim || add > lim || val > lim - add;
}
template<typename T>
inline bool is_underflow_lim(T val, T sub, T lim)
requires std::is_unsigned_v<T>
constexpr bool is_underflow_lim(T val, T sub, T lim)
{
return val < lim || sub < lim || val < lim + sub;
}
template<typename T>
inline bool is_oveflow(T val, T add)
requires std::is_unsigned_v<T>
constexpr bool is_overflow(T val, T add)
{
return val > std::numeric_limits<T>::max() - add;
}
template<typename T>
inline bool is_underflow(T val, T sub)
requires std::is_unsigned_v<T>
constexpr bool is_underflow(T val, T sub)
{
return val < std::numeric_limits<T>::min() + sub;
}
template<typename T>
inline T add_lim(T val, T add, T lim)
requires std::is_unsigned_v<T>
constexpr T add_lim(T val, T add, T lim)
{
return !is_oveflow_lim(val, add, lim) ? val + add : lim;
return !is_overflow_lim(val, add, lim) ? val + add : lim;
}
template<typename T>
inline T sub_lim(T val, T sub, T lim)
requires std::is_unsigned_v<T>
constexpr T sub_lim(T val, T sub, T lim)
{
return !is_underflow_lim(val, sub, lim) ? val - sub : lim;
}
template<typename T>
inline T clamp_low(T val, T low)
constexpr T clamp_low(T val, T low)
{
return std::max(val, low);
}
template<typename T>
inline T clamp_high(T val, T high)
constexpr T clamp_high(T val, T high)
{
return std::min(val, high);
}
template<typename T>
inline T clamp(T val, T low, T high)
constexpr T clamp(T val, T low, T high)
{
return std::max(std::min(val, high), low);
}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.14)
project(displayTests LANGUAGES CXX)
include(../cmake/project-is-top-level.cmake)
include(../cmake/folders.cmake)
# ---- Dependencies ----
if(PROJECT_IS_TOP_LEVEL)
find_package(display REQUIRED)
enable_testing()
endif()
# ---- Tests ----
add_executable(utility_test source/utility_test.cpp)
target_link_libraries(utility_test PRIVATE display::display)
target_compile_features(utility_test PRIVATE cxx_std_20)
add_test(NAME utility_test COMMAND utility_test)
# ---- End-of-file commands ----
add_folders(Test)
diff --git a/test/source/utility_test.cpp b/test/source/utility_test.cpp
@@ -0,0 +1,52 @@
#include <limits>
#include "display/utility.hpp"
#include "display/types.hpp"
int main()
{
using namespace display; // NOLINT
using lim = std::numeric_limits<sz_t>;
static constexpr const sz_t zero = 0;
static constexpr const sz_t one = 1;
// is_overflow
static_assert(!is_overflow(lim::max(), zero));
static_assert(is_overflow(lim::max(), one));
static_assert(!is_overflow(lim::min(), zero));
static_assert(!is_overflow(lim::min(), one));
// is_underflow
static_assert(!is_underflow(lim::max(), zero));
static_assert(!is_underflow(lim::max(), one));
static_assert(!is_underflow(lim::min(), zero));
static_assert(is_underflow(lim::min(), one));
// is_overflow_lim
static_assert(!is_overflow_lim(1U, 0U, 2U));
static_assert(is_overflow_lim(1U, 2U, 2U));
static_assert(is_overflow_lim(3U, 1U, 2U));
static_assert(is_overflow_lim(1U, 3U, 2U));
// is_underflow_lim
static_assert(!is_underflow_lim(2U, 1U, 1U));
static_assert(is_underflow_lim(1U, 2U, 1U));
static_assert(is_underflow_lim(0U, 1U, 1U));
static_assert(is_underflow_lim(1U, 0U, 1U));
// add_lim
static_assert(add_lim(1U, 0U, 2U) == 1);
static_assert(add_lim(1U, 2U, 2U) == 2);
static_assert(add_lim(3U, 1U, 2U) == 2);
static_assert(add_lim(1U, 3U, 2U) == 2);
// sub_lim
static_assert(sub_lim(3U, 1U, 1U) == 2);
static_assert(sub_lim(1U, 2U, 1U) == 1);
static_assert(sub_lim(0U, 1U, 1U) == 1);
static_assert(sub_lim(1U, 0U, 1U) == 1);
return 0;
}