From a1438efe2733d00d54fea4bbe288402453978379 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Fri, 23 Aug 2024 05:23:56 -0700 Subject: [PATCH] testsuite: add unit test for hostlist --- src/liblsd/Makefile.am | 16 ++++++++++++ src/liblsd/test/hostlist.c | 52 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/liblsd/test/hostlist.c diff --git a/src/liblsd/Makefile.am b/src/liblsd/Makefile.am index d56fa88b..29ad099e 100644 --- a/src/liblsd/Makefile.am +++ b/src/liblsd/Makefile.am @@ -16,3 +16,19 @@ liblsd_la_SOURCES = \ hash.h \ cbuf.c \ cbuf.h + +TESTS = \ + test_hostlist.t + +check_PROGRAMS = $(TESTS) + +TEST_EXTENSIONS = .t +T_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \ + $(top_srcdir)/config/tap-driver.sh + +test_hostlist_t_CPPFLAGS = \ + -I$(top_srcdir)/src/libtap +test_hostlist_t_SOURCES = test/hostlist.c +test_hostlist_t_LDADD = \ + $(builddir)/liblsd.la \ + $(top_builddir)/src/libtap/libtap.la diff --git a/src/liblsd/test/hostlist.c b/src/liblsd/test/hostlist.c new file mode 100644 index 00000000..fc751dc9 --- /dev/null +++ b/src/liblsd/test/hostlist.c @@ -0,0 +1,52 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "tap.h" +#include "hostlist.h" + +void lsd_fatal_error(char *file, int line, char *mesg) +{ + BAIL_OUT ("%s::%d: %s", file, line, mesg); +} + +void *lsd_nomem_error(char *file, int line, char *mesg) +{ + BAIL_OUT ("%s: out of memory", mesg); + return NULL; +} + +static void test_issue197 (void) +{ + const char *t1 = "pclstr[201-203]-p"; + const char *hosts[] = { "pclstr201-p", "pclstr202-p", "pclstr203-p" }; + hostlist_t hl; + hostlist_iterator_t itr; + char *host; + + hl = hostlist_create (t1); + itr = hostlist_iterator_create (hl); + if (!hl || !itr) + BAIL_OUT ("cannot continue without hostlist and iterator"); + ok (hl != NULL, + "decoded %s", t1); + for (int i = 0; i < 3; i++) { + host = hostlist_next (itr); + ok (host && !strcmp (host, hosts[i]), + "host[%d] is %s", i, hosts[i]); + free (host); + } + hostlist_iterator_destroy (itr); + hostlist_destroy (hl); +} + +int main (int argc, char *argv[]) +{ + plan (NO_PLAN); + test_issue197 (); + done_testing (); +} + +// vi: ts=4 sw=4 expandtab