aboutsummaryrefslogtreecommitdiffstats
path: root/test/base/ringbuffer.cpp
blob: 16060570eb6229df8b7879cf54f9f9c9e3854e7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * 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 <cstddef>
#include <cstring>

#include <cppunit/extensions/HelperMacros.h>

#include "ringbuffer.hpp"

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_EQUAL(-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_EQUAL(-1, rbuff->read(buffer,-1));
		}

		// check the returnvalue when reading on a empty ringbuffer
		void testReadEmptyRingBuffer()
		{
			char buffer[10];
			CPPUNIT_ASSERT_EQUAL(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_EQUAL(-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_EQUAL(-1, rbuff->write(buffer,-1));
		}

		// checks the retrunvalue when writeing over the limits of a
		// ringbuffer
		void testWriteOverflow()
		{
			char buffer[20];
			CPPUNIT_ASSERT_EQUAL(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_EQUAL(3, rbuff->write(w1Buffer,3));
			CPPUNIT_ASSERT_EQUAL(3, rbuff->write(w1Buffer,3));
			CPPUNIT_ASSERT_EQUAL(3, rbuff->write(w1Buffer,3));
			CPPUNIT_ASSERT_EQUAL(4, rbuff->write(w2Buffer,4));
			CPPUNIT_ASSERT_EQUAL(6, rbuff->read(rBuffer,6));
			CPPUNIT_ASSERT_EQUAL(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_EQUAL(3, rbuff->write(w1Buffer,3));
			CPPUNIT_ASSERT_EQUAL(4, rbuff->write(w2Buffer,4));
			CPPUNIT_ASSERT_EQUAL(7, rbuff->read(rBuffer,7));
			CPPUNIT_ASSERT_EQUAL(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_EQUAL(4, rbuff->write(wBuffer,4));
			CPPUNIT_ASSERT_EQUAL(4, rbuff->read(rBuffer,5));
			CPPUNIT_ASSERT_EQUAL(0, strcmp("AAA",rBuffer));
		}

		// check if the correct ringbuffer size is reported
		void testCurrentSize()
		{
			char w1Buffer[4]="AAA";
			char w2Buffer[5]="BBBB";
			CPPUNIT_ASSERT_EQUAL(4, rbuff->write(w1Buffer,4));
			CPPUNIT_ASSERT_EQUAL(5, rbuff->write(w2Buffer,5));
			CPPUNIT_ASSERT_EQUAL(10, rbuff->get_size());
		}

		// check if the available space in the ringbuffer
		void testCurrentAvailable()
		{
			char w1Buffer[4]="AAA";
			char w2Buffer[5]="BBBB";
			CPPUNIT_ASSERT_EQUAL(4, rbuff->write(w1Buffer,4));
			CPPUNIT_ASSERT_EQUAL(5, rbuff->write(w2Buffer,5));
			CPPUNIT_ASSERT_EQUAL(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_EQUAL(4, rbuff->write(w1Buffer,4));
			rbuff->flush();
			CPPUNIT_ASSERT_EQUAL(4, rbuff->write(w2Buffer,4));
			CPPUNIT_ASSERT_EQUAL(4, rbuff->read(rBuffer,4));
			CPPUNIT_ASSERT_EQUAL(0, strcmp("BBB",rBuffer));
		}

	};

	CPPUNIT_TEST_SUITE_REGISTRATION(RingBufferTest);
};