]> gitweb.hhaalo.de Git - feierabend.git/commitdiff
add test formating time
authorBastian Dehn <hhaalo@arcor.de>
Sun, 9 Jun 2024 11:37:09 +0000 (13:37 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 9 Jun 2024 11:37:09 +0000 (13:37 +0200)
src/time_format.c [new file with mode: 0644]
src/time_format.h [new file with mode: 0644]
tests/CMakeLists.txt
tests/time_format_tests.c [new file with mode: 0644]

diff --git a/src/time_format.c b/src/time_format.c
new file mode 100644 (file)
index 0000000..96963b9
--- /dev/null
@@ -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 (file)
index 0000000..cae614e
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef TIME_FORMAT_H
+#define TIME_FORMAT_H
+#include <time.h>
+
+void get_time_str(time_t time, char* timestr);
+
+#endif
\ No newline at end of file
index 222d096905a3ca49388b7206ffe0bb5c0951503a..59637dfdcb7e9b49d652c02629166d898270417d 100644 (file)
@@ -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 (file)
index 0000000..4be84da
--- /dev/null
@@ -0,0 +1,30 @@
+#define UNIT_TESTING
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#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