File: Test_Engine.cpp

package info (click to toggle)
libcommoncpp2 1.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,300 kB
  • sloc: cpp: 29,221; sh: 10,352; ansic: 1,134; makefile: 238; xml: 5
file content (351 lines) | stat: -rw-r--r-- 13,088 bytes parent folder | download | duplicates (7)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (C) 2002-2003 Chad C. Yates cyates@uidaho.edu
//
// 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.
//
// As a special exception to the GNU General Public License, permission is
// granted for additional uses of the text contained in its release
// of Common C++.
//
// The exception is that, if you link the Common C++ library with other
// files to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the Common C++ library code into it.
//
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
//
// This exception applies only to the code released under the
// name Common C++.  If you copy code from other releases into a copy of
// Common C++, as the General Public License permits, the exception does
// not apply to the code that you add in this way.  To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
//
// If you write modifications of your own for Common C++, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.

#include "Test_Engine.h"
#include <cstdio>

CPPUNIT_TEST_SUITE_REGISTRATION(EngineTest);

void EngineTest::setUp()
{
  // Create an example object with some sub objects and various data elements
  complexObject.iInteger = 5;
  complexObject.strString = "String 1";
  complexObject.strString2 = "String 2";
  complexObject.uninitializedNullPtr = NULL;  // initialized here instead of constructor to see if it will be unpersisted as a null (vs defaulting to null from the constructor)
  for(int i = 0; i < 10; i++)
	complexObject.numbers.push_back(i);
  complexObject.strings.push_back("a");
  complexObject.strings.push_back("b");
  complexObject.strings.push_back("c");
  complexObject.strings.push_back("d");
  complexObject.strings.push_back("e");

  complexObject.moreStrings.push_back("z");
  complexObject.moreStrings.push_back("y");
  complexObject.moreStrings.push_back("x");
  complexObject.moreStrings.push_back("w");
  complexObject.moreStrings.push_back("v");

  complexObject.subObjectPtr = new TestSub;
  complexObject.subObjectPtr2 = complexObject.subObjectPtr; // set to point to the same thing as subObjectPtr to test unpersisting of shared instances
  complexObject.subObjectPtr->iInteger = 5;
  complexObject.subObjectPtr->strString = "SubStringPtr 1";
  complexObject.subObjectPtr->strString2 = "SubStringPtr 2";
  for(int j = 10; j < 20; j++)
	complexObject.subObjectPtr->numbers.push_back(j);

  complexObject.subObjectRef.iInteger = 5;
  complexObject.subObjectRef.strString = "SubString2 1";
  complexObject.subObjectRef.strString2 = "SubString2 2";
  for(int k = 30; k < 35; k++)
	complexObject.subObjectRef.numbers.push_back(k);

  // make a std::deque of TestSub objects
  for(int l = 0; l < 2; l++) {
	TestSub newSubObject;
	newSubObject.iInteger = l;
	char tmp[50];
	sprintf(tmp, "test %d", l+1);
	newSubObject.strString = tmp;
	for(int k = 30; k < 35; k++)
	  newSubObject.numbers.push_back(k);
	complexObject.subObjects.push_back(newSubObject);
  }
}

void EngineTest::tearDown()
{}

