# HG changeset patch # User Mike Becker # Date 1670440304 -3600 # Node ID 406376e64fd84671d72c3b77705ec05a30f68308 # Parent ac5e7f789048d7482c218bef227b5822e05f41ec tests for compare functions diff -r ac5e7f789048 -r 406376e64fd8 src/compare.c --- 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; + } +} + diff -r ac5e7f789048 -r 406376e64fd8 src/cx/compare.h --- 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" diff -r ac5e7f789048 -r 406376e64fd8 test/CMakeLists.txt --- 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 diff -r ac5e7f789048 -r 406376e64fd8 test/test_compare.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 + +template +static void test_compare( + int (*fnc)( + void const *, + void const * + ) +) { + auto m = std::numeric_limits::max() / 400; + T x, y; + + x = (std::is_signed_v ? -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) { + 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(cx_cmp_int); +} + +TEST(Compare, Longint) { + test_compare(cx_cmp_longint); +} + +TEST(Compare, Longlong) { + test_compare(cx_cmp_longlong); +} + +TEST(Compare, Int16) { + test_compare(cx_cmp_int16); +} + +TEST(Compare, Int32) { + test_compare(cx_cmp_int32); +} + +TEST(Compare, Int64) { + test_compare(cx_cmp_int64); +} + +TEST(Compare, Uint) { + test_compare(cx_cmp_uint); +} + +TEST(Compare, Ulongint) { + test_compare(cx_cmp_ulongint); +} + +TEST(Compare, Ulonglong) { + test_compare(cx_cmp_ulonglong); +} + +TEST(Compare, Uint16) { + test_compare(cx_cmp_uint16); +} + +TEST(Compare, Uint32) { + test_compare(cx_cmp_uint32); +} + +TEST(Compare, Uint64) { + test_compare(cx_cmp_uint64); +} + +TEST(Compare, Float) { + test_compare(cx_cmp_float); +} + +TEST(Compare, Double) { + test_compare(cx_cmp_double); +} + +TEST(Compare, IntPtr) { + test_compare(cx_cmp_intptr); +} + +TEST(Compare, UintPtr) { + test_compare(cx_cmp_uintptr); +}