Skip to content

Commit

Permalink
Merge pull request #10 from Elektrobit/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
gehwolf authored Sep 27, 2024
2 parents 77bdd2d + 0d4aeac commit 0eec651
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 6 deletions.
55 changes: 50 additions & 5 deletions ci/run_utest.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
#!/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 <test name pattern>] [-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")")"
BASE_DIR="$(realpath "$CMD_PATH/..")"

TESTS_REGEX=""
PARAM=""
while [ $# -gt 0 ]; do
case ${1} in
--tests-regex|-R)
TESTS_REGEX="--tests-regex ${2}"
shift
;;
-h|--help)
print_info
exit 0 ;;
-*)
echo "error: unknown option: $1"
print_info
exit 1 ;;
*)
PARAM="$PARAM ${1}" ;;
esac
shift
done
set -- $PARAM

BUILD_TYPE="${1:-Debug}"

. "$BASE_DIR/ci/common_names.sh"
Expand All @@ -12,17 +53,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
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.2)
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
11 changes: 11 additions & 0 deletions src/safu/private/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>

#include "safu/log.h"
#include "safu/result.h"

safuResultE_t safuTimeGetLocalTime(struct tm *localTime) {
time_t currentTimestamp = 0;
Expand Down Expand Up @@ -46,3 +47,13 @@ char *safuGetCurrentDateString(char *dateFormat) {
}
return date;
}

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));
}
43 changes: 43 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 All @@ -12,6 +13,48 @@ safuResultE_t safuTimeGetLocalTime(struct tm *localTime);

char *safuGetCurrentDateString(char *dateFormat);

/*******************************************************************
* Function: safuTimeSpecCompare
*------------------------------------------------------------------
* 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 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 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
2 changes: 2 additions & 0 deletions test/utest/safu/time/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# SPDX-License-Identifier: MIT

add_subdirectory(safuTimeGetLocalTime)
add_subdirectory(safuTimeSpecCompare)
add_subdirectory(safuTimeSpecInTimeRange)
14 changes: 14 additions & 0 deletions test/utest/safu/time/safuTimeSpecCompare/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: MIT
find_package(cmocka_extensions 0.53.1 REQUIRED)

create_unit_test(
NAME
test_common_safuTimeSpecCompare_utest
SOURCES
case_selected.c
case_exhaustive.c
case_real_timestamps.c
safuTimeSpecCompare_utest.c
LIBRARIES
safu_weak
)
51 changes: 51 additions & 0 deletions test/utest/safu/time/safuTimeSpecCompare/case_exhaustive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT

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

#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);
}
}
}
}
}
42 changes: 42 additions & 0 deletions test/utest/safu/time/safuTimeSpecCompare/case_real_timestamps.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MIT

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

#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) != 0) {
print_error("failed to get newer time stamp for tests");
fail();
}

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);
}
56 changes: 56 additions & 0 deletions test/utest/safu/time/safuTimeSpecCompare/case_selected.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-License-Identifier: MIT

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

#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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
#include "safuTimeSpecCompare_utest.h"

#include <bits/time.h>
#include <time.h>

TEST_SUITE_FUNC_PROTOTYPES(_testSuiteSafuTimeSpecCompare)

int main() {
const struct CMUnitTest tests[] = {
TEST_CASE(safuTestSafuTimeSpecCompareSelected),
TEST_CASE(safuTestSafuTimeSpecCompareExhaustive),
TEST_CASE(safuTestSafuTimeSpecCompareRealTimestamps),
};

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) != 0) {
print_error("failed to get older time stamp for tests");
fail();
}
*state = olderTimestamp;
return 0;
}
static int _testSuiteSafuTimeSpecCompareTeardown(void **state) {
free(*state);
*state = NULL;
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
#ifndef __SAFU_TIME_SPEC_COMPARE_UTEST_H__
#define __SAFU_TIME_SPEC_COMPARE_UTEST_H__

#include <cmocka_extensions/cmocka_extensions.h>

TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareSelected)
TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareExhaustive)
TEST_CASE_FUNC_PROTOTYPES(safuTestSafuTimeSpecCompareRealTimestamps)

#endif /* __SAFU_TIME_SPEC_COMPARE_UTEST_H__*/
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
)
Loading

0 comments on commit 0eec651

Please sign in to comment.