aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
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.
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/list.c7
-rw-r--r--src/core/list_test.c123
3 files changed, 127 insertions, 4 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