tests: move test timeutil into "unit" directory.

We don't have to build an image for running test timeutil. We
can just build a native app to test it. So move it into "unit"
directory.

Also, add 64-bit support for unit testing framework.

Signed-off-by: Steven Wang <steven.l.wang@linux.intel.com>
This commit is contained in:
Steven Wang 2019-10-14 22:23:18 +08:00 committed by Anas Nashif
commit 3cb03efd9c
11 changed files with 72 additions and 62 deletions

View file

@ -36,7 +36,13 @@ list(APPEND INCLUDE
if(CMAKE_HOST_APPLE)
else()
if(M64_MODE)
set (CMAKE_C_FLAGS "-m64")
else()
set (CMAKE_C_FLAGS "-m32") #deprecated on macOS
endif(M64_MODE)
endif()
target_compile_options(testbinary PRIVATE

View file

@ -1,8 +0,0 @@
# 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

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

View file

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

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
project(timeutil)
set(SOURCES main.c test_gmtime.c test_s32.c test_s64.c)
include($ENV{ZEPHYR_BASE}/subsys/testsuite/unittest.cmake)

View file

@ -6,6 +6,7 @@
#include <ztest.h>
#include "timeutil_test.h"
#include "../../../lib/os/timeutil.c"
void timeutil_check(const struct timeutil_test_data *tp,
size_t count)
@ -13,10 +14,10 @@ void timeutil_check(const struct timeutil_test_data *tp,
const struct timeutil_test_data *tpe = tp + count;
while (tp < tpe) {
struct tm tm = *gmtime(&tp->unix);
time_t unix = timeutil_timegm(&tm);
struct tm tm = *gmtime(&tp->ux);
time_t uxtime = timeutil_timegm(&tm);
zassert_equal(&tm, gmtime_r(&tp->unix, &tm),
zassert_equal(&tm, gmtime_r(&tp->ux, &tm),
"gmtime_r failed");
zassert_equal(tm.tm_year, tp->tm.tm_year,
@ -43,9 +44,9 @@ void timeutil_check(const struct timeutil_test_data *tp,
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,
zassert_equal(tp->ux, uxtime,
"datetime %s reverse != %ld",
tp->civil, unix);
tp->civil, uxtime);
++tp;
}

View file

@ -11,7 +11,7 @@
static const struct timeutil_test_data tests[] = {
/* Simple tests */
{ .unix = -1,
{ .ux = -1,
.civil = "1969-12-31 23:59:59 Wed 365",
.tm = {
.tm_sec = 59,
@ -23,7 +23,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 3,
.tm_yday = 364,
} },
{ .unix = 0,
{ .ux = 0,
.civil = "1970-01-01 00:00:00 Thu 001",
.tm = {
.tm_sec = 0,
@ -35,7 +35,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 4,
.tm_yday = 0,
} },
{ .unix = 1498577363,
{ .ux = 1498577363,
.civil = "2017-06-27 15:29:23 Tue 178",
.tm = {
.tm_sec = 23,
@ -52,7 +52,7 @@ static const struct timeutil_test_data tests[] = {
* 32-bit extreme values. Lower range is limited due
* algorithm subtraction rounding to days.
*/
{ .unix = -2147483648 + 86399,
{ .ux = -2147483648 + 86399,
.civil = "1901-12-14 20:45:51 Sat 348",
.tm = {
.tm_sec = 51,
@ -64,7 +64,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 6,
.tm_yday = 347,
} },
{ .unix = 2147483647,
{ .ux = 2147483647,
.civil = "2038-01-19 03:14:07 Tue 019",
.tm = {
.tm_sec = 7,
@ -78,7 +78,7 @@ static const struct timeutil_test_data tests[] = {
} },
/* Normal leap year: 1972 */
{ .unix = 63071999,
{ .ux = 63071999,
.civil = "1971-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
@ -90,7 +90,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = 63072000,
{ .ux = 63072000,
.civil = "1972-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
@ -102,7 +102,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = 68083200,
{ .ux = 68083200,
.civil = "1972-02-28 00:00:00 Mon 059",
.tm = {
.tm_sec = 0,
@ -114,7 +114,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 1,
.tm_yday = 58,
} },
{ .unix = 68169600,
{ .ux = 68169600,
.civil = "1972-02-29 00:00:00 Tue 060",
.tm = {
.tm_sec = 0,
@ -126,7 +126,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 2,
.tm_yday = 59,
} },
{ .unix = 68256000,
{ .ux = 68256000,
.civil = "1972-03-01 00:00:00 Wed 061",
.tm = {
.tm_sec = 0,
@ -138,7 +138,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 3,
.tm_yday = 60,
} },
{ .unix = 94521600,
{ .ux = 94521600,
.civil = "1972-12-30 00:00:00 Sat 365",
.tm = {
.tm_sec = 0,
@ -150,7 +150,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 6,
.tm_yday = 364,
} },
{ .unix = 94608000,
{ .ux = 94608000,
.civil = "1972-12-31 00:00:00 Sun 366",
.tm = {
.tm_sec = 0,
@ -162,7 +162,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 0,
.tm_yday = 365,
} },
{ .unix = 94694400,
{ .ux = 94694400,
.civil = "1973-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
@ -176,7 +176,7 @@ static const struct timeutil_test_data tests[] = {
} },
/* Start of era 5, special leap year */
{ .unix = 946684799,
{ .ux = 946684799,
.civil = "1999-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
@ -188,7 +188,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = 946684800,
{ .ux = 946684800,
.civil = "2000-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
@ -200,7 +200,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = 951696000,
{ .ux = 951696000,
.civil = "2000-02-28 00:00:00 Mon 059",
.tm = {
.tm_sec = 0,
@ -212,7 +212,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 1,
.tm_yday = 58,
} },
{ .unix = 951782400,
{ .ux = 951782400,
.civil = "2000-02-29 00:00:00 Tue 060",
.tm = {
.tm_sec = 0,
@ -224,7 +224,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 2,
.tm_yday = 59,
} },
{ .unix = 951868800,
{ .ux = 951868800,
.civil = "2000-03-01 00:00:00 Wed 061",
.tm = {
.tm_sec = 0,

View file

@ -12,7 +12,7 @@
static const struct timeutil_test_data tests[] = {
/* 32-bit, but algorithm subtraction underflows */
{ .unix = -2147483648,
{ .ux = -2147483648,
.civil = "1901-12-13 20:45:52 Fri 347",
.tm = {
.tm_sec = 52,
@ -24,7 +24,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 346,
} },
{ .unix = (time_t)-2147483649,
{ .ux = (time_t)-2147483649,
.civil = "1901-12-13 20:45:51 Fri 347",
.tm = {
.tm_sec = 51,
@ -36,7 +36,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 346,
} },
{ .unix = (time_t)2147483648,
{ .ux = (time_t)2147483648,
.civil = "2038-01-19 03:14:08 Tue 019",
.tm = {
.tm_sec = 8,
@ -48,7 +48,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 2,
.tm_yday = 18,
} },
{ .unix = (time_t)64060588799,
{ .ux = (time_t)64060588799,
.civil = "3999-12-31 23:59:59 Fri 365",
.tm = {
.tm_sec = 59,
@ -60,7 +60,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = (time_t)64060588800,
{ .ux = (time_t)64060588800,
.civil = "4000-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
@ -73,7 +73,7 @@ static const struct timeutil_test_data tests[] = {
.tm_yday = 0,
} },
/* Normal century is a common year */
{ .unix = (time_t)-2208988801,
{ .ux = (time_t)-2208988801,
.civil = "1899-12-31 23:59:59 Sun 365",
.tm = {
.tm_sec = 59,
@ -85,7 +85,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 0,
.tm_yday = 364,
} },
{ .unix = (time_t)-2208988800,
{ .ux = (time_t)-2208988800,
.civil = "1900-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
@ -97,7 +97,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 1,
.tm_yday = 0,
} },
{ .unix = (time_t)-2203977600,
{ .ux = (time_t)-2203977600,
.civil = "1900-02-28 00:00:00 Wed 059",
.tm = {
.tm_sec = 0,
@ -109,7 +109,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 3,
.tm_yday = 58,
} },
{ .unix = (time_t)-2203891200,
{ .ux = (time_t)-2203891200,
.civil = "1900-03-01 00:00:00 Thu 060",
.tm = {
.tm_sec = 0,
@ -121,7 +121,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 4,
.tm_yday = 59,
} },
{ .unix = (time_t)-2177539200,
{ .ux = (time_t)-2177539200,
.civil = "1900-12-31 00:00:00 Mon 365",
.tm = {
.tm_sec = 0,
@ -133,7 +133,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 1,
.tm_yday = 364,
} },
{ .unix = (time_t)-2177452800,
{ .ux = (time_t)-2177452800,
.civil = "1901-01-01 00:00:00 Tue 001",
.tm = {
.tm_sec = 0,
@ -149,7 +149,7 @@ static const struct timeutil_test_data tests[] = {
/* Extrema, check against proleptic Gregorian calendar data:
* https://www.timeanddate.com/calendar/?year=1&country=22
*/
{ .unix = (time_t)-62167305600,
{ .ux = (time_t)-62167305600,
.civil = "-1-12-31 00:00:00 Fri 365",
.tm = {
.tm_sec = 0,
@ -161,7 +161,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 5,
.tm_yday = 364,
} },
{ .unix = (time_t)-62167219200,
{ .ux = (time_t)-62167219200,
.civil = "0-01-01 00:00:00 Sat 001",
.tm = {
.tm_sec = 0,
@ -173,7 +173,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 6,
.tm_yday = 0,
} },
{ .unix = (time_t)-62135596801,
{ .ux = (time_t)-62135596801,
.civil = "0-12-31 23:59:59 Sun 366",
.tm = {
.tm_sec = 59,
@ -185,7 +185,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 0,
.tm_yday = 365,
} },
{ .unix = (time_t)-62135596800,
{ .ux = (time_t)-62135596800,
.civil = "1-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
@ -197,7 +197,7 @@ static const struct timeutil_test_data tests[] = {
.tm_wday = 1,
.tm_yday = 0,
} },
{ .unix = (time_t)-62135596800,
{ .ux = (time_t)-62135596800,
.civil = "1-01-01 00:00:00 Mon 001",
.tm = {
.tm_sec = 0,
@ -214,7 +214,7 @@ static const struct timeutil_test_data tests[] = {
static void test_time32_errno_clear(void)
{
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
.unix = 0,
.ux = 0,
.civil = "1970-01-01 00:00:00 Thu 001",
.tm = {
.tm_sec = 0,
@ -230,9 +230,9 @@ static void test_time32_errno_clear(void)
errno = EINVAL;
time_t unix = timeutil_timegm(&tp->tm);
time_t ux = timeutil_timegm(&tp->tm);
zassert_equal(unix, tp->unix,
zassert_equal(ux, tp->ux,
"conversion incorrect");
zassert_equal(errno, 0,
"errno was not cleared");
@ -241,7 +241,7 @@ static void test_time32_errno_clear(void)
static void test_time32_epochm1(void)
{
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
.unix = -1,
.ux = -1,
.civil = "1969-12-31 23:59:59 Wed 365",
.tm = {
.tm_sec = 59,
@ -257,9 +257,9 @@ static void test_time32_epochm1(void)
errno = EINVAL;
time_t unix = timeutil_timegm(&tp->tm);
time_t ux = timeutil_timegm(&tp->tm);
zassert_equal(unix, tp->unix,
zassert_equal(ux, tp->ux,
"conversion incorrect");
zassert_equal(errno, 0,
"final errno state bad");
@ -286,9 +286,9 @@ static void test_time32_underflow(void)
"fullscale failed");
errno = 0;
time_t unix = timeutil_timegm(&tp->tm);
time_t ux = timeutil_timegm(&tp->tm);
zassert_equal(unix, -1,
zassert_equal(ux, -1,
"underflow undetected");
zassert_equal(errno, ERANGE,
"final errno state bad");
@ -315,9 +315,9 @@ static void test_time32_overflow(void)
"fullscale failed");
errno = 0;
time_t unix = timeutil_timegm(&tp->tm);
time_t ux = timeutil_timegm(&tp->tm);
zassert_equal(unix, -1,
zassert_equal(ux, -1,
"overflow undetected");
zassert_equal(errno, ERANGE,
"final errno state bad");

View file

@ -0,0 +1,10 @@
common:
tags: timeutils
type: unit
tests:
lib.os.timeutil:
extra_args: M64_MODE=0
lib.os.timeutil_64bit:
extra_args: M64_MODE=1

View file

@ -11,7 +11,7 @@
#include <sys/timeutil.h>
struct timeutil_test_data {
time_t unix;
time_t ux;
const char *civil;
struct tm tm;
};