strtodur.c (1505B)
- // utils-std: Collection of commonly available Unix tools
- // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- // SPDX-License-Identifier: MPL-2.0
- #define _POSIX_C_SOURCE 200809L
- #include "../lib/strtodur.h"
- #include <assert.h>
- #include <stdio.h> // printf
- int counter = 0;
- int err = 0;
- static void
- t_strtodur(char *str, time_t ex_sec, long ex_nsec)
- {
- int id = ++counter;
- struct timespec dur = {.tv_sec = 0, .tv_nsec = 0};
- int ret = strtodur(str, &dur);
- if(dur.tv_sec == ex_sec && dur.tv_nsec == ex_nsec && ret == 0)
- {
- printf("ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
- id,
- str,
- dur.tv_sec,
- dur.tv_nsec);
- return;
- }
- err = 1;
- printf("not ok %d - strtodur(\"%s\", _) -> {.tv_sec = %ld, .tv_nsec = %ld}\n",
- id,
- str,
- dur.tv_sec,
- dur.tv_nsec);
- if(dur.tv_sec != ex_sec || dur.tv_nsec != ex_nsec)
- printf("# Expected: {.tv_sec = %ld, .tv_nsec = %ld}\n", ex_sec, ex_nsec);
- if(ret != 0) printf("# Exit status: %d\n", ret);
- }
- int
- main(void)
- {
- int plan = 8;
- printf("1..%d\n", plan);
- // TODO: Capture errors, say with open_memstream(3)
- t_strtodur(NULL, 0, 0);
- t_strtodur((char *)"", 0, 0);
- t_strtodur((char *)",", 0, 0);
- t_strtodur((char *)"1", 1, 0);
- t_strtodur((char *)"1.", 1, 0);
- t_strtodur((char *)"1,", 1, 0);
- t_strtodur((char *)".1", 0, 1000000000 * 0.1);
- t_strtodur((char *)"0.1", 0, 1000000000 * 0.1);
- assert(counter == plan);
- return err;
- }