bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2017-2018 Dovecot authors, see the included COPYING file */
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen#include "test-lib.h"
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen#include "buffer.h"
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen#include "memarea.h"
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainenstatic bool test_callback_called = FALSE;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainenstatic void test_callback(buffer_t *buf)
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen{
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(!test_callback_called);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_callback_called = TRUE;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen buffer_free(&buf);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen}
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainenvoid test_memarea(void)
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen{
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen struct memarea *area, *area2;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen buffer_t *buf;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen size_t size;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_begin("memarea");
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen buf = buffer_create_dynamic(default_pool, 128);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen buffer_append(buf, "123", 3);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen area = memarea_init(buf->data, buf->used, test_callback, buf);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(memarea_get_refcount(area) == 1);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(memarea_get(area, &size) == buf->data && size == buf->used);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen area2 = area;
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen memarea_ref(area2);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(memarea_get_refcount(area2) == 2);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(memarea_get(area2, &size) == buf->data && size == buf->used);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen memarea_unref(&area2);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(area2 == NULL);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(!test_callback_called);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen memarea_unref(&area);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_assert(test_callback_called);
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen test_end();
4c7d704408fc77467143a945dc2d0a856f72e674Timo Sirainen}