UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "cx/test.h" 30 31 #include "cx/streams.h" 32 #include "cx/buffer.h" 33 34 CX_TEST(test_stream_bncopy) { 35 CxBuffer source, target; 36 char sbuf[32], tbuf[32]; 37 memset(tbuf, 0, 32); 38 cxBufferInit(&source, sbuf, 32, NULL, 0); 39 cxBufferInit(&target, tbuf, 32, NULL, 0); 40 cxBufferPutString(&source, "This is a stream copy test."); 41 cxBufferSeek(&source, 0, SEEK_SET); 42 char tmp[4]; 43 44 CX_TEST_DO { 45 size_t result = cx_stream_bncopy(&source, &target, 46 cxBufferReadFunc, 47 cxBufferWriteFunc, 48 tmp, 4, 0); 49 CX_TEST_ASSERT(result == 0); 50 CX_TEST_ASSERT(target.size == 0); 51 52 result = cx_stream_bncopy(&source, &target, 53 cxBufferReadFunc, 54 cxBufferWriteFunc, 55 tmp, 4, 20); 56 CX_TEST_ASSERT(result == 20); 57 CX_TEST_ASSERT(target.size == 20); 58 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); 59 60 result = cx_stream_bcopy(&source, &target, 61 cxBufferReadFunc, 62 cxBufferWriteFunc, 63 NULL, 16); 64 65 CX_TEST_ASSERT(result == 7); 66 CX_TEST_ASSERT(target.size == 27); 67 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); 68 } 69 70 cxBufferDestroy(&source); 71 cxBufferDestroy(&target); 72 } 73 74 CX_TEST(test_stream_ncopy) { 75 CxBuffer source, target; 76 char sbuf[32], tbuf[32]; 77 memset(tbuf, 0, 32); 78 cxBufferInit(&source, sbuf, 32, NULL, 0); 79 cxBufferInit(&target, tbuf, 32, NULL, 0); 80 cxBufferPutString(&source, "This is a stream copy test."); 81 cxBufferSeek(&source, 0, SEEK_SET); 82 83 CX_TEST_DO { 84 size_t result = cx_stream_ncopy(&source, &target, 85 cxBufferReadFunc, 86 cxBufferWriteFunc, 87 20); 88 CX_TEST_ASSERT(result == 20); 89 CX_TEST_ASSERT(target.size == 20); 90 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); 91 92 result = cx_stream_copy(&source, &target, 93 cxBufferReadFunc, 94 cxBufferWriteFunc); 95 96 CX_TEST_ASSERT(result == 7); 97 CX_TEST_ASSERT(target.size == 27); 98 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); 99 } 100 101 cxBufferDestroy(&source); 102 cxBufferDestroy(&target); 103 } 104 105 CxTestSuite *cx_test_suite_streams(void) { 106 CxTestSuite *suite = cx_test_suite_new("streams"); 107 108 cx_test_register(suite, test_stream_bncopy); 109 cx_test_register(suite, test_stream_ncopy); 110 111 return suite; 112 } 113