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/utils.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 (cx_read_func) cxBufferRead, |
|
47 (cx_write_func) cxBufferWrite, |
|
48 tmp, 4, 20); |
|
49 CX_TEST_ASSERT(result == 20); |
|
50 CX_TEST_ASSERT(target.size == 20); |
|
51 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); |
|
52 |
|
53 result = cx_stream_bcopy(&source, &target, |
|
54 (cx_read_func) cxBufferRead, |
|
55 (cx_write_func) cxBufferWrite, |
|
56 NULL, 16); |
|
57 |
|
58 CX_TEST_ASSERT(result == 7); |
|
59 CX_TEST_ASSERT(target.size == 27); |
|
60 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); |
|
61 } |
|
62 |
|
63 cxBufferDestroy(&source); |
|
64 cxBufferDestroy(&target); |
|
65 } |
|
66 |
|
67 CX_TEST(test_stream_ncopy) { |
|
68 CxBuffer source, target; |
|
69 char sbuf[32], tbuf[32]; |
|
70 memset(tbuf, 0, 32); |
|
71 cxBufferInit(&source, sbuf, 32, NULL, 0); |
|
72 cxBufferInit(&target, tbuf, 32, NULL, 0); |
|
73 cxBufferPutString(&source, "This is a stream copy test."); |
|
74 cxBufferSeek(&source, 0, SEEK_SET); |
|
75 |
|
76 CX_TEST_DO { |
|
77 size_t result = cx_stream_ncopy(&source, &target, |
|
78 (cx_read_func) cxBufferRead, |
|
79 (cx_write_func) cxBufferWrite, |
|
80 20); |
|
81 CX_TEST_ASSERT(result == 20); |
|
82 CX_TEST_ASSERT(target.size == 20); |
|
83 CX_TEST_ASSERT(strcmp("This is a stream cop\0", tbuf) == 0); |
|
84 |
|
85 result = cx_stream_copy(&source, &target, |
|
86 (cx_read_func) cxBufferRead, |
|
87 (cx_write_func) cxBufferWrite); |
|
88 |
|
89 CX_TEST_ASSERT(result == 7); |
|
90 CX_TEST_ASSERT(target.size == 27); |
|
91 CX_TEST_ASSERT(strcmp("This is a stream copy test.\0", tbuf) == 0); |
|
92 } |
|
93 |
|
94 cxBufferDestroy(&source); |
|
95 cxBufferDestroy(&target); |
|
96 } |
|
97 |
|
98 CxTestSuite *cx_test_suite_utils(void) { |
|
99 CxTestSuite *suite = cx_test_suite_new("utils"); |
|
100 |
|
101 cx_test_register(suite, test_stream_bncopy); |
|
102 cx_test_register(suite, test_stream_ncopy); |
|
103 |
|
104 return suite; |
|
105 } |
|