Skip to content

Commit bd9c573

Browse files
committed
testsuite: add unit test for parse_size()
Problem: parse_size() has no tests. Add unit test.
1 parent d8e97e1 commit bd9c573

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

src/common/libutil/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ TESTS = test_sha1.t \
140140
test_timestamp.t \
141141
test_environment.t \
142142
test_basemoji.t \
143-
test_sigutil.t
143+
test_sigutil.t \
144+
test_parse_size.t
144145

145146
test_ldadd = \
146147
$(top_builddir)/src/common/libutil/libutil.la \
@@ -299,3 +300,7 @@ test_basemoji_t_LDADD = $(test_ldadd)
299300
test_sigutil_t_SOURCES = test/sigutil.c
300301
test_sigutil_t_CPPFLAGS = $(test_cppflags)
301302
test_sigutil_t_LDADD = $(test_ldadd)
303+
304+
test_parse_size_t_SOURCES = test/parse_size.c
305+
test_parse_size_t_CPPFLAGS = $(test_cppflags)
306+
test_parse_size_t_LDADD = $(test_ldadd)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/************************************************************\
2+
* Copyright 2023 Lawrence Livermore National Security, LLC
3+
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
4+
*
5+
* This file is part of the Flux resource manager framework.
6+
* For details, see https://github.com/flux-framework.
7+
*
8+
* SPDX-License-Identifier: LGPL-3.0
9+
\************************************************************/
10+
11+
#if HAVE_CONFIG_H
12+
#include "config.h"
13+
#endif
14+
15+
#include <errno.h>
16+
#include <string.h>
17+
#include <stdbool.h>
18+
#include <stdint.h>
19+
20+
#include "ccan/array_size/array_size.h"
21+
#include "src/common/libtap/tap.h"
22+
#include "src/common/libutil/parse_size.h"
23+
24+
struct entry {
25+
const char *s;
26+
uint64_t val;
27+
int errnum;
28+
};
29+
30+
const struct entry testvec[] = {
31+
// bad
32+
{ "xx", 0, EINVAL },
33+
{ "", 0, EINVAL },
34+
{ "1q", 0, EINVAL },
35+
{ "1kb", 0, EINVAL },
36+
{ "-1", 0, EINVAL },
37+
{ "1E20", 0, EOVERFLOW },
38+
{ "M", 0, EINVAL },
39+
{ "1m", 0, EINVAL },
40+
{ "1g", 0, EINVAL },
41+
{ "nan", 0, EINVAL },
42+
{ "inf", 0, EINVAL },
43+
{ "1b", 0, EINVAL },
44+
// good
45+
{ "0", 0, 0 },
46+
{ "0K", 0, 0 },
47+
{ "077", 63, 0 },
48+
{ "0xff", 255, 0 },
49+
{ "+42", 42, 0 },
50+
{ "1", 1, 0 },
51+
{ "1E2", 100, 0 },
52+
{ "4k", 4096, 0 },
53+
{ "1M", 1048576, 0 },
54+
{ "2G", 2147483648, 0 },
55+
{ "0.5k", 512, 0 },
56+
{ "4T", 4398046511104, 0 },
57+
{ "18446744073709551615", UINT64_MAX, 0 },
58+
{ " 42", 42, 0 },
59+
{ "1P", 1125899906842624, 0 },
60+
{ "0.5E", 576460752303423488, 0 },
61+
};
62+
63+
int main (int argc, char **argv)
64+
{
65+
uint64_t val;
66+
int rc;
67+
68+
plan (NO_PLAN);
69+
70+
lives_ok ({parse_size (NULL, &val);},
71+
"parse_size input=NULL doesn't crash");
72+
lives_ok ({parse_size ("x", NULL);},
73+
"parse_size value=NULL doesn't crash");
74+
75+
for (int i = 0; i < ARRAY_SIZE (testvec); i++) {
76+
val = 0;
77+
errno = 0;
78+
rc = parse_size (testvec[i].s, &val);
79+
if (testvec[i].errnum == 0) {
80+
ok (rc == 0 && val == testvec[i].val,
81+
"parse_size val=%s works", testvec[i].s);
82+
if (rc == 0 && val != testvec[i].val)
83+
diag ("got %ju", (uintmax_t)val);
84+
}
85+
else {
86+
ok (rc == -1 && errno == testvec[i].errnum,
87+
"parse_size val=%s fails with errno=%d",
88+
testvec[i].s,
89+
testvec[i].errnum);
90+
}
91+
}
92+
93+
done_testing ();
94+
95+
return 0;
96+
}
97+
98+
99+
// vi: ts=4 sw=4 expandtab

0 commit comments

Comments
 (0)