|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2020 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 <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "../util/writer.h" |
|
33 |
|
34 #include <ucx/buffer.h> |
|
35 |
|
36 #include "writer.h" |
|
37 #include "testutils.h" |
|
38 |
|
39 UCX_TEST(test_writer_putc) { |
|
40 Session *sn = testutil_session(); |
|
41 TestIOStream *st = testutil_iostream(2048, TRUE); |
|
42 UcxBuffer *buf = st->buf; |
|
43 |
|
44 UCX_TEST_BEGIN; |
|
45 |
|
46 Writer writer; |
|
47 char wbuf[1024]; |
|
48 writer_init(&writer, st, wbuf, 4); |
|
49 Writer *out = &writer; |
|
50 |
|
51 writer_putc(out, 'a'); |
|
52 UCX_TEST_ASSERT(wbuf[0] == 'a', "1: wrong char at pos 0"); |
|
53 UCX_TEST_ASSERT(writer.pos == 1, "1: wrong pos"); |
|
54 |
|
55 writer_putc(out, 'b'); |
|
56 UCX_TEST_ASSERT(wbuf[1] == 'b', "2: wrong char at pos 1"); |
|
57 UCX_TEST_ASSERT(writer.pos == 2, "2: wrong pos"); |
|
58 |
|
59 writer_putc(out, 'c'); |
|
60 writer_putc(out, 'd'); |
|
61 UCX_TEST_ASSERT(wbuf[2] == 'c', "3: wrong char at pos 2"); |
|
62 UCX_TEST_ASSERT(wbuf[3] == 'd', "4: wrong char at pos 3"); |
|
63 |
|
64 writer_putc(out, 'f'); // should flush the buffer |
|
65 UCX_TEST_ASSERT(wbuf[0] == 'f', "5: wrong char at pos 0"); |
|
66 UCX_TEST_ASSERT(writer.pos == 1, "5: wrong pos"); |
|
67 UCX_TEST_ASSERT(buf->space[0] == 'a', "5: wrong char at UcxBuffer pos 0"); |
|
68 UCX_TEST_ASSERT(buf->space[1] == 'b', "5: wrong char at UcxBuffer pos 1"); |
|
69 UCX_TEST_ASSERT(buf->pos == 4, "5: wrong UcxBuffer pos"); |
|
70 |
|
71 UCX_TEST_END; |
|
72 testutil_iostream_destroy(st); |
|
73 testutil_destroy_session(sn); |
|
74 } |
|
75 |
|
76 UCX_TEST(test_writer_flush) { |
|
77 Session *sn = testutil_session(); |
|
78 TestIOStream *st = testutil_iostream(2048, TRUE); |
|
79 UcxBuffer *buf = st->buf; |
|
80 |
|
81 UCX_TEST_BEGIN; |
|
82 |
|
83 Writer writer; |
|
84 char wbuf[1024]; |
|
85 writer_init(&writer, st, wbuf, 4); |
|
86 Writer *out = &writer; |
|
87 |
|
88 writer_putc(out, 'a'); |
|
89 UCX_TEST_ASSERT(wbuf[0] == 'a', "1: wrong char at pos 0"); |
|
90 UCX_TEST_ASSERT(writer.pos == 1, "1: wrong pos"); |
|
91 |
|
92 writer_flush(out); |
|
93 UCX_TEST_ASSERT(writer.pos == 0, "wrong pos after flush"); |
|
94 UCX_TEST_ASSERT(buf->space[0] == 'a', "wrong UcxBuffer content"); |
|
95 UCX_TEST_ASSERT(buf->pos == 1, "wrong UcxBuffer pos"); |
|
96 |
|
97 writer_putc(out, 'b'); |
|
98 UCX_TEST_ASSERT(wbuf[0] == 'b', "2: wrong char at pos 0"); |
|
99 UCX_TEST_ASSERT(writer.pos == 1, "2: wrong pos"); |
|
100 |
|
101 UCX_TEST_END; |
|
102 testutil_iostream_destroy(st); |
|
103 testutil_destroy_session(sn); |
|
104 } |
|
105 |
|
106 UCX_TEST(test_writer_put) { |
|
107 Session *sn = testutil_session(); |
|
108 TestIOStream *st = testutil_iostream(2048, TRUE); |
|
109 UcxBuffer *buf = st->buf; |
|
110 |
|
111 UCX_TEST_BEGIN; |
|
112 |
|
113 Writer writer; |
|
114 char wbuf[1024]; |
|
115 writer_init(&writer, st, wbuf, 8); |
|
116 Writer *out = &writer; |
|
117 |
|
118 writer_put(out, "abcd", 4); |
|
119 UCX_TEST_ASSERT(!memcmp(wbuf, "abcd", 4), "1: wrong content"); |
|
120 UCX_TEST_ASSERT(writer.pos == 4, "1: wrong pos"); |
|
121 |
|
122 writer_put(out, "efgh", 4); |
|
123 UCX_TEST_ASSERT(!memcmp(wbuf, "abcdefgh", 8), "2: wrong content"); |
|
124 UCX_TEST_ASSERT(writer.pos == 8, "2: wrong pos"); |
|
125 |
|
126 writer_put(out, "1234", 4); |
|
127 UCX_TEST_ASSERT(!memcmp(wbuf, "1234", 4), "3: wrong content"); |
|
128 UCX_TEST_ASSERT(writer.pos == 4, "3: wrong pos"); |
|
129 UCX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh", 8), "3: wrong UcxBuffer content"); |
|
130 UCX_TEST_ASSERT(buf->pos == 8, "3: wrong UcxBuffer pos"); |
|
131 |
|
132 writer_put(out, "5678xx", 6); |
|
133 UCX_TEST_ASSERT(!memcmp(wbuf, "xx", 2), "4: wrong content"); |
|
134 UCX_TEST_ASSERT(writer.pos == 2, "4: wrong pos"); |
|
135 UCX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678", 16), "4: wrong UcxBuffer content"); |
|
136 UCX_TEST_ASSERT(buf->pos == 16, "4: wrong UcxBuffer pos"); |
|
137 |
|
138 writer_puts(out, S("345678abcdefgh12345678end.")); |
|
139 UCX_TEST_ASSERT(!memcmp(wbuf, "end.", 4), "5: wrong content"); |
|
140 UCX_TEST_ASSERT(writer.pos == 4, "5: wrong pos"); |
|
141 UCX_TEST_ASSERT(!memcmp( |
|
142 buf->space, |
|
143 "abcdefgh12345678xx345678abcdefgh12345678", |
|
144 40), |
|
145 "5: wrong UcxBuffer content"); |
|
146 UCX_TEST_ASSERT(buf->pos == 40, "5: wrong UcxBuffer pos"); |
|
147 |
|
148 UCX_TEST_END; |
|
149 testutil_iostream_destroy(st); |
|
150 testutil_destroy_session(sn); |
|
151 } |