src/server/test/writer.c

Sat, 25 Jan 2020 21:37:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 25 Jan 2020 21:37:38 +0100
branch
webdav
changeset 241
4adad7faf452
parent 232
499711b2a970
child 415
d938228c382e
permissions
-rw-r--r--

add proppatch op

/*
 * 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 <ucx/buffer.h>

#include "writer.h"
#include "testutils.h"

UCX_TEST(test_writer_putc) {
    Session *sn = testutil_session();
    TestIOStream *st = testutil_iostream(2048, TRUE);
    UcxBuffer *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'); // should flush the buffer
    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);
    UcxBuffer *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);
    UcxBuffer *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, S("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);
}

mercurial