From 625313ab94d7baaf90a2f289cb2c54b2fb6b3890 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Mon, 2 Sep 2024 18:26:43 +0200 Subject: [PATCH 01/13] Added safuTimeSpecCompare Signed-off-by: Friedrich Schwedler --- src/safu/private/time.c | 11 +++++++++++ src/safu/public/safu/time.h | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/safu/private/time.c b/src/safu/private/time.c index b36bd9e..8605af5 100644 --- a/src/safu/private/time.c +++ b/src/safu/private/time.c @@ -4,6 +4,7 @@ #include #include "safu/log.h" +#include "safu/result.h" safuResultE_t safuTimeGetLocalTime(struct tm *localTime) { time_t currentTimestamp = 0; @@ -46,3 +47,13 @@ char *safuGetCurrentDateString(char *dateFormat) { } return date; } + +safuResultE_t safuTimeSpecCompare(struct timespec const *a, struct timespec const *b, int *value) { + safuResultE_t result = SAFU_RESULT_FAILED; + if (a != NULL && b != NULL && value != NULL) { + *value = (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec) + + (a->tv_sec == b->tv_sec) * ((a->tv_nsec > b->tv_nsec) - (a->tv_nsec < b->tv_nsec)); + result = SAFU_RESULT_OK; + } + return result; +} diff --git a/src/safu/public/safu/time.h b/src/safu/public/safu/time.h index 5ccfb23..f260658 100644 --- a/src/safu/public/safu/time.h +++ b/src/safu/public/safu/time.h @@ -12,6 +12,24 @@ safuResultE_t safuTimeGetLocalTime(struct tm *localTime); char *safuGetCurrentDateString(char *dateFormat); +/******************************************************************* + * Function: safuTimeSpecCompare + *------------------------------------------------------------------ + * Description: Compares two timespec timestamps + * + * Result: + * value (int *): + * the result of the comparison + * - 1 if a is more recent than b + * - 0 if a and be are the same + * - -1 if a is older than b + * + * Return: + * - SAFU_RESTULT_OK successful comparison, + * - SAFU_RESULT_FAILED one of the timestamps was a NULL pointer + ******************************************************************/ +safuResultE_t safuTimeSpecCompare(struct timespec const *a, struct timespec const *b, int *value); + __END_DECLS #endif From 5338ba579e9d7260496e59932fd4ceb3f27ca99c Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Tue, 3 Sep 2024 14:20:06 +0200 Subject: [PATCH 02/13] add unit tests for safuTimeSpecCompare Signed-off-by: Friedrich Schwedler --- test/utest/safu/time/CMakeLists.txt | 1 + .../time/safuTimeSpecCompare/CMakeLists.txt | 13 +++ .../case_error_parameter.c | 46 ++++++++ .../time/safuTimeSpecCompare/case_success.c | 103 ++++++++++++++++++ .../safuTimeSpecCompare_utest.c | 11 ++ .../safuTimeSpecCompare_utest.h | 10 ++ 6 files changed, 184 insertions(+) create mode 100644 test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt create mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c create mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_success.c create mode 100644 test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c create mode 100644 test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h diff --git a/test/utest/safu/time/CMakeLists.txt b/test/utest/safu/time/CMakeLists.txt index 8712829..abb195d 100644 --- a/test/utest/safu/time/CMakeLists.txt +++ b/test/utest/safu/time/CMakeLists.txt @@ -1,3 +1,4 @@ # SPDX-License-Identifier: MIT add_subdirectory(safuTimeGetLocalTime) +add_subdirectory(safuTimeSpecCompare) diff --git a/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt new file mode 100644 index 0000000..d9e3d9a --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: MIT +find_package(cmocka_extensions 0.53.1 REQUIRED) + +create_unit_test( + NAME + test_common_safuTimeSpecCompare_utest + SOURCES + case_success.c + case_error_parameter.c + safuTimeSpecCompare_utest.c + LIBRARIES + safu_weak +) diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c b/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c new file mode 100644 index 0000000..abfedf6 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: MIT +#include +#include + +#include "safuTimeSpecCompare_utest.h" + +int safuTestSafuTimeSpecCompareErrorParameterSetup(UNUSED void **state) { + return 0; +} +int safuTestSafuTimeSpecCompareErrorParameterTeardown(UNUSED void **state) { + return 0; +} +#define CMP_BASELINE 74 +void safuTestSafuTimeSpecCompareErrorParameter(UNUSED void **state) { + TEST("safuTimeSpecCompareErrorParameter"); + SHOULD("%s", "fail to compare two struct timespec because of NULL pointer"); + + struct timespec timeA = {.tv_sec = 603, .tv_nsec = 4242}; + struct timespec timeB = {.tv_sec = 2348, .tv_nsec = 32}; + int cmp = CMP_BASELINE; + safuResultE_t result; + + result = safuTimeSpecCompare(&timeA, NULL, &cmp); + assert_int_equal(result, SAFU_RESULT_FAILED); + assert_int_equal(cmp, CMP_BASELINE); + + result = safuTimeSpecCompare(NULL, &timeB, &cmp); + assert_int_equal(result, SAFU_RESULT_FAILED); + assert_int_equal(cmp, CMP_BASELINE); + + result = safuTimeSpecCompare(NULL, NULL, &cmp); + assert_int_equal(result, SAFU_RESULT_FAILED); + assert_int_equal(cmp, CMP_BASELINE); + + result = safuTimeSpecCompare(&timeA, &timeB, NULL); + assert_int_equal(result, SAFU_RESULT_FAILED); + + result = safuTimeSpecCompare(&timeA, NULL, NULL); + assert_int_equal(result, SAFU_RESULT_FAILED); + + result = safuTimeSpecCompare(NULL, &timeB, NULL); + assert_int_equal(result, SAFU_RESULT_FAILED); + + result = safuTimeSpecCompare(NULL, NULL, NULL); + assert_int_equal(result, SAFU_RESULT_FAILED); +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_success.c b/test/utest/safu/time/safuTimeSpecCompare/case_success.c new file mode 100644 index 0000000..06fd692 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/case_success.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "safuTimeSpecCompare_utest.h" + +int safuTestSafuTimeSpecCompareSuccessSetup(UNUSED void **state) { + return 0; +} + +int safuTestSafuTimeSpecCompareSuccessTeardown(UNUSED void **state) { + return 0; +} + +static void _manualChecks() { + struct timespec x; + struct timespec y; + int cmp; + safuResultE_t result; + + // x > y + x.tv_sec = 6; + y.tv_sec = 4; + result = safuTimeSpecCompare(&x, &y, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + assert_int_equal(cmp, 1); + + // x < y + x.tv_sec = 6; + y.tv_sec = 8; + result = safuTimeSpecCompare(&x, &y, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + assert_int_equal(cmp, -1); + + // x > y + y.tv_sec = x.tv_sec; + x.tv_nsec = 23; + y.tv_nsec = 12; + result = safuTimeSpecCompare(&x, &y, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + assert_int_equal(cmp, 1); + + // x < y + y.tv_sec = x.tv_sec; + x.tv_nsec = 23; + y.tv_nsec = 42; + result = safuTimeSpecCompare(&x, &y, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + assert_int_equal(cmp, -1); + + // x == y + y.tv_sec = x.tv_sec; + x.tv_nsec = y.tv_nsec; + result = safuTimeSpecCompare(&x, &y, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + assert_int_equal(cmp, 0); +} + +static void _checkComparision(struct timespec const *a, struct timespec const *b) { + int cmp; + safuResultE_t result = safuTimeSpecCompare(a, b, &cmp); + assert_int_equal(result, SAFU_RESULT_OK); + int value; + if (a->tv_sec > b->tv_sec) { + value = 1; + } else if (a->tv_sec < b->tv_sec) { + value = -1; + } else if (a->tv_nsec > b->tv_nsec) { + value = 1; + } else if (a->tv_nsec < b->tv_nsec) { + value = -1; + } else { + value = 0; + } + assert_int_equal(cmp, value); +} + +static void _genericChecks() { + time_t secsList[] = {0, 1, 12, INT_MAX}; + long nsecsList[] = {0, 1, 42, INT_MAX}; + + for (size_t as = 0; as < ARRAY_SIZE(secsList); as++) { + for (size_t an = 0; an < ARRAY_SIZE(nsecsList); an++) { + struct timespec a = {.tv_sec = secsList[as], .tv_nsec = nsecsList[an]}; + for (size_t bs = 0; bs < ARRAY_SIZE(secsList); bs++) { + for (size_t bn = 0; bn < ARRAY_SIZE(nsecsList); bn++) { + struct timespec b = {.tv_sec = secsList[bs], .tv_nsec = nsecsList[bn]}; + _checkComparision(&a, &b); + } + } + } + } +} + +void safuTestSafuTimeSpecCompareSuccess(UNUSED void **state) { + TEST("safuTimeSpecCompareSuccess"); + SHOULD("%s", "successfully comare two timespec structs"); + + _manualChecks(); + _genericChecks(); +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c new file mode 100644 index 0000000..09b2c7c --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: MIT +#include "safuTimeSpecCompare_utest.h" + +int main() { + const struct CMUnitTest tests[] = { + TEST_CASE(safuTestSafuTimeSpecCompareSuccess), + TEST_CASE(safuTestSafuTimeSpecCompareErrorParameter), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h new file mode 100644 index 0000000..c4ed637 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MIT +#ifndef __SAFU_TIME_SPEC_COMPARE_UTEST_H__ +#define __SAFU_TIME_SPEC_COMPARE_UTEST_H__ + +#include + +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareSuccess) +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareErrorParameter) + +#endif /* __SAFU_TIME_SPEC_COMPARE_UTEST_H__*/ From ac08518241b6aeacba03b8d814dd4891965676b9 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Tue, 3 Sep 2024 14:20:37 +0200 Subject: [PATCH 03/13] Add option to filter for specific unit tests to run Signed-off-by: Friedrich Schwedler --- ci/run_utest.sh | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/ci/run_utest.sh b/ci/run_utest.sh index d8702c4..1b916e0 100755 --- a/ci/run_utest.sh +++ b/ci/run_utest.sh @@ -4,6 +4,23 @@ set -e -u -o pipefail CMD_PATH="$(realpath "$(dirname "$0")")" BASE_DIR="$(realpath "$CMD_PATH/..")" +TESTS_REGEX="" +PARAM="" +while [ $# -gt 0 ]; do + case ${1} in + --tests-regex|-R) + TESTS_REGEX="--tests-regex ${2}" + shift + ;; + -*) + echo "error: unknown option: $1"; exit 1 ;; + *) + PARAM="$PARAM ${1}" ;; + esac + shift +done +set -- $PARAM + BUILD_TYPE="${1:-Debug}" . "$BASE_DIR/ci/common_names.sh" @@ -12,17 +29,21 @@ if [ ! -d "$CMAKE_BUILD_DIR" ]; then "$CMD_PATH/build.sh" "$BUILD_TYPE" fi +TEST_DIR="$CMAKE_BUILD_DIR" mkdir -p "$RESULT_DIR/unit_test" cd "$RESULT_DIR/unit_test" ctest --output-on-failure --force-new-ctest-process --verbose \ --output-junit "$RESULT_DIR/unit_test/junit.xml" \ --no-compress-output \ --output-log "$RESULT_DIR/unit_test/Test.log" \ - --test-dir "$BUILD_DIR/cmake" + --test-dir "$TEST_DIR" \ + ${TESTS_REGEX} + +cp -r "${CMAKE_BUILD_DIR}/Testing/Temporary" "${RESULT_DIR}/unit_test/" -TEST_LOG_FILE="$CMAKE_BUILD_DIR/Testing/Temporary/LastTest.log" -SKIPPED_TESTS=$(sed -n -e '/^# skip/p' "$TEST_LOG_FILE" | wc -l) -if [ "$SKIPPED_TESTS" -gt 0 ]; then +TEST_LOG_FILE="${TEST_DIR}/Testing/Temporary/LastTest.log" +SKIPPED_TESTS=$(sed -n -e '/^# skip/p' "${TEST_LOG_FILE}" | wc -l) +if [ "${SKIPPED_TESTS}" -gt 0 ]; then echo "Skipped tests (${SKIPPED_TESTS}):" - grep "# skip " "$TEST_LOG_FILE" + grep "# skip " "${TEST_LOG_FILE}" fi From 4aeef3ddc6538a9194a0c63bdcd59b0dac3ec40d Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 09:51:17 +0200 Subject: [PATCH 04/13] change safuTimeSpecCompare to not use pointer And to directly return instead of using a return parameter pointer. Also removes the need for a safuResultE_t to signal if there was a NULL pointer. Signed-off-by: Friedrich Schwedler --- src/safu/private/time.c | 11 ++--- src/safu/public/safu/time.h | 15 ++---- .../time/safuTimeSpecCompare/CMakeLists.txt | 1 - .../case_error_parameter.c | 46 ------------------- .../time/safuTimeSpecCompare/case_success.c | 46 ++++++++----------- .../safuTimeSpecCompare_utest.c | 1 - .../safuTimeSpecCompare_utest.h | 1 - 7 files changed, 27 insertions(+), 94 deletions(-) delete mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c diff --git a/src/safu/private/time.c b/src/safu/private/time.c index 8605af5..bfb0018 100644 --- a/src/safu/private/time.c +++ b/src/safu/private/time.c @@ -48,12 +48,7 @@ char *safuGetCurrentDateString(char *dateFormat) { return date; } -safuResultE_t safuTimeSpecCompare(struct timespec const *a, struct timespec const *b, int *value) { - safuResultE_t result = SAFU_RESULT_FAILED; - if (a != NULL && b != NULL && value != NULL) { - *value = (a->tv_sec > b->tv_sec) - (a->tv_sec < b->tv_sec) + - (a->tv_sec == b->tv_sec) * ((a->tv_nsec > b->tv_nsec) - (a->tv_nsec < b->tv_nsec)); - result = SAFU_RESULT_OK; - } - return result; +int safuTimeSpecCompare(struct timespec a, struct timespec b) { + return (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec) + + (a.tv_sec == b.tv_sec) * ((a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec)); } diff --git a/src/safu/public/safu/time.h b/src/safu/public/safu/time.h index f260658..5614b53 100644 --- a/src/safu/public/safu/time.h +++ b/src/safu/public/safu/time.h @@ -17,18 +17,13 @@ char *safuGetCurrentDateString(char *dateFormat); *------------------------------------------------------------------ * Description: Compares two timespec timestamps * - * Result: - * value (int *): - * the result of the comparison - * - 1 if a is more recent than b - * - 0 if a and be are the same - * - -1 if a is older than b - * * Return: - * - SAFU_RESTULT_OK successful comparison, - * - SAFU_RESULT_FAILED one of the timestamps was a NULL pointer + * the result of the comparison + * - 1 if a is more recent than b + * - 0 if a and be are the same + * - -1 if a is older than b ******************************************************************/ -safuResultE_t safuTimeSpecCompare(struct timespec const *a, struct timespec const *b, int *value); +int safuTimeSpecCompare(struct timespec a, struct timespec b); __END_DECLS diff --git a/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt index d9e3d9a..e858542 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt +++ b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt @@ -6,7 +6,6 @@ create_unit_test( test_common_safuTimeSpecCompare_utest SOURCES case_success.c - case_error_parameter.c safuTimeSpecCompare_utest.c LIBRARIES safu_weak diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c b/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c deleted file mode 100644 index abfedf6..0000000 --- a/test/utest/safu/time/safuTimeSpecCompare/case_error_parameter.c +++ /dev/null @@ -1,46 +0,0 @@ -// SPDX-License-Identifier: MIT -#include -#include - -#include "safuTimeSpecCompare_utest.h" - -int safuTestSafuTimeSpecCompareErrorParameterSetup(UNUSED void **state) { - return 0; -} -int safuTestSafuTimeSpecCompareErrorParameterTeardown(UNUSED void **state) { - return 0; -} -#define CMP_BASELINE 74 -void safuTestSafuTimeSpecCompareErrorParameter(UNUSED void **state) { - TEST("safuTimeSpecCompareErrorParameter"); - SHOULD("%s", "fail to compare two struct timespec because of NULL pointer"); - - struct timespec timeA = {.tv_sec = 603, .tv_nsec = 4242}; - struct timespec timeB = {.tv_sec = 2348, .tv_nsec = 32}; - int cmp = CMP_BASELINE; - safuResultE_t result; - - result = safuTimeSpecCompare(&timeA, NULL, &cmp); - assert_int_equal(result, SAFU_RESULT_FAILED); - assert_int_equal(cmp, CMP_BASELINE); - - result = safuTimeSpecCompare(NULL, &timeB, &cmp); - assert_int_equal(result, SAFU_RESULT_FAILED); - assert_int_equal(cmp, CMP_BASELINE); - - result = safuTimeSpecCompare(NULL, NULL, &cmp); - assert_int_equal(result, SAFU_RESULT_FAILED); - assert_int_equal(cmp, CMP_BASELINE); - - result = safuTimeSpecCompare(&timeA, &timeB, NULL); - assert_int_equal(result, SAFU_RESULT_FAILED); - - result = safuTimeSpecCompare(&timeA, NULL, NULL); - assert_int_equal(result, SAFU_RESULT_FAILED); - - result = safuTimeSpecCompare(NULL, &timeB, NULL); - assert_int_equal(result, SAFU_RESULT_FAILED); - - result = safuTimeSpecCompare(NULL, NULL, NULL); - assert_int_equal(result, SAFU_RESULT_FAILED); -} diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_success.c b/test/utest/safu/time/safuTimeSpecCompare/case_success.c index 06fd692..a4898bc 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/case_success.c +++ b/test/utest/safu/time/safuTimeSpecCompare/case_success.c @@ -17,64 +17,56 @@ int safuTestSafuTimeSpecCompareSuccessTeardown(UNUSED void **state) { static void _manualChecks() { struct timespec x; struct timespec y; - int cmp; - safuResultE_t result; + int result; // x > y x.tv_sec = 6; y.tv_sec = 4; - result = safuTimeSpecCompare(&x, &y, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); - assert_int_equal(cmp, 1); + result = safuTimeSpecCompare(x, y); + assert_int_equal(result, 1); // x < y x.tv_sec = 6; y.tv_sec = 8; - result = safuTimeSpecCompare(&x, &y, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); - assert_int_equal(cmp, -1); + result = safuTimeSpecCompare(x, y); + assert_int_equal(result, -1); // x > y y.tv_sec = x.tv_sec; x.tv_nsec = 23; y.tv_nsec = 12; - result = safuTimeSpecCompare(&x, &y, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); - assert_int_equal(cmp, 1); + result = safuTimeSpecCompare(x, y); + assert_int_equal(result, 1); // x < y y.tv_sec = x.tv_sec; x.tv_nsec = 23; y.tv_nsec = 42; - result = safuTimeSpecCompare(&x, &y, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); - assert_int_equal(cmp, -1); + result = safuTimeSpecCompare(x, y); + assert_int_equal(result, -1); // x == y y.tv_sec = x.tv_sec; x.tv_nsec = y.tv_nsec; - result = safuTimeSpecCompare(&x, &y, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); - assert_int_equal(cmp, 0); + result = safuTimeSpecCompare(x, y); + assert_int_equal(result, 0); } -static void _checkComparision(struct timespec const *a, struct timespec const *b) { - int cmp; - safuResultE_t result = safuTimeSpecCompare(a, b, &cmp); - assert_int_equal(result, SAFU_RESULT_OK); +static void _checkComparision(struct timespec a, struct timespec b) { + int result = safuTimeSpecCompare(a, b); int value; - if (a->tv_sec > b->tv_sec) { + if (a.tv_sec > b.tv_sec) { value = 1; - } else if (a->tv_sec < b->tv_sec) { + } else if (a.tv_sec < b.tv_sec) { value = -1; - } else if (a->tv_nsec > b->tv_nsec) { + } else if (a.tv_nsec > b.tv_nsec) { value = 1; - } else if (a->tv_nsec < b->tv_nsec) { + } else if (a.tv_nsec < b.tv_nsec) { value = -1; } else { value = 0; } - assert_int_equal(cmp, value); + assert_int_equal(result, value); } static void _genericChecks() { @@ -87,7 +79,7 @@ static void _genericChecks() { for (size_t bs = 0; bs < ARRAY_SIZE(secsList); bs++) { for (size_t bn = 0; bn < ARRAY_SIZE(nsecsList); bn++) { struct timespec b = {.tv_sec = secsList[bs], .tv_nsec = nsecsList[bn]}; - _checkComparision(&a, &b); + _checkComparision(a, b); } } } diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c index 09b2c7c..a5354be 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c @@ -4,7 +4,6 @@ int main() { const struct CMUnitTest tests[] = { TEST_CASE(safuTestSafuTimeSpecCompareSuccess), - TEST_CASE(safuTestSafuTimeSpecCompareErrorParameter), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h index c4ed637..e4fd548 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h @@ -5,6 +5,5 @@ #include TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareSuccess) -TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareErrorParameter) #endif /* __SAFU_TIME_SPEC_COMPARE_UTEST_H__*/ From c3165a2f142a252a50504ad76a3dfde6f54b1ac6 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 10:46:10 +0200 Subject: [PATCH 05/13] Add help message to run_utest.sh Signed-off-by: Friedrich Schwedler --- ci/run_utest.sh | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ci/run_utest.sh b/ci/run_utest.sh index 1b916e0..a9d1692 100755 --- a/ci/run_utest.sh +++ b/ci/run_utest.sh @@ -1,4 +1,23 @@ #!/bin/bash +############################################################################### +print_info() { + echo " + Run the unit test suite or parts of it. Default is to run all unit + test for the Debug build. + + Usage: ${0} [build type] [--test-regex|-R ] [-h|--help] + + build type: usually Debug or Release but can be any other build type + --test-regex|-R: execute all tests mathcing the pattern + -h|--help: print this help + + Examples: + ${0} Release # run all unit test on Release build + ${0} Release -R elosRpn # run all unit test containing elosRpn in + the name for the Release build. + " +} +############################################################################### set -e -u -o pipefail CMD_PATH="$(realpath "$(dirname "$0")")" @@ -12,8 +31,13 @@ while [ $# -gt 0 ]; do TESTS_REGEX="--tests-regex ${2}" shift ;; + -h|--help) + print_info + exit 0 ;; -*) - echo "error: unknown option: $1"; exit 1 ;; + echo "error: unknown option: $1" + print_info + exit 1 ;; *) PARAM="$PARAM ${1}" ;; esac From 819f003eeac72711b3383596285934c9fee3b0c4 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 10:51:38 +0200 Subject: [PATCH 06/13] Update paramter names and documentation string Signed-off-by: Friedrich Schwedler --- src/safu/private/time.c | 6 +++--- src/safu/public/safu/time.h | 14 ++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/safu/private/time.c b/src/safu/private/time.c index bfb0018..ae57b61 100644 --- a/src/safu/private/time.c +++ b/src/safu/private/time.c @@ -48,7 +48,7 @@ char *safuGetCurrentDateString(char *dateFormat) { return date; } -int safuTimeSpecCompare(struct timespec a, struct timespec b) { - return (a.tv_sec > b.tv_sec) - (a.tv_sec < b.tv_sec) + - (a.tv_sec == b.tv_sec) * ((a.tv_nsec > b.tv_nsec) - (a.tv_nsec < b.tv_nsec)); +int safuTimeSpecCompare(struct timespec timeA, struct timespec timeB) { + return (timeA.tv_sec > timeB.tv_sec) - (timeA.tv_sec < timeB.tv_sec) + + (timeA.tv_sec == timeB.tv_sec) * ((timeA.tv_nsec > timeB.tv_nsec) - (timeA.tv_nsec < timeB.tv_nsec)); } diff --git a/src/safu/public/safu/time.h b/src/safu/public/safu/time.h index 5614b53..e64b2e6 100644 --- a/src/safu/public/safu/time.h +++ b/src/safu/public/safu/time.h @@ -17,13 +17,19 @@ char *safuGetCurrentDateString(char *dateFormat); *------------------------------------------------------------------ * Description: Compares two timespec timestamps * + * Input: + * timeA (struct timespec): + * the first timestamp to compare 1 if this is bigger/newer + * timeB (struct timespec): + * the second timestamp to compare -1 if this is bigger/newer + * * Return: * the result of the comparison - * - 1 if a is more recent than b - * - 0 if a and be are the same - * - -1 if a is older than b + * - 1 if timeA is more recent than timeB + * - 0 if timeA and timeB are the same + * - -1 if timeA is older than timeB ******************************************************************/ -int safuTimeSpecCompare(struct timespec a, struct timespec b); +int safuTimeSpecCompare(struct timespec timeA, struct timespec timeB); __END_DECLS From fe680a04b66105c6f66b8197be7a93ee17b558d4 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 11:33:45 +0200 Subject: [PATCH 07/13] Refactor and extend the safuTimeSpecCompare unit tests Signed-off-by: Friedrich Schwedler --- .../time/safuTimeSpecCompare/CMakeLists.txt | 4 +- .../safuTimeSpecCompare/case_exhaustive.c | 51 ++++++++++ .../case_real_timestamps.c | 41 ++++++++ .../time/safuTimeSpecCompare/case_selected.c | 56 +++++++++++ .../time/safuTimeSpecCompare/case_success.c | 95 ------------------- .../safuTimeSpecCompare_utest.c | 25 ++++- .../safuTimeSpecCompare_utest.h | 4 +- 7 files changed, 177 insertions(+), 99 deletions(-) create mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_exhaustive.c create mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c create mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_selected.c delete mode 100644 test/utest/safu/time/safuTimeSpecCompare/case_success.c diff --git a/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt index e858542..ef98b0a 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt +++ b/test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt @@ -5,7 +5,9 @@ create_unit_test( NAME test_common_safuTimeSpecCompare_utest SOURCES - case_success.c + case_selected.c + case_exhaustive.c + case_real_timestamps.c safuTimeSpecCompare_utest.c LIBRARIES safu_weak diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_exhaustive.c b/test/utest/safu/time/safuTimeSpecCompare/case_exhaustive.c new file mode 100644 index 0000000..6dbae66 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/case_exhaustive.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "safuTimeSpecCompare_utest.h" + +int safuTestSafuTimeSpecCompareExhaustiveSetup(UNUSED void **state) { + return 0; +} + +int safuTestSafuTimeSpecCompareExhaustiveTeardown(UNUSED void **state) { + return 0; +} + +static void _checkComparision(struct timespec a, struct timespec b) { + int result = safuTimeSpecCompare(a, b); + int value; + if (a.tv_sec > b.tv_sec) { + value = 1; + } else if (a.tv_sec < b.tv_sec) { + value = -1; + } else if (a.tv_nsec > b.tv_nsec) { + value = 1; + } else if (a.tv_nsec < b.tv_nsec) { + value = -1; + } else { + value = 0; + } + assert_int_equal(result, value); +} +void safuTestSafuTimeSpecCompareExhaustive(UNUSED void **state) { + TEST("safuTimeSpecCompareExhaustive"); + SHOULD("%s", "successfully compare two timespec structs with a lot of combinations of values"); + + time_t secsList[] = {0, 1, 12, 457789, 999999999, INT_MAX}; + long nsecsList[] = {0, 1, 42, 999999999}; + + for (size_t as = 0; as < ARRAY_SIZE(secsList); as++) { + for (size_t an = 0; an < ARRAY_SIZE(nsecsList); an++) { + struct timespec a = {.tv_sec = secsList[as], .tv_nsec = nsecsList[an]}; + for (size_t bs = 0; bs < ARRAY_SIZE(secsList); bs++) { + for (size_t bn = 0; bn < ARRAY_SIZE(nsecsList); bn++) { + struct timespec b = {.tv_sec = secsList[bs], .tv_nsec = nsecsList[bn]}; + _checkComparision(a, b); + } + } + } + } +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c b/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c new file mode 100644 index 0000000..e708dca --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "safuTimeSpecCompare_utest.h" + +int safuTestSafuTimeSpecCompareRealTimestampsSetup(UNUSED void **state) { + return 0; +} + +int safuTestSafuTimeSpecCompareRealTimestampsTeardown(UNUSED void **state) { + return 0; +} + +void safuTestSafuTimeSpecCompareRealTimestamps(void **state) { + TEST("safuTimeSpecCompareRealTimestamp"); + SHOULD( + "%s", + "successfully compare two timespec structs gotten from the system clock to make sure it's a real time stamp"); + + int result; + struct timespec *olderTimestamp = *state; + struct timespec newerTimestamp = {0}; + if (!clock_gettime(CLOCK_REALTIME, &newerTimestamp)) { + print_error("failed to get newer time stamp for tests"); + } + + result = safuTimeSpecCompare(newerTimestamp, *olderTimestamp); + assert_int_equal(result, 1); + + result = safuTimeSpecCompare(newerTimestamp, newerTimestamp); + assert_int_equal(result, 0); + + result = safuTimeSpecCompare(*olderTimestamp, newerTimestamp); + assert_int_equal(result, -1); + + result = safuTimeSpecCompare(*olderTimestamp, *olderTimestamp); + assert_int_equal(result, 0); +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_selected.c b/test/utest/safu/time/safuTimeSpecCompare/case_selected.c new file mode 100644 index 0000000..ce625d7 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecCompare/case_selected.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "safuTimeSpecCompare_utest.h" + +int safuTestSafuTimeSpecCompareSelectedSetup(UNUSED void **state) { + return 0; +} + +int safuTestSafuTimeSpecCompareSelectedTeardown(UNUSED void **state) { + return 0; +} + +void safuTestSafuTimeSpecCompareSelected(UNUSED void **state) { + TEST("safuTimeSpecCompareSelected"); + SHOULD("%s", "successfully compare two timespec structs with a selected number of specific comparisons"); + + struct timespec timeA; + struct timespec timeB; + int result; + + // x > y + timeA.tv_sec = 6; + timeB.tv_sec = 4; + result = safuTimeSpecCompare(timeA, timeB); + assert_int_equal(result, 1); + + // x < y + timeA.tv_sec = 6; + timeB.tv_sec = 8; + result = safuTimeSpecCompare(timeA, timeB); + assert_int_equal(result, -1); + + // x > y + timeB.tv_sec = timeA.tv_sec; + timeA.tv_nsec = 23; + timeB.tv_nsec = 12; + result = safuTimeSpecCompare(timeA, timeB); + assert_int_equal(result, 1); + + // x < y + timeB.tv_sec = timeA.tv_sec; + timeA.tv_nsec = 23; + timeB.tv_nsec = 42; + result = safuTimeSpecCompare(timeA, timeB); + assert_int_equal(result, -1); + + // x == y + timeB.tv_sec = timeA.tv_sec; + timeA.tv_nsec = timeB.tv_nsec; + result = safuTimeSpecCompare(timeA, timeB); + assert_int_equal(result, 0); +} diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_success.c b/test/utest/safu/time/safuTimeSpecCompare/case_success.c deleted file mode 100644 index a4898bc..0000000 --- a/test/utest/safu/time/safuTimeSpecCompare/case_success.c +++ /dev/null @@ -1,95 +0,0 @@ -// SPDX-License-Identifier: MIT - -#include -#include -#include - -#include "safuTimeSpecCompare_utest.h" - -int safuTestSafuTimeSpecCompareSuccessSetup(UNUSED void **state) { - return 0; -} - -int safuTestSafuTimeSpecCompareSuccessTeardown(UNUSED void **state) { - return 0; -} - -static void _manualChecks() { - struct timespec x; - struct timespec y; - int result; - - // x > y - x.tv_sec = 6; - y.tv_sec = 4; - result = safuTimeSpecCompare(x, y); - assert_int_equal(result, 1); - - // x < y - x.tv_sec = 6; - y.tv_sec = 8; - result = safuTimeSpecCompare(x, y); - assert_int_equal(result, -1); - - // x > y - y.tv_sec = x.tv_sec; - x.tv_nsec = 23; - y.tv_nsec = 12; - result = safuTimeSpecCompare(x, y); - assert_int_equal(result, 1); - - // x < y - y.tv_sec = x.tv_sec; - x.tv_nsec = 23; - y.tv_nsec = 42; - result = safuTimeSpecCompare(x, y); - assert_int_equal(result, -1); - - // x == y - y.tv_sec = x.tv_sec; - x.tv_nsec = y.tv_nsec; - result = safuTimeSpecCompare(x, y); - assert_int_equal(result, 0); -} - -static void _checkComparision(struct timespec a, struct timespec b) { - int result = safuTimeSpecCompare(a, b); - int value; - if (a.tv_sec > b.tv_sec) { - value = 1; - } else if (a.tv_sec < b.tv_sec) { - value = -1; - } else if (a.tv_nsec > b.tv_nsec) { - value = 1; - } else if (a.tv_nsec < b.tv_nsec) { - value = -1; - } else { - value = 0; - } - assert_int_equal(result, value); -} - -static void _genericChecks() { - time_t secsList[] = {0, 1, 12, INT_MAX}; - long nsecsList[] = {0, 1, 42, INT_MAX}; - - for (size_t as = 0; as < ARRAY_SIZE(secsList); as++) { - for (size_t an = 0; an < ARRAY_SIZE(nsecsList); an++) { - struct timespec a = {.tv_sec = secsList[as], .tv_nsec = nsecsList[an]}; - for (size_t bs = 0; bs < ARRAY_SIZE(secsList); bs++) { - for (size_t bn = 0; bn < ARRAY_SIZE(nsecsList); bn++) { - struct timespec b = {.tv_sec = secsList[bs], .tv_nsec = nsecsList[bn]}; - _checkComparision(a, b); - } - } - } - } -} - -void safuTestSafuTimeSpecCompareSuccess(UNUSED void **state) { - TEST("safuTimeSpecCompareSuccess"); - SHOULD("%s", "successfully comare two timespec structs"); - - _manualChecks(); - _genericChecks(); -} diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c index a5354be..321a8de 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c @@ -1,10 +1,31 @@ // SPDX-License-Identifier: MIT #include "safuTimeSpecCompare_utest.h" +#include +#include + +TEST_SUITE_FUNC_PROTOTYPES(_testSuiteSafuTimeSpecCompare) + int main() { const struct CMUnitTest tests[] = { - TEST_CASE(safuTestSafuTimeSpecCompareSuccess), + TEST_CASE(safuTestSafuTimeSpecCompareSelected), + TEST_CASE(safuTestSafuTimeSpecCompareExhaustive), + TEST_CASE(safuTestSafuTimeSpecCompareRealTimestamps), }; - return cmocka_run_group_tests(tests, NULL, NULL); + return RUN_TEST_SUITE(tests, _testSuiteSafuTimeSpecCompare); +} + +static int _testSuiteSafuTimeSpecCompareSetup(void **state) { + struct timespec *olderTimestamp = calloc(1, sizeof(struct timespec)); + if (olderTimestamp != NULL && !clock_gettime(CLOCK_REALTIME, olderTimestamp)) { + print_error("failed to get older time stamp for tests"); + } + *state = olderTimestamp; + return 0; +} +static int _testSuiteSafuTimeSpecCompareTeardown(void **state) { + free(*state); + *state = NULL; + return 0; } diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h index e4fd548..2903164 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.h @@ -4,6 +4,8 @@ #include -TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareSuccess) +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareSelected) +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareExhaustive) +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareRealTimestamps) #endif /* __SAFU_TIME_SPEC_COMPARE_UTEST_H__*/ From 78293eb31fd784d0191ee4d25db0c8a8ee8da40f Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 13:37:33 +0200 Subject: [PATCH 08/13] Update to 0.58.3 --- cmake/project.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/project.cmake b/cmake/project.cmake index d8ece69..0e7bcfa 100644 --- a/cmake/project.cmake +++ b/cmake/project.cmake @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -set(SAFU_VERSION 0.58.2) +set(SAFU_VERSION 0.58.3) # Attention: Aside from the version, as many things as possible in this file # should be put into functions, as this solves potential issues with commands From 73057bcdb5d455200fb47f4d297a97498b9b5dcc Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 16:22:56 +0200 Subject: [PATCH 09/13] Fix bug in safuTimeSpecCompare test case Signed-off-by: Friedrich Schwedler --- .../utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c | 3 ++- .../safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c b/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c index e708dca..cf0d83a 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c +++ b/test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c @@ -23,8 +23,9 @@ void safuTestSafuTimeSpecCompareRealTimestamps(void **state) { int result; struct timespec *olderTimestamp = *state; struct timespec newerTimestamp = {0}; - if (!clock_gettime(CLOCK_REALTIME, &newerTimestamp)) { + if (clock_gettime(CLOCK_REALTIME, &newerTimestamp) != 0) { print_error("failed to get newer time stamp for tests"); + fail(); } result = safuTimeSpecCompare(newerTimestamp, *olderTimestamp); diff --git a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c index 321a8de..3eab802 100644 --- a/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c +++ b/test/utest/safu/time/safuTimeSpecCompare/safuTimeSpecCompare_utest.c @@ -18,8 +18,9 @@ int main() { static int _testSuiteSafuTimeSpecCompareSetup(void **state) { struct timespec *olderTimestamp = calloc(1, sizeof(struct timespec)); - if (olderTimestamp != NULL && !clock_gettime(CLOCK_REALTIME, olderTimestamp)) { + if (olderTimestamp != NULL && clock_gettime(CLOCK_REALTIME, olderTimestamp) != 0) { print_error("failed to get older time stamp for tests"); + fail(); } *state = olderTimestamp; return 0; From 85118cd217bd065665ede214d541a88feeb8f798 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 14:20:56 +0200 Subject: [PATCH 10/13] Implement safuTimeSpecInTimeRange Signed-off-by: Friedrich Schwedler --- src/safu/private/time.c | 5 +++++ src/safu/public/safu/time.h | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/safu/private/time.c b/src/safu/private/time.c index ae57b61..43c1dff 100644 --- a/src/safu/private/time.c +++ b/src/safu/private/time.c @@ -52,3 +52,8 @@ int safuTimeSpecCompare(struct timespec timeA, struct timespec timeB) { return (timeA.tv_sec > timeB.tv_sec) - (timeA.tv_sec < timeB.tv_sec) + (timeA.tv_sec == timeB.tv_sec) * ((timeA.tv_nsec > timeB.tv_nsec) - (timeA.tv_nsec < timeB.tv_nsec)); } + +bool safuTimeSpecInTimeRange(struct timespec newest, struct timespec check, struct timespec oldest) { + return (((newest.tv_sec == 0 && newest.tv_nsec == 0) || safuTimeSpecCompare(newest, check) >= 0) && + ((oldest.tv_sec == 0 && oldest.tv_nsec == 0) || safuTimeSpecCompare(check, oldest) > 0)); +} diff --git a/src/safu/public/safu/time.h b/src/safu/public/safu/time.h index e64b2e6..1055648 100644 --- a/src/safu/public/safu/time.h +++ b/src/safu/public/safu/time.h @@ -2,6 +2,7 @@ #ifndef SAFU_TIME_H #define SAFU_TIME_H +#include #include #include "common.h" @@ -31,6 +32,29 @@ char *safuGetCurrentDateString(char *dateFormat); ******************************************************************/ int safuTimeSpecCompare(struct timespec timeA, struct timespec timeB); +/******************************************************************* + * Function: safuTimeSpecInTimeRange + *------------------------------------------------------------------ + * Description: Checks if newestTime >= timeToCheck > olderTime + * + * Input: + * newestTime (struct timespec): + * the newest point in the time range to check for. + * ignored if it's {0,0} + * timeToCheck (struct timespec): + * the time stamp to check if it's in the time range + * olderTime (struct timespec): + * the timestamp delimiting the time range, older than everything inside the range + * ignored if it's {0,0} + * + * Return: + * true if newestTime >= timeToCheck > olderTime + * - if newestTime is {0,0}: true if timeToCheck > olderTime + * - if olderTime is {0,0}: true if newestTime >= timeToCheck + * - always true if newestTime and olderTime both are {0,0} + ******************************************************************/ +bool safuTimeSpecInTimeRange(struct timespec newestTime, struct timespec timeToCheck, struct timespec olderTime); + __END_DECLS #endif From 879db5b37ec25c88c0e64c5732c5d13f17118b1b Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 4 Sep 2024 16:23:38 +0200 Subject: [PATCH 11/13] Add unit test for safuTimeSpecInTimeRange Signed-off-by: Friedrich Schwedler --- test/utest/safu/time/CMakeLists.txt | 1 + .../safuTimeSpecInTimeRange/CMakeLists.txt | 12 ++++ .../case_real_timestamps.c | 55 +++++++++++++++++++ .../safuTimeSpecInTimeRange_utest.c | 31 +++++++++++ .../safuTimeSpecInTimeRange_utest.h | 14 +++++ 5 files changed, 113 insertions(+) create mode 100644 test/utest/safu/time/safuTimeSpecInTimeRange/CMakeLists.txt create mode 100644 test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c create mode 100644 test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.c create mode 100644 test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.h diff --git a/test/utest/safu/time/CMakeLists.txt b/test/utest/safu/time/CMakeLists.txt index abb195d..dd83ed6 100644 --- a/test/utest/safu/time/CMakeLists.txt +++ b/test/utest/safu/time/CMakeLists.txt @@ -2,3 +2,4 @@ add_subdirectory(safuTimeGetLocalTime) add_subdirectory(safuTimeSpecCompare) +add_subdirectory(safuTimeSpecInTimeRange) diff --git a/test/utest/safu/time/safuTimeSpecInTimeRange/CMakeLists.txt b/test/utest/safu/time/safuTimeSpecInTimeRange/CMakeLists.txt new file mode 100644 index 0000000..de7b929 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/CMakeLists.txt @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: MIT +find_package(cmocka_extensions 0.53.1 REQUIRED) + +create_unit_test( + NAME + test_common_safuTimeSpecInTimeRange_utest + SOURCES + case_real_timestamps.c + safuTimeSpecInTimeRange_utest.c + LIBRARIES + safu_weak +) diff --git a/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c b/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c new file mode 100644 index 0000000..11330b6 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "safuTimeSpecInTimeRange_utest.h" + +int safuTestSafuTimeSpecInTimeRangeRealTimestampsSetup(void **state) { + struct timespec *times = *state; + int ret = clock_gettime(CLOCK_REALTIME, ×[MIDDLE]); + assert_int_equal(ret, 0); + return 0; +} + +int safuTestSafuTimeSpecInTimeRangeRealTimestampsTeardown(UNUSED void **state) { + return 0; +} + +void safuTestSafuTimeSpecInTimeRangeRealTimestamps(void **state) { + TEST("safuTimeSpecInTimeRangeRealTimestamp"); + SHOULD("%s", "check if a timespec struct is inside a time range, using timestamps gotten from system clock"); + + bool result; + struct timespec *times = *state; + int ret = clock_gettime(CLOCK_REALTIME, ×[NEWEST]); + assert_int_equal(ret, 0); + + result = safuTimeSpecInTimeRange(times[NEWEST], times[MIDDLE], times[OLDEST]); + assert_int_equal(result, true); + + result = safuTimeSpecInTimeRange(times[NOTSET], times[MIDDLE], times[OLDEST]); + assert_int_equal(result, true); + + result = safuTimeSpecInTimeRange(times[NOTSET], times[MIDDLE], times[MIDDLE]); + assert_int_equal(result, false); + + result = safuTimeSpecInTimeRange(times[NEWEST], times[MIDDLE], times[NOTSET]); + assert_int_equal(result, true); + + result = safuTimeSpecInTimeRange(times[NEWEST], times[OLDEST], times[NOTSET]); + assert_int_equal(result, true); + + result = safuTimeSpecInTimeRange(times[OLDEST], times[NEWEST], times[NOTSET]); + assert_int_equal(result, false); + + result = safuTimeSpecInTimeRange(times[NEWEST], times[NEWEST], times[OLDEST]); + assert_int_equal(result, true); + + result = safuTimeSpecInTimeRange(times[NEWEST], times[NEWEST], times[NEWEST]); + assert_int_equal(result, false); + + result = safuTimeSpecInTimeRange(times[MIDDLE], times[OLDEST], times[OLDEST]); + assert_int_equal(result, false); +} diff --git a/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.c b/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.c new file mode 100644 index 0000000..c15fc10 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: MIT +#include "safuTimeSpecInTimeRange_utest.h" + +#include + +TEST_SUITE_FUNC_PROTOTYPES(_testSuiteSafuTimeSpecInTimeRange) + +int main() { + const struct CMUnitTest tests[] = { + TEST_CASE(safuTestSafuTimeSpecInTimeRangeRealTimestamps), + }; + return RUN_TEST_SUITE(tests, _testSuiteSafuTimeSpecInTimeRange); +} + +static int _testSuiteSafuTimeSpecInTimeRangeSetup(void **state) { + struct timespec *times = calloc(4, sizeof(struct timespec)); + assert_non_null(times); + int ret = clock_gettime(CLOCK_REALTIME, ×[OLDEST]); + assert_int_equal(ret, 0); + + times[NOTSET].tv_sec = 0; + times[NOTSET].tv_nsec = 0; + + *state = times; + return 0; +} +static int _testSuiteSafuTimeSpecInTimeRangeTeardown(void **state) { + free(*state); + *state = NULL; + return 0; +} diff --git a/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.h b/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.h new file mode 100644 index 0000000..c8d6c52 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/safuTimeSpecInTimeRange_utest.h @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +#ifndef __SAFU_TIME_SPEC_IN_TIME_RANGE_UTEST_H__ +#define __SAFU_TIME_SPEC_IN_TIME_RANGE_UTEST_H__ + +#include + +#define NOTSET 0 +#define OLDEST 1 +#define MIDDLE 2 +#define NEWEST 3 + +TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecInTimeRangeRealTimestamps) + +#endif /* __SAFU_TIME_SPEC_IN_TIME_RANGE_UTEST_H__*/ From 84582ccc14e2dcf3b9347494524bd5e6d3d2a12d Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Wed, 11 Sep 2024 13:30:50 +0200 Subject: [PATCH 12/13] Add PARAM to all calls to test calls Signed-off-by: Friedrich Schwedler --- .../time/safuTimeSpecInTimeRange/case_real_timestamps.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c b/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c index 11330b6..14bd0c0 100644 --- a/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c @@ -26,30 +26,39 @@ void safuTestSafuTimeSpecInTimeRangeRealTimestamps(void **state) { int ret = clock_gettime(CLOCK_REALTIME, ×[NEWEST]); assert_int_equal(ret, 0); + PARAM("newest >= middle > oldest"); result = safuTimeSpecInTimeRange(times[NEWEST], times[MIDDLE], times[OLDEST]); assert_int_equal(result, true); + PARAM("{0,0} middle > oldest"); result = safuTimeSpecInTimeRange(times[NOTSET], times[MIDDLE], times[OLDEST]); assert_int_equal(result, true); + PARAM("{0,0} middle !> middle"); result = safuTimeSpecInTimeRange(times[NOTSET], times[MIDDLE], times[MIDDLE]); assert_int_equal(result, false); + PARAM("{0,0} middle {0,0}"); result = safuTimeSpecInTimeRange(times[NEWEST], times[MIDDLE], times[NOTSET]); assert_int_equal(result, true); + PARAM("newest >= oldest {0,0}"); result = safuTimeSpecInTimeRange(times[NEWEST], times[OLDEST], times[NOTSET]); assert_int_equal(result, true); + PARAM("oldest !>= newest {0,0}"); result = safuTimeSpecInTimeRange(times[OLDEST], times[NEWEST], times[NOTSET]); assert_int_equal(result, false); + PARAM("newest >= newest > oldest"); result = safuTimeSpecInTimeRange(times[NEWEST], times[NEWEST], times[OLDEST]); assert_int_equal(result, true); + PARAM("newest >= newest !> newest"); result = safuTimeSpecInTimeRange(times[NEWEST], times[NEWEST], times[NEWEST]); assert_int_equal(result, false); + PARAM("middle >= oldest !> oldest"); result = safuTimeSpecInTimeRange(times[MIDDLE], times[OLDEST], times[OLDEST]); assert_int_equal(result, false); } From d612974b184c7aa52941ea8b6c1553a45f3647f7 Mon Sep 17 00:00:00 2001 From: Friedrich Schwedler Date: Mon, 16 Sep 2024 09:32:59 +0200 Subject: [PATCH 13/13] Update to 0.58.4 --- cmake/project.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/project.cmake b/cmake/project.cmake index 0e7bcfa..9be7687 100644 --- a/cmake/project.cmake +++ b/cmake/project.cmake @@ -1,5 +1,5 @@ # SPDX-License-Identifier: MIT -set(SAFU_VERSION 0.58.3) +set(SAFU_VERSION 0.58.4) # Attention: Aside from the version, as many things as possible in this file # should be put into functions, as this solves potential issues with commands