| 1 |
/* $OpenBSD: tests.c,v 1.5 2025/04/15 04:00:42 djm Exp $ */ |
| 2 |
/* |
| 3 |
* Regress test for conversions |
| 4 |
* |
| 5 |
* Placed in the public domain |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "includes.h" |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "../test_helper/test_helper.h" |
| 17 |
|
| 18 |
#include "misc.h" |
| 19 |
|
| 20 |
void |
| 21 |
tests(void) |
| 22 |
{ |
| 23 |
char buf[1024]; |
| 24 |
|
| 25 |
TEST_START("conversion_convtime"); |
| 26 |
ASSERT_INT_EQ(convtime("0"), 0); |
| 27 |
ASSERT_INT_EQ(convtime("1"), 1); |
| 28 |
ASSERT_INT_EQ(convtime("1S"), 1); |
| 29 |
/* from the examples in the comment above the function */ |
| 30 |
ASSERT_INT_EQ(convtime("90m"), 5400); |
| 31 |
ASSERT_INT_EQ(convtime("1h30m"), 5400); |
| 32 |
ASSERT_INT_EQ(convtime("2d"), 172800); |
| 33 |
ASSERT_INT_EQ(convtime("1w"), 604800); |
| 34 |
|
| 35 |
/* negative time is not allowed */ |
| 36 |
ASSERT_INT_EQ(convtime("-7"), -1); |
| 37 |
ASSERT_INT_EQ(convtime("-9d"), -1); |
| 38 |
|
| 39 |
/* overflow */ |
| 40 |
snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX); |
| 41 |
ASSERT_INT_EQ(convtime(buf), INT_MAX); |
| 42 |
snprintf(buf, sizeof buf, "%llu", (unsigned long long)INT_MAX + 1); |
| 43 |
ASSERT_INT_EQ(convtime(buf), -1); |
| 44 |
|
| 45 |
/* overflow with multiplier */ |
| 46 |
snprintf(buf, sizeof buf, "%lluM", (unsigned long long)INT_MAX/60 + 1); |
| 47 |
ASSERT_INT_EQ(convtime(buf), -1); |
| 48 |
ASSERT_INT_EQ(convtime("1000000000000000000000w"), -1); |
| 49 |
TEST_DONE(); |
| 50 |
} |
| 51 |
|
| 52 |
void |
| 53 |
benchmarks(void) |
| 54 |
{ |
| 55 |
printf("no benchmarks\n"); |
| 56 |
} |