Skip to content

Commit

Permalink
GHI #32 Add UT for patomic_internal_compare_align
Browse files Browse the repository at this point in the history
  • Loading branch information
doodspav committed Jul 14, 2024
1 parent 650a731 commit 3c55b2c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/include/patomic/internal/align.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions test/kind/ut/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 8 additions & 0 deletions test/kind/ut/api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ---- Create Tests ----

create_ut(
NAME UtApiAlign
SOURCE
align.cpp
"${PATOMIC_SOURCE_DIR}/src/api/align.c"
)
75 changes: 75 additions & 0 deletions test/kind/ut/api/align.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
extern "C" {
#include <patomic/internal/align.h>
};

#include <gtest/gtest.h>


/// @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);
}

0 comments on commit 3c55b2c

Please sign in to comment.