universe@631: /* universe@631: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@631: * universe@631: * Copyright 2022 Mike Becker, Olaf Wintermann All rights reserved. universe@631: * universe@631: * Redistribution and use in source and binary forms, with or without universe@631: * modification, are permitted provided that the following conditions are met: universe@631: * universe@631: * 1. Redistributions of source code must retain the above copyright universe@631: * notice, this list of conditions and the following disclaimer. universe@631: * universe@631: * 2. Redistributions in binary form must reproduce the above copyright universe@631: * notice, this list of conditions and the following disclaimer in the universe@631: * documentation and/or other materials provided with the distribution. universe@631: * universe@631: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@631: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@631: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@631: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@631: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@631: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@631: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@631: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@631: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@631: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@631: * POSSIBILITY OF SUCH DAMAGE. universe@631: */ universe@631: universe@631: #include "cx/compare.h" universe@631: universe@631: #include universe@631: universe@631: template universe@631: static void test_compare( universe@631: int (*fnc)( universe@631: void const *, universe@631: void const * universe@631: ) universe@631: ) { universe@631: auto m = std::numeric_limits::max() / 400; universe@631: T x, y; universe@631: universe@631: x = (std::is_signed_v ? -3 : 3) * m; universe@631: y = 5 * m; universe@631: EXPECT_LT(fnc(&x, &y), 0); universe@631: EXPECT_GT(fnc(&y, &x), 0); universe@631: universe@631: x = 120 * m; universe@631: y = 348 * m; universe@631: EXPECT_LT(fnc(&x, &y), 0); universe@631: EXPECT_GT(fnc(&y, &x), 0); universe@631: universe@631: if constexpr (std::is_signed_v) { universe@631: x = -120 * m; universe@631: y = -348 * m; universe@631: EXPECT_GT(fnc(&x, &y), 0); universe@631: EXPECT_LT(fnc(&y, &x), 0); universe@631: } universe@631: universe@631: x = y; universe@631: EXPECT_EQ(fnc(&x, &y), 0); universe@631: EXPECT_EQ(fnc(&y, &x), 0); universe@631: } universe@631: universe@631: TEST(Compare, Int) { universe@631: test_compare(cx_cmp_int); universe@631: } universe@631: universe@631: TEST(Compare, Longint) { universe@631: test_compare(cx_cmp_longint); universe@631: } universe@631: universe@631: TEST(Compare, Longlong) { universe@631: test_compare(cx_cmp_longlong); universe@631: } universe@631: universe@631: TEST(Compare, Int16) { universe@631: test_compare(cx_cmp_int16); universe@631: } universe@631: universe@631: TEST(Compare, Int32) { universe@631: test_compare(cx_cmp_int32); universe@631: } universe@631: universe@631: TEST(Compare, Int64) { universe@631: test_compare(cx_cmp_int64); universe@631: } universe@631: universe@631: TEST(Compare, Uint) { universe@650: test_compare(cx_cmp_uint); universe@631: } universe@631: universe@631: TEST(Compare, Ulongint) { universe@631: test_compare(cx_cmp_ulongint); universe@631: } universe@631: universe@631: TEST(Compare, Ulonglong) { universe@631: test_compare(cx_cmp_ulonglong); universe@631: } universe@631: universe@631: TEST(Compare, Uint16) { universe@631: test_compare(cx_cmp_uint16); universe@631: } universe@631: universe@631: TEST(Compare, Uint32) { universe@631: test_compare(cx_cmp_uint32); universe@631: } universe@631: universe@631: TEST(Compare, Uint64) { universe@631: test_compare(cx_cmp_uint64); universe@631: } universe@631: universe@631: TEST(Compare, Float) { universe@631: test_compare(cx_cmp_float); universe@631: } universe@631: universe@631: TEST(Compare, Double) { universe@631: test_compare(cx_cmp_double); universe@631: } universe@631: universe@631: TEST(Compare, IntPtr) { universe@631: test_compare(cx_cmp_intptr); universe@631: } universe@631: universe@631: TEST(Compare, UintPtr) { universe@631: test_compare(cx_cmp_uintptr); universe@631: } universe@762: universe@762: TEST(Compare, Ptr) { universe@762: int data[3]; universe@762: EXPECT_EQ(0, cx_cmp_ptr(data, data)); universe@762: EXPECT_EQ(-1, cx_cmp_ptr(&data[0], &data[1])); universe@762: EXPECT_EQ(-1, cx_cmp_ptr(&data[1], &data[2])); universe@762: EXPECT_EQ(1, cx_cmp_ptr(&data[2], &data[1])); universe@762: EXPECT_EQ(1, cx_cmp_ptr(&data[1], data)); universe@762: EXPECT_EQ(0, cx_cmp_ptr(&data[1], &data[1])); universe@762: }