From: Bastian Dehn Date: Tue, 11 Nov 2025 16:54:52 +0000 (+0100) Subject: add valid date tests X-Git-Tag: 1.3.8^2~6^2~4 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=83f8ad963f0784f289ad3f8fb83da2a984260fc5;p=feierabend.git add valid date tests --- diff --git a/src/time_format.c b/src/time_format.c index 4014e01..e79911d 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "time_format.h" #define ONE_HOUR 3600 @@ -9,6 +10,8 @@ #define SHORT_WEEKDAY_STR_LEN 3 #define MAX_TIME_STR_LENGTH 7 +#define DATE_REGEX "[0-9]\\{4\\}-\\([0][1-9]\\|1[0-2]\\)-\\([0-2][1-9]\\|[1-3]0\\|3[01]\\)" + void get_time_str(time_t timediff, char* timestr) { memset(timestr, 0, MAX_TIME_STR_LENGTH); @@ -182,4 +185,31 @@ int get_day_from_str(const char* date) free(str_first_pos); str_first_pos = NULL; return value; +} + +bool validate_datestring(const char* date) +{ + regex_t* date_regex = malloc(sizeof(regex_t)); + if (date_regex == NULL) + return false; + + int comp_success = regcomp(date_regex, DATE_REGEX, REG_NOSUB); + if (comp_success != 0) { + free(date_regex); + return false; + } + + int match = regexec(date_regex, date, 0, NULL, 0); + + regfree(date_regex); + free(date_regex); + date_regex = NULL; + + if (match == REG_NOMATCH) + return false; + + if (match == 0) + return true; + + return false; } \ No newline at end of file diff --git a/src/time_format.h b/src/time_format.h index 669a94c..712cfaf 100644 --- a/src/time_format.h +++ b/src/time_format.h @@ -1,5 +1,6 @@ #ifndef TIME_FORMAT_H #define TIME_FORMAT_H +#include #include void get_time_str(time_t timediff, char* timestr); @@ -10,5 +11,6 @@ void get_short_weekday(int wday, char* weekday); int get_year_from_str(const char* date); int get_month_from_str(const char* date); int get_day_from_str(const char* date); +bool validate_datestring(const char* date); #endif \ No newline at end of file diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 1b1d731..6014dfb 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -328,6 +328,23 @@ void get_dat_from_date() assert_int_equal(result, 24); } +void validate_valid_date_string(const char* date) +{ + bool valid = validate_datestring(date); + + assert_true(valid); +} + +void validate_valid_date_string_tests() +{ + validate_valid_date_string("2025-01-01"); + validate_valid_date_string("2025-01-31"); + validate_valid_date_string("2025-10-01"); + validate_valid_date_string("2025-10-31"); + validate_valid_date_string("2025-12-01"); + validate_valid_date_string("2025-12-31"); +} + int main() { const struct CMUnitTest tests[] = { @@ -356,7 +373,8 @@ int main() cmocka_unit_test(get_seconds_from_str_minus), cmocka_unit_test(get_year_from_date), cmocka_unit_test(get_month_from_date), - cmocka_unit_test(get_dat_from_date) + cmocka_unit_test(get_dat_from_date), + cmocka_unit_test(validate_valid_date_string_tests) }; return cmocka_run_group_tests(tests, NULL, NULL);