jiridanek commented on a change in pull request #755:
URL: https://github.com/apache/qpid-dispatch/pull/755#discussion_r438895625



##########
File path: tests/hash_test.c
##########
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "test_case.h"
+#include <stdio.h>
+#include <string.h>
+#include <qpid/dispatch/iterator.h>
+#include <qpid/dispatch/hash.h>
+
+
+
+static const unsigned char *keys[] = {
+    (const unsigned char *) "/global/foo",
+    (const unsigned char *) "non-address key",
+    (const unsigned char *) "amqp://host/global/sub",
+    (const unsigned char *) "",
+};
+
+static const int key_count = sizeof(keys)/sizeof(keys[0]);
+
+
+// add using iterator key, lookup and remove using string key
+//
+static char *test_iter_keys(void *context)
+{
+
+    qd_hash_handle_t *handles[key_count];
+    qd_hash_t *hash = qd_hash(4, 10, false);
+    if (!hash)
+        return "hash table allocation failed";
+
+    //
+    // add using iterator, lookup and remove using string
+    //
+
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+        qd_hash_handle_t **handle = &handles[i];
+
+        qd_iterator_t *i_key = qd_iterator_string((const char *)key, 
ITER_VIEW_ALL);
+        if (qd_hash_insert(hash, i_key, (void *)key, handle) != QD_ERROR_NONE)
+            return "hash table insert failed";
+
+        if (strcmp((const char *) qd_hash_key_by_handle(*handle), (const char 
*)key) != 0)
+            return "hash handle key did not match";
+
+        qd_iterator_free(i_key);
+    }
+
+    if (qd_hash_size(hash) != key_count)
+        return "hash size is incorrect";
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+        unsigned char *value;
+        if (qd_hash_retrieve_str(hash, key, (void **)&value) != QD_ERROR_NONE)
+            return "Key lookup failed";
+        if (strcmp((const char *)value, (const char *)key) != 0)
+            return "key value mismatch";
+    }
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+
+        if (qd_hash_remove_str(hash, key) != QD_ERROR_NONE)
+            return "str key remove failed";
+        qd_hash_handle_free(handles[i]);
+    }
+
+    qd_hash_free(hash);
+    return 0;
+}
+
+
+// add using string key, lookup and remove using iterator key
+//
+static char *test_str_keys(void *context)
+{
+
+    qd_hash_handle_t *handles[key_count];
+    qd_hash_t *hash = qd_hash(4, 10, false);
+    if (!hash)
+        return "hash table allocation failed";
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+        qd_hash_handle_t **handle = &handles[i];
+
+        if (qd_hash_insert_str(hash, key, (void *)key, handle) != 
QD_ERROR_NONE)
+            return "hash table insert failed";
+
+        if (strcmp((const char *) qd_hash_key_by_handle(*handle), (const char 
*)key) != 0)
+            return "hash handle key did not match";
+    }
+
+    if (qd_hash_size(hash) != key_count)
+        return "hash size is incorrect";
+
+    for (int i = 0; i < key_count; ++i) {
+        qd_iterator_t *i_key = qd_iterator_string((const char *)keys[i], 
ITER_VIEW_ALL);
+        unsigned char *value;
+        if (qd_hash_retrieve(hash, i_key, (void **)&value) != QD_ERROR_NONE)
+            return "Iterator key lookup failed";
+        if (strcmp((const char *)value, (const char *)keys[i]) != 0)
+            return "key value mismatch";
+        qd_iterator_free(i_key);
+    }
+
+    for (int i = 0; i < key_count; ++i) {
+        qd_iterator_t *i_key = qd_iterator_string((const char *)keys[i], 
ITER_VIEW_ALL);
+        if (qd_hash_remove(hash, i_key) != QD_ERROR_NONE)
+            return "str key remove failed";
+        qd_hash_handle_free(handles[i]);
+        qd_iterator_free(i_key);
+    }
+
+    qd_hash_free(hash);
+    return 0;
+}
+
+
+// test lookup and remove failures using iterators
+//
+static char *test_iter_bad(void *context)
+{
+
+    qd_hash_handle_t *handles[key_count];
+    qd_hash_t *hash = qd_hash(4, 10, false);
+    if (!hash)
+        return "hash table allocation failed";
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+        qd_hash_handle_t **handle = &handles[i];
+
+        qd_iterator_t *i_key = qd_iterator_string((const char *)key, 
ITER_VIEW_ALL);
+        if (qd_hash_insert(hash, i_key, (void *)key, handle) != QD_ERROR_NONE)
+            return "hash table insert failed";
+
+        if (strcmp((const char *) qd_hash_key_by_handle(*handle), (const char 
*)key) != 0)
+            return "hash handle key did not match";
+
+        qd_iterator_free(i_key);
+    }
+
+    if (qd_hash_size(hash) != key_count)
+        return "hash size is incorrect";
+
+    qd_iterator_t *i_key = qd_iterator_string((const char *)"I DO NOT EXIST", 
ITER_VIEW_ALL);
+    void *value = (void *)i_key;   // just a non-zero value
+
+    //  key not found
+    qd_hash_retrieve(hash, i_key, &value);   // does not return error code, 
but sets value to 0
+    if (value != 0)
+        return "expected hash retrieve to find nothing";
+
+    // remove should return error
+    if (qd_hash_remove(hash, i_key) != QD_ERROR_NOT_FOUND)
+        return "expected hash remove to fail (not found)";
+
+    qd_iterator_free(i_key);
+
+    // cleanup
+
+    for (int i = 0; i < key_count; ++i) {
+        const unsigned char *key = keys[i];
+
+        if (qd_hash_remove_str(hash, key) != QD_ERROR_NONE)
+            return "str key remove failed";
+        qd_hash_handle_free(handles[i]);
+    }
+
+    qd_hash_free(hash);
+    return 0;
+}
+
+
+// test lookup and remove failures using strings
+//
+static char *test_str_bad(void *context)
+{
+
+    qd_hash_handle_t *handles[key_count];

Review comment:
       This does not compile with clang 11, 
https://travis-ci.org/github/apache/qpid-dispatch/jobs/696018472#L1704
   
   ```/Users/travis/build/apache/qpid-dispatch/tests/hash_test.c:249:31: error: 
         variable length array folded to constant array as an extension
         [-Werror,-Wgnu-folding-constant]
       qd_hash_handle_t *handles[key_count];
                                 ^~~~~~~~~```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to