tests/test_compare.c

Tue, 08 Oct 2024 19:52:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 08 Oct 2024 19:52:09 +0200
changeset 922
eabfbe9e2952
parent 787
d0f02310aa47
permissions
-rw-r--r--

also report coverage for header files

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2023 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/test.h"
#include <limits.h>
#include <float.h>

#include "cx/compare.h"

#define test_compare_gen_subroutine(T, max_number, signed_type) \
static CX_TEST_SUBROUTINE( \
        test_sub_cmp_##T, \
        cx_compare_func fnc \
) { \
    T m = max_number / 512; \
    T x, y; \
    x = (signed_type ? -3 : 3) * m; \
    y = 5 * m; \
    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    x = 120 * m; \
    y = 348 * m; \
    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    if (signed_type) { \
        x = -120 * m; \
        y = -348 * m; \
        CX_TEST_ASSERT(fnc(&x, &y) > 0); \
        CX_TEST_ASSERT(fnc(&y, &x) < 0); \
    } \
    x = y; \
    CX_TEST_ASSERT(fnc(&x, &y) == 0); \
    CX_TEST_ASSERT(fnc(&y, &x) == 0); \
}


// type aliases for types containing space characters
typedef long long cx_longlong;
typedef unsigned long cx_ulong;
typedef unsigned long long cx_ulonglong;

// generate sub routines depending on the type
test_compare_gen_subroutine(int, INT_MAX, true)
test_compare_gen_subroutine(long, LONG_MAX, true)
test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true)
test_compare_gen_subroutine(int16_t, INT16_MAX, true)
test_compare_gen_subroutine(int32_t, INT32_MAX, true)
test_compare_gen_subroutine(int64_t, INT64_MAX, true)
test_compare_gen_subroutine(unsigned, UINT_MAX, false)
test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false)
test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false)
test_compare_gen_subroutine(uint16_t, UINT16_MAX, false)
test_compare_gen_subroutine(uint32_t, UINT32_MAX, false)
test_compare_gen_subroutine(uint64_t, UINT64_MAX, false)
test_compare_gen_subroutine(float, FLT_MAX, true)
test_compare_gen_subroutine(double, DBL_MAX, true)
test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true)
test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false)

CX_TEST(test_compare_int) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
}

CX_TEST(test_compare_long) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
}

CX_TEST(test_compare_longlong) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
}

CX_TEST(test_compare_int16_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16);
}

CX_TEST(test_compare_int32_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32);
}

CX_TEST(test_compare_int64_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64);
}

CX_TEST(test_compare_unsigned) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
}

CX_TEST(test_compare_ulong) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
}

CX_TEST(test_compare_ulonglong) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
}

CX_TEST(test_compare_uint16_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16);
}

CX_TEST(test_compare_uint32_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32);
}

CX_TEST(test_compare_uint64_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64);
}

CX_TEST(test_compare_float) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
}

CX_TEST(test_compare_double) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
}

CX_TEST(test_compare_intptr_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr);
}

CX_TEST(test_compare_uintptr_t) {
    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr);
}

CX_TEST(test_compare_ptr) {
    int data[3];
    CX_TEST_DO {
        CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data));
        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1]));
        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2]));
        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1]));
        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data));
        CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1]));
    }
}

CxTestSuite *cx_test_suite_compare(void) {
    CxTestSuite *suite = cx_test_suite_new("compare");

    cx_test_register(suite, test_compare_int);
    cx_test_register(suite, test_compare_long);
    cx_test_register(suite, test_compare_longlong);
    cx_test_register(suite, test_compare_int16_t);
    cx_test_register(suite, test_compare_int32_t);
    cx_test_register(suite, test_compare_int64_t);
    cx_test_register(suite, test_compare_unsigned);
    cx_test_register(suite, test_compare_ulong);
    cx_test_register(suite, test_compare_ulonglong);
    cx_test_register(suite, test_compare_uint16_t);
    cx_test_register(suite, test_compare_uint32_t);
    cx_test_register(suite, test_compare_uint64_t);
    cx_test_register(suite, test_compare_float);
    cx_test_register(suite, test_compare_double);
    cx_test_register(suite, test_compare_intptr_t);
    cx_test_register(suite, test_compare_uintptr_t);
    cx_test_register(suite, test_compare_ptr);

    return suite;
}

mercurial