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

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

mercurial