Skip to content

Commit

Permalink
libidset/idset_format: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
garlick committed Dec 17, 2019
1 parent 2745f36 commit 49d5d80
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/common/libidset/test/idset.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,78 @@ void test_autogrow (void)
idset_destroy (idset);
}

void test_hostset (void)
{
struct idset *idset;
char buf[64];

ok (idset_format (buf, sizeof (buf), "[]xyz", 42) == 0
&& !strcmp (buf, "42xyz"),
"idset_format works with leading idset");
ok (idset_format (buf, sizeof (buf), "abc[]xyz", 42) == 0
&& !strcmp (buf, "abc42xyz"),
"idset_format works with mid idset");
ok (idset_format (buf, sizeof (buf), "abc[]", 42) == 0
&& !strcmp (buf, "abc42"),
"idset_format works with end idset");

errno = 0;
ok (idset_format (buf, sizeof (buf), "abc", 42) < 0
&& errno == EINVAL,
"idset_format fails with EINVAL no brackets");

errno = 0;
ok (idset_format (buf, sizeof (buf), "abc[", 42) < 0
&& errno == EINVAL,
"idset_format fails with EINVAL with no close bracket");

errno = 0;
ok (idset_format (buf, sizeof (buf), "abc]", 42) < 0
&& errno == EINVAL,
"idset_format fails with EINVAL with no open bracket");

errno = 0;
ok (idset_format (buf, sizeof (buf), "abc][", 42) < 0
&& errno == EINVAL,
"idset_format fails with EINVAL with backwards brackets");

errno = 0;
ok (idset_format (buf, 4, "abc[]", 1) < 0
&& errno == EOVERFLOW,
"idset_format fails with EOVERFLOW when buffer exhausted");

idset = idset_decode_embedded ("[0-99]xyz");
ok (idset != NULL && idset_count (idset) == 100,
"idset_decode_embedded works with leading idset");
idset_destroy (idset);

idset = idset_decode_embedded ("abc[0-99]xyz");
ok (idset != NULL && idset_count (idset) == 100,
"idset_decode_embedded works with mid idset");
idset_destroy (idset);

idset = idset_decode_embedded ("abc[0-99]");
ok (idset != NULL && idset_count (idset) == 100,
"idset_decode_embedded works with end idset");
idset_destroy (idset);

errno = 0;
ok (!idset_decode_embedded ("abc") && errno == EINVAL,
"idset_decode_embedded fails with EINVAL with no brackets");

errno = 0;
ok (!idset_decode_embedded ("abc[") && errno == EINVAL,
"idset_decode_embedded fails with EINVAL with no close bracket");

errno = 0;
ok (!idset_decode_embedded ("abc]") && errno == EINVAL,
"idset_decode_embedded fails with EINVAL with no open bracket");

errno = 0;
ok (!idset_decode_embedded ("abc][") && errno == EINVAL,
"idset_decode_embedded fails with EINVAL with backwards bracket");
}

void issue_1974(void)
{
struct idset *idset;
Expand Down Expand Up @@ -579,6 +651,7 @@ int main (int argc, char *argv[])
test_equal ();
test_copy ();
test_autogrow ();
test_hostset ();
issue_1974 ();
issue_2336 ();

Expand Down

0 comments on commit 49d5d80

Please sign in to comment.