Wed, 20 Dec 2023 18:13:30 +0100
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 |
1.1 --- a/src/cx/test.h Wed Dec 20 17:57:18 2023 +0100 1.2 +++ b/src/cx/test.h Wed Dec 20 18:13:30 2023 +0100 1.3 @@ -220,7 +220,7 @@ 1.4 char total[80]; 1.5 int len = snprintf( 1.6 total, 80, 1.7 - " Total: %u\n Success: %u\n Failure: %u\n", 1.8 + " Total: %u\n Success: %u\n Failure: %u\n\n", 1.9 suite->success + suite->failure, suite->success, suite->failure 1.10 ); 1.11 out_writer(total, 1, len, out_target);
2.1 --- a/tests/Makefile Wed Dec 20 17:57:18 2023 +0100 2.2 +++ b/tests/Makefile Wed Dec 20 18:13:30 2023 +0100 2.3 @@ -27,7 +27,7 @@ 2.4 2.5 TEST_DIR=$(build_dir)/tests 2.6 2.7 -SRC = test_utils.c ucxtest.c 2.8 +SRC = test_utils.c test_hash_key.c ucxtest.c 2.9 2.10 OBJ_EXT=.o 2.11 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) 2.12 @@ -45,6 +45,12 @@ 2.13 2.14 FORCE: 2.15 2.16 +$(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \ 2.17 + ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \ 2.18 + ../src/cx/allocator.h 2.19 + @echo "Compiling $<" 2.20 + $(CC) -o $@ $(CFLAGS) -c $< 2.21 + 2.22 $(TEST_DIR)/test_map_generics$(OBJ_EXT): test_map_generics.c \ 2.23 test_map_generics.h ../src/cx/map.h ../src/cx/common.h \ 2.24 ../src/cx/collection.h ../src/cx/allocator.h ../src/cx/iterator.h \
3.1 --- a/tests/selftest.cpp Wed Dec 20 17:57:18 2023 +0100 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,39 +0,0 @@ 3.4 -/* 3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 - * 3.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 3.8 - * 3.9 - * Redistribution and use in source and binary forms, with or without 3.10 - * modification, are permitted provided that the following conditions are met: 3.11 - * 3.12 - * 1. Redistributions of source code must retain the above copyright 3.13 - * notice, this list of conditions and the following disclaimer. 3.14 - * 3.15 - * 2. Redistributions in binary form must reproduce the above copyright 3.16 - * notice, this list of conditions and the following disclaimer in the 3.17 - * documentation and/or other materials provided with the distribution. 3.18 - * 3.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 - * POSSIBILITY OF SUCH DAMAGE. 3.30 - */ 3.31 - 3.32 -#include <gtest/gtest.h> 3.33 -#include <cx/common.h> 3.34 - 3.35 -TEST(SelfTest, BasicAssertion) { 3.36 - EXPECT_EQ(7 * 6, 42); 3.37 -} 3.38 - 3.39 -TEST(SelfTest, UcxVersion) { 3.40 - EXPECT_GE(UCX_VERSION_MAJOR, 3); 3.41 - EXPECT_GE(UCX_VERSION, 3 << 16); 3.42 -}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/tests/test_hash_key.c Wed Dec 20 18:13:30 2023 +0100 4.3 @@ -0,0 +1,97 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 4.8 + * 4.9 + * Redistribution and use in source and binary forms, with or without 4.10 + * modification, are permitted provided that the following conditions are met: 4.11 + * 4.12 + * 1. Redistributions of source code must retain the above copyright 4.13 + * notice, this list of conditions and the following disclaimer. 4.14 + * 4.15 + * 2. Redistributions in binary form must reproduce the above copyright 4.16 + * notice, this list of conditions and the following disclaimer in the 4.17 + * documentation and/or other materials provided with the distribution. 4.18 + * 4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 + * POSSIBILITY OF SUCH DAMAGE. 4.30 + */ 4.31 + 4.32 +#include "cx/test.h" 4.33 + 4.34 +#include "cx/hash_key.h" 4.35 +#include "cx/string.h" 4.36 + 4.37 +CX_TEST(test_hash_key_functions) { 4.38 + char const* str = "my key"; 4.39 + size_t len = strlen(str); 4.40 + 4.41 + CxHashKey str_key = cx_hash_key_str(str); 4.42 + CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*)str, len); 4.43 + CxHashKey obj_key = cx_hash_key(str, len); 4.44 + CxHashKey cxstr_key = cx_hash_key_cxstr(cx_str(str)); 4.45 + 4.46 + CX_TEST_DO { 4.47 + CX_TEST_ASSERT(str_key.hash == bytes_key.hash); 4.48 + CX_TEST_ASSERT(obj_key.hash == bytes_key.hash); 4.49 + CX_TEST_ASSERT(cxstr_key.hash == bytes_key.hash); 4.50 + CX_TEST_ASSERT(str_key.len == len); 4.51 + CX_TEST_ASSERT(cxstr_key.len == len); 4.52 + CX_TEST_ASSERT(bytes_key.len == len); 4.53 + CX_TEST_ASSERT(bytes_key.len == len); 4.54 + CX_TEST_ASSERT(str_key.data == str); 4.55 + } 4.56 +} 4.57 + 4.58 +CX_TEST(test_hash_key_empty_string) { 4.59 + char const* str = ""; 4.60 + 4.61 + CxHashKey str_key = cx_hash_key_str(str); 4.62 + CxHashKey bytes_key = cx_hash_key_bytes((unsigned char const*) str, 0); 4.63 + CxHashKey obj_key = cx_hash_key(str, 0); 4.64 + 4.65 + CX_TEST_DO { 4.66 + CX_TEST_ASSERT(bytes_key.hash == 4152238450u); 4.67 + CX_TEST_ASSERT(str_key.hash == 4152238450u); 4.68 + CX_TEST_ASSERT(obj_key.hash == 4152238450u); 4.69 + CX_TEST_ASSERT(str_key.len == 0); 4.70 + CX_TEST_ASSERT(bytes_key.len == 0); 4.71 + CX_TEST_ASSERT(bytes_key.len == 0); 4.72 + CX_TEST_ASSERT(str_key.data == str); 4.73 + } 4.74 +} 4.75 + 4.76 +CX_TEST(test_hash_key_null) { 4.77 + CxHashKey str_key = cx_hash_key_str(NULL); 4.78 + CxHashKey bytes_key = cx_hash_key_bytes(NULL, 0); 4.79 + CxHashKey obj_key = cx_hash_key(NULL, 0); 4.80 + 4.81 + CX_TEST_DO { 4.82 + CX_TEST_ASSERT(bytes_key.hash == 1574210520u); 4.83 + CX_TEST_ASSERT(str_key.hash == 1574210520u); 4.84 + CX_TEST_ASSERT(obj_key.hash == 1574210520u); 4.85 + CX_TEST_ASSERT(str_key.len == 0); 4.86 + CX_TEST_ASSERT(bytes_key.len == 0); 4.87 + CX_TEST_ASSERT(bytes_key.len == 0); 4.88 + CX_TEST_ASSERT(str_key.data == NULL); 4.89 + } 4.90 +} 4.91 + 4.92 +CxTestSuite *cx_test_suite_hash_key(void) { 4.93 + CxTestSuite *suite = cx_test_suite_new("hash_key"); 4.94 + 4.95 + cx_test_register(suite, test_hash_key_functions); 4.96 + cx_test_register(suite, test_hash_key_empty_string); 4.97 + cx_test_register(suite, test_hash_key_null); 4.98 + 4.99 + return suite; 4.100 +}
5.1 --- a/tests/test_hash_key.cpp Wed Dec 20 17:57:18 2023 +0100 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,85 +0,0 @@ 5.4 -/* 5.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5.6 - * 5.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 5.8 - * 5.9 - * Redistribution and use in source and binary forms, with or without 5.10 - * modification, are permitted provided that the following conditions are met: 5.11 - * 5.12 - * 1. Redistributions of source code must retain the above copyright 5.13 - * notice, this list of conditions and the following disclaimer. 5.14 - * 5.15 - * 2. Redistributions in binary form must reproduce the above copyright 5.16 - * notice, this list of conditions and the following disclaimer in the 5.17 - * documentation and/or other materials provided with the distribution. 5.18 - * 5.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 5.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5.29 - * POSSIBILITY OF SUCH DAMAGE. 5.30 - */ 5.31 - 5.32 -#include "cx/hash_key.h" 5.33 -#include "cx/string.h" 5.34 - 5.35 -#include <gtest/gtest.h> 5.36 - 5.37 -TEST(cx_hash_key, functions) { 5.38 - auto str = "my key"; 5.39 - auto len = strlen(str); 5.40 - 5.41 - auto str_key = cx_hash_key_str(str); 5.42 - auto bytes_key = cx_hash_key_bytes( 5.43 - reinterpret_cast<unsigned char const *>(str), len); 5.44 - auto obj_key = cx_hash_key( 5.45 - reinterpret_cast<void const *>(str), len); 5.46 - auto cxstr_key = cx_hash_key_cxstr(cx_str(str)); 5.47 - 5.48 - EXPECT_EQ(str_key.hash, bytes_key.hash); 5.49 - EXPECT_EQ(obj_key.hash, bytes_key.hash); 5.50 - EXPECT_EQ(cxstr_key.hash, bytes_key.hash); 5.51 - EXPECT_EQ(str_key.len, len); 5.52 - EXPECT_EQ(cxstr_key.len, len); 5.53 - EXPECT_EQ(bytes_key.len, len); 5.54 - EXPECT_EQ(bytes_key.len, len); 5.55 - EXPECT_EQ(str_key.data, str); 5.56 -} 5.57 - 5.58 -TEST(cx_hash_key, empty_string) { 5.59 - auto str = ""; 5.60 - 5.61 - auto str_key = cx_hash_key_str(str); 5.62 - auto bytes_key = cx_hash_key_bytes( 5.63 - reinterpret_cast<unsigned char const *>(str), 0); 5.64 - auto obj_key = cx_hash_key( 5.65 - reinterpret_cast<void const *>(str), 0); 5.66 - 5.67 - EXPECT_EQ(bytes_key.hash, 4152238450u); 5.68 - EXPECT_EQ(str_key.hash, 4152238450u); 5.69 - EXPECT_EQ(obj_key.hash, 4152238450u); 5.70 - EXPECT_EQ(str_key.len, 0); 5.71 - EXPECT_EQ(bytes_key.len, 0); 5.72 - EXPECT_EQ(bytes_key.len, 0); 5.73 - EXPECT_EQ(str_key.data, str); 5.74 -} 5.75 - 5.76 -TEST(cx_hash_key, null_ptr) { 5.77 - auto str_key = cx_hash_key_str(nullptr); 5.78 - auto bytes_key = cx_hash_key_bytes(nullptr, 0); 5.79 - auto obj_key = cx_hash_key(nullptr, 0); 5.80 - 5.81 - EXPECT_EQ(bytes_key.hash, 1574210520u); 5.82 - EXPECT_EQ(str_key.hash, 1574210520u); 5.83 - EXPECT_EQ(obj_key.hash, 1574210520u); 5.84 - EXPECT_EQ(str_key.len, 0); 5.85 - EXPECT_EQ(bytes_key.len, 0); 5.86 - EXPECT_EQ(bytes_key.len, 0); 5.87 - EXPECT_EQ(str_key.data, nullptr); 5.88 -}
6.1 --- a/tests/test_utils.c Wed Dec 20 17:57:18 2023 +0100 6.2 +++ b/tests/test_utils.c Wed Dec 20 18:13:30 2023 +0100 6.3 @@ -252,5 +252,3 @@ 6.4 6.5 return suite; 6.6 } 6.7 - 6.8 -
7.1 --- a/tests/ucxtest.c Wed Dec 20 17:57:18 2023 +0100 7.2 +++ b/tests/ucxtest.c Wed Dec 20 18:13:30 2023 +0100 7.3 @@ -29,6 +29,7 @@ 7.4 #include "cx/test.h" 7.5 7.6 CxTestSuite *cx_test_suite_utils(void); 7.7 +CxTestSuite *cx_test_suite_hash_key(void); 7.8 7.9 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure 7.10 7.11 @@ -39,17 +40,20 @@ 7.12 7.13 // create test suites 7.14 CxTestSuite 7.15 - *utils = cx_test_suite_utils(); 7.16 + *utils = cx_test_suite_utils(), 7.17 + *hash_key = cx_test_suite_hash_key(); 7.18 7.19 // run tests 7.20 run_tests(utils); 7.21 + run_tests(hash_key); 7.22 7.23 // print overall result 7.24 - printf("\n\n*** OVERALL RESULT ***\n"); 7.25 + printf("=== OVERALL RESULT ===\n"); 7.26 printf(" Total: %u\n Success: %u\n Failure: %u\n", 7.27 success + failure, success, failure); 7.28 7.29 cx_test_suite_free(utils); 7.30 + cx_test_suite_free(hash_key); 7.31 7.32 return failure > 0 ? 1 : 0; 7.33 }