tests/test_compare.c

changeset 787
d0f02310aa47
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test_compare.c	Sun Dec 31 15:02:11 2023 +0100
     1.3 @@ -0,0 +1,183 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "cx/test.h"
    1.33 +#include <limits.h>
    1.34 +#include <float.h>
    1.35 +
    1.36 +#include "cx/compare.h"
    1.37 +
    1.38 +#define test_compare_gen_subroutine(T, max_number, signed_type) \
    1.39 +static CX_TEST_SUBROUTINE( \
    1.40 +        test_sub_cmp_##T, \
    1.41 +        cx_compare_func fnc \
    1.42 +) { \
    1.43 +    T m = max_number / 512; \
    1.44 +    T x, y; \
    1.45 +    x = (signed_type ? -3 : 3) * m; \
    1.46 +    y = 5 * m; \
    1.47 +    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    1.48 +    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    1.49 +    x = 120 * m; \
    1.50 +    y = 348 * m; \
    1.51 +    CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    1.52 +    CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    1.53 +    if (signed_type) { \
    1.54 +        x = -120 * m; \
    1.55 +        y = -348 * m; \
    1.56 +        CX_TEST_ASSERT(fnc(&x, &y) > 0); \
    1.57 +        CX_TEST_ASSERT(fnc(&y, &x) < 0); \
    1.58 +    } \
    1.59 +    x = y; \
    1.60 +    CX_TEST_ASSERT(fnc(&x, &y) == 0); \
    1.61 +    CX_TEST_ASSERT(fnc(&y, &x) == 0); \
    1.62 +}
    1.63 +
    1.64 +
    1.65 +// type aliases for types containing space characters
    1.66 +typedef long long cx_longlong;
    1.67 +typedef unsigned long cx_ulong;
    1.68 +typedef unsigned long long cx_ulonglong;
    1.69 +
    1.70 +// generate sub routines depending on the type
    1.71 +test_compare_gen_subroutine(int, INT_MAX, true)
    1.72 +test_compare_gen_subroutine(long, LONG_MAX, true)
    1.73 +test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true)
    1.74 +test_compare_gen_subroutine(int16_t, INT16_MAX, true)
    1.75 +test_compare_gen_subroutine(int32_t, INT32_MAX, true)
    1.76 +test_compare_gen_subroutine(int64_t, INT64_MAX, true)
    1.77 +test_compare_gen_subroutine(unsigned, UINT_MAX, false)
    1.78 +test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false)
    1.79 +test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false)
    1.80 +test_compare_gen_subroutine(uint16_t, UINT16_MAX, false)
    1.81 +test_compare_gen_subroutine(uint32_t, UINT32_MAX, false)
    1.82 +test_compare_gen_subroutine(uint64_t, UINT64_MAX, false)
    1.83 +test_compare_gen_subroutine(float, FLT_MAX, true)
    1.84 +test_compare_gen_subroutine(double, DBL_MAX, true)
    1.85 +test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true)
    1.86 +test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false)
    1.87 +
    1.88 +CX_TEST(test_compare_int) {
    1.89 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
    1.90 +}
    1.91 +
    1.92 +CX_TEST(test_compare_long) {
    1.93 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
    1.94 +}
    1.95 +
    1.96 +CX_TEST(test_compare_longlong) {
    1.97 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
    1.98 +}
    1.99 +
   1.100 +CX_TEST(test_compare_int16_t) {
   1.101 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16);
   1.102 +}
   1.103 +
   1.104 +CX_TEST(test_compare_int32_t) {
   1.105 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32);
   1.106 +}
   1.107 +
   1.108 +CX_TEST(test_compare_int64_t) {
   1.109 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64);
   1.110 +}
   1.111 +
   1.112 +CX_TEST(test_compare_unsigned) {
   1.113 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
   1.114 +}
   1.115 +
   1.116 +CX_TEST(test_compare_ulong) {
   1.117 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
   1.118 +}
   1.119 +
   1.120 +CX_TEST(test_compare_ulonglong) {
   1.121 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
   1.122 +}
   1.123 +
   1.124 +CX_TEST(test_compare_uint16_t) {
   1.125 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16);
   1.126 +}
   1.127 +
   1.128 +CX_TEST(test_compare_uint32_t) {
   1.129 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32);
   1.130 +}
   1.131 +
   1.132 +CX_TEST(test_compare_uint64_t) {
   1.133 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64);
   1.134 +}
   1.135 +
   1.136 +CX_TEST(test_compare_float) {
   1.137 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
   1.138 +}
   1.139 +
   1.140 +CX_TEST(test_compare_double) {
   1.141 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
   1.142 +}
   1.143 +
   1.144 +CX_TEST(test_compare_intptr_t) {
   1.145 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr);
   1.146 +}
   1.147 +
   1.148 +CX_TEST(test_compare_uintptr_t) {
   1.149 +    CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr);
   1.150 +}
   1.151 +
   1.152 +CX_TEST(test_compare_ptr) {
   1.153 +    int data[3];
   1.154 +    CX_TEST_DO {
   1.155 +        CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data));
   1.156 +        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1]));
   1.157 +        CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2]));
   1.158 +        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1]));
   1.159 +        CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data));
   1.160 +        CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1]));
   1.161 +    }
   1.162 +}
   1.163 +
   1.164 +CxTestSuite *cx_test_suite_compare(void) {
   1.165 +    CxTestSuite *suite = cx_test_suite_new("compare");
   1.166 +
   1.167 +    cx_test_register(suite, test_compare_int);
   1.168 +    cx_test_register(suite, test_compare_long);
   1.169 +    cx_test_register(suite, test_compare_longlong);
   1.170 +    cx_test_register(suite, test_compare_int16_t);
   1.171 +    cx_test_register(suite, test_compare_int32_t);
   1.172 +    cx_test_register(suite, test_compare_int64_t);
   1.173 +    cx_test_register(suite, test_compare_unsigned);
   1.174 +    cx_test_register(suite, test_compare_ulong);
   1.175 +    cx_test_register(suite, test_compare_ulonglong);
   1.176 +    cx_test_register(suite, test_compare_uint16_t);
   1.177 +    cx_test_register(suite, test_compare_uint32_t);
   1.178 +    cx_test_register(suite, test_compare_uint64_t);
   1.179 +    cx_test_register(suite, test_compare_float);
   1.180 +    cx_test_register(suite, test_compare_double);
   1.181 +    cx_test_register(suite, test_compare_intptr_t);
   1.182 +    cx_test_register(suite, test_compare_uintptr_t);
   1.183 +    cx_test_register(suite, test_compare_ptr);
   1.184 +
   1.185 +    return suite;
   1.186 +}

mercurial