예제 #1
0
/* Attempts to write the specified number of bytes to the stream, returning the
 * number of bytes written. */
size_t
ubik_stream_write(struct ubik_stream *dst, void *src, size_t len)
{
        size_t written;
        ubik_error err;

        switch (dst->stream_type)
        {
        case STREAM_TYPE_FILE_R:
                return 0;
        case STREAM_TYPE_FILE_W:
                written = fwrite(src, 1, len, dst->file);
#ifdef UBIK_EAGER_FLUSH
                fflush(dst->file);
#endif
                return written;
        case STREAM_TYPE_BUFFER:
                err = OK;
                if (len + dst->buffer->write >= dst->buffer->end)
                        err = _buf_realloc(dst->buffer, len);
                if (err != OK)
                        return 0;
                memcpy(dst->buffer->write, src, len);
                dst->buffer->write += len;
                return len;
        case STREAM_TYPE_GENERATOR:
                if (dst->gen->write != NULL)
                        return dst->gen->write(dst->gen, src, len);
                return 0;
        }
        return 0;
}
예제 #2
0
END_TEST


START_TEST (test_buf_reallocation)
{
  /* Given that I have an empty buffer */
  ta_buf_t b = TA_BUF_INIT;

  /* When I initialize it with a given size */
  ta_buf_alloc (&b, 5);

  /* Then I see that the internal allocated size should be multiplied
   * for 1.5 and aligned to be multiple of 8 */
  fail_if (b.allocated_size != 8, 0);

  /* When I realloc it again */
  _buf_realloc (&b, 35);

  /* Then I see that the allocated size was both multiplied and
   * aligned */
  fail_if (b.allocated_size != 48, "The allocated size is wrong", 0);
}