rcuja: fix delete
[userspace-rcu.git] / tests / test_urcu_ja.c
index 7c6190daa485429ab896e7db58b2db5c87895dd7..dde3572faf76a0201a9bf4d1430cbab77a62e37a 100644 (file)
@@ -123,14 +123,14 @@ void rcu_copy_mutex_unlock(void)
        }
 }
 
-#if 0
 void free_node_cb(struct rcu_head *head)
 {
        struct ja_test_node *node =
-               caa_container_of(head, struct ja_test_node, head);
+               caa_container_of(head, struct ja_test_node, node.head);
        free(node);
 }
 
+#if 0
 static
 void test_delete_all_nodes(struct cds_lfht *ht)
 {
@@ -207,11 +207,11 @@ int test_8bit_key(void)
 
        printf("Test #2: successful key lookup (8-bit).\n");
        for (key = 0; key < 200; key++) {
-               struct cds_hlist_head *head;
+               struct cds_hlist_head head;
 
                rcu_read_lock();
                head = cds_ja_lookup(test_ja, key);
-               if (!head) {
+               if (cds_hlist_empty(&head)) {
                        fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
                        assert(0);
                }
@@ -220,11 +220,11 @@ int test_8bit_key(void)
        printf("OK\n");
        printf("Test #3: unsuccessful key lookup (8-bit).\n");
        for (key = 200; key < 240; key++) {
-               struct cds_hlist_head *head;
+               struct cds_hlist_head head;
 
                rcu_read_lock();
                head = cds_ja_lookup(test_ja, key);
-               if (head) {
+               if (!cds_hlist_empty(&head)) {
                        fprintf(stderr,
                                "Error unexpected lookup node %" PRIu64 "\n",
                                key);
@@ -233,8 +233,34 @@ int test_8bit_key(void)
                rcu_read_unlock();
        }
        printf("OK\n");
+       printf("Test #4: remove keys (8-bit).\n");
+       for (key = 0; key < 200; key++) {
+               struct cds_hlist_head head;
+               struct ja_test_node *node;
+
+               rcu_read_lock();
+               head = cds_ja_lookup(test_ja, key);
+               node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
+               if (!node) {
+                       fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+                       assert(0);
+               }
+               ret = cds_ja_del(test_ja, key, &node->node);
+               if (ret) {
+                       fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
+                       assert(0);
+               }
+               call_rcu(&node->node.head, free_node_cb);
+               head = cds_ja_lookup(test_ja, key);
+               if (!cds_hlist_empty(&head)) {
+                       fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
+                       assert(0);
+               }
+               rcu_read_unlock();
+       }
+       printf("OK\n");
 
-       ret = cds_ja_destroy(test_ja);
+       ret = cds_ja_destroy(test_ja, free_node_cb);
        if (ret) {
                fprintf(stderr, "Error destroying judy array\n");
                return -1;
@@ -257,8 +283,8 @@ int test_16bit_key(void)
 
        /* Add keys */
        printf("Test #1: add keys (16-bit).\n");
-       //for (key = 0; key < 10000; key++) {
-       for (key = 0; key < 65536; key+=256) {
+       for (key = 0; key < 10000; key++) {
+       //for (key = 0; key < 65536; key+=256) {
                struct ja_test_node *node =
                        calloc(sizeof(*node), 1);
 
@@ -275,13 +301,13 @@ int test_16bit_key(void)
        printf("OK\n");
 
        printf("Test #2: successful key lookup (16-bit).\n");
-       //for (key = 0; key < 10000; key++) {
-       for (key = 0; key < 65536; key+=256) {
-               struct cds_hlist_head *head;
+       for (key = 0; key < 10000; key++) {
+       //for (key = 0; key < 65536; key+=256) {
+               struct cds_hlist_head head;
 
                rcu_read_lock();
                head = cds_ja_lookup(test_ja, key);
-               if (!head) {
+               if (cds_hlist_empty(&head)) {
                        fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
                        assert(0);
                }
@@ -290,11 +316,11 @@ int test_16bit_key(void)
        printf("OK\n");
        printf("Test #3: unsuccessful key lookup (16-bit).\n");
        for (key = 11000; key <= 11002; key++) {
-               struct cds_hlist_head *head;
+               struct cds_hlist_head head;
 
                rcu_read_lock();
                head = cds_ja_lookup(test_ja, key);
-               if (head) {
+               if (!cds_hlist_empty(&head)) {
                        fprintf(stderr,
                                "Error unexpected lookup node %" PRIu64 "\n",
                                key);
@@ -303,8 +329,35 @@ int test_16bit_key(void)
                rcu_read_unlock();
        }
        printf("OK\n");
+       printf("Test #4: remove keys (16-bit).\n");
+       for (key = 0; key < 10000; key++) {
+       //for (key = 0; key < 65536; key+=256) {
+               struct cds_hlist_head head;
+               struct ja_test_node *node;
+
+               rcu_read_lock();
+               head = cds_ja_lookup(test_ja, key);
+               node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
+               if (!node) {
+                       fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+                       assert(0);
+               }
+               ret = cds_ja_del(test_ja, key, &node->node);
+               if (ret) {
+                       fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
+                       assert(0);
+               }
+               call_rcu(&node->node.head, free_node_cb);
+               head = cds_ja_lookup(test_ja, key);
+               if (!cds_hlist_empty(&head)) {
+                       fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
+                       assert(0);
+               }
+               rcu_read_unlock();
+       }
+       printf("OK\n");
 
-       ret = cds_ja_destroy(test_ja);
+       ret = cds_ja_destroy(test_ja, free_node_cb);
        if (ret) {
                fprintf(stderr, "Error destroying judy array\n");
                return -1;
@@ -356,11 +409,11 @@ int test_sparse_key(unsigned int bits)
        printf("Test #2: successful key lookup (%u-bit).\n", bits);
        zerocount = 0;
        for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
-               struct cds_hlist_head *head;
+               struct cds_hlist_head head;
 
                rcu_read_lock();
                head = cds_ja_lookup(test_ja, key);
-               if (!head) {
+               if (cds_hlist_empty(&head)) {
                        fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
                        assert(0);
                }
@@ -373,11 +426,11 @@ int test_sparse_key(unsigned int bits)
                printf("Test #3: unsuccessful key lookup (%u-bit).\n", bits);
                zerocount = 0;
                for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
-                       struct cds_hlist_head *head;
+                       struct cds_hlist_head head;
 
                        rcu_read_lock();
                        head = cds_ja_lookup(test_ja, key + 42);
-                       if (head) {
+                       if (!cds_hlist_empty(&head)) {
                                fprintf(stderr,
                                        "Error unexpected lookup node %" PRIu64 "\n",
                                        key + 42);
@@ -389,8 +442,37 @@ int test_sparse_key(unsigned int bits)
                }
                printf("OK\n");
        }
+       printf("Test #4: remove keys (16-bit).\n");
+       zerocount = 0;
+       for (key = 0; key <= max_key && (key != 0 || zerocount < 1); key += 1ULL << (bits - 8)) {
+               struct cds_hlist_head head;
+               struct ja_test_node *node;
+
+               rcu_read_lock();
+               head = cds_ja_lookup(test_ja, key);
+               node = cds_hlist_first_entry_rcu(&head, struct ja_test_node, node.list);
+               if (!node) {
+                       fprintf(stderr, "Error lookup node %" PRIu64 "\n", key);
+                       assert(0);
+               }
+               ret = cds_ja_del(test_ja, key, &node->node);
+               if (ret) {
+                       fprintf(stderr, "Error (%d) removing node %" PRIu64 "\n", ret, key);
+                       assert(0);
+               }
+               call_rcu(&node->node.head, free_node_cb);
+               head = cds_ja_lookup(test_ja, key);
+               if (!cds_hlist_empty(&head)) {
+                       fprintf(stderr, "Error lookup %" PRIu64 ": %p (after delete) failed. Node is not expected.\n", key, head.next);
+                       assert(0);
+               }
+               rcu_read_unlock();
+               if (key == 0)
+                       zerocount++;
+       }
+       printf("OK\n");
 
-       ret = cds_ja_destroy(test_ja);
+       ret = cds_ja_destroy(test_ja, free_node_cb);
        if (ret) {
                fprintf(stderr, "Error destroying judy array\n");
                return -1;
This page took 0.028479 seconds and 4 git commands to generate.