1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #include "StreamSerialiserTests.h"
29 #include "OgreStreamSerialiser.h"
30 #include "OgreFileSystem.h"
31 #include "OgreException.h"
32 #include "OgreVector3.h"
33 
34 using namespace Ogre;
35 
36 // Register the suite
37 CPPUNIT_TEST_SUITE_REGISTRATION( StreamSerialiserTests );
38 
setUp()39 void StreamSerialiserTests::setUp()
40 {
41 }
tearDown()42 void StreamSerialiserTests::tearDown()
43 {
44 }
45 
testWriteBasic()46 void StreamSerialiserTests::testWriteBasic()
47 {
48 	FileSystemArchive arch("./", "FileSystem", false);
49 	arch.load();
50 
51 	String fileName = "testSerialiser.dat";
52 	Vector3 aTestVector(0.3, 15.2, -12.0);
53 	String aTestString = "Some text here";
54 	int aTestValue = 99;
55 	uint32 chunkID = StreamSerialiser::makeIdentifier("TEST");
56 	// write the data
57 	{
58 
59 		DataStreamPtr stream = arch.create(fileName);
60 
61 		StreamSerialiser serialiser(stream);
62 
63 		serialiser.writeChunkBegin(chunkID);
64 
65 
66 		serialiser.write(&aTestVector);
67 		serialiser.write(&aTestString);
68 		serialiser.write(&aTestValue);
69 		serialiser.writeChunkEnd(chunkID);
70 	}
71 
72 	// read it back
73 	{
74 
75 		DataStreamPtr stream = arch.open(fileName);
76 
77 		StreamSerialiser serialiser(stream);
78 
79 		const StreamSerialiser::Chunk* c = serialiser.readChunkBegin();
80 
81 		CPPUNIT_ASSERT_EQUAL(chunkID, c->id);
82 		CPPUNIT_ASSERT_EQUAL(sizeof(Vector3) + sizeof(int) + aTestString.size() + 4, (size_t)c->length);
83 
84 		Vector3 inVector;
85 		String inString;
86 		int inValue;
87 
88 		serialiser.read(&inVector);
89 		serialiser.read(&inString);
90 		serialiser.read(&inValue);
91 		serialiser.readChunkEnd(chunkID);
92 
93 		CPPUNIT_ASSERT_EQUAL(aTestVector, inVector);
94 		CPPUNIT_ASSERT_EQUAL(aTestString, inString);
95 		CPPUNIT_ASSERT_EQUAL(aTestValue, inValue);
96 
97 	}
98 
99 
100 
101 	arch.remove(fileName);
102 
103 	CPPUNIT_ASSERT(!arch.exists(fileName));
104 }
105