migrate hash_key tests - relates to #342

Wed, 20 Dec 2023 18:13:30 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 20 Dec 2023 18:13:30 +0100
changeset 768
0e1cf2cd500e
parent 767
d31f4d4075dc
child 769
b53e0e003d7e

migrate hash_key tests - relates to #342

src/cx/test.h file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/selftest.cpp file | annotate | diff | comparison | revisions
tests/test_hash_key.c file | annotate | diff | comparison | revisions
tests/test_hash_key.cpp file | annotate | diff | comparison | revisions
tests/test_utils.c file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
--- a/src/cx/test.h	Wed Dec 20 17:57:18 2023 +0100
+++ b/src/cx/test.h	Wed Dec 20 18:13:30 2023 +0100
@@ -220,7 +220,7 @@
     char total[80];
     int len = snprintf(
             total, 80,
-            "  Total:   %u\n  Success: %u\n  Failure: %u\n",
+            "  Total:   %u\n  Success: %u\n  Failure: %u\n\n",
             suite->success + suite->failure, suite->success, suite->failure
     );
     out_writer(total, 1, len, out_target);
--- a/tests/Makefile	Wed Dec 20 17:57:18 2023 +0100
+++ b/tests/Makefile	Wed Dec 20 18:13:30 2023 +0100
@@ -27,7 +27,7 @@
 
 TEST_DIR=$(build_dir)/tests
 
-SRC = test_utils.c ucxtest.c
+SRC = test_utils.c test_hash_key.c ucxtest.c
 
 OBJ_EXT=.o
 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT))
@@ -45,6 +45,12 @@
 
 FORCE:
 
