tests/test_compare.c

Sun, 31 Dec 2023 15:02:11 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 31 Dec 2023 15:02:11 +0100
changeset 787
d0f02310aa47
permissions
-rw-r--r--

migrate compare tests - relates to #342

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/test.h"
    30 #include <limits.h>
    31 #include <float.h>
    33 #include "cx/compare.h"
    35 #define test_compare_gen_subroutine(T, max_number, signed_type) \
    36 static CX_TEST_SUBROUTINE( \
    37         test_sub_cmp_##T, \
    38         cx_compare_func fnc \
    39 ) { \
    40     T m = max_number / 512; \
    41     T x, y; \
    42     x = (signed_type ? -3 : 3) * m; \
    43     y = 5 * m; \
    44     CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    45     CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    46     x = 120 * m; \
    47     y = 348 * m; \
    48     CX_TEST_ASSERT(fnc(&x, &y) < 0); \
    49     CX_TEST_ASSERT(fnc(&y, &x) > 0); \
    50     if (signed_type) { \
    51         x = -120 * m; \
    52         y = -348 * m; \
    53         CX_TEST_ASSERT(fnc(&x, &y) > 0); \
    54         CX_TEST_ASSERT(fnc(&y, &x) < 0); \
    55     } \
    56     x = y; \
    57     CX_TEST_ASSERT(fnc(&x, &y) == 0); \
    58     CX_TEST_ASSERT(fnc(&y, &x) == 0); \
    59 }
    62 // type aliases for types containing space characters
    63 typedef long long cx_longlong;
    64 typedef unsigned long cx_ulong;
    65 typedef unsigned long long cx_ulonglong;
    67 // generate sub routines depending on the type
    68 test_compare_gen_subroutine(int, INT_MAX, true)
    69 test_compare_gen_subroutine(long, LONG_MAX, true)
    70 test_compare_gen_subroutine(cx_longlong, LLONG_MAX, true)
    71 test_compare_gen_subroutine(int16_t, INT16_MAX, true)
    72 test_compare_gen_subroutine(int32_t, INT32_MAX, true)
    73 test_compare_gen_subroutine(int64_t, INT64_MAX, true)
    74 test_compare_gen_subroutine(unsigned, UINT_MAX, false)
    75 test_compare_gen_subroutine(cx_ulong, ULONG_MAX, false)
    76 test_compare_gen_subroutine(cx_ulonglong, ULLONG_MAX, false)
    77 test_compare_gen_subroutine(uint16_t, UINT16_MAX, false)
    78 test_compare_gen_subroutine(uint32_t, UINT32_MAX, false)
    79 test_compare_gen_subroutine(uint64_t, UINT64_MAX, false)
    80 test_compare_gen_subroutine(float, FLT_MAX, true)
    81 test_compare_gen_subroutine(double, DBL_MAX, true)
    82 test_compare_gen_subroutine(intptr_t, INTPTR_MAX, true)
    83 test_compare_gen_subroutine(uintptr_t, UINTPTR_MAX, false)
    85 CX_TEST(test_compare_int) {
    86     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int, cx_cmp_int);
    87 }
    89 CX_TEST(test_compare_long) {
    90     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_long, cx_cmp_longint);
    91 }
    93 CX_TEST(test_compare_longlong) {
    94     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_longlong, cx_cmp_longlong);
    95 }
    97 CX_TEST(test_compare_int16_t) {
    98     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int16_t, cx_cmp_int16);
    99 }
   101 CX_TEST(test_compare_int32_t) {
   102     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int32_t, cx_cmp_int32);
   103 }
   105 CX_TEST(test_compare_int64_t) {
   106     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_int64_t, cx_cmp_int64);
   107 }
   109 CX_TEST(test_compare_unsigned) {
   110     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_unsigned, cx_cmp_uint);
   111 }
   113 CX_TEST(test_compare_ulong) {
   114     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulong, cx_cmp_ulongint);
   115 }
   117 CX_TEST(test_compare_ulonglong) {
   118     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_cx_ulonglong, cx_cmp_ulonglong);
   119 }
   121 CX_TEST(test_compare_uint16_t) {
   122     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint16_t, cx_cmp_uint16);
   123 }
   125 CX_TEST(test_compare_uint32_t) {
   126     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint32_t, cx_cmp_uint32);
   127 }
   129 CX_TEST(test_compare_uint64_t) {
   130     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uint64_t, cx_cmp_uint64);
   131 }
   133 CX_TEST(test_compare_float) {
   134     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_float, cx_cmp_float);
   135 }
   137 CX_TEST(test_compare_double) {
   138     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_double, cx_cmp_double);
   139 }
   141 CX_TEST(test_compare_intptr_t) {
   142     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_intptr_t, cx_cmp_intptr);
   143 }
   145 CX_TEST(test_compare_uintptr_t) {
   146     CX_TEST_DO CX_TEST_CALL_SUBROUTINE(test_sub_cmp_uintptr_t, cx_cmp_uintptr);
   147 }
   149 CX_TEST(test_compare_ptr) {
   150     int data[3];
   151     CX_TEST_DO {
   152         CX_TEST_ASSERT(0 == cx_cmp_ptr(data, data));
   153         CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[0], &data[1]));
   154         CX_TEST_ASSERT(-1 == cx_cmp_ptr(&data[1], &data[2]));
   155         CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[2], &data[1]));
   156         CX_TEST_ASSERT(1 == cx_cmp_ptr(&data[1], data));
   157         CX_TEST_ASSERT(0 == cx_cmp_ptr(&data[1], &data[1]));
   158     }
   159 }
   161 CxTestSuite *cx_test_suite_compare(void) {
   162     CxTestSuite *suite = cx_test_suite_new("compare");
   164     cx_test_register(suite, test_compare_int);
   165     cx_test_register(suite, test_compare_long);
   166     cx_test_register(suite, test_compare_longlong);
   167     cx_test_register(suite, test_compare_int16_t);
   168     cx_test_register(suite, test_compare_int32_t);
   169     cx_test_register(suite, test_compare_int64_t);
   170     cx_test_register(suite, test_compare_unsigned);
   171     cx_test_register(suite, test_compare_ulong);
   172     cx_test_register(suite, test_compare_ulonglong);
   173     cx_test_register(suite, test_compare_uint16_t);
   174     cx_test_register(suite, test_compare_uint32_t);
   175     cx_test_register(suite, test_compare_uint64_t);
   176     cx_test_register(suite, test_compare_float);
   177     cx_test_register(suite, test_compare_double);
   178     cx_test_register(suite, test_compare_intptr_t);
   179     cx_test_register(suite, test_compare_uintptr_t);
   180     cx_test_register(suite, test_compare_ptr);
   182     return suite;
   183 }

mercurial