Add timespec_{read,write}_file() and unit tests

This commit is contained in:
Pim van Pelt
2018-11-04 17:40:12 +01:00
parent 5b79487ab8
commit f5ea934dfb
4 changed files with 110 additions and 1 deletions

View File

@ -89,6 +89,13 @@ static void test_timespec_state(void) {
struct mgos_timespec *ts = timespec_create();
struct tm target;
char buf[500];
char tmp_file[30];
int fd;
snprintf (tmp_file, sizeof(tmp_file)-1, "/tmp/timespec_XXXXXX");
fd = mkstemp(tmp_file);
close(fd);
LOG(LL_INFO, ("Temporary filename: %s", tmp_file));
ASSERT(ts, "Created timespec");
timespec_get_spec(ts, buf, sizeof(buf)-1);
@ -157,7 +164,11 @@ static void test_timespec_state(void) {
// Test timespec retrieval
timespec_get_spec(ts, buf, sizeof(buf)-1);
ASSERT(0 == strcmp(buf, "23:01:02-01:02:03,14:11:05-14:59:52,12:00:00-13:00:00,08:00:00-10:00:00"), "timespec_get_spec() didn't match input spec(s)");
LOG(LL_INFO, ("Spec: '%s'", buf));
// Write to file and read back
ASSERT(timespec_write_file(ts, tmp_file), "Could not write timespec to file");
ASSERT(timespec_read_file(ts, tmp_file), "Could not read timespec from file");
ASSERT(0 == strcmp(buf, "23:01:02-01:02:03,14:11:05-14:59:52,12:00:00-13:00:00,08:00:00-10:00:00"), "timespec_read_file() didn't match input spec(s)");
ASSERT(timespec_destroy(&ts), "Destroyed timespec");
@ -187,6 +198,11 @@ static void test_timespec_state(void) {
timespec_get_spec(ts, buf, sizeof(buf)-1);
ASSERT(0 == strcmp(buf, "03:02:03-04:03:04,02:00:00-02:00:01,23:00:00-01:00:00,00:00:00-00:00:00,08:00:00-10:00:00"), "timespec_get_spec() didn't match input spec(s)");
// Write to file and read back
ASSERT(timespec_write_file(ts, tmp_file), "Could not write timespec to file");
ASSERT(timespec_read_file(ts, tmp_file), "Could not read timespec from file");
ASSERT(0 == strcmp(buf, "03:02:03-04:03:04,02:00:00-02:00:01,23:00:00-01:00:00,00:00:00-00:00:00,08:00:00-10:00:00"), "timespec_read_file() didn't match input spec(s)");
ASSERT(timespec_destroy(&ts), "Destroyed timespec");
// Some other time counting tests
@ -201,7 +217,15 @@ static void test_timespec_state(void) {
timespec_get_spec(ts, buf, sizeof(buf)-1);
ASSERT(0 == strcmp(buf, "12:00:00-00:00:00,00:00:00-12:00:00"), "timespec_get_spec() didn't match input spec(s)");
// Write to file and read back
ASSERT(timespec_write_file(ts, tmp_file), "Could not write timespec to file");
ASSERT(timespec_read_file(ts, tmp_file), "Could not read timespec from file");
ASSERT(0 == strcmp(buf, "12:00:00-00:00:00,00:00:00-12:00:00"), "timespec_read_file() didn't match input spec(s)");
ASSERT(timespec_destroy(&ts), "Destroyed timespec");
LOG(LL_INFO, ("Removing temporary file: %s", tmp_file));
unlink(tmp_file);
}
void test_timespec() {