void EngineTest::testPrimitives()
{
  // write primitive types
  std::fstream outputArchive("EnginePrimitiveTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  TEST_PRIMITIVE_OUTPUT(int8, 0x01);
  TEST_PRIMITIVE_OUTPUT(uint8, 0x01);
  TEST_PRIMITIVE_OUTPUT(int16, 0x0123);
  TEST_PRIMITIVE_OUTPUT(uint16, 0x0123);
  TEST_PRIMITIVE_OUTPUT(int32, 0x01234567);
  TEST_PRIMITIVE_OUTPUT(uint32, 0x01234567);
  //TEST_PRIMITIVE_OUTPUT(int64, 0x0123456789ABCDEF); // warning: integer constant larger than the maximum value of an unsigned long int
  //TEST_PRIMITIVE_OUTPUT(uint64, 0x0123456789ABCDEF); // warning: integer constant larger than the maximum value of an unsigned long int
  TEST_PRIMITIVE_OUTPUT(float, 3.141592653589793238462643f);
  TEST_PRIMITIVE_OUTPUT(double, 3.141592653589793238462643);
  TEST_PRIMITIVE_OUTPUT(string, "abcdefghijklmnopqrstuvwxyz0123456789");

  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // read primitive types back in and check
  std::fstream inputArchive("EnginePrimitiveTest.dat", std::ios::in|std::ios::binary);
  Engine inputEngine(inputArchive, ost::Engine::modeRead);

  TEST_PRIMITIVE_INPUT(int8);
  TEST_PRIMITIVE_INPUT(uint8);
  TEST_PRIMITIVE_INPUT(int16);
  TEST_PRIMITIVE_INPUT(uint16);
  TEST_PRIMITIVE_INPUT(int32);
  TEST_PRIMITIVE_INPUT(uint32);
  //TEST_PRIMITIVE_INPUT(int64);
  //TEST_PRIMITIVE_INPUT(uint64);
  TEST_PRIMITIVE_INPUT(float);
  TEST_PRIMITIVE_INPUT(double);
  TEST_PRIMITIVE_INPUT(string);

  inputArchive.close();
}


void EngineTest::testRawBinary()
{
  int i;

  // write raw binary data
  std::fstream outputArchive("EngineRawBinaryTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  unsigned int binaryBuffer[BINARY_BUFFER_SIZE];
  for(i = 0; i < BINARY_BUFFER_SIZE; i++)
	binaryBuffer[i] = i;

  outputEngine.writeBinary((const uint8*) binaryBuffer, sizeof(binaryBuffer));
  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // read binary data back in and check
  std::fstream inputArchive("EngineRawBinaryTest.dat", std::ios::in|std::ios::binary);
  Engine inputEngine(inputArchive, ost::Engine::modeRead);

  unsigned int binaryBuffer2[BINARY_BUFFER_SIZE];
  inputEngine.readBinary((uint8*) binaryBuffer2, sizeof(binaryBuffer2));
  inputArchive.close();

  for(i = 0; i < BINARY_BUFFER_SIZE; i++)
	CPPUNIT_ASSERT(binaryBuffer[i] == binaryBuffer2[i]);
}

void EngineTest::testSTLVector()
{
  int i;

  // write STL data
  std::fstream outputArchive("EngineSTLVectorTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  std::vector<int32> intVector;
  std::vector<int32>* pIntVector = new std::vector<int32>;
  for(i = 0; i < STL_CONTAINER_SIZE; i++) {
	intVector.push_back(i);
	pIntVector->push_back(i);
  }

  outputEngine << intVector;
  outputEngine << *pIntVector;
  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // read STL std::vector back in and check
  std::fstream inputArchive("EngineSTLVectorTest.dat", std::ios::in|std::ios::binary);
  Engine inputEngine(inputArchive, ost::Engine::modeRead);

  std::vector<int32> intVector2;
  inputEngine >> intVector2;
  CPPUNIT_ASSERT(intVector == intVector2);

  std::vector<int32>* pIntVector2 = new std::vector<int32>;
  inputEngine >> *pIntVector2;
  CPPUNIT_ASSERT(*pIntVector == *pIntVector2);
  delete pIntVector2;

  inputArchive.close();

  delete pIntVector;
}

void EngineTest::testSTLDeque()
{
  int i;

  // write STL std::deque data
  std::fstream outputArchive("EngineSTLDequeTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  std::deque<int32> intDeque;
  std::deque<int32>* pIntDeque = new std::deque<int32>;
  for(i = 0; i < STL_CONTAINER_SIZE; i++) {
	intDeque.push_back(i);
	pIntDeque->push_back(i);
  }

  outputEngine << intDeque;
  outputEngine << *pIntDeque;
  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // read STL std::deque back in and check
  std::fstream inputArchive("EngineSTLDequeTest.dat", std::ios::in|std::ios::binary);
  Engine inputEngine(inputArchive, ost::Engine::modeRead);

  std::deque<int32> intDeque2;
  inputEngine >> intDeque2;
  CPPUNIT_ASSERT(intDeque == intDeque2);

  std::deque<int32>* pIntDeque2 = new std::deque<int32>;
  inputEngine >> *pIntDeque2;
  CPPUNIT_ASSERT(*pIntDeque == *pIntDeque2);
  delete pIntDeque2;

  inputArchive.close();

  delete pIntDeque;
}

void EngineTest::testSTLMap()
{
  int i;

  // write STL std::map data
  std::fstream outputArchive("EngineSTLMapTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  std::map<int32, int32> intMap;
  std::map<int32, int32>* pIntMap = new std::map<int32, int32>;
  for(i = 0; i < STL_CONTAINER_SIZE; i++) {
	intMap.insert(std::pair<int32, int32>(i, i+10));
	pIntMap->insert(std::pair<int32, int32>(i, i+11));
  }

  outputEngine << intMap;
  outputEngine << *pIntMap;
  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // read STL std::map back in and check
  std::fstream inputArchive("EngineSTLMapTest.dat", std::ios::in|std::ios::binary);
  Engine inputEngine(inputArchive, ost::Engine::modeRead);

  std::map<int32, int32> intMap2;
  inputEngine >> intMap2;
  CPPUNIT_ASSERT(intMap == intMap2);

  std::map<int32, int32>* pIntMap2 = new std::map<int32, int32>;
  inputEngine >> *pIntMap2;
  CPPUNIT_ASSERT(*pIntMap == *pIntMap2);
  delete pIntMap2;

  inputArchive.close();

  delete pIntMap;
}

void EngineTest::testComplexObject()
{
  // write BaseObject hierarchy
  std::fstream outputArchive("EngineComplexObjectTest.dat", std::ios::out|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);
  outputEngine << complexObject;
  outputEngine.sync(); // flush Engine buffers before closing file
  outputArchive.close();

  // Unpersist a new object structure into an uninitialized object
  {
	Test* myObjPtr = NULL; // must initialize pointer or new persistence engine will think it is already allocated
	std::fstream inputArchive("EngineComplexObjectTest.dat", std::ios::in);
	Engine inputEngine(inputArchive, ost::Engine::modeRead);
	inputEngine >> myObjPtr;
	inputArchive.close();

	CPPUNIT_ASSERT_MESSAGE("Unpersisted into unallocated pointer == Original", *myObjPtr == complexObject);
	CPPUNIT_ASSERT_MESSAGE("nullPtr is NULL", myObjPtr->nullPtr == NULL);
	CPPUNIT_ASSERT_MESSAGE("uninitializedNullPtr is NULL", myObjPtr->uninitializedNullPtr == NULL);

	myObjPtr->subObjectPtr->strString2 = "Changed SubStringPtr 1";
	CPPUNIT_ASSERT_MESSAGE("subObjectPtr.strString2 should always equal subObjectPtr2.strString2", myObjPtr->subObjectPtr->strString2 == myObjPtr->subObjectPtr2->strString2);
	CPPUNIT_ASSERT_MESSAGE("subObjectPtr.strString2 should now equal 'Changed SubStringPtr 1'", myObjPtr->subObjectPtr->strString2 == "Changed SubStringPtr 1");
	CPPUNIT_ASSERT_MESSAGE("subObjectPtr.strString2 should still equal subObjectPtr2.strString2", myObjPtr->subObjectPtr->strString2 == myObjPtr->subObjectPtr2->strString2);
  }

  // Unpersist a new object structure into an instantiated class variable
  {
	Test myObjInstance;
	std::fstream inputArchive("EngineComplexObjectTest.dat", std::ios::in);
	Engine inputEngine(inputArchive, ost::Engine::modeRead);
	inputEngine >> myObjInstance;
	inputArchive.close();

	CPPUNIT_ASSERT_MESSAGE("Unpersisted into default constructed instance == Original", myObjInstance == complexObject);
	CPPUNIT_ASSERT_MESSAGE("nullPtr is NULL", myObjInstance.nullPtr == NULL);
	CPPUNIT_ASSERT_MESSAGE("UninitializedNullPtr is NULL", myObjInstance.uninitializedNullPtr == NULL);
  }

  // Unpersist a new object structure into a pre-initialized pointer to an object
  {
	Test* myObjAllocatedPtr = new Test;
	std::fstream inputArchive("EngineComplexObjectTest.dat", std::ios::in);
	Engine inputEngine(inputArchive, ost::Engine::modeRead);
	inputEngine >> myObjAllocatedPtr;
	outputEngine.sync(); // flush Engine buffers before closing file
	inputArchive.close();

	CPPUNIT_ASSERT_MESSAGE("Unpersisted into pre-allocated pointer", *myObjAllocatedPtr == complexObject);
	CPPUNIT_ASSERT_MESSAGE("nullPtr is NULL", myObjAllocatedPtr->nullPtr == NULL);
	CPPUNIT_ASSERT_MESSAGE("uninitializedNullPtr is NULL", myObjAllocatedPtr->uninitializedNullPtr == NULL);
  }
}

void EngineTest::testModeExceptions()
{
  // write primitive types
  std::fstream outputArchive("EnginePrimitiveTest.dat", std::ios::in|std::ios::binary);
  Engine outputEngine(outputArchive, ost::Engine::modeWrite);

  int32 test_Int32 = 0x01234567;
  try {
	outputEngine << test_Int32;
	//CPPUNIT_FAIL("Call to persist to an input stream should throw an exception");
  }
  catch(PersistException &ex) {
	CPPUNIT_ASSERT_MESSAGE(ex.getString(), true);
  }

  outputArchive.close();
}