test/memstream_tests.c

Wed, 10 Oct 2012 09:32:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Oct 2012 09:32:06 +0200
changeset 58
733f22fca61a
parent 56
76caac0da4a0
permissions
-rw-r--r--

discarded memprintf / memscanf

Reason: memscanf has no chance to get the amount of bytes read

/*
 *
 */

#include "memstream_tests.h"

UCX_TEST_IMPLEMENT(test_ucx_memseektell) {
    char *buffer = malloc(16);
    memset(buffer, 32, 7);
    buffer[7] = 0;

    UcxMemstream *m = ucx_memopen(buffer, 16);
    int r;

    UCX_TEST_BEGIN

    r = ucx_memseek(m, 5, SEEK_SET);
    UCX_TEST_ASSERT(r == 0, "seek SET+5 failed");
    UCX_TEST_ASSERT(ucx_memtell(m) == 5, "seek SET+5 set wrong position");

    r = ucx_memseek(m, 20, SEEK_SET);
    UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail");
    UCX_TEST_ASSERT(ucx_memtell(m) == 5,
            "failed seek shall leave pos unchanged");

    r = ucx_memseek(m, 5, SEEK_CUR);
    UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed");
    UCX_TEST_ASSERT(ucx_memtell(m) == 10, "seek CUR+5 set wrong position");

    r = ucx_memseek(m, 10, SEEK_CUR);
    UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail");
    UCX_TEST_ASSERT(ucx_memtell(m) == 10,
            "failed seek shall leave pos unchanged");

    r = ucx_memseek(m, -5, SEEK_END);
    UCX_TEST_ASSERT(r == 0, "seek END-5 failed");
    UCX_TEST_ASSERT(ucx_memtell(m) == 2, "seek END-5 set wrong position");

    r = ucx_memseek(m, -10, SEEK_END);
    UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail");
    UCX_TEST_ASSERT(ucx_memtell(m) == 2,
            "failed seek shall leave pos unchanged");

    UCX_TEST_END

    ucx_memclose(m);
    free(buffer);
}

UCX_TEST_IMPLEMENT(test_ucx_memputc) {
    char *buffer = malloc(16);
    memset(buffer, 32, 16);

    UcxMemstream *m = ucx_memopen(buffer, 16);
    int r;

    UCX_TEST_BEGIN

    ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48);
    UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts");
    ucx_memseek(m, 10, SEEK_CUR);
    ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48);
    UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after last 3 puts");
    UCX_TEST_ASSERT(ucx_memeof(m), "eof not set");
    UCX_TEST_ASSERT(ucx_memputc(m, 48) == EOF, "put shall return EOF on memof");
    UCX_TEST_ASSERT(memcmp(buffer, "000          000", 16) == 0,
            "buffer contains incorrect content");

    UCX_TEST_END

    ucx_memclose(m);
    free(buffer);
}

UCX_TEST_IMPLEMENT(test_ucx_memgetc) {
    char *buffer = malloc(16);
    memset(buffer, 32, 8);
    for (int i = 8; i < 16 ; i++) {
        buffer[i] = 40+i;
    }

    UcxMemstream *m = ucx_memopen(buffer, 16);
    int r;

    UCX_TEST_BEGIN

    char rb[16];
    for (int i = 0 ; i < 16 ; i++) {
        UCX_TEST_ASSERT(ucx_memtell(m) == i, "pos wrong during read loop");
        UCX_TEST_ASSERT(!ucx_memeof(m),
                "EOF shall not be set during read loop");
        rb[i] = ucx_memgetc(m);
    }
    UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after read loop");
    UCX_TEST_ASSERT(ucx_memeof(m), "EOF not set");
    UCX_TEST_ASSERT(memcmp(rb, "        01234567", 16) == 0,
            "read data incorrect");

    UCX_TEST_END

    ucx_memclose(m);
    free(buffer);
}

UCX_TEST_IMPLEMENT(test_ucx_memwrite) {
    char *buffer = malloc(16);
    memset(buffer, 32, 8);
    for (int i = 8; i < 16 ; i++) {
        buffer[i] = 40+i;
    }

    UcxMemstream *m = ucx_memopen(buffer, 16);
    int r;

    UCX_TEST_BEGIN

    char* teststring = "this is way too much";
    r = ucx_memwrite(teststring, 1, 20, m);
    UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
    UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
            "buffer data incorrect");
    UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set");

    ucx_memseek(m, 8, SEEK_SET);
    r = ucx_memwrite("not", 1, 3, m);
    UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
    UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
            "modified buffer is incorrect");

    char* threebytestring = "  t  h  r  e  e   ";
    memset(buffer, 49, 16);
    ucx_memseek(m, 0, SEEK_SET);
    r = ucx_memwrite(threebytestring, 3, 6, m);
    UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed");
    UCX_TEST_ASSERT(ucx_memtell(m) == 15,
            "position after write of three byte string incorrect");
    UCX_TEST_ASSERT(!ucx_memeof(m), "eof shall not be set");
    UCX_TEST_ASSERT(memcmp(buffer, "  t  h  r  e  e1", 16) == 0,
                "bufer is incorrect after three byte string has been written");

    UCX_TEST_END

    ucx_memclose(m);
    free(buffer);
}

UCX_TEST_IMPLEMENT(test_ucx_memread) {
    char *buffer = malloc(16);
    memset(buffer, 56, 8);
    for (int i = 8; i < 16 ; i++) {
        buffer[i] = 40+i;
    }

    UcxMemstream *m = ucx_memopen(buffer, 16);
    int r;

    UCX_TEST_BEGIN

    char rb[16];
    memset(rb, 32, 16);

    ucx_memseek(m, 8, SEEK_SET);
    r = ucx_memread(rb, 1, 16, m);
    UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end");
    UCX_TEST_ASSERT(memcmp(rb, "01234567        ", 16) == 0,
            "buffer incorrect after first read");
    UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set");

    ucx_memseek(m, 0, SEEK_SET);
    r = ucx_memread(rb+8, 1, 8, m);
    UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes");
    UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0,
                "buffer incorrect after second read");

    ucx_memseek(m, 0, SEEK_SET);
    r = ucx_memread(rb, 3, 6, m);
    UCX_TEST_ASSERT(r == 15,
            "three byte read did not read the desired amount of bytes");
    UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0,
                    "buffer incorrect after three byte read");

    UCX_TEST_END

    ucx_memclose(m);
    free(buffer);
}

mercurial