rsys

Basic data structures and low-level features
git clone git://git.meso-star.fr/rsys.git
Log | Files | Refs | README | LICENSE

commit f214688f6b723e0cc7633128ecde65f8b629c2d5
parent bb47b8c7080fef280782cdbc00a07a4a2f0403b6
Author: vaplv <vaplv@free.fr>
Date:   Tue, 26 Jul 2022 15:20:02 +0200

Improve time_dump function testing

Diffstat:
Msrc/test_time.c | 100++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 89 insertions(+), 11 deletions(-)

diff --git a/src/test_time.c b/src/test_time.c @@ -17,12 +17,36 @@ #include <stdlib.h> #include <string.h> +static struct time +build_time + (const int64_t days, + const int64_t hours, + const int64_t mins, + const int64_t secs, + const int64_t msecs, + const int64_t usecs, + const int64_t nsecs) +{ + struct time t; + + t.sec = days * 3600 * 24; + t.sec += hours * 3600; + t.sec += mins * 60; + t.sec += secs; + + t.nsec = msecs * 1000000; + t.nsec += usecs * 1000; + t.nsec += nsecs; + + return t; +} + int main(int argc, char** argv) { struct time start, end, res; char dump[512]; - char buf[32]; + char* tk; int64_t time = 0; int64_t i = 0; size_t dump_len; @@ -63,20 +87,74 @@ main(int argc, char** argv) CHK(time_sub(&res, &end, &end) == &res); CHK(time_val(&res, TIME_NSEC) == 0); + + res = build_time(1, 10, 55, 30, 998, 763, 314); + dump[0] = '?'; + time_dump(&res, TIME_ALL, &dump_len, dump, 0); + CHK(dump[0] = '?'); time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); - printf("%s\n", dump); - time_dump(&res, 0, NULL, dump, sizeof(dump)); + CHK(dump_len == strlen(dump)); + printf("%s\n", dump); - res.sec = 5; - res.nsec = 524198207; - time_dump(&res, TIME_ALL, &dump_len, buf, sizeof(buf)); - CHK(dump_len >= strlen(buf)); - printf("%s\n", buf); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "998")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "763")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "314")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsecs")); + CHK((tk = strtok(NULL, " ")) == NULL); - time_dump(&res, TIME_ALL, &dump_len, dump, sizeof(dump)); - CHK(dump_len >= strlen(dump)); - printf("%s\n", dump); + /* Check string truncation */ + time_dump(&res, TIME_ALL, NULL, dump, dump_len - 27 + 1/*null char*/); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "hours")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "55")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "secs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "99")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, TIME_MIN|TIME_MSEC, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "2095")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "mins")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "30998")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, 0, &dump_len, dump, sizeof(dump)); + CHK(dump_len == 0 && dump[0] == '\0'); + res = build_time(0, 0, 0, 0, 0, 0, 0); + time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "nsec")); + CHK((tk = strtok(NULL, " ")) == NULL); + + time_dump(&res, TIME_DAY, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "0")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "day")); + CHK((tk = strtok(NULL, " ")) == NULL); + + res = build_time(0, 0, 1, 0, 235, 10, 0); + time_dump(&res, TIME_ALL, NULL, dump, sizeof(dump)); + CHK((tk = strtok(dump, " ")) && !strcmp(tk, "1")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "min")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "235")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "msecs")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "10")); + CHK((tk = strtok(NULL, " ")) && !strcmp(tk, "usecs")); + CHK((tk = strtok(NULL, " ")) == NULL); return 0; }