From: Bastian Dehn Date: Wed, 12 Nov 2025 14:43:04 +0000 (+0100) Subject: add validate hour function X-Git-Tag: 1.3.8^2~4 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=f94d79eec400c9bb20c752f92bd511dbaa7c6795;p=feierabend.git add validate hour function --- diff --git a/src/time_format.c b/src/time_format.c index cc862a2..43c0372 100644 --- a/src/time_format.c +++ b/src/time_format.c @@ -11,6 +11,7 @@ #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])$" +#define HOUR_REGEX "^([0-9]|1[0-9]|2[0-3])$" void get_time_str(time_t timediff, char* timestr) { @@ -187,23 +188,23 @@ int get_day_from_str(const char* date) return value; } -bool validate_datestring(const char* date) +bool validate_string(const char* str, const char* regex_pattern) { - regex_t* date_regex = malloc(sizeof(regex_t)); - if (date_regex == NULL) + regex_t* regex = malloc(sizeof(regex_t)); + if (regex == NULL) return false; - int comp_success = regcomp(date_regex, DATE_REGEX, REG_NOSUB | REG_EXTENDED); + int comp_success = regcomp(regex, regex_pattern, REG_NOSUB | REG_EXTENDED); if (comp_success != 0) { - free(date_regex); + free(regex); return false; } - int match = regexec(date_regex, date, 0, NULL, 0); + int match = regexec(regex, str, 0, NULL, 0); - regfree(date_regex); - free(date_regex); - date_regex = NULL; + regfree(regex); + free(regex); + regex = NULL; if (match == REG_NOMATCH) return false; @@ -212,4 +213,14 @@ bool validate_datestring(const char* date) return true; return false; +} + +bool validate_datestring(const char* date) +{ + return validate_string(date, DATE_REGEX); +} + +bool validate_hourstring(const char* hour) +{ + return validate_string(hour, HOUR_REGEX); } \ No newline at end of file diff --git a/src/time_format.h b/src/time_format.h index 712cfaf..ad33272 100644 --- a/src/time_format.h +++ b/src/time_format.h @@ -12,5 +12,6 @@ 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); +bool validate_hourstring(const char* hour); #endif \ No newline at end of file diff --git a/tests/time_format_tests.c b/tests/time_format_tests.c index 0937a80..ae4b111 100644 --- a/tests/time_format_tests.c +++ b/tests/time_format_tests.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "../src/time_format.h" void get_zero_hour_and_fifteen_minutes() @@ -365,6 +366,45 @@ void validate_invalid_date_string_tests() validate_invalid_date_string("20255-01-11"); } +void validate_vaild_hour_string(const char* hour) +{ + bool valid = validate_hourstring(hour); + + assert_true(valid); +} + +void validate_valid_hour_string_test() +{ + char* hour = malloc(sizeof(char) * 3); + if (hour == NULL) + return; + + for (int i = 0; i < 24; i++) { + memset(hour, 0, 3); + sprintf(hour, "%d", i); + validate_vaild_hour_string(hour); + } + + free(hour); + hour = NULL; +} + +void validate_invaild_hour_string(const char* hour) +{ + bool valid = validate_hourstring(hour); + + assert_false(valid); +} + +void validate_invalid_hour_string_test() +{ + validate_invaild_hour_string("text"); + validate_invaild_hour_string("-1"); + validate_invaild_hour_string("01"); + validate_invaild_hour_string("25"); + validate_invaild_hour_string("100"); +} + int main() { const struct CMUnitTest tests[] = { @@ -395,7 +435,9 @@ int main() cmocka_unit_test(get_month_from_date), cmocka_unit_test(get_dat_from_date), cmocka_unit_test(validate_valid_date_string_tests), - cmocka_unit_test(validate_invalid_date_string_tests) + cmocka_unit_test(validate_invalid_date_string_tests), + cmocka_unit_test(validate_valid_hour_string_test), + cmocka_unit_test(validate_invalid_hour_string_test) }; return cmocka_run_group_tests(tests, NULL, NULL);