Skip to content

Commit

Permalink
libc: time.h: add test groups for gmtime, mktime, strftime
Browse files Browse the repository at this point in the history
JIRA: RTOS-609
  • Loading branch information
jmaksymowicz committed Sep 26, 2023
1 parent aeff883 commit 52b78d5
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 207 deletions.
1 change: 1 addition & 0 deletions libc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
5 changes: 5 additions & 0 deletions libc/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
79 changes: 23 additions & 56 deletions time/test_gmtime.c → libc/time/gmtime.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +16,8 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "test_common.h"
#include "time_common.h"


#define NCOLS 9
#define T1_LEN 50
Expand All @@ -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,
Expand Down
110 changes: 110 additions & 0 deletions libc/time/host_generation.c
Original file line number Diff line number Diff line change
@@ -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 <time.h>
#include <stdio.h>
#include <stdlib.h>

#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);

Check warning on line 61 in libc/time/host_generation.c

View workflow job for this annotation

GitHub Actions / call-ci / build (host-generic-pc)

format '%lld' expects argument of type 'long long int', but argument 2 has type 'time_t' {aka 'long int'} [-Wformat=]
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));

Check warning on line 87 in libc/time/host_generation.c

View workflow job for this annotation

GitHub Actions / call-ci / build (host-generic-pc)

format '%lld' expects argument of type 'long long int', but argument 2 has type 'time_t' {aka 'long int'} [-Wformat=]
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");
}
30 changes: 30 additions & 0 deletions libc/time/main.c
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit 52b78d5

Please sign in to comment.