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