tstRTCircBuf.cpp revision 292dc462b10ba7ef37b871434e332aecf8ad97df
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/* $Id$ */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/** @file
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * IPRT Testcase - Lock free circular buffers.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/*
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Copyright (C) 2010 Oracle Corporation
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * available from http://www.virtualbox.org. This file is free software;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * you can redistribute it and/or modify it under the terms of the GNU
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * General Public License (GPL) as published by the Free Software
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * The contents of this file may alternatively be used under the terms
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * of the Common Development and Distribution License Version 1.0
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * VirtualBox OSE distribution, in which case the provisions of the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * CDDL are applicable instead of those of the GPL.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * You may elect to license modified versions of this file under the
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * terms and conditions of either the GPL or the CDDL or both.
df504dfa2b5b186f4cac7f7859eabd09b30eebf0vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/*******************************************************************************
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync* Header Files *
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync*******************************************************************************/
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#include <iprt/circbuf.h>
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#include <iprt/err.h>
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#include <iprt/string.h>
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync#include <iprt/test.h>
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync/**
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync * Basic API checks.
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsyncstatic void tst1(void)
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync{
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync void *pvBuf;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync size_t cbSize;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync char pcTestPattern1[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync char pcTestPattern2[] = { 0x8, 0x9, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9 };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync char pcTestPattern3[] = { 0x5, 0x6, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Create */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTestISub("Creation");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync PRTCIRCBUF pBuf;
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK_RC(RTCircBufCreate(&pBuf, 10), VINF_SUCCESS);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Full write */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTestISub("Full write");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(cbSize == 10);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync memcpy(pvBuf, pcTestPattern1, 10);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufReleaseWriteBlock(pBuf, 10);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync// RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern1, 10) == 0); /* Check the internal state */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Half read */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTestISub("Half read");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufAcquireReadBlock(pBuf, 5, &pvBuf, &cbSize);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(cbSize == 5);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(memcmp(pvBuf, pcTestPattern1, 5) == 0);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufReleaseReadBlock(pBuf, 5);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 5);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 5);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Sub write */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTestISub("Sub write");
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(cbSize == 2);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync memcpy(pvBuf, &pcTestPattern1[8], 2);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTCircBufReleaseWriteBlock(pBuf, 2);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 3);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 7);
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync// RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern2, 10) == 0); /* Check the internal state */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Split tests */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync /* Split read */
e0e0c19eefceaf5d4ec40f9466b58a771f50e799vboxsync RTTestISub("Split read");
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufAcquireReadBlock(pBuf, 7, &pvBuf, &cbSize);
df504dfa2b5b186f4cac7f7859eabd09b30eebf0vboxsync RTTESTI_CHECK(cbSize == 5);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(memcmp(pvBuf, &pcTestPattern1[5], 5) == 0);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufReleaseReadBlock(pBuf, 5);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 8);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 2);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufAcquireReadBlock(pBuf, 2, &pvBuf, &cbSize);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(cbSize == 2);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(memcmp(pvBuf, &pcTestPattern1[8], 2) == 0);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufReleaseReadBlock(pBuf, 2);
df504dfa2b5b186f4cac7f7859eabd09b30eebf0vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 10);
df504dfa2b5b186f4cac7f7859eabd09b30eebf0vboxsync RTTESTI_CHECK(RTCircBufUsed(pBuf) == 0);
df504dfa2b5b186f4cac7f7859eabd09b30eebf0vboxsync
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync /* Split write */
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTestISub("Split write");
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufAcquireWriteBlock(pBuf, 10, &pvBuf, &cbSize);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(cbSize == 8);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync memcpy(pvBuf, pcTestPattern1, 8);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTCircBufReleaseWriteBlock(pBuf, 8);
106ee6c5692ad0546845ce72b29e0fddbed2a455vboxsync RTTESTI_CHECK(RTCircBufFree(pBuf) == 2);
RTTESTI_CHECK(RTCircBufUsed(pBuf) == 8);
RTCircBufAcquireWriteBlock(pBuf, 2, &pvBuf, &cbSize);
RTTESTI_CHECK(cbSize == 2);
memcpy(pvBuf, &pcTestPattern1[5], 2);
RTCircBufReleaseWriteBlock(pBuf, 2);
RTTESTI_CHECK(RTCircBufFree(pBuf) == 0);
RTTESTI_CHECK(RTCircBufUsed(pBuf) == 10);
// RTTESTI_CHECK(memcmp(pBuf->pvBuf, pcTestPattern3, 10) == 0); /* Check the internal state */
/* Destroy */
RTCircBufDestroy(pBuf);
}
int main()
{
RTTEST hTest;
int rc = RTTestInitAndCreate("tstRTCircBuf", &hTest);
if (rc)
return rc;
RTTestBanner(hTest);
tst1();
return RTTestSummaryAndDestroy(hTest);
}