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 |
--- a/src/compare.c Sat Nov 26 16:58:41 2022 +0100 +++ b/src/compare.c Wed Dec 07 20:11:44 2022 +0100 @@ -161,9 +161,12 @@ } } -int cx_cmp_double(void const *d1, void const *d2) { - double a = *((const double*) d1); - double b = *((const double*) d2); +int cx_cmp_double( + void const *d1, + void const *d2 +) { + double a = *((const double *) d1); + double b = *((const double *) d2); if (fabs(a - b) < 1e-14) { return 0; } else { @@ -171,12 +174,29 @@ } } -int cx_cmp_ptr(void const *ptr1, void const *ptr2) { - const intptr_t p1 = (const intptr_t) ptr1; - const intptr_t p2 = (const intptr_t) ptr2; +int cx_cmp_intptr( + void const *ptr1, + void const *ptr2 +) { + intptr_t p1 = *(const intptr_t *) ptr1; + intptr_t p2 = *(const intptr_t *) ptr2; if (p1 == p2) { return 0; } else { - return p1 < p2 ? -1 : 1; + return p1 < p2 ? -1 : 1; } } + +int cx_cmp_uintptr( + void const *ptr1, + void const *ptr2 +) { + uintptr_t p1 = *(const uintptr_t *) ptr1; + uintptr_t p2 = *(const uintptr_t *) ptr2; + if (p1 == p2) { + return 0; + } else { + return p1 < p2 ? -1 : 1; + } +} +
--- a/src/cx/compare.h Sat Nov 26 16:58:41 2022 +0100 +++ b/src/cx/compare.h Wed Dec 07 20:11:44 2022 +0100 @@ -180,17 +180,36 @@ * @return -1, if *d1 is less than *d2, 0 if both are equal, * 1 if *d1 is greater than *d2 */ -int cx_cmp_double(void const *d1, void const *d2); +int cx_cmp_double( + void const *d1, + void const *d2 +); /** - * Compares two pointers. + * Compares the integer representation of two pointers. * - * @param ptr1 pointer one - * @param ptr2 pointer two - * @return -1 if ptr1 is less than ptr2, 0 if both are equal, - * 1 if ptr1 is greater than ptr2 + * @param ptr1 pointer to pointer one (intptr_t const*) + * @param ptr2 pointer to pointer two (intptr_t const*) + * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, + * 1 if *ptr1 is greater than *ptr2 */ -int cx_cmp_ptr(void const *ptr1, void const *ptr2); +int cx_cmp_intptr( + void const *ptr1, + void const *ptr2 +); + +/** + * Compares the unsigned integer representation of two pointers. + * + * @param ptr1 pointer to pointer one (uintptr_t const*) + * @param ptr2 pointer to pointer two (uintptr_t const*) + * @return -1 if *ptr1 is less than *ptr2, 0 if both are equal, + * 1 if *ptr1 is greater than *ptr2 + */ +int cx_cmp_uintptr( + void const *ptr1, + void const *ptr2 +); #ifdef __cplusplus } // extern "C"
--- a/test/CMakeLists.txt Sat Nov 26 16:58:41 2022 +0100 +++ b/test/CMakeLists.txt Wed Dec 07 20:11:44 2022 +0100 @@ -15,6 +15,7 @@ add_executable(ucxtest test_allocator.cpp + test_compare.cpp test_string.cpp test_buffer.cpp test_list.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/test_compare.cpp Wed Dec 07 20:11:44 2022 +0100 @@ -0,0 +1,127 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2022 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/compare.h" + +#include <gtest/gtest.h> + +template<typename T> +static void test_compare( + int (*fnc)( + void const *, + void const * + ) +) { + auto m = std::numeric_limits<T>::max() / 400; + T x, y; + + x = (std::is_signed_v<T> ? -3 : 3) * m; + y = 5 * m; + EXPECT_LT(fnc(&x, &y), 0); + EXPECT_GT(fnc(&y, &x), 0); + + x = 120 * m; + y = 348 * m; + EXPECT_LT(fnc(&x, &y), 0); + EXPECT_GT(fnc(&y, &x), 0); + + if constexpr (std::is_signed_v<T>) { + x = -120 * m; + y = -348 * m; + EXPECT_GT(fnc(&x, &y), 0); + EXPECT_LT(fnc(&y, &x), 0); + } + + x = y; + EXPECT_EQ(fnc(&x, &y), 0); + EXPECT_EQ(fnc(&y, &x), 0); +} + +TEST(Compare, Int) { + test_compare<int>(cx_cmp_int); +} + +TEST(Compare, Longint) { + test_compare<long int>(cx_cmp_longint); +} + +TEST(Compare, Longlong) { + test_compare<long long>(cx_cmp_longlong); +} + +TEST(Compare, Int16) { + test_compare<int16_t>(cx_cmp_int16); +} + +TEST(Compare, Int32) { + test_compare<int32_t>(cx_cmp_int32); +} + +TEST(Compare, Int64) { + test_compare<int64_t>(cx_cmp_int64); +} + +TEST(Compare, Uint) { + test_compare<uint>(cx_cmp_uint); +} + +TEST(Compare, Ulongint) { + test_compare<unsigned long int>(cx_cmp_ulongint); +} + +TEST(Compare, Ulonglong) { + test_compare<unsigned long long>(cx_cmp_ulonglong); +} + +TEST(Compare, Uint16) { + test_compare<uint16_t>(cx_cmp_uint16); +} + +TEST(Compare, Uint32) { + test_compare<uint32_t>(cx_cmp_uint32); +} + +TEST(Compare, Uint64) { + test_compare<uint64_t>(cx_cmp_uint64); +} + +TEST(Compare, Float) { + test_compare<float>(cx_cmp_float); +} + +TEST(Compare, Double) { + test_compare<double>(cx_cmp_double); +} + +TEST(Compare, IntPtr) { + test_compare<intptr_t>(cx_cmp_intptr); +} + +TEST(Compare, UintPtr) { + test_compare<uintptr_t>(cx_cmp_uintptr); +}