diff options
author | Robin Nehls <nehls@mi.fu-berlin.de> | 2010-01-05 19:10:52 +0100 |
---|---|---|
committer | Alexander Sulfrian <alexander@sulfrian.net> | 2013-01-05 17:17:43 +0100 |
commit | 63b09d923c6d1dcee03ac0ec84c547864f7870f5 (patch) | |
tree | 76ec25b8a6145ea570c24868dbfcbfd02a280830 | |
parent | 7193f8eed605ee0eb9b437d228235605f67bfc0d (diff) | |
download | usdx-63b09d923c6d1dcee03ac0ec84c547864f7870f5.tar.gz usdx-63b09d923c6d1dcee03ac0ec84c547864f7870f5.tar.xz usdx-63b09d923c6d1dcee03ac0ec84c547864f7870f5.zip |
Added test for ringbuffer.cpp
Diffstat (limited to '')
-rw-r--r-- | test/base/ringbuffer.cpp | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/test/base/ringbuffer.cpp b/test/base/ringbuffer.cpp new file mode 100644 index 00000000..99631108 --- /dev/null +++ b/test/base/ringbuffer.cpp @@ -0,0 +1,181 @@ +/* + * UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "ringbuffer.hpp" +#include <cstring> +#include <cppunit/extensions/HelperMacros.h> + +namespace usdx +{ + class RingBufferTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(RingBufferTest); + CPPUNIT_TEST(testReadNullBuffer); + CPPUNIT_TEST(testReadNegativeCount); + CPPUNIT_TEST(testReadEmptyRingBuffer); + CPPUNIT_TEST(testWriteNullBuffer); + CPPUNIT_TEST(testWriteNegativeCount); + CPPUNIT_TEST(testWriteOverflow); + CPPUNIT_TEST(testWriteOverflow2); + CPPUNIT_TEST(testWriteRead); + CPPUNIT_TEST(testReadMoreThanAvailable); + CPPUNIT_TEST(testCurrentSize); + CPPUNIT_TEST(testCurrentAvailable); + CPPUNIT_TEST(testFlush); + CPPUNIT_TEST_SUITE_END(); + private: + RingBuffer *rbuff; + public: + void setUp() + { + rbuff = new RingBuffer(10); + + } + + void tearDown() + { + delete rbuff; + } + + // check if the read function reports an error in form of a + // negative value when the given buffer is NULL + void testReadNullBuffer() + { + CPPUNIT_ASSERT( -1 == rbuff->read(NULL,10) ); + } + + // check if the read function reports an error in form of a + // negative value when the given count is negative + void testReadNegativeCount() + { + char buffer[10]; + CPPUNIT_ASSERT( -1 == rbuff->read(buffer,-1) ); + } + + // check the returnvalue when reading on a empty ringbuffer + void testReadEmptyRingBuffer() + { + char buffer[10]; + CPPUNIT_ASSERT( 0 == rbuff->read(buffer,10) ); + } + + // check in the read function reports an error in form of a + // negative value when the given buffer is NULL + void testWriteNullBuffer() + { + CPPUNIT_ASSERT( -1 == rbuff->write(NULL,10) ); + } + + // check if the read function reports an error in form of a + // negative value when the given count is negative + void testWriteNegativeCount() + { + char buffer[10]; + CPPUNIT_ASSERT( -1 == rbuff->write(buffer,-1) ); + } + + // checks the retrunvalue when writeing over the limits of a + // ringbuffer + void testWriteOverflow() + { + char buffer[20]; + CPPUNIT_ASSERT( 0 == rbuff->read(buffer,20) ); + } + + // check if the read value is ajusted correctly, when + // a buffer overflow happens + void testWriteOverflow2() + { + char w1Buffer[4]="AAA"; + char w2Buffer[4]="BBB"; + char rBuffer[7]="CCCCCC"; + CPPUNIT_ASSERT( 3 == rbuff->write(w1Buffer,3) ); + CPPUNIT_ASSERT( 3 == rbuff->write(w1Buffer,3) ); + CPPUNIT_ASSERT( 3 == rbuff->write(w1Buffer,3) ); + CPPUNIT_ASSERT( 4 == rbuff->write(w2Buffer,4) ); + CPPUNIT_ASSERT( 6 == rbuff->read(rBuffer,6) ); + CPPUNIT_ASSERT( 0 == strcmp("AAAAAA",rBuffer) ); + } + + // check if inputdata equals outputdata + void testWriteRead() + { + char w1Buffer[4]="AAA"; + char w2Buffer[4]="BBB"; + char rBuffer[7]="CCCCCC"; + CPPUNIT_ASSERT( 3 == rbuff->write(w1Buffer,3) ); + CPPUNIT_ASSERT( 4 == rbuff->write(w2Buffer,4) ); + CPPUNIT_ASSERT( 7 == rbuff->read(rBuffer,7) ); + CPPUNIT_ASSERT( 0 == strcmp("AAABBB",rBuffer) ); + } + + // check what happens when read trys to read more data than + // available + void testReadMoreThanAvailable() + { + char wBuffer[4]="AAA"; + char rBuffer[5]="BBBB"; + CPPUNIT_ASSERT( 4 == rbuff->write(wBuffer,4) ); + CPPUNIT_ASSERT( 4 == rbuff->read(rBuffer,5) ); + CPPUNIT_ASSERT( 0 == strcmp("AAA",rBuffer) ); + } + + // check if the correct ringbuffer size is reported + void testCurrentSize() + { + char w1Buffer[4]="AAA"; + char w2Buffer[5]="BBBB"; + CPPUNIT_ASSERT( 4 == rbuff->write(w1Buffer,4) ); + CPPUNIT_ASSERT( 5 == rbuff->write(w2Buffer,5) ); + CPPUNIT_ASSERT( 10 == rbuff->get_size() ); + } + + // check if the available space in the ringbuffer + void testCurrentAvailable() + { + char w1Buffer[4]="AAA"; + char w2Buffer[5]="BBBB"; + CPPUNIT_ASSERT( 4 == rbuff->write(w1Buffer,4) ); + CPPUNIT_ASSERT( 5 == rbuff->write(w2Buffer,5) ); + CPPUNIT_ASSERT( 9 == rbuff->get_available() ); + } + + // check if the flush function clears the buffer correctly + void testFlush() + { + char w1Buffer[4]="AAA"; + char w2Buffer[4]="BBB"; + char rBuffer[4]="CCC"; + CPPUNIT_ASSERT( 4 == rbuff->write(w1Buffer,4) ); + rbuff->flush(); + CPPUNIT_ASSERT( 4 == rbuff->write(w2Buffer,4) ); + CPPUNIT_ASSERT( 4 == rbuff->read(rBuffer,4) ); + CPPUNIT_ASSERT( 0 == strcmp("BBB",rBuffer) ); + } + + }; + + CPPUNIT_TEST_SUITE_REGISTRATION(RingBufferTest); +}; |