|
| 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