tests/lib/timeutil: add tests for time conversions

This verifies gmtime and timeutil_timegm against each other and
reference data for a wide range of instances.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-07-01 11:00:07 -05:00 committed by Carles Cufí
commit 7251d8c380
8 changed files with 590 additions and 0 deletions

View file

@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(timeutil)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1 @@
CONFIG_ZTEST=y

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include "timeutil_test.h"
void timeutil_check(const struct timeutil_test_data *tp,
size_t count)
{
const struct timeutil_test_data *tpe = tp + count;
while (tp < tpe) {
struct tm tm = *gmtime(&tp->unix);
time_t unix = timeutil_timegm(&tm);
zassert_equal(&tm, gmtime_r(&tp->unix, &tm),
"gmtime_r failed");
zassert_equal(tm.tm_year, tp->tm.tm_year,
"datetime %s year %d != %d",
tp->civil, tm.tm_year, tp->tm.tm_year);
zassert_equal(tm.tm_mon, tp->tm.tm_mon,
"datetime %s mon %d != %d",
tp->civil, tm.tm_mon, tp->tm.tm_mon);
zassert_equal(tm.tm_mday, tp->tm.tm_mday,
"datetime %s mday %d != %d",
tp->civil, tm.tm_mday, tp->tm.tm_mday);
zassert_equal(tm.tm_hour, tp->tm.tm_hour,
"datetime %s hour %d != %d",
tp->civil, tm.tm_hour, tp->tm.tm_hour);
zassert_equal(tm.tm_min, tp->tm.tm_min,
"datetime %s min %d != %d",
tp->civil, tm.tm_min, tp->tm.tm_min);
zassert_equal(tm.tm_sec, tp->tm.tm_sec,
"datetime %s sec %d != %d",
tp->civil, tm.tm_sec, tp->tm.tm_sec);
zassert_equal(tm.tm_wday, tp->tm.tm_wday,
"datetime %s wday %d != %d",
tp->civil, tm.tm_wday, tp->tm.tm_wday);
zassert_equal(tm.tm_yday, tp->tm.tm_yday,
"datetime %s yday %d != %d",
tp->civil, tm.tm_yday, tp->tm.tm_yday);
zassert_equal(tp->unix, unix,
"datetime %s reverse != %ld",
tp->civil, unix);
++tp;
}
}
/*test case main entry*/
void test_main(void)
{
ztest_test_suite(test_timeutil_api,
ztest_unit_test(test_gmtime),
ztest_unit_test(test_s32),
ztest_unit_test(test_s64)
);
ztest_run_test_suite(test_timeutil_api);
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Tests that gmtime matches gmtime_r */
#include <string.h>
#include <ztest.h>
#include "timeutil_test.h"
void test_gmtime(void)
{
struct tm tm;
time_t time = 1561994005;
zassert_equal(&tm, gmtime_r(&time, &tm),
"gmtime_r return failed");
struct tm *tp = gmtime(&time);
zassert_true(memcmp(&tm, tp, sizeof(tm)) == 0,
"gmtime disagrees with gmtime_r");
}

View file

@ -0,0 +1,244 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Tests where time_t is a 32-bit value */
#include <ztest.h>
#include "timeutil_test.h"
static const struct timeutil_test_data tests[] = {
/* Simple tests */
{ .unix = -1,
.civil = "1969-12-31 23:59:59 Wed 365",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 69,
.tm_wday = 3,
.tm_yday = 364,
} },
{ .unix = 0,
.civil = "1970-01-01 00:00:00 Thu 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 70,
.tm_wday = 4,
.tm_yday = 0,
} },
{ .unix = 1498577363,
.civil = "2017-06-27 15:29:23 Tue 178",
.tm = {
.tm_sec = 23,
.tm_min = 29,
.tm_hour = 15,
.tm_mday = 27,
.tm_mon = 5,
.tm_year = 117,
.tm_wday = 2,
.tm_yday = 177,
} },
/*
* 32-bit extreme values. Lower range is limited due
* algorithm subtraction rounding to days.
*/
{ .unix = -2147483648 + 86399,
.civil = "1901-12-14 20:45:51 Sat 348",
.tm = {
.tm_sec = 51,
.tm_min = 45,
.tm_hour = 20,
.tm_mday = 14,
.tm_mon = 11,
.tm_year = 1,
.tm_wday = 6,
.tm_yday = 347,
} },
{ .unix = 2147483647,
.civil = "2038-01-19 03:14:07 Tue 019",
.tm = {
.tm_sec = 7,
.tm_min = 14,
.tm_hour = 3,
.tm_mday = 19,
.tm_mon = 0,
.tm_year = 138,
.tm_wday = 2,
.tm_yday = 18,
} },
/* Normal leap year: 1972 */
{ .unix = 63071999,
.civil = "1971-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 71,
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = 63072000,
.civil = "1972-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 72,
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = 68083200,
.civil = "1972-02-28 00:00:00 Mon 059",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 28,
.tm_mon = 1,
.tm_year = 72,
.tm_wday = 1,
.tm_yday = 58,
} },
{ .unix = 68169600,
.civil = "1972-02-29 00:00:00 Tue 060",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 29,
.tm_mon = 1,
.tm_year = 72,
.tm_wday = 2,
.tm_yday = 59,
} },
{ .unix = 68256000,
.civil = "1972-03-01 00:00:00 Wed 061",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 2,
.tm_year = 72,
.tm_wday = 3,
.tm_yday = 60,
} },
{ .unix = 94521600,
.civil = "1972-12-30 00:00:00 Sat 365",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 30,
.tm_mon = 11,
.tm_year = 72,
.tm_wday = 6,
.tm_yday = 364,
} },
{ .unix = 94608000,
.civil = "1972-12-31 00:00:00 Sun 366",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 72,
.tm_wday = 0,
.tm_yday = 365,
} },
{ .unix = 94694400,
.civil = "1973-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 73,
.tm_wday = 1,
.tm_yday = 0,
} },
/* Start of era 5, special leap year */
{ .unix = 946684799,
.civil = "1999-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 99,
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = 946684800,
.civil = "2000-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 100,
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = 951696000,
.civil = "2000-02-28 00:00:00 Mon 059",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 28,
.tm_mon = 1,
.tm_year = 100,
.tm_wday = 1,
.tm_yday = 58,
} },
{ .unix = 951782400,
.civil = "2000-02-29 00:00:00 Tue 060",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 29,
.tm_mon = 1,
.tm_year = 100,
.tm_wday = 2,
.tm_yday = 59,
} },
{ .unix = 951868800,
.civil = "2000-03-01 00:00:00 Wed 061",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 2,
.tm_year = 100,
.tm_wday = 3,
.tm_yday = 60,
} },
};
void test_s32(void)
{
timeutil_check(tests, sizeof(tests) / sizeof(*tests));
}

