universe@787: /* universe@787: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@787: * universe@787: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@787: * universe@787: * Redistribution and use in source and binary forms, with or without universe@787: * modification, are permitted provided that the following conditions are met: universe@787: * universe@787: * 1. Redistributions of source code must retain the above copyright universe@787: * notice, this list of conditions and the following disclaimer. universe@787: * universe@787: * 2. Redistributions in binary form must reproduce the above copyright universe@787: * notice, this list of conditions and the following disclaimer in the universe@787: * documentation and/or other materials provided with the distribution. universe@787: * universe@787: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@787: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@787: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@787: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@787: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@787: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@787: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@787: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@787: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@787: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@787: * POSSIBILITY OF SUCH DAMAGE. universe@787: */ universe@787: universe@787: #include "cx/test.h" universe@787: #include universe@787: #include universe@787: universe@787: #include "cx/compare.h" universe@787: universe@787: #define test_compare_gen_subroutine(T, max_number, signed_type) \ universe@787: static CX_TEST_SUBROUTINE( \ universe@787: test_sub_cmp_##T, \ universe@787: cx_compare_func fnc \ universe@787: ) { \ universe@787: T m = max_number / 512; \ universe@787: T x, y; \ universe@787: x = (signed_type ? -3 : 3) * m; \ universe@787: y = 5 * m; \ universe@787: CX_TEST_ASSERT(fnc(&x, &y) < 0); \ universe@787: CX_TEST_ASSERT(fnc(&y, &x) > 0); \ universe@787: x = 120 * m; \ universe@787: y = 348 * m; \ universe@787: CX_TEST_ASSERT(fnc(&x, &y) < 0); \ universe@787: CX_TEST_ASSERT(fnc(&y, &x) > 0); \ universe@787: if (signed_type) { \ universe@787: x = -120 * m; \ universe@787: y = -348 * m; \ universe@787: CX_TEST_ASSERT(fnc(&x, &y) > 0); \ universe@787: CX_TEST_ASSERT(fnc(&y, &x) < 0); \ universe@787: } \ universe@787: x = y; \ universe@787: CX_TEST_ASSERT(fnc(&x, &y) == 0); \ universe@787: CX_TEST_ASSERT(fnc(&y, &x) == 0); \ universe@787: } universe@787: universe@787: universe@787: // type aliases for types containing space characters universe@787: typedef long long cx_longlong; universe@787: typedef unsigned long cx_ulong; universe@787: typedef unsigned long long cx_ulonglong; universe@787: universe@787: // generate sub routines depending on the type universe@787: test_compare_gen_subroutine(int, INT_MAX, true) universe@787: test_compare_gen_subroutine(long, LONG_MAX, true) universe@787: test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true) universe@787: test_compare_gen_subroutine(int16_t, INT16_MAX, true) universe@787: test_compare_gen_subroutine(int32_t, INT32_MAX, true) universe@787: test_compare_gen_subroutine(int64_t, INT64_MAX, true) universe@787: test_compare_gen_subroutine(unsigned, UINT_MAX, false) universe@787: test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false) universe@787: test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false) universe@787: test_compare_gen_subroutine(uint16_t, UINT16_MAX, false) universe@787: test_compare_gen_subroutine(uint32_t, UINT32_MAX, false) universe@787: test_compare_gen_subroutine(uint64_t, UINT64_MAX, false) universe@787: test_compare_gen_subroutine(float, FLT_MAX, true) universe@787: test_compare_gen_subroutine(double, DBL_MAX, true) universe@787: test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true) universe@787: test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false) universe@787: universe@787: CX_TEST(test_compare_int) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int); universe@787: } universe@787: universe@787: CX_TEST(test_compare_long) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint); universe@787: } universe@787: universe@787: CX_TEST(test_compare_longlong) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong); universe@787: } universe@787: universe@787: CX_TEST(test_compare_int16_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16); universe@787: } universe@787: universe@787: CX_TEST(test_compare_int32_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32); universe@787: } universe@787: universe@787: CX_TEST(test_compare_int64_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64); universe@787: } universe@787: universe@787: CX_TEST(test_compare_unsigned) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint); universe@787: } universe@787: universe@787: CX_TEST(test_compare_ulong) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint); universe@787: } universe@787: universe@787: CX_TEST(test_compare_ulonglong) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong); universe@787: } universe@787: universe@787: CX_TEST(test_compare_uint16_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16); universe@787: } universe@787: universe@787: CX_TEST(test_compare_uint32_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32); universe@787: } universe@787: universe@787: CX_TEST(test_compare_uint64_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64); universe@787: } universe@787: universe@787: CX_TEST(test_compare_float) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float); universe@787: } universe@787: universe@787: CX_TEST(test_compare_double) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double); universe@787: } universe@787: universe@787: CX_TEST(test_compare_intptr_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr); universe@787: } universe@787: universe@787: CX_TEST(test_compare_uintptr_t) { universe@787: CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr); universe@787: } universe@787: universe@787: CX_TEST(test_compare_ptr) { universe@787: int data[3]; universe@787: CX_TEST_DO { universe@787: CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data)); universe@787: CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1])); universe@787: CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2])); universe@787: CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1])); universe@787: CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data)); universe@787: CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1])); universe@787: } universe@787: } universe@787: universe@787: CxTestSuite *cx_test_suite_compare(void) { universe@787: CxTestSuite *suite = cx_test_suite_new("compare"); universe@787: universe@787: cx_test_register(suite, test_compare_int); universe@787: cx_test_register(suite, test_compare_long); universe@787: cx_test_register(suite, test_compare_longlong); universe@787: cx_test_register(suite, test_compare_int16_t); universe@787: cx_test_register(suite, test_compare_int32_t); universe@787: cx_test_register(suite, test_compare_int64_t); universe@787: cx_test_register(suite, test_compare_unsigned); universe@787: cx_test_register(suite, test_compare_ulong); universe@787: cx_test_register(suite, test_compare_ulonglong); universe@787: cx_test_register(suite, test_compare_uint16_t); universe@787: cx_test_register(suite, test_compare_uint32_t); universe@787: cx_test_register(suite, test_compare_uint64_t); universe@787: cx_test_register(suite, test_compare_float); universe@787: cx_test_register(suite, test_compare_double); universe@787: cx_test_register(suite, test_compare_intptr_t); universe@787: cx_test_register(suite, test_compare_uintptr_t); universe@787: cx_test_register(suite, test_compare_ptr); universe@787: universe@787: return suite; universe@787: }