Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libc: time.h: add test groups for gmtime, mktime, strftime #255

Merged
merged 1 commit into from
Oct 19, 2023

Conversation

jmaksymowicz
Copy link
Contributor

Moved tests for time functions (gmtime, mktime and strftime) that existed previously and added them to libc automatic testing.

Description

Tests for the mentioned time functions existed, but were never compiled or checked. The tests for gmtime and mktime functions pass. Some tests for strftime would fail (because not all formats are implemented, or because padding is not implemented), so I set them to be ignored for now.

Motivation and Context

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

How Has This Been Tested?

  • Already covered by automatic testing.
  • New test added: (add PR link here).
  • Tested by hand on: ia32-generic-qemu, armv7a9-zynq7000-zturn

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing linter checks and tests passed.
  • My changes generate no new compilation warnings for any of the targets.

Special treatment

  • This PR needs additional PRs to work (list the PRs, preferably in merge-order).
  • I will merge this PR by myself when appropriate.

@github-actions
Copy link

github-actions bot commented Sep 22, 2023

Unit Test Results

5 948 tests  +64   5 291 ✔️ +50   38m 3s ⏱️ + 1m 8s
   324 suites +  8      657 💤 +14 
       1 files   ±  0          0 ±  0 

Results for commit 31f95c9. ± Comparison against base commit 21e66a3.

♻️ This comment has been updated with latest results.

Copy link
Member

@nalajcie nalajcie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that you've moved the obsolete tests to unity framework!

I've put some nitpicks found by reading the code (I didn't run it).

@@ -16,7 +16,9 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "test_common.h"
#include "../../test_common.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't include files from outside of libc subdir - move necessary functions to the common part of libc tests (or to time subfolder if you don't think they would be shared between libc tests submodules)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I made a new header file with common functions for time tests.

{
return 0;
}
TEST_ASSERT_FALSE_MESSAGE(compare_tm(t, &t_exp), "Incorrect struct tm");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it would be better to provide TEST_ASSERT_* attributed compare_tm?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{
struct tm *t, t_exp;
char buff[120];
init_tm(&t_exp, output);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we could directly keep struct tm in output tab, but you can keep it as is (as it's not directly related to your changes)

#include <stdio.h>
#include <stdlib.h>

#include "../../test_common.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't include files from outside of libc subdir

❗ Please note that this file introduces new compilation warnings

@@ -16,7 +16,9 @@
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "test_common.h"
#include "../../test_common.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't include files from outside of libc subdir

libc/time/main.c Outdated
Comment on lines 28 to 29
UnityMain(argc, (const char **)argv, runner);
return 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is ^C^V from other files but maybe:

Suggested change
UnityMain(argc, (const char **)argv, runner);
return 0;
int failures = UnityMain(argc, (const char **)argv, runner);
return (failures == 0) : 0 : 1;

}
timestamp = function_under_test(&t);
TEST_ASSERT_EQUAL_INT_MESSAGE(timestamp_exp, timestamp, "Incorrect time_t");
TEST_ASSERT_FALSE_MESSAGE(compare_tm(&t, &t_exp), "Incorrect struct tm");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TEST_ASSERT_* attributed compare_tm ?

printf("STRFTIME TEST STARTED\n");
save_env();
#ifdef __phoenix__
TEST_IGNORE();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe use TEST_IGNORE_MESSAGE("reason, eg. issue ID")

why these tests are ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added messages to tests that are ignored for now. The issue is phoenix-rtos/phoenix-rtos-project#351.

Comment on lines 134 to 135
#endif
#ifndef __phoenix__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#ifndef __phoenix__
#else

Or don't put it in else (the code after TEST_IGNORE won't run anyway)

@jmaksymowicz jmaksymowicz force-pushed the libc_time_tests branch 2 times, most recently from 52b78d5 to 2281a4e Compare September 26, 2023 13:34
@nalajcie
Copy link
Member

image

Copy link
Contributor

@damianloew damianloew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adapting this piece of code to our environment. The improvements looks clean, small remarks left.

RUN_TEST_CASE(time_gmtime, basic_test);
}


static const time_t input_vector[] = {
/* 29 FEB 2016 - leap year */
61414866000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The values tested are in fact 1900 years further:
Screenshot from 2023-10-05 16-43-46
Screenshot from 2023-10-05 16-01-49

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it's not your code and those values are not so crucial, but it would be nice to correct them before enabling in CI (they may be misleading).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for mktime tests.

}


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t_old is not needed.

.tm_hour = rand() % 23,
.tm_mday = rand() % 30,
.tm_mon = rand() % 11,
.tm_year = 1980 + rand() % 60,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue mentioned above.

Suggested change
.tm_year = 1980 + rand() % 60,
.tm_year = 80 + rand() % 60,


static const struct test_data test_vector[T1_LEN] = {
static const struct test_data basic_formatting[] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we could place the declarations at the beginning of the file
like it's done in the rest of time tests. So sth like:

Suggested change
static const struct test_data basic_formatting[] = {
static const struct test_data basic_formatting[T1_LEN];
static const struct test_data additional_format_chars[T2_LEN];
...
static const struct test_data basic_formatting[] = {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we could place test data below TEST_GROUP_RUNNER.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively you could switch to the convention applied in this test in mktime/strftime tests - we just want to be consistent.

#include "time_common.h"


#define NCOLS 9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[clang-format-pr] reported by reviewdog 🐶
suggested fix

Suggested change
#define NCOLS 9
#define NCOLS 9

Copy link
Contributor

@damianloew damianloew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for applying the suggestions, please rebase changes and we can merge it :)

Copy link
Contributor

@damianloew damianloew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@damianloew damianloew merged commit 351d060 into master Oct 19, 2023
30 checks passed
@damianloew damianloew deleted the libc_time_tests branch October 19, 2023 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants