From: Bastian Dehn Date: Sun, 9 Jun 2024 11:37:09 +0000 (+0200) Subject: add test formating time X-Git-Tag: 1.0.0^2~41 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=4d175d9a91c395e2f4591b5c77f62646cccd9ae2;p=feierabend.git add test formating time --- diff --git a/src/time_format.c b/src/time_format.c new file mode 100644 index 0000000..96963b9 --- /dev/null +++ b/src/time_format.c @@ -0,0 +1,10 @@ +#include "time_format.h" + +void get_time_str(time_t time, char* timestr) +{ + struct tm time_cal; + + gmtime_r(&time, &time_cal); + + sprintf(timestr, "%02d:%02d", time_cal.tm_hour, time_cal.tm_min); +} \ No newline at end of file diff --git a/src/time_format.h b/src/time_format.h new file mode 100644 index 0000000..cae614e --- /dev/null +++ b/src/time_format.h @@ -0,0 +1,7 @@ +#ifndef TIME_FORMAT_H +#define TIME_FORMAT_H +#include + +void get_time_str(time_t time, char* timestr); + +#endif \ No newline at end of file diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 222d096..59637df 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,11 +15,19 @@ ADD_EXECUTABLE(worktime_tests TARGET_LINK_LIBRARIES(worktime_tests ${CMOCKA}) +ADD_EXECUTABLE(time_format_tests + time_format_tests.c + ../src/time_format.c) +TARGET_LINK_LIBRARIES(time_format_tests + ${CMOCKA}) + INCLUDE(CTest) ADD_TEST(NAME break_tests COMMAND break_tests) ADD_TEST(NAME worktime_tests COMMAND worktime_tests) +ADD_TEST(NAME time_format_tests + COMMAND time_format_tests) ADD_CUSTOM_TARGET(run-tests ALL "ctest" "--verbose" diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c new file mode 100644 index 0000000..4be84da --- /dev/null +++ b/tests/time_format_tests.c @@ -0,0 +1,30 @@ +#define UNIT_TESTING +#include +#include +#include +#include +#include +#include "../src/time_format.h" + +void get_zero_hour_and_fifteen_minutes() +{ + time_t time = 900; + char* timestring = malloc(sizeof(char) * 7); + memset(timestring, 0, 7); + + get_time_str(time, timestring); + + assert_string_equal(timestring, "00:15"); + + free(timestring); + timestring = NULL; +} + +int main() +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(get_zero_hour_and_fifteen_minutes) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} \ No newline at end of file