1.1 --- a/tests/test_utils.c Wed Dec 20 16:46:14 2023 +0100 1.2 +++ b/tests/test_utils.c Wed Dec 20 17:57:18 2023 +0100 1.3 @@ -26,11 +26,230 @@ 1.4 * POSSIBILITY OF SUCH DAMAGE. 1.5 */ 1.6 1.7 -#include "test_utils.h" 1.8 +#include "cx/test.h" 1.9 + 1.10 +#include "cx/utils.h" 1.11 +#include "cx/buffer.h" 1.12 + 1.13 +CX_TEST(test_stream_bncopy) { 1.14 + CxBuffer source, target; 1.15 + char sbuf[32], tbuf[32]; 1.16 + memset(tbuf, 0, 32); 1.17 + cxBufferInit(&source, sbuf, 32, NULL, 0); 1.18 + cxBufferInit(&target, tbuf, 32, NULL, 0); 1.19 + cxBufferPutString(&source, "This is a stream copy test."); 1.20 + cxBufferSeek(&source, 0, SEEK_SET); 1.21 + char tmp[4]; 1.22 + 1.23 + CX_TEST_DO { 1.24 + size_t result = cx_stream_bncopy(&source, &target, 1.25 + (cx_read_func) cxBufferRead, 1.26 + (cx_write_func) cxBufferWrite, 1.27 + tmp, 4, 20); 1.28 + CX_TEST_ASSERT(result == 20); 1.29 + CX_TEST_ASSERT(target.size == 20); 1.30 + CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); 1.31 + 1.32 + result = cx_stream_bcopy(&source, &target, 1.33 + (cx_read_func) cxBufferRead, 1.34 + (cx_write_func) cxBufferWrite, 1.35 + NULL, 16); 1.36 + 1.37 + CX_TEST_ASSERT(result == 7); 1.38 + CX_TEST_ASSERT(target.size == 27); 1.39 + CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); 1.40 + } 1.41 + 1.42 + cxBufferDestroy(&source); 1.43 + cxBufferDestroy(&target); 1.44 +} 1.45 + 1.46 +CX_TEST(test_stream_ncopy) { 1.47 + CxBuffer source, target; 1.48 + char sbuf[32], tbuf[32]; 1.49 + memset(tbuf, 0, 32); 1.50 + cxBufferInit(&source, sbuf, 32, NULL, 0); 1.51 + cxBufferInit(&target, tbuf, 32, NULL, 0); 1.52 + cxBufferPutString(&source, "This is a stream copy test."); 1.53 + cxBufferSeek(&source, 0, SEEK_SET); 1.54 + 1.55 + CX_TEST_DO { 1.56 + size_t result = cx_stream_ncopy(&source, &target, 1.57 + (cx_read_func) cxBufferRead, 1.58 + (cx_write_func) cxBufferWrite, 1.59 + 20); 1.60 + CX_TEST_ASSERT(result == 20); 1.61 + CX_TEST_ASSERT(target.size == 20); 1.62 + CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); 1.63 + 1.64 + result = cx_stream_copy(&source, &target, 1.65 + (cx_read_func) cxBufferRead, 1.66 + (cx_write_func) cxBufferWrite); 1.67 + 1.68 + CX_TEST_ASSERT(result == 7); 1.69 + CX_TEST_ASSERT(target.size == 27); 1.70 + CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); 1.71 + } 1.72 + 1.73 + cxBufferDestroy(&source); 1.74 + cxBufferDestroy(&target); 1.75 +} 1.76 + 1.77 +CX_TEST(test_forn) { 1.78 + unsigned j; 1.79 + j = 0; 1.80 + CX_TEST_DO { 1.81 + cx_for_n(i, 50) { 1.82 + CX_TEST_ASSERT(i == j); 1.83 + j++; 1.84 + } 1.85 + } 1.86 +} 1.87 + 1.88 +CX_TEST(test_swap_ptr) { 1.89 + int i = 5; 1.90 + int j = 8; 1.91 + int *ip = &i; 1.92 + int *jp = &j; 1.93 + CX_TEST_DO { 1.94 + cx_swap_ptr(ip, jp); 1.95 + CX_TEST_ASSERT(ip == &j); 1.96 + CX_TEST_ASSERT(jp == &i); 1.97 + } 1.98 +} 1.99 + 1.100 +CX_TEST(test_szmul) { 1.101 + size_t r; 1.102 + int e; 1.103 + 1.104 + CX_TEST_DO { 1.105 + e = cx_szmul(5, 7, &r); 1.106 + CX_TEST_ASSERT(e == 0); 1.107 + CX_TEST_ASSERT(r == 35); 1.108 + 1.109 + size_t s = SIZE_MAX & ~3; 1.110 + 1.111 + e = cx_szmul(s / 4, 2, &r); 1.112 + CX_TEST_ASSERT(e == 0); 1.113 + CX_TEST_ASSERT(r == s / 2); 1.114 + 1.115 + e = cx_szmul(2, s / 4, &r); 1.116 + CX_TEST_ASSERT(e == 0); 1.117 + CX_TEST_ASSERT(r == s / 2); 1.118 + 1.119 + e = cx_szmul(s / 4, 4, &r); 1.120 + CX_TEST_ASSERT(e == 0); 1.121 + CX_TEST_ASSERT(r == s); 1.122 + 1.123 + e = cx_szmul(4, s / 4, &r); 1.124 + CX_TEST_ASSERT(e == 0); 1.125 + CX_TEST_ASSERT(r == s); 1.126 + 1.127 + e = cx_szmul(s / 4, 5, &r); 1.128 + CX_TEST_ASSERT(e != 0); 1.129 + 1.130 + e = cx_szmul(5, s / 4, &r); 1.131 + CX_TEST_ASSERT(e != 0); 1.132 + 1.133 + e = cx_szmul(SIZE_MAX - 4, 0, &r); 1.134 + CX_TEST_ASSERT(e == 0); 1.135 + CX_TEST_ASSERT(r == 0); 1.136 + 1.137 + e = cx_szmul(0, SIZE_MAX - 1, &r); 1.138 + CX_TEST_ASSERT(e == 0); 1.139 + CX_TEST_ASSERT(r == 0); 1.140 + 1.141 + e = cx_szmul(SIZE_MAX, 0, &r); 1.142 + CX_TEST_ASSERT(e == 0); 1.143 + CX_TEST_ASSERT(r == 0); 1.144 + 1.145 + e = cx_szmul(0, SIZE_MAX, &r); 1.146 + CX_TEST_ASSERT(e == 0); 1.147 + CX_TEST_ASSERT(r == 0); 1.148 + 1.149 + e = cx_szmul(0, 0, &r); 1.150 + CX_TEST_ASSERT(e == 0); 1.151 + CX_TEST_ASSERT(r == 0); 1.152 + } 1.153 +} 1.154 + 1.155 +#ifdef CX_SZMUL_BUILTIN 1.156 + 1.157 +// also test the custom implementation 1.158 +#undef CX_SZMUL_BUILTIN 1.159 + 1.160 +#include "../src/szmul.c" 1.161 + 1.162 +#define CX_SZMUL_BUILTIN 1.163 + 1.164 +CX_TEST(test_szmul_impl) { 1.165 + size_t r; 1.166 + int e; 1.167 + 1.168 + CX_TEST_DO { 1.169 + e = cx_szmul_impl(5, 7, &r); 1.170 + CX_TEST_ASSERT(e == 0); 1.171 + CX_TEST_ASSERT(r == 35); 1.172 + 1.173 + size_t s = SIZE_MAX & ~3; 1.174 + 1.175 + e = cx_szmul_impl(s / 4, 2, &r); 1.176 + CX_TEST_ASSERT(e == 0); 1.177 + CX_TEST_ASSERT(r == s / 2); 1.178 + 1.179 + e = cx_szmul_impl(2, s / 4, &r); 1.180 + CX_TEST_ASSERT(e == 0); 1.181 + CX_TEST_ASSERT(r == s / 2); 1.182 + 1.183 + e = cx_szmul_impl(s / 4, 4, &r); 1.184 + CX_TEST_ASSERT(e == 0); 1.185 + CX_TEST_ASSERT(r == s); 1.186 + 1.187 + e = cx_szmul_impl(4, s / 4, &r); 1.188 + CX_TEST_ASSERT(e == 0); 1.189 + CX_TEST_ASSERT(r == s); 1.190 + 1.191 + e = cx_szmul_impl(s / 4, 5, &r); 1.192 + CX_TEST_ASSERT(e != 0); 1.193 + 1.194 + e = cx_szmul_impl(5, s / 4, &r); 1.195 + CX_TEST_ASSERT(e != 0); 1.196 + 1.197 + e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); 1.198 + CX_TEST_ASSERT(e == 0); 1.199 + CX_TEST_ASSERT(r == 0); 1.200 + 1.201 + e = cx_szmul_impl(0, SIZE_MAX - 1, &r); 1.202 + CX_TEST_ASSERT(e == 0); 1.203 + CX_TEST_ASSERT(r == 0); 1.204 + 1.205 + e = cx_szmul_impl(SIZE_MAX, 0, &r); 1.206 + CX_TEST_ASSERT(e == 0); 1.207 + CX_TEST_ASSERT(r == 0); 1.208 + 1.209 + e = cx_szmul_impl(0, SIZE_MAX, &r); 1.210 + CX_TEST_ASSERT(e == 0); 1.211 + CX_TEST_ASSERT(r == 0); 1.212 + 1.213 + e = cx_szmul_impl(0, 0, &r); 1.214 + CX_TEST_ASSERT(e == 0); 1.215 + CX_TEST_ASSERT(r == 0); 1.216 + } 1.217 +} 1.218 + 1.219 +#endif // CX_SZMUL_BUILTIN 1.220 + 1.221 1.222 CxTestSuite *cx_test_suite_utils(void) { 1.223 CxTestSuite *suite = cx_test_suite_new("utils"); 1.224 1.225 + cx_test_register(suite, test_stream_bncopy); 1.226 + cx_test_register(suite, test_stream_ncopy); 1.227 + cx_test_register(suite, test_forn); 1.228 + cx_test_register(suite, test_swap_ptr); 1.229 + cx_test_register(suite, test_szmul); 1.230 + cx_test_register(suite, test_szmul_impl); 1.231 + 1.232 return suite; 1.233 } 1.234