+$(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \
+ ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \
+ ../src/cx/allocator.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(TEST_DIR)/test_map_generics$(OBJ_EXT): test_map_generics.c \
  test_map_generics.h ../src/cx/map.h ../src/cx/common.h \
  ../src/cx/collection.h ../src/cx/allocator.h ../src/cx/iterator.h \
--- a/tests/selftest.cpp	Wed Dec 20 17:57:18 2023 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,39 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <gtest/gtest.h>
-#include <cx/common.h>
-
-TEST(SelfTest, BasicAssertion) {
-    EXPECT_EQ(7 * 6, 42);
-}
-
-TEST(SelfTest, UcxVersion) {
-    EXPECT_GE(UCX_VERSION_MAJOR, 3);
-    EXPECT_GE(UCX_VERSION, 3 << 16);
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_hash_key.c	Wed Dec 20 18:13:30 2023 +0100
@@ -0,0 +1,97 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/test.h"
+
+#include "cx/hash_key.h"
+#include "cx/string.h"
+
+CX_TEST(test_hash_key_functions) {
+    char const* str = "my key";
+    size_t len = strlen(str);
+
+    CxHashKey str_key = cx_hash_key_str(str);
+    CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*)str, len);
+    CxHashKey obj_key = cx_hash_key(str, len);
+    CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str));
+
+    CX_TEST_DO {
+        CX_TEST_ASSERT(str_key.hash == bytes_key.hash);
+        CX_TEST_ASSERT(obj_key.hash == bytes_key.hash);
+        CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash);
+        CX_TEST_ASSERT(str_key.len == len);
+        CX_TEST_ASSERT(cxstr_key.len == len);
+        CX_TEST_ASSERT(bytes_key.len == len);
+        CX_TEST_ASSERT(bytes_key.len == len);
+        CX_TEST_ASSERT(str_key.data == str);
+    }
+}
+
+CX_TEST(test_hash_key_empty_string) {
+    char const* str = "";
+
+    CxHashKey str_key = cx_hash_key_str(str);
+    CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*) str, 0);
+    CxHashKey obj_key = cx_hash_key(str, 0);
+
+    CX_TEST_DO {
+        CX_TEST_ASSERT(bytes_key.hash == 4152238450u);
+        CX_TEST_ASSERT(str_key.hash == 4152238450u);
+        CX_TEST_ASSERT(obj_key.hash == 4152238450u);
+        CX_TEST_ASSERT(str_key.len == 0);
+        CX_TEST_ASSERT(bytes_key.len == 0);
+        CX_TEST_ASSERT(bytes_key.len == 0);
+        CX_TEST_ASSERT(str_key.data == str);
+    }
+}
+
+CX_TEST(test_hash_key_null) {
+    CxHashKey str_key = cx_hash_key_str(NULL);
+    CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0);
+    CxHashKey obj_key = cx_hash_key(NULL, 0);
+
+    CX_TEST_DO {
+        CX_TEST_ASSERT(bytes_key.hash == 1574210520u);
+        CX_TEST_ASSERT(str_key.hash == 1574210520u);
+        CX_TEST_ASSERT(obj_key.hash == 1574210520u);
+        CX_TEST_ASSERT(str_key.len == 0);
+        CX_TEST_ASSERT(bytes_key.len == 0);
+        CX_TEST_ASSERT(bytes_key.len == 0);
+        CX_TEST_ASSERT(str_key.data == NULL);
+    }
+}
+
+CxTestSuite *cx_test_suite_hash_key(void) {
+    CxTestSuite *suite = cx_test_suite_new("hash_key");
+
+    cx_test_register(suite, test_hash_key_functions);
+    cx_test_register(suite, test_hash_key_empty_string);
+    cx_test_register(suite, test_hash_key_null);
+
+    return suite;
+}
--- a/tests/test_hash_key.cpp	Wed Dec 20 17:57:18 2023 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,85 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/hash_key.h"
-#include "cx/string.h"
-
-#include <gtest/gtest.h>
-
-TEST(cx_hash_key, functions) {
-    auto str = "my key";
-    auto len = strlen(str);
-
-    auto str_key = cx_hash_key_str(str);
-    auto bytes_key = cx_hash_key_bytes(
-            reinterpret_cast<unsigned char const *>(str), len);
-    auto obj_key = cx_hash_key(
-            reinterpret_cast<void const *>(str), len);
-    auto cxstr_key = cx_hash_key_cxstr(cx_str(str));
-
-    EXPECT_EQ(str_key.hash, bytes_key.hash);
-    EXPECT_EQ(obj_key.hash, bytes_key.hash);
-    EXPECT_EQ(cxstr_key.hash, bytes_key.hash);
-    EXPECT_EQ(str_key.len, len);
-    EXPECT_EQ(cxstr_key.len, len);
-    EXPECT_EQ(bytes_key.len, len);
-    EXPECT_EQ(bytes_key.len, len);
-    EXPECT_EQ(str_key.data, str);
-}
-
-TEST(cx_hash_key, empty_string) {
-    auto str = "";
-
-    auto str_key = cx_hash_key_str(str);
-    auto bytes_key = cx_hash_key_bytes(
-            reinterpret_cast<unsigned char const *>(str), 0);
-    auto obj_key = cx_hash_key(
-            reinterpret_cast<void const *>(str), 0);
-
-    EXPECT_EQ(bytes_key.hash, 4152238450u);
-    EXPECT_EQ(str_key.hash, 4152238450u);
-    EXPECT_EQ(obj_key.hash, 4152238450u);
-    EXPECT_EQ(str_key.len, 0);
-    EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(str_key.data, str);
-}
-
-TEST(cx_hash_key, null_ptr) {
-    auto str_key = cx_hash_key_str(nullptr);
-    auto bytes_key = cx_hash_key_bytes(nullptr, 0);
-    auto obj_key = cx_hash_key(nullptr, 0);
-
-    EXPECT_EQ(bytes_key.hash, 1574210520u);
-    EXPECT_EQ(str_key.hash, 1574210520u);
-    EXPECT_EQ(obj_key.hash, 1574210520u);
-    EXPECT_EQ(str_key.len, 0);
-    EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(bytes_key.len, 0);
-    EXPECT_EQ(str_key.data, nullptr);
-}
--- a/tests/test_utils.c	Wed Dec 20 17:57:18 2023 +0100
+++ b/tests/test_utils.c	Wed Dec 20 18:13:30 2023 +0100
@@ -252,5 +252,3 @@
 
     return suite;
 }
-
-
--- a/tests/ucxtest.c	Wed Dec 20 17:57:18 2023 +0100
+++ b/tests/ucxtest.c	Wed Dec 20 18:13:30 2023 +0100
@@ -29,6 +29,7 @@
 #include "cx/test.h"
 
 CxTestSuite *cx_test_suite_utils(void);
+CxTestSuite *cx_test_suite_hash_key(void);
 
 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure
 
@@ -39,17 +40,20 @@
 
     // create test suites
     CxTestSuite
-    *utils = cx_test_suite_utils();
+    *utils = cx_test_suite_utils(),
+    *hash_key = cx_test_suite_hash_key();
 
     // run tests
     run_tests(utils);
+    run_tests(hash_key);
 
     // print overall result
-    printf("\n\n*** OVERALL RESULT ***\n");
+    printf("=== OVERALL RESULT ===\n");
     printf("  Total:   %u\n  Success: %u\n  Failure: %u\n",
            success + failure, success, failure);
 
     cx_test_suite_free(utils);
+    cx_test_suite_free(hash_key);
 
     return failure > 0 ? 1 : 0;
 }

mercurial