From 886af0816a986e169e8754188a955ea822054251 Mon Sep 17 00:00:00 2001 From: Aleksandar Fabijanic Date: Wed, 25 Apr 2012 04:43:01 +0000 Subject: [PATCH] FIFOBuffer implementation and tests --- Foundation/Foundation_CE_vs90.vcproj | 13462 +++++++++--------- Foundation/Foundation_vs100.vcxproj | 1 + Foundation/Foundation_vs100.vcxproj.filters | 3 + Foundation/Foundation_vs71.vcproj | 3 + Foundation/Foundation_vs80.vcproj | 4 + Foundation/Foundation_vs90.vcproj | 4 + Foundation/include/Poco/Buffer.h | 4 +- Foundation/include/Poco/FIFOBuffer.h | 211 + Foundation/testsuite/src/CoreTest.cpp | 188 + Foundation/testsuite/src/CoreTest.h | 2 + 10 files changed, 7152 insertions(+), 6730 deletions(-) create mode 100644 Foundation/include/Poco/FIFOBuffer.h diff --git a/Foundation/Foundation_CE_vs90.vcproj b/Foundation/Foundation_CE_vs90.vcproj index fef6e5942..387780d02 100644 --- a/Foundation/Foundation_CE_vs90.vcproj +++ b/Foundation/Foundation_CE_vs90.vcproj @@ -1,6729 +1,6733 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Foundation/Foundation_vs100.vcxproj b/Foundation/Foundation_vs100.vcxproj index 0f5f5feb8..36fb912dc 100644 --- a/Foundation/Foundation_vs100.vcxproj +++ b/Foundation/Foundation_vs100.vcxproj @@ -988,6 +988,7 @@ + diff --git a/Foundation/Foundation_vs100.vcxproj.filters b/Foundation/Foundation_vs100.vcxproj.filters index 7a25aacad..88c187504 100644 --- a/Foundation/Foundation_vs100.vcxproj.filters +++ b/Foundation/Foundation_vs100.vcxproj.filters @@ -1799,6 +1799,9 @@ Core\Header Files + + Core\Header Files + diff --git a/Foundation/Foundation_vs71.vcproj b/Foundation/Foundation_vs71.vcproj index 1c3ff169b..25639d03b 100644 --- a/Foundation/Foundation_vs71.vcproj +++ b/Foundation/Foundation_vs71.vcproj @@ -841,6 +841,9 @@ + + diff --git a/Foundation/Foundation_vs80.vcproj b/Foundation/Foundation_vs80.vcproj index 49e625610..7a561585d 100644 --- a/Foundation/Foundation_vs80.vcproj +++ b/Foundation/Foundation_vs80.vcproj @@ -1131,6 +1131,10 @@ RelativePath=".\include\Poco\Exception.h" > + + diff --git a/Foundation/Foundation_vs90.vcproj b/Foundation/Foundation_vs90.vcproj index 5912a465b..0caec5b51 100644 --- a/Foundation/Foundation_vs90.vcproj +++ b/Foundation/Foundation_vs90.vcproj @@ -1128,6 +1128,10 @@ RelativePath=".\include\Poco\Exception.h" > + + diff --git a/Foundation/include/Poco/Buffer.h b/Foundation/include/Poco/Buffer.h index bcb6e99cc..d2f244722 100644 --- a/Foundation/include/Poco/Buffer.h +++ b/Foundation/include/Poco/Buffer.h @@ -77,6 +77,8 @@ public: /// new buffer. NewSize can be larger or smaller than /// the current size, but it must not be 0. { + poco_assert(newSize); + T* ptr = new T[newSize]; if (preserveContent) { @@ -87,7 +89,7 @@ public: _ptr = ptr; _size = newSize; } - + std::size_t size() const /// Returns the size of the buffer. { diff --git a/Foundation/include/Poco/FIFOBuffer.h b/Foundation/include/Poco/FIFOBuffer.h new file mode 100644 index 000000000..84a2736e7 --- /dev/null +++ b/Foundation/include/Poco/FIFOBuffer.h @@ -0,0 +1,211 @@ +// +// FIFOBuffer.h +// +// $Id: //poco/1.4/Foundation/include/Poco/FIFOBuffer.h#2 $ +// +// Library: Foundation +// Package: Core +// Module: FIFOBuffer +// +// Definition of the FIFOBuffer class. +// +// Copyright (c) 2006, Applied Informatics Software Engineering GmbH. +// and Contributors. +// +// Permission is hereby granted, free of charge, to any person or organization +// obtaining a copy of the software and accompanying documentation covered by +// this license (the "Software") to use, reproduce, display, distribute, +// execute, and transmit the Software, and to prepare derivative works of the +// Software, and to permit third-parties to whom the Software is furnished to +// do so, all subject to the following: +// +// The copyright notices in the Software and this entire statement, including +// the above license grant, this restriction and the following disclaimer, +// must be included in all copies of the Software, in whole or in part, and +// all derivative works of the Software, unless such copies or derivative +// works are solely in the form of machine-executable object code generated by +// a source language processor. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT +// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE +// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, +// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. +// + + +#ifndef Foundation_FIFOBuffer_INCLUDED +#define Foundation_FIFOBuffer_INCLUDED + + +#include "Poco/Foundation.h" +#include "Poco/Exception.h" +#include "Poco/Buffer.h" +#include "Poco/Mutex.h" +#include "Poco/Format.h" + + +namespace Poco { + + +template +class FIFOBuffer + /// A simple buffer class with support for re-entrant, + /// FIFO-style read/write operations as well as + /// introspection into the size of buffer, amount of unread data + /// and available space. + /// + /// This class is useful everywhere where a FIFO functionality + /// is needed. +{ +public: + FIFOBuffer(std::size_t size): + _buffer(size), + _used(0) + /// Creates and allocates the FIFOBuffer. + { + } + + ~FIFOBuffer() + /// Destroys the FIFOBuffer. + { + } + + void resize(std::size_t newSize, bool preserveContent = true) + /// Resizes the buffer. If preserveContent is true, + /// the content of the old buffer is preserved. + /// New size can be larger or smaller than + /// the current size, but it must not be 0. + /// Additionally, if the new length is smaller + /// than currently used length and preserveContent + /// is true, InvalidAccessException is thrown. + { + Mutex::ScopedLock lock(_mutex); + + if (preserveContent && (newSize < _used)) + throw InvalidAccessException("Can not resize FIFO without data loss."); + + _buffer.resize(newSize, preserveContent); + if (_used > _buffer.size()) + _used = _buffer.size(); + } + + Buffer& peek(Poco::Buffer& buffer, std::size_t length = 0) const + /// Peeks into the data currently in the FIFO + /// without actually extracting it. + /// Resizes the supplied buffer to the size of + ///.data written to it. If length is not + /// supplied by the caller, the current FIFO used length + /// is substituted for it. + /// + /// Returns the reference to the buffer. + { + Mutex::ScopedLock lock(_mutex); + + if (0 == length || length > _used) length = _used; + poco_assert (length <= _buffer.size()); + buffer.resize(length); + std::memcpy(buffer.begin(), _buffer.begin(), length * sizeof(T)); + return buffer; + } + + Buffer& read(Poco::Buffer& buffer, std::size_t length = 0) + /// Copies the data currently in the FIFO + /// into the supplied buffer. + /// Resizes the supplied buffer to the size of + ///.data written to it. + /// + /// Returns the reference to the buffer. + { + Mutex::ScopedLock lock(_mutex); + + std::size_t usedBefore = _used; + peek(buffer, length); + _used -= buffer.size(); + if (_used) + std::memmove(_buffer.begin(), _buffer.begin() + usedBefore - _used, _used); + + return buffer; + } + + std::size_t write(const Buffer& buffer) + /// Writes data.size() to the FIFO buffer. + /// If there is no sufficient space for the whole + /// buffer to be written, data up to available + /// length is written. + /// + /// Returns the length of data written. + { + Mutex::ScopedLock lock(_mutex); + poco_assert (_used <= _buffer.size()); + std::size_t available = _buffer.size() - _used; + std::size_t len = buffer.size() > available ? available : buffer.size(); + std::memcpy(_buffer.begin() + _used, buffer.begin(), len * sizeof(T)); + _used += len; + poco_assert (_used <= _buffer.size()); + return len; + } + + std::size_t size() const + /// Returns the size of the buffer. + { + return _buffer.size(); + } + + std::size_t used() const + /// Returns the size of the used portion of the buffer. + { + return _used; + } + + std::size_t available() const + /// Returns the size of the available portion of the buffer. + { + return _buffer.size() - _used; + } + + T& operator [] (std::size_t index) + /// Returns value at index position. + /// Throws InvalidAccessException if index is larger than + /// the last valid (used) buffer position. + { + if (index >= _used) + throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1)); + + return _buffer[index]; + } + + const T& operator [] (std::size_t index) const + /// Returns value at index position. + /// Throws InvalidAccessException if index is larger than + /// the last valid (used) buffer position. + { + if (index >= _used) + throw InvalidAccessException(format("Index out of bounds: %z (max index allowed: %z)", index, _used - 1)); + + return _buffer[index]; + } + + bool isEmpty() const + /// Returns true is buffer is empty, flase otherwise. + { + return 0 == _used; + } + +private: + FIFOBuffer(); + FIFOBuffer(const FIFOBuffer&); + FIFOBuffer& operator = (const FIFOBuffer&); + + Buffer _buffer; + std::size_t _used; + mutable Mutex _mutex; +}; + + +} // namespace Poco + + +#endif // Foundation_FIFOBuffer_INCLUDED diff --git a/Foundation/testsuite/src/CoreTest.cpp b/Foundation/testsuite/src/CoreTest.cpp index 2c12ecdbc..2f2527da3 100644 --- a/Foundation/testsuite/src/CoreTest.cpp +++ b/Foundation/testsuite/src/CoreTest.cpp @@ -39,9 +39,11 @@ #include "Poco/Thread.h" #include "Poco/Runnable.h" #include "Poco/Buffer.h" +#include "Poco/FIFOBuffer.h" #include "Poco/AtomicCounter.h" #include "Poco/Nullable.h" #include "Poco/Ascii.h" +#include "Poco/Exception.h" #include #include #include @@ -53,9 +55,11 @@ using Poco::Environment; using Poco::Thread; using Poco::Runnable; using Poco::Buffer; +using Poco::FIFOBuffer; using Poco::AtomicCounter; using Poco::Nullable; using Poco::Ascii; +using Poco::InvalidAccessException; namespace @@ -206,6 +210,188 @@ void CoreTest::testBuffer() } +void CoreTest::testFIFOBufferChar() +{ + typedef char T; + + FIFOBuffer f(20); + Buffer b(10); + std::vector v; + + for (T c = '0'; c < '0' + 10; ++c) + v.push_back(c); + + std::memcpy(b.begin(), &v[0], sizeof(T) * v.size()); + f.write(b); + assert (20 == f.size()); + assert (10 == f.used()); + assert (!f.isEmpty()); + assert ('0' == f[0]); + assert ('1' == f[1]); + assert ('2' == f[2]); + assert ('3' == f[3]); + assert ('4' == f[4]); + assert ('5' == f[5]); + assert ('6' == f[6]); + assert ('7' == f[7]); + assert ('8' == f[8]); + assert ('9' == f[9]); + + b.resize(5); + f.read(b, b.size()); + assert (20 == f.size()); + assert (5 == f.used()); + assert (!f.isEmpty()); + assert ('5' == f[0]); + assert ('6' == f[1]); + assert ('7' == f[2]); + assert ('8' == f[3]); + assert ('9' == f[4]); + try { T i = f[10]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + v.clear(); + for (T c = 'a'; c < 'a' + 10; ++c) + v.push_back(c); + + b.resize(10); + std::memcpy(b.begin(), &v[0], sizeof(T) * v.size()); + f.write(b); + assert (20 == f.size()); + assert (15 == f.used()); + assert (!f.isEmpty()); + assert ('5' == f[0]); + assert ('6' == f[1]); + assert ('7' == f[2]); + assert ('8' == f[3]); + assert ('9' == f[4]); + assert ('a' == f[5]); + assert ('b' == f[6]); + assert ('c' == f[7]); + assert ('d' == f[8]); + assert ('e' == f[9]); + assert ('f' == f[10]); + assert ('g' == f[11]); + assert ('h' == f[12]); + assert ('i' == f[13]); + assert ('j' == f[14]); + try { T i = f[15]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + f.read(b, 10); + assert (20 == f.size()); + assert (5 == f.used()); + assert (!f.isEmpty()); + assert ('f' == f[0]); + assert ('g' == f[1]); + assert ('h' == f[2]); + assert ('i' == f[3]); + assert ('j' == f[4]); + try { T i = f[5]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + f.read(b, 6); + assert (5 == b.size()); + assert (20 == f.size()); + assert (0 == f.used()); + try { T i = f[0]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + assert (f.isEmpty()); +} + + +void CoreTest::testFIFOBufferInt() +{ + typedef char T; + + FIFOBuffer f(20); + Buffer b(10); + std::vector v; + + for (T c = 0; c < 10; ++c) + v.push_back(c); + + std::memcpy(b.begin(), &v[0], sizeof(T) * v.size()); + f.write(b); + assert (20 == f.size()); + assert (10 == f.used()); + assert (!f.isEmpty()); + assert (0 == f[0]); + assert (1 == f[1]); + assert (2 == f[2]); + assert (3 == f[3]); + assert (4 == f[4]); + assert (5 == f[5]); + assert (6 == f[6]); + assert (7 == f[7]); + assert (8 == f[8]); + assert (9 == f[9]); + + b.resize(5); + f.read(b, b.size()); + assert (20 == f.size()); + assert (5 == f.used()); + assert (!f.isEmpty()); + assert (5 == f[0]); + assert (6 == f[1]); + assert (7 == f[2]); + assert (8 == f[3]); + assert (9 == f[4]); + try { T i = f[10]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + v.clear(); + for (T c = 10; c < 20; ++c) + v.push_back(c); + + b.resize(10); + std::memcpy(b.begin(), &v[0], sizeof(T) * v.size()); + f.write(b); + assert (20 == f.size()); + assert (15 == f.used()); + assert (!f.isEmpty()); + assert (5 == f[0]); + assert (6 == f[1]); + assert (7 == f[2]); + assert (8 == f[3]); + assert (9 == f[4]); + assert (10 == f[5]); + assert (11 == f[6]); + assert (12 == f[7]); + assert (13 == f[8]); + assert (14 == f[9]); + assert (15 == f[10]); + assert (16 == f[11]); + assert (17 == f[12]); + assert (18 == f[13]); + assert (19 == f[14]); + try { T i = f[15]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + f.read(b, 10); + assert (20 == f.size()); + assert (5 == f.used()); + assert (!f.isEmpty()); + assert (15 == f[0]); + assert (16 == f[1]); + assert (17 == f[2]); + assert (18 == f[3]); + assert (19 == f[4]); + try { T i = f[5]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + f.read(b, 6); + assert (5 == b.size()); + assert (20 == f.size()); + assert (0 == f.used()); + try { T i = f[0]; fail ("must fail"); } + catch (InvalidAccessException&) { } + + assert (f.isEmpty()); +} + + void CoreTest::testAtomicCounter() { AtomicCounter ac; @@ -376,6 +562,8 @@ CppUnit::Test* CoreTest::suite() CppUnit_addTest(pSuite, CoreTest, testBugcheck); CppUnit_addTest(pSuite, CoreTest, testEnvironment); CppUnit_addTest(pSuite, CoreTest, testBuffer); + CppUnit_addTest(pSuite, CoreTest, testFIFOBufferChar); + CppUnit_addTest(pSuite, CoreTest, testFIFOBufferInt); CppUnit_addTest(pSuite, CoreTest, testAtomicCounter); CppUnit_addTest(pSuite, CoreTest, testNullable); CppUnit_addTest(pSuite, CoreTest, testAscii); diff --git a/Foundation/testsuite/src/CoreTest.h b/Foundation/testsuite/src/CoreTest.h index d841cfe3c..0dbd846b0 100644 --- a/Foundation/testsuite/src/CoreTest.h +++ b/Foundation/testsuite/src/CoreTest.h @@ -52,6 +52,8 @@ public: void testFPE(); void testEnvironment(); void testBuffer(); + void testFIFOBufferChar(); + void testFIFOBufferInt(); void testAtomicCounter(); void testNullable(); void testAscii();