tests/test_compare.cpp

Mon, 18 Dec 2023 16:04:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 16:04:21 +0100
changeset 762
4523f6d42512
parent 653
e081643aae2a
permissions
-rw-r--r--

add cx_cmp_ptr() - fix #340

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2022 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/compare.h"
    31 #include <gtest/gtest.h>
    33 template<typename T>
    34 static void test_compare(
    35         int (*fnc)(
    36                 void const *,
    37                 void const *
    38         )
    39 ) {
    40     auto m = std::numeric_limits<T>::max() / 400;
    41     T x, y;
    43     x = (std::is_signed_v<T> ? -3 : 3) * m;
    44     y = 5 * m;
    45     EXPECT_LT(fnc(&x, &y), 0);
    46     EXPECT_GT(fnc(&y, &x), 0);
    48     x = 120 * m;
    49     y = 348 * m;
    50     EXPECT_LT(fnc(&x, &y), 0);
    51     EXPECT_GT(fnc(&y, &x), 0);
    53     if constexpr (std::is_signed_v<T>) {
    54         x = -120 * m;
    55         y = -348 * m;
    56         EXPECT_GT(fnc(&x, &y), 0);
    57         EXPECT_LT(fnc(&y, &x), 0);
    58     }
    60     x = y;
    61     EXPECT_EQ(fnc(&x, &y), 0);
    62     EXPECT_EQ(fnc(&y, &x), 0);
    63 }
    65 TEST(Compare, Int) {
    66     test_compare<int>(cx_cmp_int);
    67 }
    69 TEST(Compare, Longint) {
    70     test_compare<long int>(cx_cmp_longint);
    71 }
    73 TEST(Compare, Longlong) {
    74     test_compare<long long>(cx_cmp_longlong);
    75 }
    77 TEST(Compare, Int16) {
    78     test_compare<int16_t>(cx_cmp_int16);
    79 }
    81 TEST(Compare, Int32) {
    82     test_compare<int32_t>(cx_cmp_int32);
    83 }
    85 TEST(Compare, Int64) {
    86     test_compare<int64_t>(cx_cmp_int64);
    87 }
    89 TEST(Compare, Uint) {
    90     test_compare<unsigned int>(cx_cmp_uint);
    91 }
    93 TEST(Compare, Ulongint) {
    94     test_compare<unsigned long int>(cx_cmp_ulongint);
    95 }
    97 TEST(Compare, Ulonglong) {
    98     test_compare<unsigned long long>(cx_cmp_ulonglong);
    99 }
   101 TEST(Compare, Uint16) {
   102     test_compare<uint16_t>(cx_cmp_uint16);
   103 }
   105 TEST(Compare, Uint32) {
   106     test_compare<uint32_t>(cx_cmp_uint32);
   107 }
   109 TEST(Compare, Uint64) {
   110     test_compare<uint64_t>(cx_cmp_uint64);
   111 }
   113 TEST(Compare, Float) {
   114     test_compare<float>(cx_cmp_float);
   115 }
   117 TEST(Compare, Double) {
   118     test_compare<double>(cx_cmp_double);
   119 }
   121 TEST(Compare, IntPtr) {
   122     test_compare<intptr_t>(cx_cmp_intptr);
   123 }
   125 TEST(Compare, UintPtr) {
   126     test_compare<uintptr_t>(cx_cmp_uintptr);
   127 }
   129 TEST(Compare, Ptr) {
   130     int data[3];
   131     EXPECT_EQ(0, cx_cmp_ptr(data, data));
   132     EXPECT_EQ(-1, cx_cmp_ptr(&data[0], &data[1]));
   133     EXPECT_EQ(-1, cx_cmp_ptr(&data[1], &data[2]));
   134     EXPECT_EQ(1, cx_cmp_ptr(&data[2], &data[1]));
   135     EXPECT_EQ(1, cx_cmp_ptr(&data[1], data));
   136     EXPECT_EQ(0, cx_cmp_ptr(&data[1], &data[1]));
   137 }

mercurial