diff --git a/src/include/patomic/internal/align.h b/src/include/patomic/internal/align.h index d0a2675a0..3c142066f 100644 --- a/src/include/patomic/internal/align.h +++ b/src/include/patomic/internal/align.h @@ -12,6 +12,10 @@ * * @details * Priority is given to "recommended", then "minimum", then "size_within". + * + * @return + * Returns -1 if lhs is a looser requirement than rhs, 1 if lhs is a stricter + * requirement than rhs, otherwise 0. */ int patomic_internal_compare_align( diff --git a/test/kind/ut/CMakeLists.txt b/test/kind/ut/CMakeLists.txt index fe6d8adb6..4194fd724 100644 --- a/test/kind/ut/CMakeLists.txt +++ b/test/kind/ut/CMakeLists.txt @@ -2,3 +2,6 @@ add_custom_target(${test_target_name}-ut) add_dependencies(${test_target_name} ${test_target_name}-ut) + +# add all subdirectories +add_subdirectory(api) diff --git a/test/kind/ut/api/CMakeLists.txt b/test/kind/ut/api/CMakeLists.txt new file mode 100644 index 000000000..2f715fdaa --- /dev/null +++ b/test/kind/ut/api/CMakeLists.txt @@ -0,0 +1,8 @@ +# ---- Create Tests ---- + +create_ut( + NAME UtApiAlign + SOURCE + align.cpp + "${PATOMIC_SOURCE_DIR}/src/api/align.c" +) diff --git a/test/kind/ut/api/align.cpp b/test/kind/ut/api/align.cpp new file mode 100644 index 000000000..6734d4923 --- /dev/null +++ b/test/kind/ut/api/align.cpp @@ -0,0 +1,75 @@ +extern "C" { +#include +}; + +#include + + +/// @brief Test fixture. +class UtApiAlign : public testing::Test +{}; + + +/// @brief Comparing patomic_align_t prioritizes recommend over all other +/// members. +TEST_F(UtApiAlign, compare_align_recommended_ne) +{ + // setup + // larger recommended is stricter + patomic_align_t lt { 1, 16, 16 }; + patomic_align_t gt { 2, 8, 8 }; + + // test + // check values + EXPECT_LT(lt.recommended, gt.recommended); + EXPECT_GT(lt.minimum, gt.minimum); + EXPECT_GT(lt.size_within, gt.size_within); + // do comparison + EXPECT_LT(patomic_internal_compare_align(lt, gt), 0); + EXPECT_GT(patomic_internal_compare_align(gt, lt), 0); +} + +/// @brief Comparing patomic_align_t prioritizes minimum over all other members +/// if recommend compares equal. +TEST_F(UtApiAlign, compare_align_minimum_ne_recommended_eq) +{ + // setup + // larger recommended is stricter + patomic_align_t lt { 64, 1, 16 }; + patomic_align_t gt { 64, 2, 8 }; + + // test + // check values + EXPECT_EQ(lt.recommended, gt.recommended); + EXPECT_LT(lt.minimum, gt.minimum); + EXPECT_GT(lt.size_within, gt.size_within); + // do comparison + EXPECT_LT(patomic_internal_compare_align(lt, gt), 0); + EXPECT_GT(patomic_internal_compare_align(gt, lt), 0); +} + +/// @brief Comparing patomic_align_t prioritizes size_within after all other +/// members. +TEST_F(UtApiAlign, compare_align_size_within_any_minimum_eq_recommended_eq) +{ + // setup + // smaller size_within is stricter (except zero which is least strict) + patomic_align_t zero { 64, 32, 0 }; + patomic_align_t lt { 64, 32, 2 }; + patomic_align_t gt { 64, 32, 1 }; + + // test + // check values + EXPECT_EQ(0, zero.size_within); + EXPECT_EQ(lt.recommended, gt.recommended); + EXPECT_EQ(lt.minimum, gt.minimum); + EXPECT_GT(lt.size_within, gt.size_within); + // do comparison + EXPECT_EQ(patomic_internal_compare_align(zero, zero), 0); + EXPECT_EQ(patomic_internal_compare_align(lt, lt), 0); + EXPECT_EQ(patomic_internal_compare_align(gt, gt), 0); + EXPECT_LT(patomic_internal_compare_align(zero, lt), 0); + EXPECT_LT(patomic_internal_compare_align(lt, gt), 0); + EXPECT_GT(patomic_internal_compare_align(gt, lt), 0); + EXPECT_GT(patomic_internal_compare_align(lt, zero), 0); +}