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 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 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..14bd0c0 --- /dev/null +++ b/test/utest/safu/time/safuTimeSpecInTimeRange/case_real_timestamps.c @@ -0,0 +1,64 @@ +// 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); + + 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); +} 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__*/