diff --git a/libc/Makefile b/libc/Makefile index 01dd59054..0d62dfea4 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -22,3 +22,4 @@ $(eval $(call add_test_libc,misc)) $(eval $(call add_test_libc,stdio)) $(eval $(call add_test_libc,stdlib)) $(eval $(call add_test_libc,string)) +$(eval $(call add_test_libc,time)) diff --git a/libc/test.yaml b/libc/test.yaml index 32cbe4bfa..ef51d4796 100644 --- a/libc/test.yaml +++ b/libc/test.yaml @@ -39,3 +39,8 @@ test: execute: test-libc-string targets: include: [host-generic-pc] + + - name: time + execute: test-libc-time + targets: + include: [host-generic-pc] diff --git a/time/test_gmtime.c b/libc/time/gmtime.c similarity index 66% rename from time/test_gmtime.c rename to libc/time/gmtime.c index 1ae6cdd36..f126b850c 100644 --- a/time/test_gmtime.c +++ b/libc/time/gmtime.c @@ -1,9 +1,9 @@ /* * Phoenix-RTOS * - * libphoenix + * test-libc-time * - * test/test_strftime.c + * Tests of gmtime function * * Copyright 2020 Phoenix Systems * Author: Marcin Brzykcy @@ -16,7 +16,8 @@ #include #include #include -#include "test_common.h" +#include "time_common.h" + #define NCOLS 9 #define T1_LEN 50 @@ -25,77 +26,43 @@ static const time_t input_vector[T1_LEN]; static const int output_vector[T1_LEN][NCOLS]; -/* help function for future test generation if needed - not used in test functions */ -void generate_input_host(void) +TEST_GROUP(time_gmtime); + + +TEST_SETUP(time_gmtime) { - int i; - struct tm t; - srand(time(NULL)); - for (i = 0; i < T1_LEN; i++) { - t = (struct tm) { .tm_sec = rand() % 59, .tm_min = rand() % 59, .tm_hour = rand() % 23, - .tm_mday = rand() % 30, .tm_mon = rand() % 11, .tm_year = 1980 + rand() % 60, - .tm_wday = 0, .tm_yday = 0, .tm_isdst = 0 }; - printf("%lld", mktime(&t)); - if (i != T1_LEN - 1) - printf(",\n"); - } - printf("\n"); + tzset(); } - -/* help function for future test generation if needed - not used in test functions */ -void generate_output_host(void) +TEST_TEAR_DOWN(time_gmtime) { - int i; - struct tm *t; - printf("Printing host output data. Struct tm member values:\n"); - for (i = 0; i < T1_LEN; i++) { - t = gmtime(&input_vector[i]); - printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", - t->tm_sec, t->tm_min, t->tm_hour, t->tm_mday, t->tm_mon, t->tm_year, - t->tm_wday, t->tm_yday, t->tm_isdst); - if (i != T1_LEN - 1) - printf(",\n"); - } - printf("\n"); } -int gmtime_assert(const time_t input, const int output[]) +static void gmtime_assert(const time_t input, const int output[]) { struct tm *t, t_exp; - char buff[120]; init_tm(&t_exp, output); t = gmtime(&input); - if (compare_tm(t, &t_exp)) { - if (verbose_test()) { - tm_to_str(t, buff); - printf("Testcase failed\nTimestamp: %lld\nOutput tm: %s\n", input, buff); - tm_to_str(&t_exp, buff); - printf("Expected: %s \n\n", buff); - } - return 1; - } - else - { - return 0; - } + struct_tm_assert_equal(&t_exp, t); } -int main(void) -{ - printf("GMTIME TEST STARTED\n"); - save_env(); - int i, failed = 0; - for (i=0; i < T1_LEN; i++) { - failed += gmtime_assert(input_vector[i], output_vector[i]); +TEST(time_gmtime, basic_test) +{ + for (int i = 0; i < T1_LEN; i++) { + gmtime_assert(input_vector[i], output_vector[i]); } - printf("Performed %d testcases %d failed\n", T1_LEN, failed); - return 0; } + +TEST_GROUP_RUNNER(time_gmtime) +{ + RUN_TEST_CASE(time_gmtime, basic_test); +} + + static const time_t input_vector[] = { /* 29 FEB 2016 - leap year */ 61414866000, diff --git a/libc/time/host_generation.c b/libc/time/host_generation.c new file mode 100644 index 000000000..5ea3d7de8 --- /dev/null +++ b/libc/time/host_generation.c @@ -0,0 +1,110 @@ +/* + * Phoenix-RTOS + * + * test-libc-time + * + * Code for generating test cases. + * + * Copyright 2020 Phoenix Systems + * Author: Marcin Brzykcy + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include +#include +#include + +#include "time_common.h" + + +#define NCOLS 9 + + +/* help function for future test generation if needed - not used in test functions */ +void generate_input_mktime(int input_length) +{ + int i; + srand(time(NULL)); + for (i = 0; i < input_length; i++) { + printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", rand() % 59, rand() % 59, rand() % 23, + rand() % 30, rand() % 11, 1980 + rand() % 60, 0, 0, 0); + if (i != input_length - 1) + printf(",\n"); + } + printf("\n"); +} + + +/* help function for future test generation if needed - not used in test functions */ +void generate_output_mktime(const int input_vector[][NCOLS], int input_length) +{ + int i; + struct tm t; + time_t timestamp; + printf("Printing host output data. Struct tm member values:\n"); + for (i = 0; i < input_length; i++) { + init_tm(&t, input_vector[i]); + timestamp = mktime(&t); + printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", + t.tm_sec, t.tm_min, t.tm_hour, t.tm_mday, t.tm_mon, t.tm_year, + t.tm_wday, t.tm_yday, t.tm_isdst); + if (i != input_length - 1) + printf(",\n"); + } + printf("\nTimestamp values:\n{"); + for (i = 0; i < input_length; i++) { + init_tm(&t, input_vector[i]); + timestamp = mktime(&t); + printf("%lld", timestamp); + if (i != input_length - 1) + printf(", "); + } + printf("}\n"); +} + + +/* help function for future test generation if needed - not used in test functions */ +void generate_input_host_gmtime(int input_length) +{ + int i; + struct tm t; + srand(time(NULL)); + for (i = 0; i < input_length; i++) { + t = (struct tm) { + .tm_sec = rand() % 59, + .tm_min = rand() % 59, + .tm_hour = rand() % 23, + .tm_mday = rand() % 30, + .tm_mon = rand() % 11, + .tm_year = 1980 + rand() % 60, + .tm_wday = 0, + .tm_yday = 0, + .tm_isdst = 0, + }; + printf("%lld", mktime(&t)); + if (i != input_length - 1) + printf(",\n"); + } + printf("\n"); +} + + +/* help function for future test generation if needed - not used in test functions */ +void generate_output_host_gmtime(const time_t input_vector[], int input_length) +{ + int i; + struct tm *t; + printf("Printing host output data. Struct tm member values:\n"); + for (i = 0; i < input_length; i++) { + t = gmtime(&input_vector[i]); + printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", + t->tm_sec, t->tm_min, t->tm_hour, t->tm_mday, t->tm_mon, t->tm_year, + t->tm_wday, t->tm_yday, t->tm_isdst); + if (i != input_length - 1) + printf(",\n"); + } + printf("\n"); +} diff --git a/libc/time/main.c b/libc/time/main.c new file mode 100644 index 000000000..e14627467 --- /dev/null +++ b/libc/time/main.c @@ -0,0 +1,30 @@ +/* + * Phoenix-RTOS + * + * test-libc-time + * + * Main entry point. + * + * Copyright 2023 Phoenix Systems + * Author: Jacek Maksymowicz + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#include "unity_fixture.h" + +void runner(void) +{ + RUN_TEST_GROUP(time_mktime); + RUN_TEST_GROUP(time_gmtime); + RUN_TEST_GROUP(time_strftime); +} + + +int main(int argc, char *argv[]) +{ + int failures = UnityMain(argc, (const char **)argv, runner); + return (failures == 0) ? 0 : 1; +} diff --git a/time/test_mktime.c b/libc/time/mktime.c similarity index 93% rename from time/test_mktime.c rename to libc/time/mktime.c index 3bb134ffb..fcf8c754b 100644 --- a/time/test_mktime.c +++ b/libc/time/mktime.c @@ -1,9 +1,9 @@ /* * Phoenix-RTOS * - * libphoenix + * test-libc-time * - * test/test_strftime.c + * Tests of mktime function. * * Copyright 2020 Phoenix Systems * Author: Marcin Brzykcy @@ -16,7 +16,16 @@ #include #include #include -#include "test_common.h" +#include "time_common.h" + +/* Under glibc mktime always returns time in local timezone and timegm always in UTC. + * libphoenix does not have timegm (it's a non-standard extension), but its mktime behaves the same. + */ +#ifdef __phoenix__ +#define function_under_test mktime +#else +#define function_under_test timegm +#endif #define NCOLS 9 #define T1_LEN 9 @@ -40,113 +49,71 @@ static const int input_t4[T4_LEN][NCOLS]; static const int output_t4_1[T4_LEN][NCOLS]; static const time_t output_t4_2[T4_LEN]; -/* help function for future test generation if needed - not used in test functions */ -void generate_input_host(int input_length) +TEST_GROUP(time_mktime); + + +TEST_SETUP(time_mktime) { - int i; - srand(time(NULL)); - for (i = 0; i < input_length; i++) { - printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", rand() % 59, rand() % 59, rand() % 23, - rand() % 30, rand() % 11, 1980 + rand() % 60, 0, 0, 0); - if (i != input_length - 1) - printf(",\n"); - } - printf("\n"); + tzset(); } -/* help function for future test generation if needed - not used in test functions */ -void generate_output_host(const int input_vector[][NCOLS], int input_length) +TEST_TEAR_DOWN(time_mktime) { - int i; - struct tm t; - time_t timestamp; - printf("Printing host output data. Struct tm member values:\n"); - for (i = 0; i < input_length; i++) { - init_tm(&t, input_vector[i]); - timestamp = mktime(&t); - printf("{ %d, %d, %d, %d, %d, %d, %d, %d, %d }", - t.tm_sec, t.tm_min, t.tm_hour, t.tm_mday, t.tm_mon, t.tm_year, - t.tm_wday, t.tm_yday, t.tm_isdst); - if (i != input_length - 1) - printf(",\n"); - - } - printf("\nTimestamp values:\n{"); - for (i = 0; i < input_length; i++) { - init_tm(&t, input_vector[i]); - timestamp = mktime(&t); - printf("%lld", timestamp); - if (i != input_length - 1) - printf(", "); - } - printf("}\n"); } -int mktime_assert(const int input[], const int output[], const time_t timestamp_exp) +static void mktime_assert(const int input[], const int output[], const time_t timestamp_exp) { struct tm t, t_old, t_exp; time_t timestamp; - char buff[120]; init_tm(&t, input); init_tm(&t_old, input); init_tm(&t_exp, output); - timestamp = mktime(&t); - if (compare_tm(&t, &t_exp) || ((timestamp) != (timestamp_exp))) { - if (verbose_test()) { - printf("Testcase failed\n"); - tm_to_str(&t_old, buff); - printf("Original struct tm:\n%s\n", buff); - tm_to_str(&t, buff); - printf("Output struct tm:\n%s\n", buff); - tm_to_str(&t_exp, buff); - printf("Expected output struct tm:\n%s\n", buff); - printf("Timestamp: %lld, expected: %lld\n\n", timestamp, timestamp_exp); - } - return 1; - } - else { - return 0; - } + timestamp = function_under_test(&t); + TEST_ASSERT_EQUAL_INT_MESSAGE(timestamp_exp, timestamp, "Incorrect time_t"); + struct_tm_assert_equal(&t_exp, &t); } -void test_mktime(const int input_vector[][NCOLS], int input_length, const int output_vector1[][NCOLS], const time_t output_vector2[]) +static void perform_test(const int input_vector[][NCOLS], int input_length, const int output_vector1[][NCOLS], const time_t output_vector2[]) { - int i, failed = 0; - for (i = 0; i < input_length; i++) { - if (mktime_assert(input_vector[i], output_vector1[i], output_vector2[i])) - failed++; + for (int i = 0; i < input_length; i++) { + mktime_assert(input_vector[i], output_vector1[i], output_vector2[i]); } - printf("Performed %d testcases %d failed\n", input_length, failed); } -int main(int argc, char *argv[]) +TEST(time_mktime, leap_year) { - /* - * Time zones and dst are not tested, because it's not implemented in tzset() function - */ - printf("MKTIME TEST STARTED\n"); - save_env(); + perform_test(input_t1, T1_LEN, output_t1_1, output_t1_2); +} - tzset(); - printf("Timezone info: tzname[0]=%s tzname[1]=%s \n", tzname[0], tzname[1]); - printf("\nLeap year normalization\n"); - test_mktime(input_t1, T1_LEN, output_t1_1, output_t1_2); +TEST(time_mktime, multiple_values_overflow) +{ + perform_test(input_t2, T2_LEN, output_t2_1, output_t2_2); +} - printf("\nNormalization during multiple values overflow\n"); - test_mktime(input_t2, T2_LEN, output_t2_1, output_t2_2); - printf("\nRandom dates with normalization\n"); - test_mktime(input_t3, T3_LEN, output_t3_1, output_t3_2); +TEST(time_mktime, random_dates_with_normalization) +{ + perform_test(input_t3, T3_LEN, output_t3_1, output_t3_2); +} - printf("\nRandom dates without normalization\n"); - test_mktime(input_t4, T4_LEN, output_t4_1, output_t4_2); - return 0; +TEST(time_mktime, random_dates_without_normalization) +{ + perform_test(input_t4, T4_LEN, output_t4_1, output_t4_2); +} + + +TEST_GROUP_RUNNER(time_mktime) +{ + RUN_TEST_CASE(time_mktime, leap_year); + RUN_TEST_CASE(time_mktime, multiple_values_overflow); + RUN_TEST_CASE(time_mktime, random_dates_with_normalization); + RUN_TEST_CASE(time_mktime, random_dates_without_normalization); } diff --git a/time/test_strftime.c b/libc/time/strftime.c similarity index 52% rename from time/test_strftime.c rename to libc/time/strftime.c index 65abe2881..a4b18a8bf 100644 --- a/time/test_strftime.c +++ b/libc/time/strftime.c @@ -1,9 +1,9 @@ /* * Phoenix-RTOS * - * libphoenix + * test-libc-time * - * test/test_strftime.c + * Tests of strftime function * * Copyright 2020 Phoenix Systems * Author: Marcin Brzykcy @@ -17,10 +17,10 @@ #include #include #include -#include "test_common.h" +#include "time_common.h" + #define BUFF_LEN 35 -#define T1_LEN 37 struct test_data { const struct tm *t; @@ -31,28 +31,48 @@ struct test_data { int ret; }; -static const struct tm t1 = { .tm_sec = 1, .tm_min = 1, .tm_hour = 6, .tm_mday = 2, .tm_mon = 2, - .tm_year = 111, .tm_wday = 0, .tm_yday = 2, .tm_isdst = 0 }; - -static const struct tm t2 = { .tm_sec = 11, .tm_min = 12, .tm_hour = 13, .tm_mday = 23, .tm_mon = 11, - .tm_year = 95, .tm_wday = 6, .tm_yday = 235, .tm_isdst = 0 }; - -static const struct tm t3 = { .tm_sec = 11, .tm_min = 12, .tm_hour = 13, .tm_mday = 23, .tm_mon = 11, - .tm_year = 105, .tm_wday = 1, .tm_yday = 235, .tm_isdst = 0 }; +static const struct tm t1 = { .tm_sec = 1, .tm_min = 1, .tm_hour = 6, .tm_mday = 2, .tm_mon = 2, .tm_year = 111, .tm_wday = 0, .tm_yday = 2, .tm_isdst = 0 }; +static const struct tm t2 = { .tm_sec = 11, .tm_min = 12, .tm_hour = 13, .tm_mday = 23, .tm_mon = 11, .tm_year = 95, .tm_wday = 6, .tm_yday = 235, .tm_isdst = 0 }; +static const struct tm t3 = { .tm_sec = 11, .tm_min = 12, .tm_hour = 13, .tm_mday = 23, .tm_mon = 11, .tm_year = 105, .tm_wday = 1, .tm_yday = 235, .tm_isdst = 0 }; +static const struct tm t4 = { .tm_sec = 0, .tm_min = 0, .tm_hour = 0, .tm_mday = 2, .tm_mon = 0, .tm_year = 99, .tm_wday = 6, .tm_yday = 2, .tm_isdst = 0 }; -static const struct test_data test_vector[T1_LEN] = { +static const struct test_data basic_formatting[] = { { .t = &t1, .format = "%A", .n = 5, .output = "", .ret = 0 }, { .t = &t1, .format = "%A", .n = 6, .output = "", .ret = 0 }, { .t = &t1, .format = "%A", .n = 7, .output = "Sunday", .ret = 1 }, { .t = &t1, .format = "%a %A %b %B", .n = BUFF_LEN, .output = "Sun Sunday Mar March", .ret = 1 }, - { .t = &t1, .format = "%h", .n = BUFF_LEN, .output = "Mar", .ret = 1 }, { .t = &t1, .format = "lorem ipsum %a", .n = BUFF_LEN, .output = "lorem ipsum Sun", .ret = 1 }, { .t = &t1, .format = "%i %a", .n = BUFF_LEN, .output = "%i Sun", .ret = 1 }, { .t = &t1, .format = "lorem %i ips%aum", .n = BUFF_LEN, .output = "lorem %i ipsSunum", .ret = 1 }, + { .t = &t1, .format = "%Y %y | %B %b %m | %d %e", .n = BUFF_LEN, .output = "2011 11 | March Mar 03 | 02 2", .ret = 1 }, + { .t = &t1, .format = "%A %a %w | %j | %H:%M:%S", .n = BUFF_LEN, .output = "Sunday Sun 0 | 003 | 06:01:01", .ret = 1 }, +}; + +static const struct test_data additional_format_chars[] = { + { .t = &t1, .format = "%c", .n = BUFF_LEN, .output = "Sun Mar 2 06:01:01 2011", .ret = 1 }, + { .t = &t2, .format = "%C", .n = BUFF_LEN, .output = "19", .ret = 1 }, + { .t = &t1, .format = "%h", .n = BUFF_LEN, .output = "Mar", .ret = 1 }, + { .t = &t4, .format = "%D", .n = BUFF_LEN, .output = "01/02/99", .ret = 1 }, + { .t = &t4, .format = "%F", .n = BUFF_LEN, .output = "1999-01-02", .ret = 1 }, + { .t = &t4, .format = "%h", .n = BUFF_LEN, .output = "Jan", .ret = 1 }, + { .t = &t4, .format = "%I", .n = BUFF_LEN, .output = "12", .ret = 1 }, + { .t = &t4, .format = "%n", .n = BUFF_LEN, .output = "\n", .ret = 1 }, + { .t = &t4, .format = "%p", .n = BUFF_LEN, .output = "AM", .ret = 1 }, + { .t = &t4, .format = "%R", .n = BUFF_LEN, .output = "00:00", .ret = 1 }, + { .t = &t4, .format = "%r", .n = BUFF_LEN, .output = "12:00:00 AM", .ret = 1 }, + { .t = &t4, .format = "%T", .n = BUFF_LEN, .output = "00:00:00", .ret = 1 }, + { .t = &t4, .format = "%t", .n = BUFF_LEN, .output = "\t", .ret = 1 }, + { .t = &t4, .format = "%u", .n = BUFF_LEN, .output = "6", .ret = 1 }, + { .t = &t4, .format = "%U", .n = BUFF_LEN, .output = "00", .ret = 1 }, + { .t = &t4, .format = "%W", .n = BUFF_LEN, .output = "00", .ret = 1 }, + { .t = &t4, .format = "%x", .n = BUFF_LEN, .output = "01/02/99", .ret = 1 }, + { .t = &t4, .format = "%X", .n = BUFF_LEN, .output = "00:00:00", .ret = 1 }, + { .t = &t4, .format = "%z", .n = BUFF_LEN, .output = "+0000", .ret = 1 }, +}; + +static const struct test_data format_with_padding[] = { { .t = &t2, .format = "%6b%12B", .n = BUFF_LEN, .output = " Dec December", .ret = 1 }, { .t = &t2, .format = "%6a%12A", .n = BUFF_LEN, .output = " Sat Saturday", .ret = 1 }, - { .t = &t3, .format = "%a %A %b %B", .n = BUFF_LEN, .output = "Mon Monday Dec December", .ret = 1 }, - { .t = &t1, .format = "%c", .n = BUFF_LEN, .output = "Sun Mar 2 06:01:01 2011", .ret = 1 }, { .t = &t1, .format = "%C %6C %07C %1C", .n = BUFF_LEN, .output = "20 000020 0000020 20", .ret = 1 }, { .t = &t2, .format = "%C %6C %07C", .n = BUFF_LEN, .output = "19 000019 0000019", .ret = 1 }, { .t = &t3, .format = "%6d %3d %2d %d", .n = BUFF_LEN, .output = "000023 023 23 23", .ret = 1 }, @@ -69,47 +89,79 @@ static const struct test_data test_vector[T1_LEN] = { { .t = &t1, .format = "%p%5p %r", .n = BUFF_LEN, .output = "AM AM 06:01:01 AM", .ret = 1 }, { .t = &t2, .format = "%p%5p %r", .n = BUFF_LEN, .output = "PM PM 01:12:11 PM", .ret = 1 }, { .t = &t2, .format = "%20r", .n = BUFF_LEN, .output = " 01:12:11 PM", .ret = 1 }, - { .t = &t2, .format = "%g", .n = BUFF_LEN, .output = "95", .ret = 1 }, { .t = &t2, .format = "%g %05g %G %07G", .n = BUFF_LEN, .output = "95 00095 1995 0001995", .ret = 1 }, { .t = &t2, .format = "%R %R %S %05S", .n = BUFF_LEN, .output = "13:12 13:12 11 00011", .ret = 1 }, { .t = &t2, .format = "%T %15T", .n = BUFF_LEN, .output = "13:12:11 13:12:11", .ret = 1 }, { .t = &t2, .format = "%u %05u %w %05w", .n = BUFF_LEN, .output = "6 00006 6 00006", .ret = 1 }, { .t = &t2, .format = "%U %05U %W %05W", .n = BUFF_LEN, .output = "33 00033 33 00033", .ret = 1 }, { .t = &t3, .format = "%u %05u %U %5U %V %5V", .n = BUFF_LEN, .output = "1 00001 34 00034 35 00035", .ret = 1 }, - { .t = &t3, .format = "%x %X", .n = BUFF_LEN, .output = "12/23/05 13:12:11", .ret = 1 }, - { .t = &t2, .format = "%y %05y %Y %05Y", .n = BUFF_LEN, .output = "95 00095 1995 01995", .ret = 1 } + { .t = &t2, .format = "%y %05y %Y %05Y", .n = BUFF_LEN, .output = "95 00095 1995 01995", .ret = 1 }, }; +#define SIZE_OF_TABLE(x) (sizeof(x) / sizeof(x[0])) + + +TEST_GROUP(time_strftime); -int strftime_assert(const struct test_data *data) + +TEST_SETUP(time_strftime) +{ + tzset(); +} + + +TEST_TEAR_DOWN(time_strftime) +{ +} + + +static void strftime_assert(const struct test_data *data) { char buff[BUFF_LEN]; int ret = strftime(buff, data->n, data->format, data->t); if (ret == 0 && data->ret == 0) { - return 0; + return; } - if (strcmp(buff, data->output) || ret != strlen(data->output)) { - if (verbose_test()) { - printf("Format string \"%s\"\nOutput string \"%s\"\n", data->format, buff); - printf("Expected \"%s\"\n", data->output); - printf("Return %d Expected: %d\n", ret, (int)strlen(data->output)); - } - return 1; + + TEST_ASSERT_EQUAL_STRING(data->output, buff); + TEST_ASSERT_EQUAL_INT_MESSAGE(strlen(data->output), ret, "Incorrect output length returned"); +} + + +TEST(time_strftime, basic_formatting) +{ + for (int i = 0; i < SIZE_OF_TABLE(basic_formatting); i++) { + strftime_assert(&basic_formatting[i]); } - return 0; } -int main(void) +TEST(time_strftime, additional_format_chars) { - printf("STRFTIME TEST STARTED\n"); - save_env(); +/* Disabled because of #351 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/351 (point 1) */ +#ifdef __phoenix__ + TEST_IGNORE_MESSAGE("#351 issue"); +#endif + for (int i = 0; i < SIZE_OF_TABLE(additional_format_chars); i++) { + strftime_assert(&additional_format_chars[i]); + } +} - int i, failed = 0; - for (i = 0; i < T1_LEN; i++) { - failed += strftime_assert(&test_vector[i]); +TEST(time_strftime, format_with_padding) +{ +/* Disabled because of #351 issue: https://github.com/phoenix-rtos/phoenix-rtos-project/issues/351 (point 2) */ +#ifdef __phoenix__ + TEST_IGNORE_MESSAGE("#351 issue"); +#endif + for (int i = 0; i < SIZE_OF_TABLE(format_with_padding); i++) { + strftime_assert(&format_with_padding[i]); } - printf("Performed %d testcases %d failed\n", T1_LEN, failed); - return 0; } + +TEST_GROUP_RUNNER(time_strftime) +{ + RUN_TEST_CASE(time_strftime, basic_formatting); + RUN_TEST_CASE(time_strftime, additional_format_chars); + RUN_TEST_CASE(time_strftime, format_with_padding); +} diff --git a/libc/time/time_common.h b/libc/time/time_common.h new file mode 100644 index 000000000..676dbfe0d --- /dev/null +++ b/libc/time/time_common.h @@ -0,0 +1,69 @@ +/* + * Phoenix-RTOS + * + * test-libc-time + * + * Common utility functions for time.h tests. + * + * Copyright 2023 Phoenix Systems + * Author: Jacek Maksymowicz + * + * This file is part of Phoenix-RTOS. + * + * %LICENSE% + */ + +#ifndef _TEST_LIBC_TIME_COMMON_H +#define _TEST_LIBC_TIME_COMMON_H + +#include +#include + +#include "unity_fixture.h" + +static inline void tm_to_str(struct tm *t, char *buff) +{ + sprintf( + buff, + "tm_sec %d, tm_min %d, tm_hour %d, tm_mday %d\n" + "tm_mon %d tm_year %d tm_wday %d tm_yday %d tm_isdst %d", + t->tm_sec, + t->tm_min, + t->tm_hour, + t->tm_mday, + t->tm_mon, + t->tm_year, + t->tm_wday, + t->tm_yday, + t->tm_isdst); +} + + +static inline void init_tm(struct tm *t, const int *input) +{ + t->tm_sec = input[0]; + t->tm_min = input[1]; + t->tm_hour = input[2]; + t->tm_mday = input[3]; + t->tm_mon = input[4]; + t->tm_year = input[5]; + t->tm_wday = input[6]; + t->tm_yday = input[7]; + t->tm_isdst = input[8]; +} + + +static inline void struct_tm_assert_equal(struct tm *expected, struct tm *got) +{ + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_sec, got->tm_sec, "tm_sec"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_min, got->tm_min, "tm_min"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_hour, got->tm_hour, "tm_hour"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_mday, got->tm_mday, "tm_mday"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_mon, got->tm_mon, "tm_mon"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_year, got->tm_year, "tm_year"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_wday, got->tm_wday, "tm_wday"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_yday, got->tm_yday, "tm_yday"); + TEST_ASSERT_EQUAL_MESSAGE(expected->tm_isdst, got->tm_isdst, "tm_isdst"); +} + +#endif /* _TEST_LIBC_TIME_COMMON_H */ diff --git a/test_common.h b/test_common.h index 49f5afeee..a9122ee95 100644 --- a/test_common.h +++ b/test_common.h @@ -16,44 +16,11 @@ #ifndef TEST_COMMON_H #define TEST_COMMON_H -#include #include #include static int test_verbosity; -static inline void tm_to_str(struct tm *t, char *buff) -{ - sprintf(buff, "tm_sec %d, tm_min %d, tm_hour %d, tm_mday %d\n" - "tm_mon %d tm_year %d tm_wday %d tm_yday %d tm_isdst %d", - t->tm_sec, t->tm_min, t->tm_hour, t->tm_mday, t->tm_mon, t->tm_year, - t->tm_wday, t->tm_yday, t->tm_isdst); - -} - - -static inline void init_tm(struct tm *t, const int *input) -{ - t->tm_sec = input[0]; - t->tm_min = input[1]; - t->tm_hour = input[2]; - t->tm_mday = input[3]; - t->tm_mon = input[4]; - t->tm_year = input[5]; - t->tm_wday = input[6]; - t->tm_yday = input[7]; - t->tm_isdst = input[8]; -} - - -static inline int compare_tm(struct tm *t1, struct tm *t2) -{ - return ((t1->tm_sec != t2->tm_sec) || (t1->tm_min != t2->tm_min) || (t1->tm_hour != t2->tm_hour) || - (t1->tm_mday != t2->tm_mday) || (t1->tm_mon != t2->tm_mon) || (t1->tm_year != t2->tm_year) || - (t1->tm_wday != t2->tm_wday) || (t1->tm_yday != t2->tm_yday) || (t1->tm_isdst != t2->tm_isdst)); -} - - static inline void save_env(void) { char *c = getenv("VERBOSE_TEST");