universe@766: /* universe@766: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@766: * universe@766: * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. universe@766: * universe@766: * Redistribution and use in source and binary forms, with or without universe@766: * modification, are permitted provided that the following conditions are met: universe@766: * universe@766: * 1. Redistributions of source code must retain the above copyright universe@766: * notice, this list of conditions and the following disclaimer. universe@766: * universe@766: * 2. Redistributions in binary form must reproduce the above copyright universe@766: * notice, this list of conditions and the following disclaimer in the universe@766: * documentation and/or other materials provided with the distribution. universe@766: * universe@766: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@766: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@766: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@766: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@766: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@766: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@766: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@766: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@766: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@766: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@766: * POSSIBILITY OF SUCH DAMAGE. universe@766: */ universe@766: universe@767: #include "cx/test.h" universe@767: universe@767: #include "cx/utils.h" universe@767: #include "cx/buffer.h" universe@767: universe@767: CX_TEST(test_stream_bncopy) { universe@767: CxBuffer source, target; universe@767: char sbuf[32], tbuf[32]; universe@767: memset(tbuf, 0, 32); universe@767: cxBufferInit(&source, sbuf, 32, NULL, 0); universe@767: cxBufferInit(&target, tbuf, 32, NULL, 0); universe@767: cxBufferPutString(&source, "This is a stream copy test."); universe@767: cxBufferSeek(&source, 0, SEEK_SET); universe@767: char tmp[4]; universe@767: universe@767: CX_TEST_DO { universe@767: size_t result = cx_stream_bncopy(&source, &target, universe@767: (cx_read_func) cxBufferRead, universe@767: (cx_write_func) cxBufferWrite, universe@767: tmp, 4, 20); universe@767: CX_TEST_ASSERT(result == 20); universe@767: CX_TEST_ASSERT(target.size == 20); universe@767: CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); universe@767: universe@767: result = cx_stream_bcopy(&source, &target, universe@767: (cx_read_func) cxBufferRead, universe@767: (cx_write_func) cxBufferWrite, universe@767: NULL, 16); universe@767: universe@767: CX_TEST_ASSERT(result == 7); universe@767: CX_TEST_ASSERT(target.size == 27); universe@767: CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); universe@767: } universe@767: universe@767: cxBufferDestroy(&source); universe@767: cxBufferDestroy(&target); universe@767: } universe@767: universe@767: CX_TEST(test_stream_ncopy) { universe@767: CxBuffer source, target; universe@767: char sbuf[32], tbuf[32]; universe@767: memset(tbuf, 0, 32); universe@767: cxBufferInit(&source, sbuf, 32, NULL, 0); universe@767: cxBufferInit(&target, tbuf, 32, NULL, 0); universe@767: cxBufferPutString(&source, "This is a stream copy test."); universe@767: cxBufferSeek(&source, 0, SEEK_SET); universe@767: universe@767: CX_TEST_DO { universe@767: size_t result = cx_stream_ncopy(&source, &target, universe@767: (cx_read_func) cxBufferRead, universe@767: (cx_write_func) cxBufferWrite, universe@767: 20); universe@767: CX_TEST_ASSERT(result == 20); universe@767: CX_TEST_ASSERT(target.size == 20); universe@767: CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); universe@767: universe@767: result = cx_stream_copy(&source, &target, universe@767: (cx_read_func) cxBufferRead, universe@767: (cx_write_func) cxBufferWrite); universe@767: universe@767: CX_TEST_ASSERT(result == 7); universe@767: CX_TEST_ASSERT(target.size == 27); universe@767: CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); universe@767: } universe@767: universe@767: cxBufferDestroy(&source); universe@767: cxBufferDestroy(&target); universe@767: } universe@767: universe@767: CX_TEST(test_forn) { universe@767: unsigned j; universe@767: j = 0; universe@767: CX_TEST_DO { universe@767: cx_for_n(i, 50) { universe@767: CX_TEST_ASSERT(i == j); universe@767: j++; universe@767: } universe@767: } universe@767: } universe@767: universe@767: CX_TEST(test_swap_ptr) { universe@767: int i = 5; universe@767: int j = 8; universe@767: int *ip = &i; universe@767: int *jp = &j; universe@767: CX_TEST_DO { universe@767: cx_swap_ptr(ip, jp); universe@767: CX_TEST_ASSERT(ip == &j); universe@767: CX_TEST_ASSERT(jp == &i); universe@767: } universe@767: } universe@767: universe@767: CX_TEST(test_szmul) { universe@767: size_t r; universe@767: int e; universe@767: universe@767: CX_TEST_DO { universe@767: e = cx_szmul(5, 7, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 35); universe@767: universe@767: size_t s = SIZE_MAX & ~3; universe@767: universe@767: e = cx_szmul(s / 4, 2, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s / 2); universe@767: universe@767: e = cx_szmul(2, s / 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s / 2); universe@767: universe@767: e = cx_szmul(s / 4, 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s); universe@767: universe@767: e = cx_szmul(4, s / 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s); universe@767: universe@767: e = cx_szmul(s / 4, 5, &r); universe@767: CX_TEST_ASSERT(e != 0); universe@767: universe@767: e = cx_szmul(5, s / 4, &r); universe@767: CX_TEST_ASSERT(e != 0); universe@767: universe@767: e = cx_szmul(SIZE_MAX - 4, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul(0, SIZE_MAX - 1, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul(SIZE_MAX, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul(0, SIZE_MAX, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul(0, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: } universe@767: } universe@767: universe@767: #ifdef CX_SZMUL_BUILTIN universe@767: universe@767: // also test the custom implementation universe@767: #undef CX_SZMUL_BUILTIN universe@767: universe@767: #include "../src/szmul.c" universe@767: universe@767: #define CX_SZMUL_BUILTIN universe@767: universe@767: CX_TEST(test_szmul_impl) { universe@767: size_t r; universe@767: int e; universe@767: universe@767: CX_TEST_DO { universe@767: e = cx_szmul_impl(5, 7, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 35); universe@767: universe@767: size_t s = SIZE_MAX & ~3; universe@767: universe@767: e = cx_szmul_impl(s / 4, 2, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s / 2); universe@767: universe@767: e = cx_szmul_impl(2, s / 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s / 2); universe@767: universe@767: e = cx_szmul_impl(s / 4, 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s); universe@767: universe@767: e = cx_szmul_impl(4, s / 4, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == s); universe@767: universe@767: e = cx_szmul_impl(s / 4, 5, &r); universe@767: CX_TEST_ASSERT(e != 0); universe@767: universe@767: e = cx_szmul_impl(5, s / 4, &r); universe@767: CX_TEST_ASSERT(e != 0); universe@767: universe@767: e = cx_szmul_impl(SIZE_MAX - 4, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul_impl(0, SIZE_MAX - 1, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul_impl(SIZE_MAX, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul_impl(0, SIZE_MAX, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: universe@767: e = cx_szmul_impl(0, 0, &r); universe@767: CX_TEST_ASSERT(e == 0); universe@767: CX_TEST_ASSERT(r == 0); universe@767: } universe@767: } universe@767: universe@767: #endif // CX_SZMUL_BUILTIN universe@767: universe@766: universe@766: CxTestSuite *cx_test_suite_utils(void) { universe@766: CxTestSuite *suite = cx_test_suite_new("utils"); universe@766: universe@767: cx_test_register(suite, test_stream_bncopy); universe@767: cx_test_register(suite, test_stream_ncopy); universe@767: cx_test_register(suite, test_forn); universe@767: cx_test_register(suite, test_swap_ptr); universe@767: cx_test_register(suite, test_szmul); olaf@841: #ifdef CX_SZMUL_BUILTIN universe@767: cx_test_register(suite, test_szmul_impl); olaf@841: #endif universe@767: universe@766: return suite; universe@766: }