diff -r 2e6e9d9f2159 -r c9d008861178 tests/test_compare.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_compare.cpp Wed Feb 08 20:26:26 2023 +0100 @@ -0,0 +1,127 @@ +/* + * 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 + +template +static void test_compare( + int (*fnc)( + void const *, + void const * + ) +) { + auto m = std::numeric_limits::max() / 400; + T x, y; + + x = (std::is_signed_v ? -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) { + 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(cx_cmp_int); +} + +TEST(Compare, Longint) { + test_compare(cx_cmp_longint); +} + +TEST(Compare, Longlong) { + test_compare(cx_cmp_longlong); +} + +TEST(Compare, Int16) { + test_compare(cx_cmp_int16); +} + +TEST(Compare, Int32) { + test_compare(cx_cmp_int32); +} + +TEST(Compare, Int64) { + test_compare(cx_cmp_int64); +} + +TEST(Compare, Uint) { + test_compare(cx_cmp_uint); +} + +TEST(Compare, Ulongint) { + test_compare(cx_cmp_ulongint); +} + +TEST(Compare, Ulonglong) { + test_compare(cx_cmp_ulonglong); +} + +TEST(Compare, Uint16) { + test_compare(cx_cmp_uint16); +} + +TEST(Compare, Uint32) { + test_compare(cx_cmp_uint32); +} + +TEST(Compare, Uint64) { + test_compare(cx_cmp_uint64); +} + +TEST(Compare, Float) { + test_compare(cx_cmp_float); +} + +TEST(Compare, Double) { + test_compare(cx_cmp_double); +} + +TEST(Compare, IntPtr) { + test_compare(cx_cmp_intptr); +} + +TEST(Compare, UintPtr) { + test_compare(cx_cmp_uintptr); +}