Sat, 22 Nov 2025 14:27:01 +0100
port old ucx2 tests to ucx3
| 232 | 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 | ||
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
34 | #include <cx/buffer.h> |
| 232 | 35 | |
| 36 | #include "writer.h" | |
| 37 | #include "testutils.h" | |
| 38 | ||
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
39 | CX_TEST(test_writer_putc) { |
| 232 | 40 | Session *sn = testutil_session(); |
| 41 | TestIOStream *st = testutil_iostream(2048, TRUE); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
42 | CxBuffer *buf = st->buf; |
| 232 | 43 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
44 | CX_TEST_DO { |
| 232 | 45 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
46 | Writer writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
47 | char wbuf[1024]; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
48 | writer_init(&writer, st, wbuf, 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
49 | Writer *out = &writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
50 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
51 | writer_putc(out, 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
52 | CX_TEST_ASSERT(wbuf[0] == 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
53 | CX_TEST_ASSERT(writer.pos == 1); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
54 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
55 | writer_putc(out, 'b'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
56 | CX_TEST_ASSERT(wbuf[1] == 'b'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
57 | CX_TEST_ASSERT(writer.pos == 2); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
58 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
59 | writer_putc(out, 'c'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
60 | writer_putc(out, 'd'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
61 | CX_TEST_ASSERT(wbuf[2] == 'c'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
62 | CX_TEST_ASSERT(wbuf[3] == 'd'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
63 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
64 | writer_putc(out, 'f'); // should flush the buffer |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
65 | CX_TEST_ASSERT(wbuf[0] == 'f'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
66 | CX_TEST_ASSERT(writer.pos == 1); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
67 | CX_TEST_ASSERT(buf->space[0] == 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
68 | CX_TEST_ASSERT(buf->space[1] == 'b'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
69 | CX_TEST_ASSERT(buf->pos == 4); |
| 232 | 70 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
71 | } |
| 232 | 72 | testutil_iostream_destroy(st); |
| 73 | testutil_destroy_session(sn); | |
| 74 | } | |
| 75 | ||
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
76 | CX_TEST(test_writer_flush) { |
| 232 | 77 | Session *sn = testutil_session(); |
| 78 | TestIOStream *st = testutil_iostream(2048, TRUE); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
79 | CxBuffer *buf = st->buf; |
| 232 | 80 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
81 | CX_TEST_DO { |
| 232 | 82 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
83 | Writer writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
84 | char wbuf[1024]; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
85 | writer_init(&writer, st, wbuf, 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
86 | Writer *out = &writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
87 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
88 | writer_putc(out, 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
89 | CX_TEST_ASSERT(wbuf[0] == 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
90 | CX_TEST_ASSERT(writer.pos == 1); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
91 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
92 | writer_flush(out); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
93 | CX_TEST_ASSERT(writer.pos == 0); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
94 | CX_TEST_ASSERT(buf->space[0] == 'a'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
95 | CX_TEST_ASSERT(buf->pos == 1); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
96 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
97 | writer_putc(out, 'b'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
98 | CX_TEST_ASSERT(wbuf[0] == 'b'); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
99 | CX_TEST_ASSERT(writer.pos == 1); |
| 232 | 100 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
101 | } |
| 232 | 102 | testutil_iostream_destroy(st); |
| 103 | testutil_destroy_session(sn); | |
| 104 | } | |
| 105 | ||
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
106 | CX_TEST(test_writer_put) { |
| 232 | 107 | Session *sn = testutil_session(); |
| 108 | TestIOStream *st = testutil_iostream(2048, TRUE); | |
|
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
232
diff
changeset
|
109 | CxBuffer *buf = st->buf; |
| 232 | 110 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
111 | CX_TEST_DO { |
| 232 | 112 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
113 | Writer writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
114 | char wbuf[1024]; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
115 | writer_init(&writer, st, wbuf, 8); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
116 | Writer *out = &writer; |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
117 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
118 | writer_put(out, "abcd", 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
119 | CX_TEST_ASSERT(!memcmp(wbuf, "abcd", 4)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
120 | CX_TEST_ASSERT(writer.pos == 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
121 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
122 | writer_put(out, "efgh", 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
123 | CX_TEST_ASSERT(!memcmp(wbuf, "abcdefgh", 8)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
124 | CX_TEST_ASSERT(writer.pos == 8); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
125 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
126 | writer_put(out, "1234", 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
127 | CX_TEST_ASSERT(!memcmp(wbuf, "1234", 4)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
128 | CX_TEST_ASSERT(writer.pos == 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
129 | CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh", 8)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
130 | CX_TEST_ASSERT(buf->pos == 8); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
131 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
132 | writer_put(out, "5678xx", 6); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
133 | CX_TEST_ASSERT(!memcmp(wbuf, "xx", 2)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
134 | CX_TEST_ASSERT(writer.pos == 2); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
135 | CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678", 16)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
136 | CX_TEST_ASSERT(buf->pos == 16); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
137 | |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
138 | writer_puts(out, cx_str("345678abcdefgh12345678end.")); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
139 | CX_TEST_ASSERT(!memcmp(wbuf, "end.", 4)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
140 | CX_TEST_ASSERT(writer.pos == 4); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
141 | CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678xx345678abcdefgh12345678", 40)); |
|
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
142 | CX_TEST_ASSERT(buf->pos == 40); |
| 232 | 143 | |
|
633
392ec9026b07
port old ucx2 tests to ucx3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
144 | } |
| 232 | 145 | testutil_iostream_destroy(st); |
| 146 | testutil_destroy_session(sn); | |
| 147 | } |