diff options
Diffstat (limited to 'regress/unittests/conversion')
-rw-r--r-- | regress/unittests/conversion/Makefile | 10 | ||||
-rw-r--r-- | regress/unittests/conversion/tests.c | 51 |
2 files changed, 61 insertions, 0 deletions
diff --git a/regress/unittests/conversion/Makefile b/regress/unittests/conversion/Makefile new file mode 100644 index 000000000..cde97dc28 --- /dev/null +++ b/regress/unittests/conversion/Makefile | |||
@@ -0,0 +1,10 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2017/03/14 01:20:29 dtucker Exp $ | ||
2 | |||
3 | PROG=test_conversion | ||
4 | SRCS=tests.c | ||
5 | REGRESS_TARGETS=run-regress-${PROG} | ||
6 | |||
7 | run-regress-${PROG}: ${PROG} | ||
8 | env ${TEST_ENV} ./${PROG} | ||
9 | |||
10 | .include <bsd.regress.mk> | ||
diff --git a/regress/unittests/conversion/tests.c b/regress/unittests/conversion/tests.c new file mode 100644 index 000000000..6dd77ef42 --- /dev/null +++ b/regress/unittests/conversion/tests.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: tests.c,v 1.1 2017/03/14 01:20:29 dtucker 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 <sys/param.h> | ||
12 | #include <stdio.h> | ||
13 | #ifdef HAVE_STDINT_H | ||
14 | #include <stdint.h> | ||
15 | #endif | ||
16 | #include <stdlib.h> | ||
17 | #include <string.h> | ||
18 | |||
19 | #include "../test_helper/test_helper.h" | ||
20 | |||
21 | #include "misc.h" | ||
22 | |||
23 | void | ||
24 | tests(void) | ||
25 | { | ||
26 | char buf[1024]; | ||
27 | |||
28 | TEST_START("conversion_convtime"); | ||
29 | ASSERT_LONG_EQ(convtime("0"), 0); | ||
30 | ASSERT_LONG_EQ(convtime("1"), 1); | ||
31 | ASSERT_LONG_EQ(convtime("1S"), 1); | ||
32 | /* from the examples in the comment above the function */ | ||
33 | ASSERT_LONG_EQ(convtime("90m"), 5400); | ||
34 | ASSERT_LONG_EQ(convtime("1h30m"), 5400); | ||
35 | ASSERT_LONG_EQ(convtime("2d"), 172800); | ||
36 | ASSERT_LONG_EQ(convtime("1w"), 604800); | ||
37 | |||
38 | /* negative time is not allowed */ | ||
39 | ASSERT_LONG_EQ(convtime("-7"), -1); | ||
40 | ASSERT_LONG_EQ(convtime("-9d"), -1); | ||
41 | |||
42 | /* overflow */ | ||
43 | snprintf(buf, sizeof buf, "%llu", (unsigned long long)LONG_MAX + 1); | ||
44 | ASSERT_LONG_EQ(convtime(buf), -1); | ||
45 | |||
46 | /* overflow with multiplier */ | ||
47 | snprintf(buf, sizeof buf, "%lluM", (unsigned long long)LONG_MAX/60 + 1); | ||
48 | ASSERT_LONG_EQ(convtime(buf), -1); | ||
49 | ASSERT_LONG_EQ(convtime("1000000000000000000000w"), -1); | ||
50 | TEST_DONE(); | ||
51 | } | ||