Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / tools / nel_unit_test / ut_misc_stream.h
blobe7f74ba6c9cf195b40c587659d7dc54cf9e58d7b
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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
30 public:
31 CUTMiscStream ()
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
45 void copyOnWrite()
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;
58 uint32 i = 1;
59 s1.serial(i);
61 s2 = s1;
62 s3 = s2;
64 TEST_ASSERT(s1.buffer() == s2.buffer());
65 TEST_ASSERT(s1.buffer() == s3.buffer());
67 // change s1
68 s1.serial(i);
69 TEST_ASSERT(s1.buffer() != s2.buffer());
70 TEST_ASSERT(s2.buffer() == s3.buffer());
72 s2.invert();
73 s3 = s2;
75 TEST_ASSERT(s2.buffer() == s3.buffer());
77 s2.serial(i);
79 TEST_ASSERT(s2.buffer() == s3.buffer());
84 enum TEnum
86 e_a,
87 e_b,
88 e_c,
89 e_d
92 void constAndStream()
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);
102 const TEnum e(e_a);
103 string str2("titi");
104 uint32 i2(123456);
105 TEnum e2(e_b);
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);
114 // this work as well
115 s1.serial(str2);
116 s1.serial(i2);
117 s1.serialEnum(e2);
119 is1.serial(str2);
120 is1.serial(i2);
121 is1.serialEnum(e2);
123 const NLMISC::CMemStream &s2 = s1;
124 const NLMISC::IStream &is2 = s2;
126 string str3;
127 uint32 i3;
128 TEnum e3(e_c);
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);
138 s1.invert();
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);
158 void memStreamSwap()
160 NLMISC::CMemStream ms2;
162 string s;
164 NLMISC::CMemStream ms1;
166 s = "foo1";
167 ms1.serial(s);
168 s = "foo2";
169 ms1.serial(s);
170 s = "";
172 ms2.swap(ms1);
174 // check that ms1 is empty now
175 TEST_ASSERT(ms1.length() == 0);
178 TEST_ASSERT(!ms2.isReading());
179 ms2.invert();
180 ms2.serial(s);
181 TEST_ASSERT(s == "foo1");
182 ms2.serial(s);
183 TEST_ASSERT(s == "foo2");
187 #endif