summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2020-11-24 22:17:44 -0800
committerGarrett D'Amore <garrett@damore.org>2020-11-24 22:17:44 -0800
commite1dc8358c8515ec456e255585e2952c6c01b62ea (patch)
tree757ed88b70c05d3ad53ec9e6355cd86384f50fa9
parent5ea2a1845f3393e91d6d102a8a89f339dd24f467 (diff)
downloadnng-e1dc8358c8515ec456e255585e2952c6c01b62ea.tar.gz
nng-e1dc8358c8515ec456e255585e2952c6c01b62ea.tar.bz2
nng-e1dc8358c8515ec456e255585e2952c6c01b62ea.zip
Convert list to new test framework; detached node fixes.
List nodes that are not part of a list should return NULL when asking for the next or previous item.
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/list.c7
-rw-r--r--src/core/list_test.c123
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/list.c102
5 files changed, 127 insertions, 107 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 2c954938..f0af8638 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -81,6 +81,7 @@ nng_test(aio_test)
nng_test(buf_size_test)
nng_test(errors_test)
nng_test(id_test)
+nng_test(list_test)
nng_test(reconnect_test)
nng_test(sock_test)
nng_test(url_test)
diff --git a/src/core/list.c b/src/core/list.c
index f489a705..65e86670 100644
--- a/src/core/list.c
+++ b/src/core/list.c
@@ -1,7 +1,6 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
// Copyright 2017 Capitar IT Group BV <info@capitar.com>
-// Copyright 2017 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -114,7 +113,7 @@ nni_list_next(const nni_list *list, void *item)
{
nni_list_node *node = NODE(list, item);
- if ((node = node->ln_next) == &list->ll_head) {
+ if (((node = node->ln_next) == &list->ll_head) || (node == NULL)) {
return (NULL);
}
return (ITEM(list, node));
@@ -125,7 +124,7 @@ nni_list_prev(const nni_list *list, void *item)
{
nni_list_node *node = NODE(list, item);
- if ((node = node->ln_prev) == &list->ll_head) {
+ if (((node = node->ln_prev) == &list->ll_head) || (node == NULL)) {
return (NULL);
}
return (ITEM(list, node));
diff --git a/src/core/list_test.c b/src/core/list_test.c
new file mode 100644
index 00000000..e121e4c5
--- /dev/null
+++ b/src/core/list_test.c
@@ -0,0 +1,123 @@
+//
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
+//
+// This software is supplied under the terms of the MIT License, a
+// copy of which should be located in the distribution where this
+// file was obtained (LICENSE.txt). A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#include "nng_impl.h"
+#include "stubs.h"
+#include <nuts.h>
+
+typedef struct {
+ int pad[2];
+ nni_list_node node_a;
+ nni_list_node node_b;
+} my_struct;
+
+static void
+test_list_init_empty(void)
+{
+ nni_list a;
+ nni_list b;
+
+ NNI_LIST_INIT(&a, my_struct, node_a);
+ NNI_LIST_INIT(&b, my_struct, node_b);
+
+ NUTS_TRUE(nni_list_first(&a) == NULL);
+ NUTS_TRUE(nni_list_last(&b) == NULL);
+
+ NUTS_TRUE(a.ll_offset == 8);
+ NUTS_TRUE(b.ll_offset == (8 + sizeof(nni_list_node)));
+}
+
+static void
+test_list_add_item(void)
+{
+ nni_list a;
+ nni_list b;
+ my_struct item;
+
+ NNI_LIST_INIT(&a, my_struct, node_a);
+ NNI_LIST_INIT(&b, my_struct, node_b);
+
+ NUTS_TRUE(nni_list_first(&a) == NULL);
+ NUTS_TRUE(nni_list_last(&b) == NULL);
+
+ NNI_LIST_NODE_INIT(&item.node_a);
+ NNI_LIST_NODE_INIT(&item.node_b);
+
+ nni_list_append(&a, &item);
+
+ // it's the first item.
+ NUTS_TRUE(nni_list_first(&a) == &item);
+
+ // it's also the last item.
+ NUTS_TRUE(nni_list_last(&a) == &item);
+
+ // and there are no other items.
+ NUTS_TRUE(nni_list_next(&a, &item) == NULL);
+ NUTS_TRUE(nni_list_prev(&a, &item) == NULL);
+
+ // not on the other list.
+ NUTS_TRUE(nni_list_first(&b) == NULL);
+ NUTS_TRUE(nni_list_last(&b) == NULL);
+
+ // removing it works
+ nni_list_remove(&a, &item);
+
+ // And that leaves the list empty.
+ NUTS_TRUE(nni_list_first(&a) == NULL);
+ NUTS_TRUE(nni_list_last(&a) == NULL);
+}
+
+static void
+test_list_two_items(void)
+{
+ nni_list a;
+ nni_list b;
+ my_struct item1;
+ my_struct item2;
+
+ NNI_LIST_INIT(&a, my_struct, node_a);
+ NNI_LIST_INIT(&b, my_struct, node_b);
+
+ NNI_LIST_NODE_INIT(&item1.node_a);
+ NNI_LIST_NODE_INIT(&item1.node_b);
+ NNI_LIST_NODE_INIT(&item2.node_a);
+ NNI_LIST_NODE_INIT(&item2.node_b);
+
+ nni_list_append(&a, &item1);
+ nni_list_append(&a, &item2);
+
+ NUTS_TRUE(nni_list_first(&a) == &item1);
+ NUTS_TRUE(nni_list_last(&a) == &item2);
+ NUTS_TRUE(nni_list_next(&a, &item1) == &item2);
+ NUTS_TRUE(nni_list_prev(&a, &item2) == &item1);
+
+ NUTS_TRUE(nni_list_next(&a, &item2) == NULL);
+ NUTS_TRUE(nni_list_prev(&a, &item1) == NULL);
+
+ // remove the first
+ nni_list_remove(&a, &item1);
+ NUTS_TRUE(nni_list_first(&a) == &item2);
+ NUTS_TRUE(nni_list_last(&a) == &item2);
+ NUTS_TRUE(nni_list_next(&a, &item2) == NULL);
+ NUTS_TRUE(nni_list_prev(&a, &item2) == NULL);
+
+ // remove the second
+ nni_list_remove(&a, &item2);
+ NUTS_TRUE(nni_list_first(&a) == NULL);
+ NUTS_TRUE(nni_list_last(&a) == NULL);
+ NUTS_TRUE(nni_list_next(&a, &item2) == NULL);
+ NUTS_TRUE(nni_list_prev(&a, &item2) == NULL);
+}
+
+NUTS_TESTS = {
+ { "list empty", test_list_init_empty },
+ { "list add one", test_list_add_item },
+ { "list add two", test_list_two_items },
+ { NULL, NULL },
+}; \ No newline at end of file
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index a662d495..e3cbc5ad 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -132,7 +132,6 @@ add_nng_test(inproc 5)
add_nng_test(ipc 5)
add_nng_test(ipcsupp 10)
add_nng_test(ipcwinsec 5)
-add_nng_test(list 5)
add_nng_test(message 5)
add_nng_test(multistress 60)
add_nng_test(nonblock 60)
diff --git a/tests/list.c b/tests/list.c
deleted file mode 100644
index b0da1196..00000000
--- a/tests/list.c
+++ /dev/null
@@ -1,102 +0,0 @@
-//
-// Copyright 2016 Garrett D'Amore <garrett@damore.org>
-//
-// This software is supplied under the terms of the MIT License, a
-// copy of which should be located in the distribution where this
-// file was obtained (LICENSE.txt). A copy of the license may also be
-// found online at https://opensource.org/licenses/MIT.
-//
-
-#include "core/list.c"
-#include "convey.h"
-#include "stubs.h"
-
-typedef struct {
- int pad[2];
- nni_list_node nodea;
- nni_list_node nodeb;
-} mystruct;
-
-TestMain("Linked Lists", {
- Convey("Given a couple lists", {
- nni_list alist;
- nni_list blist;
-
- NNI_LIST_INIT(&alist, mystruct, nodea);
- NNI_LIST_INIT(&blist, mystruct, nodeb);
-
- So(alist.ll_offset == 8);
- So(blist.ll_offset == (8 + sizeof (nni_list_node)));
-
- Convey("The list starts empty", {
- So(nni_list_first(&alist) == NULL);
- So(nni_list_last(&blist) == NULL);
- });
-
- Convey("And we can add an item", {
- mystruct item;
-
- NNI_LIST_NODE_INIT(&item.nodea);
- NNI_LIST_NODE_INIT(&item.nodeb);
-
- nni_list_append(&alist, &item);
-
- Convey("It is the first item", {
- So(nni_list_first(&alist) == &item);
- });
- Convey("It is the last item", {
- So(nni_list_last(&alist) == &item);
- });
- Convey("It is the only item", {
- So(nni_list_next(&alist, &item) == NULL);
- So(nni_list_prev(&alist, &item) == NULL);
- });
- Convey("It isn't on the other list", {
- So(nni_list_first(&blist) == NULL);
- So(nni_list_last(&blist) == NULL);
- });
- Convey("We can remove it", {
- nni_list_remove(&alist, &item);
- So(nni_list_first(&alist) == NULL);
- So(nni_list_last(&alist) == NULL);
- });
- });
-
- Convey("We can add two items", {
- mystruct item1;
- mystruct item2;
-
- NNI_LIST_NODE_INIT(&item1.nodea);
- NNI_LIST_NODE_INIT(&item1.nodeb);
- NNI_LIST_NODE_INIT(&item2.nodea);
- NNI_LIST_NODE_INIT(&item2.nodeb);
-
- nni_list_append(&alist, &item1);
- nni_list_append(&alist, &item2);
-
- So(nni_list_first(&alist) == &item1);
- So(nni_list_last(&alist) == &item2);
- So(nni_list_next(&alist, &item1) == &item2);
- So(nni_list_prev(&alist, &item2) == &item1);
-
- So(nni_list_next(&alist, &item2) == NULL);
- So(nni_list_prev(&alist, &item1) == NULL);
-
- Convey("Removing the first works", {
- nni_list_remove(&alist, &item1);
- So(nni_list_first(&alist) == &item2);
- So(nni_list_last(&alist) == &item2);
- So(nni_list_next(&alist, &item2) == NULL);
- So(nni_list_prev(&alist, &item2) == NULL);
- });
-
- Convey("Removing the second works", {
- nni_list_remove(&alist, &item2);
- So(nni_list_first(&alist) == &item1);
- So(nni_list_last(&alist) == &item1);
- So(nni_list_next(&alist, &item1) == NULL);
- So(nni_list_prev(&alist, &item1) == NULL);
- });
- });
- });
-})