View file

@ -0,0 +1,220 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Tests where time_t requires a 64-bit value */
#include <ztest.h>
#include "timeutil_test.h"
static const struct timeutil_test_data tests[] = {
/* 32-bit, but algorithm subtraction underflows */
{ .unix = -2147483648,
.civil = "1901-12-13 20:45:52 Fri 347",
.tm = {
.tm_sec = 52,
.tm_min = 45,
.tm_hour = 20,
.tm_mday = 13,
.tm_mon = 11,
.tm_year = 1,
.tm_wday = 5,
.tm_yday = 346,
} },
{ .unix = (time_t)-2147483649,
.civil = "1901-12-13 20:45:51 Fri 347",
.tm = {
.tm_sec = 51,
.tm_min = 45,
.tm_hour = 20,
.tm_mday = 13,
.tm_mon = 11,
.tm_year = 1,
.tm_wday = 5,
.tm_yday = 346,
} },
{ .unix = (time_t)2147483648,
.civil = "2038-01-19 03:14:08 Tue 019",
.tm = {
.tm_sec = 8,
.tm_min = 14,
.tm_hour = 3,
.tm_mday = 19,
.tm_mon = 0,
.tm_year = 138,
.tm_wday = 2,
.tm_yday = 18,
} },
{ .unix = (time_t)64060588799,
.civil = "3999-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 2099,
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = (time_t)64060588800,
.civil = "4000-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 2100,
.tm_wday = 6,
.tm_yday = 0,
} },
/* Normal century is a common year */
{ .unix = (time_t)-2208988801,
.civil = "1899-12-31 23:59:59 Sun 365",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = -1,
.tm_wday = 0,
.tm_yday = 364,
} },
{ .unix = (time_t)-2208988800,
.civil = "1900-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 0,
.tm_wday = 1,
.tm_yday = 0,
} },
{ .unix = (time_t)-2203977600,
.civil = "1900-02-28 00:00:00 Wed 059",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 28,
.tm_mon = 1,
.tm_year = 0,
.tm_wday = 3,
.tm_yday = 58,
} },
{ .unix = (time_t)-2203891200,
.civil = "1900-03-01 00:00:00 Thu 060",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 2,
.tm_year = 0,
.tm_wday = 4,
.tm_yday = 59,
} },
{ .unix = (time_t)-2177539200,
.civil = "1900-12-31 00:00:00 Mon 365",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = 0,
.tm_wday = 1,
.tm_yday = 364,
} },
{ .unix = (time_t)-2177452800,
.civil = "1901-01-01 00:00:00 Tue 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = 1,
.tm_wday = 2,
.tm_yday = 0,
} },
/* Extrema, check against proleptic Gregorian calendar data:
* https://www.timeanddate.com/calendar/?year=1&country=22
*/
{ .unix = (time_t)-62167305600,
.civil = "-1-12-31 00:00:00 Fri 365",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = -1901,
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = (time_t)-62167219200,
.civil = "0-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = -1900,
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = (time_t)-62135596801,
.civil = "0-12-31 23:59:59 Sun 366",
.tm = {
.tm_sec = 59,
.tm_min = 59,
.tm_hour = 23,
.tm_mday = 31,
.tm_mon = 11,
.tm_year = -1900,
.tm_wday = 0,
.tm_yday = 365,
} },
{ .unix = (time_t)-62135596800,
.civil = "1-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = -1899,
.tm_wday = 1,
.tm_yday = 0,
} },
{ .unix = (time_t)-62135596800,
.civil = "1-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
.tm_min = 0,
.tm_hour = 0,
.tm_mday = 1,
.tm_mon = 0,
.tm_year = -1899,
.tm_wday = 1,
.tm_yday = 0,
} },
};
void test_s64(void)
{
if (sizeof(time_t) < 8U) {
ztest_test_skip();
return;
}
timeutil_check(tests, sizeof(tests) / sizeof(*tests));
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2019 Peter Bigot Consulting
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef TIMEUTIL_TEST_H
#define TIMEUTIL_TEST_H
#include <stdlib.h>
#include <sys/timeutil.h>
struct timeutil_test_data {
time_t unix;
const char *civil;
struct tm tm;
};
void timeutil_check(const struct timeutil_test_data *tp,
size_t count);
void test_gmtime(void);
void test_s32(void);
void test_s64(void);
#endif /* TIMEUTIL_TEST_H */

View file

@ -0,0 +1,3 @@
tests:
libraries.libc.minimal:
tags: timeutils