#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)
{
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;
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
#include <cmocka.h>
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
#include "../src/time_format.h"
void get_zero_hour_and_fifteen_minutes()
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[] = {
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);