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