test/test_compare.cpp

changeset 631
406376e64fd8
child 650
77021e06b1a8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/test_compare.cpp	Wed Dec 07 20:11:44 2022 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2022 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "cx/compare.h"
    1.33 +
    1.34 +#include <gtest/gtest.h>
    1.35 +
    1.36 +template<typename T>
    1.37 +static void test_compare(
    1.38 +        int (*fnc)(
    1.39 +                void const *,
    1.40 +                void const *
    1.41 +        )
    1.42 +) {
    1.43 +    auto m = std::numeric_limits<T>::max() / 400;
    1.44 +    T x, y;
    1.45 +
    1.46 +    x = (std::is_signed_v<T> ? -3 : 3) * m;
    1.47 +    y = 5 * m;
    1.48 +    EXPECT_LT(fnc(&x, &y), 0);
    1.49 +    EXPECT_GT(fnc(&y, &x), 0);
    1.50 +
    1.51 +    x = 120 * m;
    1.52 +    y = 348 * m;
    1.53 +    EXPECT_LT(fnc(&x, &y), 0);
    1.54 +    EXPECT_GT(fnc(&y, &x), 0);
    1.55 +
    1.56 +    if constexpr (std::is_signed_v<T>) {
    1.57 +        x = -120 * m;
    1.58 +        y = -348 * m;
    1.59 +        EXPECT_GT(fnc(&x, &y), 0);
    1.60 +        EXPECT_LT(fnc(&y, &x), 0);
    1.61 +    }
    1.62 +
    1.63 +    x = y;
    1.64 +    EXPECT_EQ(fnc(&x, &y), 0);
    1.65 +    EXPECT_EQ(fnc(&y, &x), 0);
    1.66 +}
    1.67 +
    1.68 +TEST(Compare, Int) {
    1.69 +    test_compare<int>(cx_cmp_int);
    1.70 +}
    1.71 +
    1.72 +TEST(Compare, Longint) {
    1.73 +    test_compare<long int>(cx_cmp_longint);
    1.74 +}
    1.75 +
    1.76 +TEST(Compare, Longlong) {
    1.77 +    test_compare<long long>(cx_cmp_longlong);
    1.78 +}
    1.79 +
    1.80 +TEST(Compare, Int16) {
    1.81 +    test_compare<int16_t>(cx_cmp_int16);
    1.82 +}
    1.83 +
    1.84 +TEST(Compare, Int32) {
    1.85 +    test_compare<int32_t>(cx_cmp_int32);
    1.86 +}
    1.87 +
    1.88 +TEST(Compare, Int64) {
    1.89 +    test_compare<int64_t>(cx_cmp_int64);
    1.90 +}
    1.91 +
    1.92 +TEST(Compare, Uint) {
    1.93 +    test_compare<uint>(cx_cmp_uint);
    1.94 +}
    1.95 +
    1.96 +TEST(Compare, Ulongint) {
    1.97 +    test_compare<unsigned long int>(cx_cmp_ulongint);
    1.98 +}
    1.99 +
   1.100 +TEST(Compare, Ulonglong) {
   1.101 +    test_compare<unsigned long long>(cx_cmp_ulonglong);
   1.102 +}
   1.103 +
   1.104 +TEST(Compare, Uint16) {
   1.105 +    test_compare<uint16_t>(cx_cmp_uint16);
   1.106 +}
   1.107 +
   1.108 +TEST(Compare, Uint32) {
   1.109 +    test_compare<uint32_t>(cx_cmp_uint32);
   1.110 +}
   1.111 +
   1.112 +TEST(Compare, Uint64) {
   1.113 +    test_compare<uint64_t>(cx_cmp_uint64);
   1.114 +}
   1.115 +
   1.116 +TEST(Compare, Float) {
   1.117 +    test_compare<float>(cx_cmp_float);
   1.118 +}
   1.119 +
   1.120 +TEST(Compare, Double) {
   1.121 +    test_compare<double>(cx_cmp_double);
   1.122 +}
   1.123 +
   1.124 +TEST(Compare, IntPtr) {
   1.125 +    test_compare<intptr_t>(cx_cmp_intptr);
   1.126 +}
   1.127 +
   1.128 +TEST(Compare, UintPtr) {
   1.129 +    test_compare<uintptr_t>(cx_cmp_uintptr);
   1.130 +}

mercurial