1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #ifndef UT_MISC_STREAM
18 #define UT_MISC_STREAM
20 #include <nel/misc/stream.h>
21 #include <nel/misc/bit_mem_stream.h>
23 // The following line is known to crash in a Ryzom service
24 NLMISC::CBitMemStream
globalBms( false, 2048 ); // global to avoid reallocation
26 // Test suite for stream based classes
27 // ! not complete at all at time of writing !
28 class CUTMiscStream
: public Test::Suite
33 TEST_ADD(CUTMiscStream::constAndStream
);
34 TEST_ADD(CUTMiscStream::memStreamSwap
);
35 TEST_ADD(CUTMiscStream::copyOnWrite
);
36 TEST_ADD(CUTMiscStream::preallocatedBitStream
);
39 void preallocatedBitStream()
41 NLMISC::CBitMemStream
localBms( false, 2048 ); // global to avoid reallocation
47 // test the copy on write strategy in the mem stream (and derived) class.
48 // The point is to be able to copy a mem stream (e.g a NLNET::CMessage)
49 // but to do not copy the stream buffer.
50 // If more than one stream use the same buffer, any attempt to
51 // modifye the buffer content while lead to a buffer duplication
53 NLMISC::CMemStream s1
;
54 NLMISC::CMemStream s2
;
55 NLMISC::CMemStream s3
;
64 TEST_ASSERT(s1
.buffer() == s2
.buffer());
65 TEST_ASSERT(s1
.buffer() == s3
.buffer());
69 TEST_ASSERT(s1
.buffer() != s2
.buffer());
70 TEST_ASSERT(s2
.buffer() == s3
.buffer());
75 TEST_ASSERT(s2
.buffer() == s3
.buffer());
79 TEST_ASSERT(s2
.buffer() == s3
.buffer());
94 // check that we can serialize with const stream or const object
97 NLMISC::CMemStream s1
;
98 NLMISC::IStream
&is1
= s1
;
100 const string
str("toto");
101 const uint32
i(1234546);
107 // no need for const cast any more
108 nlWriteSerial(s1
, str
);
109 nlWriteSerial(s1
, i
);
110 nlWrite(s1
, serialEnum
, e
);
111 nlWriteSerial(is1
, str
);
112 nlWriteSerial(is1
, i
);
113 nlWrite(is1
, serialEnum
, i
);
123 const NLMISC::CMemStream
&s2
= s1
;
124 const NLMISC::IStream
&is2
= s2
;
129 // cant write in a const stream
130 TEST_THROWS(nlReadSerial(s2
, str3
), NLMISC::ENotInputStream
);
131 TEST_THROWS(nlReadSerial(s2
, i3
), NLMISC::ENotInputStream
);
132 TEST_THROWS(nlRead(s2
, serialEnum
, e3
), NLMISC::ENotInputStream
);
133 TEST_THROWS(nlReadSerial(is2
, str3
), NLMISC::ENotInputStream
);
134 TEST_THROWS(nlReadSerial(is2
, i3
), NLMISC::ENotInputStream
);
135 TEST_THROWS(nlRead(is2
, serialEnum
, e3
), NLMISC::ENotInputStream
);
140 nlReadSerial(s2
, str3
);
141 nlReadSerial(s2
, i3
);
142 nlRead(s2
, serialEnum
, e3
);
143 nlReadSerial(is2
, str3
);
144 nlReadSerial(is2
, i3
);
145 nlRead(is2
, serialEnum
, e3
);
148 // cant read a const value
149 TEST_THROWS(nlWriteSerial(s1
, str
), NLMISC::ENotOutputStream
);
150 TEST_THROWS(nlWriteSerial(s1
, i
), NLMISC::ENotOutputStream
);
151 TEST_THROWS(nlWrite(s1
, serialEnum
, e
), NLMISC::ENotOutputStream
);
152 TEST_THROWS(nlWriteSerial(is1
, str
), NLMISC::ENotOutputStream
);
153 TEST_THROWS(nlWriteSerial(is1
, i
), NLMISC::ENotOutputStream
);
154 TEST_THROWS(nlWrite(is1
, serialEnum
, e
), NLMISC::ENotOutputStream
);
160 NLMISC::CMemStream ms2
;
164 NLMISC::CMemStream ms1
;
174 // check that ms1 is empty now
175 TEST_ASSERT(ms1
.length() == 0);
178 TEST_ASSERT(!ms2
.isReading());
181 TEST_ASSERT(s
== "foo1");
183 TEST_ASSERT(s
== "foo2");