File: ScopePoolTest.cxx

package info (click to toggle)
clam 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 20,981
  • sloc: cpp: 92,504; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (268 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download | duplicates (2)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
 *                         UNIVERSITAT POMPEU FABRA
 *
 *
 * 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; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <cppunit/extensions/HelperMacros.h>
#include "cppUnitHelper.hxx"

#include "Pool.hxx" // CLAM
#include "DataTypes.hxx" // CLAM


namespace CLAMTest
{

class ScopePoolTest;

CPPUNIT_TEST_SUITE_REGISTRATION( ScopePoolTest );

class ScopePoolTest : public CppUnit::TestFixture
{
	CPPUNIT_TEST_SUITE( ScopePoolTest );
	CPPUNIT_TEST( testGetWritePool_ReturnsSameMemory );
	CPPUNIT_TEST( testGetReadPool_ReturnsConstMemory );
	CPPUNIT_TEST( testGetWritePool_withStrings );
	CPPUNIT_TEST( testGetWritePool_withWrongType );
	CPPUNIT_TEST( testGetReadPool_withWrongType );
	CPPUNIT_TEST( testGetReadPool_withoutGetWritePoolFirst );
	CPPUNIT_TEST( testConstruction_givesSizeZeroByDefault );
	CPPUNIT_TEST( testSetSize_overAZeroSizePool );
	CPPUNIT_TEST( testSetSize_overANonZeroSizePool );
	CPPUNIT_TEST( testInsert );
	CPPUNIT_TEST( testRemove );
	CPPUNIT_TEST_SUITE_END();

public:
	/// Common initialization, executed before each test method
	void setUp() { }

	/// Common clean up, executed after each test method
	void tearDown() { }

private:
	void testGetWritePool_ReturnsSameMemory()
	{
		const unsigned poolSize=5;
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");
		
		CLAM::ScopePool pool(spec,poolSize);
		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < poolSize; i++)
			data[i] = i*i;
		CLAM::TData * data2 = pool.GetWritePool<CLAM::TData>("MyAttribute");
		CPPUNIT_ASSERT_EQUAL(data,data2);
	}

	void testGetReadPool_ReturnsConstMemory()
	{
		const unsigned poolSize=5;
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec,poolSize);
		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < poolSize; i++)
			data[i] = i*i;
		const CLAM::ScopePool & pool2 = pool;
		const CLAM::TData * data2 = pool2.GetReadPool<CLAM::TData>("MyAttribute");
		CPPUNIT_ASSERT_EQUAL(const_cast<const CLAM::TData*>(data),data2);
	}

	void testGetWritePool_withStrings()
	{
		const unsigned poolSize=5;
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<std::string>("MyAttribute");

		CLAM::ScopePool pool(spec,poolSize);
		std::string * data = pool.GetWritePool<std::string>("MyAttribute");
		for (unsigned i = 0; i < poolSize; i++)
		{
			std::ostringstream os;
			os << "Hola " << i*i;
			data[i] += os.str();
		}
		const CLAM::ScopePool & pool2 = pool;
		const std::string * data2 = pool2.GetReadPool<std::string>("MyAttribute");
		const std::string expected = "Hola 16";
		CPPUNIT_ASSERT_EQUAL(expected,data2[4]);
	}

	void testGetWritePool_withWrongType()
	{
		const unsigned poolSize=5;
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<std::string>("MyAttribute");

		CLAM::ScopePool pool(spec,poolSize);
		try
		{
			pool.GetWritePool<int>("MyAttribute");
			CPPUNIT_FAIL("Should have thrown an exception");
		}
		catch (CLAM::ErrAssertionFailed & err)
		{
			const std::string expected = std::string("Attribute 'TestScope:MyAttribute' has been used as type '")
					+ typeid(int).name() + "' but it really was of type '" + typeid(std::string).name() + "'";
			CPPUNIT_ASSERT_EQUAL(expected, std::string(err.what()));
		}
	}

	void testGetReadPool_withWrongType()
	{
		const unsigned poolSize=5;
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<std::string>("MyAttribute");

		CLAM::ScopePool pool(spec,poolSize);
		const CLAM::ScopePool & pool2 = pool;
		try
		{
			pool2.GetReadPool<int>("MyAttribute");
			CPPUNIT_FAIL("Should have thrown an exception");
		}
		catch (CLAM::ErrAssertionFailed & err)
		{
			const std::string expected = std::string("Attribute 'TestScope:MyAttribute' has been used as type '")
					+ typeid(int).name() + "' but it really was of type '" + typeid(std::string).name() + "'";
			CPPUNIT_ASSERT_EQUAL(expected, std::string(err.what()));
		}
	}

	void testGetReadPool_withoutGetWritePoolFirst()
	{
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec,20);
		const CLAM::ScopePool & constPool = pool;

		try
		{
			constPool.GetReadPool<CLAM::TData>("MyAttribute");
			CPPUNIT_FAIL("Should have thrown an exception");
		}
		catch (CLAM::ErrAssertionFailed & err)
		{
			const std::string expected = "Getting data from a non instanciated attribute 'TestScope':'MyAttribute'";
			CPPUNIT_ASSERT_EQUAL(expected, std::string(err.what()));
		}
	}

	void testConstruction_givesSizeZeroByDefault()
	{
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec);
		CPPUNIT_ASSERT_EQUAL(0u,pool.GetSize());

	}

	void testSetSize_overAZeroSizePool()
	{
		const unsigned poolSize=5;

		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec);

		pool.SetSize(poolSize);

		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < poolSize; i++)
			data[i] = i*i;
		CLAM::TData * data2 = pool.GetWritePool<CLAM::TData>("MyAttribute");
		CPPUNIT_ASSERT_EQUAL(data,data2);
	}

	void testSetSize_overANonZeroSizePool()
	{
		const unsigned poolSize=5;

		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec,7);

		pool.SetSize(poolSize);

		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < poolSize; i++)
			data[i] = i*i;
		CLAM::TData * data2 = pool.GetWritePool<CLAM::TData>("MyAttribute");
		CPPUNIT_ASSERT_EQUAL(data,data2);
	}

	void testInsert()
	{
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec,3);

		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < pool.GetSize(); i++)
			data[i] = 1000 + i*i;

		pool.Insert(2);

		unsigned expectedSize = 4;
		CLAM::TData expected[]={1000,1001,0,1004};
		CPPUNIT_ASSERT_EQUAL(expectedSize,pool.GetSize());
		const CLAM::TData * result = pool.GetReadPool<CLAM::TData>("MyAttribute");
		for (unsigned i=0; i<pool.GetSize(); i++)
			CPPUNIT_ASSERT_EQUAL(expected[i],result[i]);
	}
	void testRemove()
	{
		CLAM::DescriptionScope spec("TestScope");
		spec.Add<CLAM::TData>("MyAttribute");

		CLAM::ScopePool pool(spec,3);

		CLAM::TData * data = pool.GetWritePool<CLAM::TData>("MyAttribute");
		for (unsigned i = 0; i < pool.GetSize(); i++)
			data[i] = 1000 + i*i;

		pool.Remove(1);

		unsigned expectedSize = 2;
		CLAM::TData expected[]={1000,1004};
		CPPUNIT_ASSERT_EQUAL(expectedSize,pool.GetSize());
		const CLAM::TData * result = pool.GetReadPool<CLAM::TData>("MyAttribute");
		for (unsigned i=0; i<pool.GetSize(); i++)
			CPPUNIT_ASSERT_EQUAL(expected[i],result[i]);
	}

	// TODO: Insert outside bounds
	// TODO: Remove outside bounds

};






} // namespace CLAMTest