Fri, 23 May 2025 12:44:24 +0200
make test-compile depend on both static and shared
the shared lib is not needed for the tests,
but when run with coverage, gcov will be confused
when outdated line information is available from
a previous shared build
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2021 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. */ /** * @file compare.h * @brief A collection of simple compare functions. * @author Mike Becker * @author Olaf Wintermann * @copyright 2-Clause BSD License */ #ifndef UCX_COMPARE_H #define UCX_COMPARE_H #include "common.h" #ifdef __cplusplus extern "C" { #endif /** * A comparator function comparing two arbitrary values. * * All functions from compare.h with the cx_cmp prefix are * compatible with this signature and can be used as * compare function for collections, or other implementations * that need to be type-agnostic. * * For simple comparisons the cx_vcmp family of functions * can be used, but they are NOT compatible with this function * pointer. */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export typedef int (*cx_compare_func)( const void *left, const void *right ); /** * Compares two integers of type int. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to integer one * @param i2 pointer to integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_int(const void *i1, const void *i2); /** * Compares two integers of type int. * * @param i1 integer one * @param i2 integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_int(int i1, int i2); /** * Compares two integers of type long int. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to long integer one * @param i2 pointer to long integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_longint(const void *i1, const void *i2); /** * Compares two integers of type long int. * * @param i1 long integer one * @param i2 long integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_longint(long int i1, long int i2); /** * Compares two integers of type long long. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to long long one * @param i2 pointer to long long two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_longlong(const void *i1, const void *i2); /** * Compares two integers of type long long. * * @param i1 long long int one * @param i2 long long int two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_longlong(long long int i1, long long int i2); /** * Compares two integers of type int16_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to int16_t one * @param i2 pointer to int16_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_int16(const void *i1, const void *i2); /** * Compares two integers of type int16_t. * * @param i1 int16_t one * @param i2 int16_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_int16(int16_t i1, int16_t i2); /** * Compares two integers of type int32_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to int32_t one * @param i2 pointer to int32_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_int32(const void *i1, const void *i2); /** * Compares two integers of type int32_t. * * @param i1 int32_t one * @param i2 int32_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_int32(int32_t i1, int32_t i2); /** * Compares two integers of type int64_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to int64_t one * @param i2 pointer to int64_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_int64(const void *i1, const void *i2); /** * Compares two integers of type int64_t. * * @param i1 int64_t one * @param i2 int64_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_int64(int64_t i1, int64_t i2); /** * Compares two integers of type unsigned int. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to unsigned integer one * @param i2 pointer to unsigned integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_uint(const void *i1, const void *i2); /** * Compares two integers of type unsigned int. * * @param i1 unsigned integer one * @param i2 unsigned integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_uint(unsigned int i1, unsigned int i2); /** * Compares two integers of type unsigned long int. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to unsigned long integer one * @param i2 pointer to unsigned long integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_ulongint(const void *i1, const void *i2); /** * Compares two integers of type unsigned long int. * * @param i1 unsigned long integer one * @param i2 unsigned long integer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_ulongint(unsigned long int i1, unsigned long int i2); /** * Compares two integers of type unsigned long long. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to unsigned long long one * @param i2 pointer to unsigned long long two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_ulonglong(const void *i1, const void *i2); /** * Compares two integers of type unsigned long long. * * @param i1 unsigned long long one * @param i2 unsigned long long two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_ulonglong(unsigned long long int i1, unsigned long long int i2); /** * Compares two integers of type uint16_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to uint16_t one * @param i2 pointer to uint16_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_uint16(const void *i1, const void *i2); /** * Compares two integers of type uint16_t. * * @param i1 uint16_t one * @param i2 uint16_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_uint16(uint16_t i1, uint16_t i2); /** * Compares two integers of type uint32_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to uint32_t one * @param i2 pointer to uint32_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_uint32(const void *i1, const void *i2); /** * Compares two integers of type uint32_t. * * @param i1 uint32_t one * @param i2 uint32_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_uint32(uint32_t i1, uint32_t i2); /** * Compares two integers of type uint64_t. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param i1 pointer to uint64_t one * @param i2 pointer to uint64_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_uint64(const void *i1, const void *i2); /** * Compares two integers of type uint64_t. * * @param i1 uint64_t one * @param i2 uint64_t two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_uint64(uint64_t i1, uint64_t i2); /** * Compares two real numbers of type float with precision 1e-6f. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param f1 pointer to float one * @param f2 pointer to float two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_float(const void *f1, const void *f2); /** * Compares two real numbers of type float with precision 1e-6f. * * @param f1 float one * @param f2 float two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_float(float f1, float f2); /** * Compares two real numbers of type double with precision 1e-14. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param d1 pointer to double one * @param d2 pointer to double two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_double(const void *d1, const void *d2); /** * Compares two real numbers of type double with precision 1e-14. * * @param d1 double one * @param d2 double two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_double(double d1, double d2); /** * Compares the integer representation of two pointers. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param ptr1 pointer to pointer one (const intptr_t*) * @param ptr2 pointer to pointer two (const intptr_t*) * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_intptr(const void *ptr1, const void *ptr2); /** * Compares the integer representation of two pointers. * * @param ptr1 pointer one * @param ptr2 pointer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_intptr(intptr_t ptr1, intptr_t ptr2); /** * Compares the unsigned integer representation of two pointers. * * @note the parameters deliberately have type @c void* to be * compatible with #cx_compare_func without the need of a cast. * * @param ptr1 pointer to pointer one (const uintptr_t*) * @param ptr2 pointer to pointer two (const uintptr_t*) * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_uintptr(const void *ptr1, const void *ptr2); /** * Compares the unsigned integer representation of two pointers. * * @param ptr1 pointer one * @param ptr2 pointer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nodiscard cx_attr_export int cx_vcmp_uintptr(uintptr_t ptr1, uintptr_t ptr2); /** * Compares the pointers specified in the arguments without dereferencing. * * @param ptr1 pointer one * @param ptr2 pointer two * @retval -1 if the left argument is less than the right argument * @retval 0 if both arguments are equal * @retval 1 if the left argument is greater than the right argument */ cx_attr_nonnull cx_attr_nodiscard cx_attr_export int cx_cmp_ptr(const void *ptr1, const void *ptr2); #ifdef __cplusplus } // extern "C" #endif #endif //UCX_COMPARE_H