Wed, 07 Dec 2022 20:11:44 +0100
tests for compare functions
src/compare.c | file | annotate | diff | comparison | revisions | |
src/cx/compare.h | file | annotate | diff | comparison | revisions | |
test/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
test/test_compare.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/compare.c Sat Nov 26 16:58:41 2022 +0100 1.2 +++ b/src/compare.c Wed Dec 07 20:11:44 2022 +0100 1.3 @@ -161,9 +161,12 @@ 1.4 } 1.5 } 1.6 1.7 -int cx_cmp_double(void const *d1, void const *d2) { 1.8 - double a = *((const double*) d1); 1.9 - double b = *((const double*) d2); 1.10 +int cx_cmp_double( 1.11 + void const *d1, 1.12 + void const *d2 1.13 +) { 1.14 + double a = *((const double *) d1); 1.15 + double b = *((const double *) d2); 1.16 if (fabs(a - b) < 1e-14) { 1.17 return 0; 1.18 } else { 1.19 @@ -171,12 +174,29 @@ 1.20 } 1.21 } 1.22 1.23 -int cx_cmp_ptr(void const *ptr1, void const *ptr2) { 1.24 - const intptr_t p1 = (const intptr_t) ptr1; 1.25 - const intptr_t p2 = (const intptr_t) ptr2; 1.26 +int cx_cmp_intptr( 1.27 + void const *ptr1, 1.28 + void const *ptr2 1.29 +) { 1.30 + intptr_t p1 = *(const intptr_t *) ptr1; 1.31 + intptr_t p2 = *(const intptr_t *) ptr2; 1.32 if (p1 == p2) { 1.33 return 0; 1.34 } else { 1.35 - return p1 < p2 ? -1 : 1; 1.36 + return p1 < p2 ? -1 : 1; 1.37 } 1.38 } 1.39 + 1.40 +int cx_cmp_uintptr( 1.41 + void const *ptr1, 1.42 + void const *ptr2 1.43 +) { 1.44 + uintptr_t p1 = *(const uintptr_t *) ptr1; 1.45 + uintptr_t p2 = *(const uintptr_t *) ptr2; 1.46 + if (p1 == p2) { 1.47 + return 0; 1.48 + } else { 1.49 + return p1 < p2 ? -1 : 1; 1.50 + } 1.51 +} 1.52 +
2.1 --- a/src/cx/compare.h Sat Nov 26 16:58:41 2022 +0100 2.2 +++ b/src/cx/compare.h Wed Dec 07 20:11:44 2022 +0100 2.3 @@ -180,17 +180,36 @@ 2.4 * @return -1, if *d1 is less than *d2, 0 if both are equal, 2.5 * 1 if *d1 is greater than *d2 2.6 */ 2.7 -int cx_cmp_double(void const *d1, void const *d2); 2.8 +int cx_cmp_double( 2.9 + void const *d1, 2.10 + void const *d2 2.11 +); 2.12 2.13 /** 2.14 - * Compares two pointers. 2.15 + * Compares the integer representation of two pointers. 2.16 * 2.17 - * @param ptr1 pointer one 2.18 - * @param ptr2 pointer two 2.19 - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, 2.20 - * 1 if ptr1 is greater than ptr2 2.21 + * @param ptr1 pointer to pointer one (intptr_t const*) 2.22 + * @param ptr2 pointer to pointer two (intptr_t const*) 2.23 + * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, 2.24 + * 1 if *ptr1 is greater than *ptr2 2.25 */ 2.26 -int cx_cmp_ptr(void const *ptr1, void const *ptr2); 2.27 +int cx_cmp_intptr( 2.28 + void const *ptr1, 2.29 + void const *ptr2 2.30 +); 2.31 + 2.32 +/** 2.33 + * Compares the unsigned integer representation of two pointers. 2.34 + * 2.35 + * @param ptr1 pointer to pointer one (uintptr_t const*) 2.36 + * @param ptr2 pointer to pointer two (uintptr_t const*) 2.37 + * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, 2.38 + * 1 if *ptr1 is greater than *ptr2 2.39 + */ 2.40 +int cx_cmp_uintptr( 2.41 + void const *ptr1, 2.42 + void const *ptr2 2.43 +); 2.44 2.45 #ifdef __cplusplus 2.46 } // extern "C"
3.1 --- a/test/CMakeLists.txt Sat Nov 26 16:58:41 2022 +0100 3.2 +++ b/test/CMakeLists.txt Wed Dec 07 20:11:44 2022 +0100 3.3 @@ -15,6 +15,7 @@ 3.4 3.5 add_executable(ucxtest 3.6 test_allocator.cpp 3.7 + test_compare.cpp 3.8 test_string.cpp 3.9 test_buffer.cpp 3.10 test_list.cpp
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/test_compare.cpp Wed Dec 07 20:11:44 2022 +0100 4.3 @@ -0,0 +1,127 @@ 4.4 +/* 4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 + * 4.7 + * Copyright 2022 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/compare.h" 4.33 + 4.34 +#include <gtest/gtest.h> 4.35 + 4.36 +template<typename T> 4.37 +static void test_compare( 4.38 + int (*fnc)( 4.39 + void const *, 4.40 + void const * 4.41 + ) 4.42 +) { 4.43 + auto m = std::numeric_limits<T>::max() / 400; 4.44 + T x, y; 4.45 + 4.46 + x = (std::is_signed_v<T> ? -3 : 3) * m; 4.47 + y = 5 * m; 4.48 + EXPECT_LT(fnc(&x, &y), 0); 4.49 + EXPECT_GT(fnc(&y, &x), 0); 4.50 + 4.51 + x = 120 * m; 4.52 + y = 348 * m; 4.53 + EXPECT_LT(fnc(&x, &y), 0); 4.54 + EXPECT_GT(fnc(&y, &x), 0); 4.55 + 4.56 + if constexpr (std::is_signed_v<T>) { 4.57 + x = -120 * m; 4.58 + y = -348 * m; 4.59 + EXPECT_GT(fnc(&x, &y), 0); 4.60 + EXPECT_LT(fnc(&y, &x), 0); 4.61 + } 4.62 + 4.63 + x = y; 4.64 + EXPECT_EQ(fnc(&x, &y), 0); 4.65 + EXPECT_EQ(fnc(&y, &x), 0); 4.66 +} 4.67 + 4.68 +TEST(Compare, Int) { 4.69 + test_compare<int>(cx_cmp_int); 4.70 +} 4.71 + 4.72 +TEST(Compare, Longint) { 4.73 + test_compare<long int>(cx_cmp_longint); 4.74 +} 4.75 + 4.76 +TEST(Compare, Longlong) { 4.77 + test_compare<long long>(cx_cmp_longlong); 4.78 +} 4.79 + 4.80 +TEST(Compare, Int16) { 4.81 + test_compare<int16_t>(cx_cmp_int16); 4.82 +} 4.83 + 4.84 +TEST(Compare, Int32) { 4.85 + test_compare<int32_t>(cx_cmp_int32); 4.86 +} 4.87 + 4.88 +TEST(Compare, Int64) { 4.89 + test_compare<int64_t>(cx_cmp_int64); 4.90 +} 4.91 + 4.92 +TEST(Compare, Uint) { 4.93 + test_compare<uint>(cx_cmp_uint); 4.94 +} 4.95 + 4.96 +TEST(Compare, Ulongint) { 4.97 + test_compare<unsigned long int>(cx_cmp_ulongint); 4.98 +} 4.99 + 4.100 +TEST(Compare, Ulonglong) { 4.101 + test_compare<unsigned long long>(cx_cmp_ulonglong); 4.102 +} 4.103 + 4.104 +TEST(Compare, Uint16) { 4.105 + test_compare<uint16_t>(cx_cmp_uint16); 4.106 +} 4.107 + 4.108 +TEST(Compare, Uint32) { 4.109 + test_compare<uint32_t>(cx_cmp_uint32); 4.110 +} 4.111 + 4.112 +TEST(Compare, Uint64) { 4.113 + test_compare<uint64_t>(cx_cmp_uint64); 4.114 +} 4.115 + 4.116 +TEST(Compare, Float) { 4.117 + test_compare<float>(cx_cmp_float); 4.118 +} 4.119 + 4.120 +TEST(Compare, Double) { 4.121 + test_compare<double>(cx_cmp_double); 4.122 +} 4.123 + 4.124 +TEST(Compare, IntPtr) { 4.125 + test_compare<intptr_t>(cx_cmp_intptr); 4.126 +} 4.127 + 4.128 +TEST(Compare, UintPtr) { 4.129 + test_compare<uintptr_t>(cx_cmp_uintptr); 4.130 +}