Sat, 22 Nov 2025 14:27:01 +0100
port old ucx2 tests to ucx3
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2020 Olaf Wintermann. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <stdio.h> #include <stdlib.h> #include "../util/writer.h" #include <cx/buffer.h> #include "writer.h" #include "testutils.h" CX_TEST(test_writer_putc) { Session *sn = testutil_session(); TestIOStream *st = testutil_iostream(2048, TRUE); CxBuffer *buf = st->buf; CX_TEST_DO { Writer writer; char wbuf[1024]; writer_init(&writer, st, wbuf, 4); Writer *out = &writer; writer_putc(out, 'a'); CX_TEST_ASSERT(wbuf[0] == 'a'); CX_TEST_ASSERT(writer.pos == 1); writer_putc(out, 'b'); CX_TEST_ASSERT(wbuf[1] == 'b'); CX_TEST_ASSERT(writer.pos == 2); writer_putc(out, 'c'); writer_putc(out, 'd'); CX_TEST_ASSERT(wbuf[2] == 'c'); CX_TEST_ASSERT(wbuf[3] == 'd'); writer_putc(out, 'f'); // should flush the buffer CX_TEST_ASSERT(wbuf[0] == 'f'); CX_TEST_ASSERT(writer.pos == 1); CX_TEST_ASSERT(buf->space[0] == 'a'); CX_TEST_ASSERT(buf->space[1] == 'b'); CX_TEST_ASSERT(buf->pos == 4); } testutil_iostream_destroy(st); testutil_destroy_session(sn); } CX_TEST(test_writer_flush) { Session *sn = testutil_session(); TestIOStream *st = testutil_iostream(2048, TRUE); CxBuffer *buf = st->buf; CX_TEST_DO { Writer writer; char wbuf[1024]; writer_init(&writer, st, wbuf, 4); Writer *out = &writer; writer_putc(out, 'a'); CX_TEST_ASSERT(wbuf[0] == 'a'); CX_TEST_ASSERT(writer.pos == 1); writer_flush(out); CX_TEST_ASSERT(writer.pos == 0); CX_TEST_ASSERT(buf->space[0] == 'a'); CX_TEST_ASSERT(buf->pos == 1); writer_putc(out, 'b'); CX_TEST_ASSERT(wbuf[0] == 'b'); CX_TEST_ASSERT(writer.pos == 1); } testutil_iostream_destroy(st); testutil_destroy_session(sn); } CX_TEST(test_writer_put) { Session *sn = testutil_session(); TestIOStream *st = testutil_iostream(2048, TRUE); CxBuffer *buf = st->buf; CX_TEST_DO { Writer writer; char wbuf[1024]; writer_init(&writer, st, wbuf, 8); Writer *out = &writer; writer_put(out, "abcd", 4); CX_TEST_ASSERT(!memcmp(wbuf, "abcd", 4)); CX_TEST_ASSERT(writer.pos == 4); writer_put(out, "efgh", 4); CX_TEST_ASSERT(!memcmp(wbuf, "abcdefgh", 8)); CX_TEST_ASSERT(writer.pos == 8); writer_put(out, "1234", 4); CX_TEST_ASSERT(!memcmp(wbuf, "1234", 4)); CX_TEST_ASSERT(writer.pos == 4); CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh", 8)); CX_TEST_ASSERT(buf->pos == 8); writer_put(out, "5678xx", 6); CX_TEST_ASSERT(!memcmp(wbuf, "xx", 2)); CX_TEST_ASSERT(writer.pos == 2); CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678", 16)); CX_TEST_ASSERT(buf->pos == 16); writer_puts(out, cx_str("345678abcdefgh12345678end.")); CX_TEST_ASSERT(!memcmp(wbuf, "end.", 4)); CX_TEST_ASSERT(writer.pos == 4); CX_TEST_ASSERT(!memcmp(buf->space, "abcdefgh12345678xx345678abcdefgh12345678", 40)); CX_TEST_ASSERT(buf->pos == 40); } testutil_iostream_destroy(st); testutil_destroy_session(sn); }