Skip to content

Commit

Permalink
Merge branch 'task/#24323-implement-safuTimeSpecInTimeRange' into 'in…
Browse files Browse the repository at this point in the history
…tegration'

Task #24323 - Implement safuTimeSpecInTimeRange

See merge request elektrobit/base-os/safu!37
  • Loading branch information
ThomasBrinker committed Sep 16, 2024
2 parents 135a58b + d612974 commit 0d4aeac
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmake/project.cmake
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/safu/private/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
24 changes: 24 additions & 0 deletions src/safu/public/safu/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#ifndef SAFU_TIME_H
#define SAFU_TIME_H

#include <stdbool.h>
#include <time.h>

#include "common.h"
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions test/utest/safu/time/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

add_subdirectory(safuTimeGetLocalTime)
add_subdirectory(safuTimeSpecCompare)
add_subdirectory(safuTimeSpecInTimeRange)
12 changes: 12 additions & 0 deletions test/utest/safu/time/safuTimeSpecInTimeRange/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

#include <limits.h>
#include <safu/result.h>
#include <safu/time.h>

#include "safuTimeSpecInTimeRange_utest.h"

int safuTestSafuTimeSpecInTimeRangeRealTimestampsSetup(void **state) {
struct timespec *times = *state;
int ret = clock_gettime(CLOCK_REALTIME, &times[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, &times[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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
#include "safuTimeSpecInTimeRange_utest.h"

#include <time.h>

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, &times[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;
}
Original file line number Diff line number Diff line change
@@ -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 <cmocka_extensions/cmocka_extensions.h>

#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__*/

0 comments on commit 0d4aeac

Please sign in to comment.