Sun, 31 Dec 2023 15:02:11 +0100
migrate compare tests - relates to #342
tests/Makefile | file | annotate | diff | comparison | revisions | |
tests/test_compare.c | file | annotate | diff | comparison | revisions | |
tests/test_compare.cpp | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
1.1 --- a/tests/Makefile Sun Dec 31 14:29:46 2023 +0100 1.2 +++ b/tests/Makefile Sun Dec 31 15:02:11 2023 +0100 1.3 @@ -28,6 +28,7 @@ 1.4 TEST_DIR=$(build_dir)/tests 1.5 1.6 SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \ 1.7 + test_compare.c \ 1.8 test_string.c test_printf.c test_mempool.c test_hash_map.c ucxtest.c 1.9 1.10 OBJ_EXT=.o 1.11 @@ -51,6 +52,11 @@ 1.12 @echo "Compiling $<" 1.13 $(CC) -o $@ $(CFLAGS) -c $< 1.14 1.15 +$(TEST_DIR)/test_compare$(OBJ_EXT): test_compare.c ../src/cx/test.h \ 1.16 + ../src/cx/compare.h ../src/cx/common.h 1.17 + @echo "Compiling $<" 1.18 + $(CC) -o $@ $(CFLAGS) -c $< 1.19 + 1.20 $(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \ 1.21 ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \ 1.22 ../src/cx/allocator.h
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/test_compare.c Sun Dec 31 15:02:11 2023 +0100 2.3 @@ -0,0 +1,183 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * 2.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 2.8 + * 2.9 + * Redistribution and use in source and binary forms, with or without 2.10 + * modification, are permitted provided that the following conditions are met: 2.11 + * 2.12 + * 1. Redistributions of source code must retain the above copyright 2.13 + * notice, this list of conditions and the following disclaimer. 2.14 + * 2.15 + * 2. Redistributions in binary form must reproduce the above copyright 2.16 + * notice, this list of conditions and the following disclaimer in the 2.17 + * documentation and/or other materials provided with the distribution. 2.18 + * 2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 + * POSSIBILITY OF SUCH DAMAGE. 2.30 + */ 2.31 + 2.32 +#include "cx/test.h" 2.33 +#include <limits.h> 2.34 +#include <float.h> 2.35 + 2.36 +#include "cx/compare.h" 2.37 + 2.38 +#define test_compare_gen_subroutine(T, max_number, signed_type) \ 2.39 +static CX_TEST_SUBROUTINE( \ 2.40 + test_sub_cmp_##T, \ 2.41 + cx_compare_func fnc \ 2.42 +) { \ 2.43 + T m = max_number / 512; \ 2.44 + T x, y; \ 2.45 + x = (signed_type ? -3 : 3) * m; \ 2.46 + y = 5 * m; \ 2.47 + CX_TEST_ASSERT(fnc(&x, &y) < 0); \ 2.48 + CX_TEST_ASSERT(fnc(&y, &x) > 0); \ 2.49 + x = 120 * m; \ 2.50 + y = 348 * m; \ 2.51 + CX_TEST_ASSERT(fnc(&x, &y) < 0); \ 2.52 + CX_TEST_ASSERT(fnc(&y, &x) > 0); \ 2.53 + if (signed_type) { \ 2.54 + x = -120 * m; \ 2.55 + y = -348 * m; \ 2.56 + CX_TEST_ASSERT(fnc(&x, &y) > 0); \ 2.57 + CX_TEST_ASSERT(fnc(&y, &x) < 0); \ 2.58 + } \ 2.59 + x = y; \ 2.60 + CX_TEST_ASSERT(fnc(&x, &y) == 0); \ 2.61 + CX_TEST_ASSERT(fnc(&y, &x) == 0); \ 2.62 +} 2.63 + 2.64 + 2.65 +// type aliases for types containing space characters 2.66 +typedef long long cx_longlong; 2.67 +typedef unsigned long cx_ulong; 2.68 +typedef unsigned long long cx_ulonglong; 2.69 + 2.70 +// generate sub routines depending on the type 2.71 +test_compare_gen_subroutine(int, INT_MAX, true) 2.72 +test_compare_gen_subroutine(long, LONG_MAX, true) 2.73 +test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true) 2.74 +test_compare_gen_subroutine(int16_t, INT16_MAX, true) 2.75 +test_compare_gen_subroutine(int32_t, INT32_MAX, true) 2.76 +test_compare_gen_subroutine(int64_t, INT64_MAX, true) 2.77 +test_compare_gen_subroutine(unsigned, UINT_MAX, false) 2.78 +test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false) 2.79 +test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false) 2.80 +test_compare_gen_subroutine(uint16_t, UINT16_MAX, false) 2.81 +test_compare_gen_subroutine(uint32_t, UINT32_MAX, false) 2.82 +test_compare_gen_subroutine(uint64_t, UINT64_MAX, false) 2.83 +test_compare_gen_subroutine(float, FLT_MAX, true) 2.84 +test_compare_gen_subroutine(double, DBL_MAX, true) 2.85 +test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true) 2.86 +test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false) 2.87 + 2.88 +CX_TEST(test_compare_int) { 2.89 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int); 2.90 +} 2.91 + 2.92 +CX_TEST(test_compare_long) { 2.93 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint); 2.94 +} 2.95 + 2.96 +CX_TEST(test_compare_longlong) { 2.97 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong); 2.98 +} 2.99 + 2.100 +CX_TEST(test_compare_int16_t) { 2.101 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16); 2.102 +} 2.103 + 2.104 +CX_TEST(test_compare_int32_t) { 2.105 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32); 2.106 +} 2.107 + 2.108 +CX_TEST(test_compare_int64_t) { 2.109 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64); 2.110 +} 2.111 + 2.112 +CX_TEST(test_compare_unsigned) { 2.113 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint); 2.114 +} 2.115 + 2.116 +CX_TEST(test_compare_ulong) { 2.117 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint); 2.118 +} 2.119 + 2.120 +CX_TEST(test_compare_ulonglong) { 2.121 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong); 2.122 +} 2.123 + 2.124 +CX_TEST(test_compare_uint16_t) { 2.125 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16); 2.126 +} 2.127 + 2.128 +CX_TEST(test_compare_uint32_t) { 2.129 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32); 2.130 +} 2.131 + 2.132 +CX_TEST(test_compare_uint64_t) { 2.133 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64); 2.134 +} 2.135 + 2.136 +CX_TEST(test_compare_float) { 2.137 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float); 2.138 +} 2.139 + 2.140 +CX_TEST(test_compare_double) { 2.141 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double); 2.142 +} 2.143 + 2.144 +CX_TEST(test_compare_intptr_t) { 2.145 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr); 2.146 +} 2.147 + 2.148 +CX_TEST(test_compare_uintptr_t) { 2.149 + CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr); 2.150 +} 2.151 + 2.152 +CX_TEST(test_compare_ptr) { 2.153 + int data[3]; 2.154 + CX_TEST_DO { 2.155 + CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data)); 2.156 + CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1])); 2.157 + CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2])); 2.158 + CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1])); 2.159 + CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data)); 2.160 + CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1])); 2.161 + } 2.162 +} 2.163 + 2.164 +CxTestSuite *cx_test_suite_compare(void) { 2.165 + CxTestSuite *suite = cx_test_suite_new("compare"); 2.166 + 2.167 + cx_test_register(suite, test_compare_int); 2.168 + cx_test_register(suite, test_compare_long); 2.169 + cx_test_register(suite, test_compare_longlong); 2.170 + cx_test_register(suite, test_compare_int16_t); 2.171 + cx_test_register(suite, test_compare_int32_t); 2.172 + cx_test_register(suite, test_compare_int64_t); 2.173 + cx_test_register(suite, test_compare_unsigned); 2.174 + cx_test_register(suite, test_compare_ulong); 2.175 + cx_test_register(suite, test_compare_ulonglong); 2.176 + cx_test_register(suite, test_compare_uint16_t); 2.177 + cx_test_register(suite, test_compare_uint32_t); 2.178 + cx_test_register(suite, test_compare_uint64_t); 2.179 + cx_test_register(suite, test_compare_float); 2.180 + cx_test_register(suite, test_compare_double); 2.181 + cx_test_register(suite, test_compare_intptr_t); 2.182 + cx_test_register(suite, test_compare_uintptr_t); 2.183 + cx_test_register(suite, test_compare_ptr); 2.184 + 2.185 + return suite; 2.186 +}
3.1 --- a/tests/test_compare.cpp Sun Dec 31 14:29:46 2023 +0100 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,137 +0,0 @@ 3.4 -/* 3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 - * 3.7 - * Copyright 2022 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 "cx/compare.h" 3.33 - 3.34 -#include <gtest/gtest.h> 3.35 - 3.36 -template<typename T> 3.37 -static void test_compare( 3.38 - int (*fnc)( 3.39 - void const *, 3.40 - void const * 3.41 - ) 3.42 -) { 3.43 - auto m = std::numeric_limits<T>::max() / 400; 3.44 - T x, y; 3.45 - 3.46 - x = (std::is_signed_v<T> ? -3 : 3) * m; 3.47 - y = 5 * m; 3.48 - EXPECT_LT(fnc(&x, &y), 0); 3.49 - EXPECT_GT(fnc(&y, &x), 0); 3.50 - 3.51 - x = 120 * m; 3.52 - y = 348 * m; 3.53 - EXPECT_LT(fnc(&x, &y), 0); 3.54 - EXPECT_GT(fnc(&y, &x), 0); 3.55 - 3.56 - if constexpr (std::is_signed_v<T>) { 3.57 - x = -120 * m; 3.58 - y = -348 * m; 3.59 - EXPECT_GT(fnc(&x, &y), 0); 3.60 - EXPECT_LT(fnc(&y, &x), 0); 3.61 - } 3.62 - 3.63 - x = y; 3.64 - EXPECT_EQ(fnc(&x, &y), 0); 3.65 - EXPECT_EQ(fnc(&y, &x), 0); 3.66 -} 3.67 - 3.68 -TEST(Compare, Int) { 3.69 - test_compare<int>(cx_cmp_int); 3.70 -} 3.71 - 3.72 -TEST(Compare, Longint) { 3.73 - test_compare<long int>(cx_cmp_longint); 3.74 -} 3.75 - 3.76 -TEST(Compare, Longlong) { 3.77 - test_compare<long long>(cx_cmp_longlong); 3.78 -} 3.79 - 3.80 -TEST(Compare, Int16) { 3.81 - test_compare<int16_t>(cx_cmp_int16); 3.82 -} 3.83 - 3.84 -TEST(Compare, Int32) { 3.85 - test_compare<int32_t>(cx_cmp_int32); 3.86 -} 3.87 - 3.88 -TEST(Compare, Int64) { 3.89 - test_compare<int64_t>(cx_cmp_int64); 3.90 -} 3.91 - 3.92 -TEST(Compare, Uint) { 3.93 - test_compare<unsigned int>(cx_cmp_uint); 3.94 -} 3.95 - 3.96 -TEST(Compare, Ulongint) { 3.97 - test_compare<unsigned long int>(cx_cmp_ulongint); 3.98 -} 3.99 - 3.100 -TEST(Compare, Ulonglong) { 3.101 - test_compare<unsigned long long>(cx_cmp_ulonglong); 3.102 -} 3.103 - 3.104 -TEST(Compare, Uint16) { 3.105 - test_compare<uint16_t>(cx_cmp_uint16); 3.106 -} 3.107 - 3.108 -TEST(Compare, Uint32) { 3.109 - test_compare<uint32_t>(cx_cmp_uint32); 3.110 -} 3.111 - 3.112 -TEST(Compare, Uint64) { 3.113 - test_compare<uint64_t>(cx_cmp_uint64); 3.114 -} 3.115 - 3.116 -TEST(Compare, Float) { 3.117 - test_compare<float>(cx_cmp_float); 3.118 -} 3.119 - 3.120 -TEST(Compare, Double) { 3.121 - test_compare<double>(cx_cmp_double); 3.122 -} 3.123 - 3.124 -TEST(Compare, IntPtr) { 3.125 - test_compare<intptr_t>(cx_cmp_intptr); 3.126 -} 3.127 - 3.128 -TEST(Compare, UintPtr) { 3.129 - test_compare<uintptr_t>(cx_cmp_uintptr); 3.130 -} 3.131 - 3.132 -TEST(Compare, Ptr) { 3.133 - int data[3]; 3.134 - EXPECT_EQ(0, cx_cmp_ptr(data, data)); 3.135 - EXPECT_EQ(-1, cx_cmp_ptr(&data[0], &data[1])); 3.136 - EXPECT_EQ(-1, cx_cmp_ptr(&data[1], &data[2])); 3.137 - EXPECT_EQ(1, cx_cmp_ptr(&data[2], &data[1])); 3.138 - EXPECT_EQ(1, cx_cmp_ptr(&data[1], data)); 3.139 - EXPECT_EQ(0, cx_cmp_ptr(&data[1], &data[1])); 3.140 -}
4.1 --- a/tests/ucxtest.c Sun Dec 31 14:29:46 2023 +0100 4.2 +++ b/tests/ucxtest.c Sun Dec 31 15:02:11 2023 +0100 4.3 @@ -30,6 +30,7 @@ 4.4 4.5 CxTestSuite *cx_test_suite_testing_allocator(void); 4.6 CxTestSuite *cx_test_suite_utils(void); 4.7 +CxTestSuite *cx_test_suite_compare(void); 4.8 CxTestSuite *cx_test_suite_hash_key(void); 4.9 CxTestSuite *cx_test_suite_allocator(void); 4.10 CxTestSuite *cx_test_suite_string(void); 4.11 @@ -48,6 +49,7 @@ 4.12 execute_test_suites( 4.13 cx_test_suite_testing_allocator(), 4.14 cx_test_suite_utils(), 4.15 + cx_test_suite_compare(), 4.16 cx_test_suite_hash_key(), 4.17 cx_test_suite_allocator(), 4.18 cx_test_suite_string(),