#include <stdio.h>
#include <stdlib.h>
#include "../util/writer.h"
#include <cx/buffer.h>
#include "writer.h"
#include "testutils.h"
UCX_TEST(test_writer_putc) {
Session *sn = testutil_session();
TestIOStream *st = testutil_iostream(
2048,
TRUE);
CxBuffer *buf = st->buf;
UCX_TEST_BEGIN;
Writer writer;
char wbuf[
1024];
writer_init(&writer, st, wbuf,
4);
Writer *out = &writer;
writer_putc(out,
'a');
UCX_TEST_ASSERT(wbuf[
0] ==
'a',
"1: wrong char at pos 0");
UCX_TEST_ASSERT(writer.pos ==
1,
"1: wrong pos");
writer_putc(out,
'b');
UCX_TEST_ASSERT(wbuf[
1] ==
'b',
"2: wrong char at pos 1");
UCX_TEST_ASSERT(writer.pos ==
2,
"2: wrong pos");
writer_putc(out,
'c');
writer_putc(out,
'd');
UCX_TEST_ASSERT(wbuf[
2] ==
'c',
"3: wrong char at pos 2");
UCX_TEST_ASSERT(wbuf[
3] ==
'd',
"4: wrong char at pos 3");
writer_putc(out,
'f');
UCX_TEST_ASSERT(wbuf[
0] ==
'f',
"5: wrong char at pos 0");
UCX_TEST_ASSERT(writer.pos ==
1,
"5: wrong pos");
UCX_TEST_ASSERT(buf->space[
0] ==
'a',
"5: wrong char at UcxBuffer pos 0");
UCX_TEST_ASSERT(buf->space[
1] ==
'b',
"5: wrong char at UcxBuffer pos 1");
UCX_TEST_ASSERT(buf->pos ==
4,
"5: wrong UcxBuffer pos");
UCX_TEST_END;
testutil_iostream_destroy(st);
testutil_destroy_session(sn);
}
UCX_TEST(test_writer_flush) {
Session *sn = testutil_session();
TestIOStream *st = testutil_iostream(
2048,
TRUE);
CxBuffer *buf = st->buf;
UCX_TEST_BEGIN;
Writer writer;
char wbuf[
1024];
writer_init(&writer, st, wbuf,
4);
Writer *out = &writer;
writer_putc(out,
'a');
UCX_TEST_ASSERT(wbuf[
0] ==
'a',
"1: wrong char at pos 0");
UCX_TEST_ASSERT(writer.pos ==
1,
"1: wrong pos");
writer_flush(out);
UCX_TEST_ASSERT(writer.pos ==
0,
"wrong pos after flush");
UCX_TEST_ASSERT(buf->space[
0] ==
'a',
"wrong UcxBuffer content");
UCX_TEST_ASSERT(buf->pos ==
1,
"wrong UcxBuffer pos");
writer_putc(out,
'b');
UCX_TEST_ASSERT(wbuf[
0] ==
'b',
"2: wrong char at pos 0");
UCX_TEST_ASSERT(writer.pos ==
1,
"2: wrong pos");
UCX_TEST_END;
testutil_iostream_destroy(st);
testutil_destroy_session(sn);
}
UCX_TEST(test_writer_put) {
Session *sn = testutil_session();
TestIOStream *st = testutil_iostream(
2048,
TRUE);
CxBuffer *buf = st->buf;
UCX_TEST_BEGIN;
Writer writer;
char wbuf[
1024];
writer_init(&writer, st, wbuf,
8);
Writer *out = &writer;
writer_put(out,
"abcd",
4);
UCX_TEST_ASSERT(!memcmp(wbuf,
"abcd",
4),
"1: wrong content");
UCX_TEST_ASSERT(writer.pos ==
4,
"1: wrong pos");
writer_put(out,
"efgh",
4);
UCX_TEST_ASSERT(!memcmp(wbuf,
"abcdefgh",
8),
"2: wrong content");
UCX_TEST_ASSERT(writer.pos ==
8,
"2: wrong pos");
writer_put(out,
"1234",
4);
UCX_TEST_ASSERT(!memcmp(wbuf,
"1234",
4),
"3: wrong content");
UCX_TEST_ASSERT(writer.pos ==
4,
"3: wrong pos");
UCX_TEST_ASSERT(!memcmp(buf->space,
"abcdefgh",
8),
"3: wrong UcxBuffer content");
UCX_TEST_ASSERT(buf->pos ==
8,
"3: wrong UcxBuffer pos");
writer_put(out,
"5678xx",
6);
UCX_TEST_ASSERT(!memcmp(wbuf,
"xx",
2),
"4: wrong content");
UCX_TEST_ASSERT(writer.pos ==
2,
"4: wrong pos");
UCX_TEST_ASSERT(!memcmp(buf->space,
"abcdefgh12345678",
16),
"4: wrong UcxBuffer content");
UCX_TEST_ASSERT(buf->pos ==
16,
"4: wrong UcxBuffer pos");
writer_puts(out, cx_str(
"345678abcdefgh12345678end."));
UCX_TEST_ASSERT(!memcmp(wbuf,
"end.",
4),
"5: wrong content");
UCX_TEST_ASSERT(writer.pos ==
4,
"5: wrong pos");
UCX_TEST_ASSERT(!memcmp(
buf->space,
"abcdefgh12345678xx345678abcdefgh12345678",
40),
"5: wrong UcxBuffer content");
UCX_TEST_ASSERT(buf->pos ==
40,
"5: wrong UcxBuffer pos");
UCX_TEST_END;
testutil_iostream_destroy(st);
testutil_destroy_session(sn);
}