1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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