tests/test_compare.cpp

Tue, 20 Jun 2023 19:00:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 20 Jun 2023 19:00:52 +0200
changeset 715
ab9d03750630
parent 653
e081643aae2a
child 762
4523f6d42512
permissions
-rw-r--r--

add header and footer to list of build dependencies

/*
 * 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<unsigned int>(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);
}

mercurial