Thu, 25 Jan 2024 22:01:12 +0100
add cx_array_add() + fix type of cx_array_default_reallocator
universe@766 | 1 | /* |
universe@766 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@766 | 3 | * |
universe@766 | 4 | * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. |
universe@766 | 5 | * |
universe@766 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@766 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@766 | 8 | * |
universe@766 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@766 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@766 | 11 | * |
universe@766 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@766 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@766 | 14 | * documentation and/or other materials provided with the distribution. |
universe@766 | 15 | * |
universe@766 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@766 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@766 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@766 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@766 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@766 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@766 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@766 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@766 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@766 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@766 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@766 | 27 | */ |
universe@766 | 28 | |
universe@767 | 29 | #include "cx/test.h" |
universe@767 | 30 | |
universe@767 | 31 | #include "cx/utils.h" |
universe@767 | 32 | #include "cx/buffer.h" |
universe@767 | 33 | |
universe@767 | 34 | CX_TEST(test_stream_bncopy) { |
universe@767 | 35 | CxBuffer source, target; |
universe@767 | 36 | char sbuf[32], tbuf[32]; |
universe@767 | 37 | memset(tbuf, 0, 32); |
universe@767 | 38 | cxBufferInit(&source, sbuf, 32, NULL, 0); |
universe@767 | 39 | cxBufferInit(&target, tbuf, 32, NULL, 0); |
universe@767 | 40 | cxBufferPutString(&source, "This is a stream copy test."); |
universe@767 | 41 | cxBufferSeek(&source, 0, SEEK_SET); |
universe@767 | 42 | char tmp[4]; |
universe@767 | 43 | |
universe@767 | 44 | CX_TEST_DO { |
universe@767 | 45 | size_t result = cx_stream_bncopy(&source, &target, |
universe@767 | 46 | (cx_read_func) cxBufferRead, |
universe@767 | 47 | (cx_write_func) cxBufferWrite, |
universe@767 | 48 | tmp, 4, 20); |
universe@767 | 49 | CX_TEST_ASSERT(result == 20); |
universe@767 | 50 | CX_TEST_ASSERT(target.size == 20); |
universe@767 | 51 | CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); |
universe@767 | 52 | |
universe@767 | 53 | result = cx_stream_bcopy(&source, &target, |
universe@767 | 54 | (cx_read_func) cxBufferRead, |
universe@767 | 55 | (cx_write_func) cxBufferWrite, |
universe@767 | 56 | NULL, 16); |
universe@767 | 57 | |
universe@767 | 58 | CX_TEST_ASSERT(result == 7); |
universe@767 | 59 | CX_TEST_ASSERT(target.size == 27); |
universe@767 | 60 | CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); |
universe@767 | 61 | } |
universe@767 | 62 | |
universe@767 | 63 | cxBufferDestroy(&source); |
universe@767 | 64 | cxBufferDestroy(&target); |
universe@767 | 65 | } |
universe@767 | 66 | |
universe@767 | 67 | CX_TEST(test_stream_ncopy) { |
universe@767 | 68 | CxBuffer source, target; |
universe@767 | 69 | char sbuf[32], tbuf[32]; |
universe@767 | 70 | memset(tbuf, 0, 32); |
universe@767 | 71 | cxBufferInit(&source, sbuf, 32, NULL, 0); |
universe@767 | 72 | cxBufferInit(&target, tbuf, 32, NULL, 0); |
universe@767 | 73 | cxBufferPutString(&source, "This is a stream copy test."); |
universe@767 | 74 | cxBufferSeek(&source, 0, SEEK_SET); |
universe@767 | 75 | |
universe@767 | 76 | CX_TEST_DO { |
universe@767 | 77 | size_t result = cx_stream_ncopy(&source, &target, |
universe@767 | 78 | (cx_read_func) cxBufferRead, |
universe@767 | 79 | (cx_write_func) cxBufferWrite, |
universe@767 | 80 | 20); |
universe@767 | 81 | CX_TEST_ASSERT(result == 20); |
universe@767 | 82 | CX_TEST_ASSERT(target.size == 20); |
universe@767 | 83 | CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); |
universe@767 | 84 | |
universe@767 | 85 | result = cx_stream_copy(&source, &target, |
universe@767 | 86 | (cx_read_func) cxBufferRead, |
universe@767 | 87 | (cx_write_func) cxBufferWrite); |
universe@767 | 88 | |
universe@767 | 89 | CX_TEST_ASSERT(result == 7); |
universe@767 | 90 | CX_TEST_ASSERT(target.size == 27); |
universe@767 | 91 | CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); |
universe@767 | 92 | } |
universe@767 | 93 | |
universe@767 | 94 | cxBufferDestroy(&source); |
universe@767 | 95 | cxBufferDestroy(&target); |
universe@767 | 96 | } |
universe@767 | 97 | |
universe@767 | 98 | CX_TEST(test_forn) { |
universe@767 | 99 | unsigned j; |
universe@767 | 100 | j = 0; |
universe@767 | 101 | CX_TEST_DO { |
universe@767 | 102 | cx_for_n(i, 50) { |
universe@767 | 103 | CX_TEST_ASSERT(i == j); |
universe@767 | 104 | j++; |
universe@767 | 105 | } |
universe@767 | 106 | } |
universe@767 | 107 | } |
universe@767 | 108 | |
universe@767 | 109 | CX_TEST(test_swap_ptr) { |
universe@767 | 110 | int i = 5; |
universe@767 | 111 | int j = 8; |
universe@767 | 112 | int *ip = &i; |
universe@767 | 113 | int *jp = &j; |
universe@767 | 114 | CX_TEST_DO { |
universe@767 | 115 | cx_swap_ptr(ip, jp); |
universe@767 | 116 | CX_TEST_ASSERT(ip == &j); |
universe@767 | 117 | CX_TEST_ASSERT(jp == &i); |
universe@767 | 118 | } |
universe@767 | 119 | } |
universe@767 | 120 | |
universe@767 | 121 | CX_TEST(test_szmul) { |
universe@767 | 122 | size_t r; |
universe@767 | 123 | int e; |
universe@767 | 124 | |
universe@767 | 125 | CX_TEST_DO { |
universe@767 | 126 | e = cx_szmul(5, 7, &r); |
universe@767 | 127 | CX_TEST_ASSERT(e == 0); |
universe@767 | 128 | CX_TEST_ASSERT(r == 35); |
universe@767 | 129 | |
universe@767 | 130 | size_t s = SIZE_MAX & ~3; |
universe@767 | 131 | |
universe@767 | 132 | e = cx_szmul(s / 4, 2, &r); |
universe@767 | 133 | CX_TEST_ASSERT(e == 0); |
universe@767 | 134 | CX_TEST_ASSERT(r == s / 2); |
universe@767 | 135 | |
universe@767 | 136 | e = cx_szmul(2, s / 4, &r); |
universe@767 | 137 | CX_TEST_ASSERT(e == 0); |
universe@767 | 138 | CX_TEST_ASSERT(r == s / 2); |
universe@767 | 139 | |
universe@767 | 140 | e = cx_szmul(s / 4, 4, &r); |
universe@767 | 141 | CX_TEST_ASSERT(e == 0); |
universe@767 | 142 | CX_TEST_ASSERT(r == s); |
universe@767 | 143 | |
universe@767 | 144 | e = cx_szmul(4, s / 4, &r); |
universe@767 | 145 | CX_TEST_ASSERT(e == 0); |
universe@767 | 146 | CX_TEST_ASSERT(r == s); |
universe@767 | 147 | |
universe@767 | 148 | e = cx_szmul(s / 4, 5, &r); |
universe@767 | 149 | CX_TEST_ASSERT(e != 0); |
universe@767 | 150 | |
universe@767 | 151 | e = cx_szmul(5, s / 4, &r); |
universe@767 | 152 | CX_TEST_ASSERT(e != 0); |
universe@767 | 153 | |
universe@767 | 154 | e = cx_szmul(SIZE_MAX - 4, 0, &r); |
universe@767 | 155 | CX_TEST_ASSERT(e == 0); |
universe@767 | 156 | CX_TEST_ASSERT(r == 0); |
universe@767 | 157 | |
universe@767 | 158 | e = cx_szmul(0, SIZE_MAX - 1, &r); |
universe@767 | 159 | CX_TEST_ASSERT(e == 0); |
universe@767 | 160 | CX_TEST_ASSERT(r == 0); |
universe@767 | 161 | |
universe@767 | 162 | e = cx_szmul(SIZE_MAX, 0, &r); |
universe@767 | 163 | CX_TEST_ASSERT(e == 0); |
universe@767 | 164 | CX_TEST_ASSERT(r == 0); |
universe@767 | 165 | |
universe@767 | 166 | e = cx_szmul(0, SIZE_MAX, &r); |
universe@767 | 167 | CX_TEST_ASSERT(e == 0); |
universe@767 | 168 | CX_TEST_ASSERT(r == 0); |
universe@767 | 169 | |
universe@767 | 170 | e = cx_szmul(0, 0, &r); |
universe@767 | 171 | CX_TEST_ASSERT(e == 0); |
universe@767 | 172 | CX_TEST_ASSERT(r == 0); |
universe@767 | 173 | } |
universe@767 | 174 | } |
universe@767 | 175 | |
universe@767 | 176 | #ifdef CX_SZMUL_BUILTIN |
universe@767 | 177 | |
universe@767 | 178 | // also test the custom implementation |
universe@767 | 179 | #undef CX_SZMUL_BUILTIN |
universe@767 | 180 | |
universe@767 | 181 | #include "../src/szmul.c" |
universe@767 | 182 | |
universe@767 | 183 | #define CX_SZMUL_BUILTIN |
universe@767 | 184 | |
universe@767 | 185 | CX_TEST(test_szmul_impl) { |
universe@767 | 186 | size_t r; |
universe@767 | 187 | int e; |
universe@767 | 188 | |
universe@767 | 189 | CX_TEST_DO { |
universe@767 | 190 | e = cx_szmul_impl(5, 7, &r); |
universe@767 | 191 | CX_TEST_ASSERT(e == 0); |
universe@767 | 192 | CX_TEST_ASSERT(r == 35); |
universe@767 | 193 | |
universe@767 | 194 | size_t s = SIZE_MAX & ~3; |
universe@767 | 195 | |
universe@767 | 196 | e = cx_szmul_impl(s / 4, 2, &r); |
universe@767 | 197 | CX_TEST_ASSERT(e == 0); |
universe@767 | 198 | CX_TEST_ASSERT(r == s / 2); |
universe@767 | 199 | |
universe@767 | 200 | e = cx_szmul_impl(2, s / 4, &r); |
universe@767 | 201 | CX_TEST_ASSERT(e == 0); |
universe@767 | 202 | CX_TEST_ASSERT(r == s / 2); |
universe@767 | 203 | |
universe@767 | 204 | e = cx_szmul_impl(s / 4, 4, &r); |
universe@767 | 205 | CX_TEST_ASSERT(e == 0); |
universe@767 | 206 | CX_TEST_ASSERT(r == s); |
universe@767 | 207 | |
universe@767 | 208 | e = cx_szmul_impl(4, s / 4, &r); |
universe@767 | 209 | CX_TEST_ASSERT(e == 0); |
universe@767 | 210 | CX_TEST_ASSERT(r == s); |
universe@767 | 211 | |
universe@767 | 212 | e = cx_szmul_impl(s / 4, 5, &r); |
universe@767 | 213 | CX_TEST_ASSERT(e != 0); |
universe@767 | 214 | |
universe@767 | 215 | e = cx_szmul_impl(5, s / 4, &r); |
universe@767 | 216 | CX_TEST_ASSERT(e != 0); |
universe@767 | 217 | |
universe@767 | 218 | e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); |
universe@767 | 219 | CX_TEST_ASSERT(e == 0); |
universe@767 | 220 | CX_TEST_ASSERT(r == 0); |
universe@767 | 221 | |
universe@767 | 222 | e = cx_szmul_impl(0, SIZE_MAX - 1, &r); |
universe@767 | 223 | CX_TEST_ASSERT(e == 0); |
universe@767 | 224 | CX_TEST_ASSERT(r == 0); |
universe@767 | 225 | |
universe@767 | 226 | e = cx_szmul_impl(SIZE_MAX, 0, &r); |
universe@767 | 227 | CX_TEST_ASSERT(e == 0); |
universe@767 | 228 | CX_TEST_ASSERT(r == 0); |
universe@767 | 229 | |
universe@767 | 230 | e = cx_szmul_impl(0, SIZE_MAX, &r); |
universe@767 | 231 | CX_TEST_ASSERT(e == 0); |
universe@767 | 232 | CX_TEST_ASSERT(r == 0); |
universe@767 | 233 | |
universe@767 | 234 | e = cx_szmul_impl(0, 0, &r); |
universe@767 | 235 | CX_TEST_ASSERT(e == 0); |
universe@767 | 236 | CX_TEST_ASSERT(r == 0); |
universe@767 | 237 | } |
universe@767 | 238 | } |
universe@767 | 239 | |
universe@767 | 240 | #endif // CX_SZMUL_BUILTIN |
universe@767 | 241 | |
universe@766 | 242 | |
universe@766 | 243 | CxTestSuite *cx_test_suite_utils(void) { |
universe@766 | 244 | CxTestSuite *suite = cx_test_suite_new("utils"); |
universe@766 | 245 | |
universe@767 | 246 | cx_test_register(suite, test_stream_bncopy); |
universe@767 | 247 | cx_test_register(suite, test_stream_ncopy); |
universe@767 | 248 | cx_test_register(suite, test_forn); |
universe@767 | 249 | cx_test_register(suite, test_swap_ptr); |
universe@767 | 250 | cx_test_register(suite, test_szmul); |
universe@767 | 251 | cx_test_register(suite, test_szmul_impl); |
universe@767 | 252 | |
universe@766 | 253 | return suite; |
universe@766 | 254 | } |