test_strtodur.c (1088B)
- // SPDX-FileCopyrightText: 2025 Haelwenn (lanodan) Monnier <contact+cmd-timer@hacktivis.me>
- // SPDX-License-Identifier: MIT
- #define _POSIX_C_SOURCE 200809L
- #include "timer.h"
- #include <assert.h>
- #include <stdio.h>
- int counter = 0;
- int err = 0;
- static void
- t(char *str, unsigned long exp)
- {
- int id = ++counter;
- unsigned long res = strtodur(str);
- if(res == exp)
- {
- printf("ok %d - %s -> %ld\n", id, str, res);
- return;
- }
- err = 1;
- printf("not ok %d - %s -> %ld\n", id, str, res);
- printf("# Expected: %ld\n", exp);
- }
- int
- main()
- {
- int plan = 13;
- printf("1..%d\n", plan);
- #define T_MIN 60
- #define T_HOUR 60 * T_MIN
- #define T_DAY 24 * T_HOUR
- t(NULL, 0);
- t("", 0);
- t("5", 5);
- t("5s", 5);
- t("5m", 5 * T_MIN);
- t("5m5s", (5 * T_MIN) + 5);
- t("5h", 5 * T_HOUR);
- t("5h5m", (5 * T_HOUR) + (5 * T_MIN));
- t("5h5m5s", (5 * T_HOUR) + (5 * T_MIN) + 5);
- t("5d", 5 * T_DAY);
- t("5d5h", (5 * T_DAY) + (5 * T_HOUR));
- t("5d5h5m", (5 * T_DAY) + (5 * T_HOUR) + (5 * T_MIN));
- t("5d5h5m5s", (5 * T_DAY) + (5 * T_HOUR) + (5 * T_MIN) + 5);
- assert(counter == plan);
- return err;
- }