Added aqua_speed for rite geo 50 tryker
[ryzomcore.git] / nel / tools / nel_unit_test / ut_net_message.h
blob66ae55b30c613276210874de0de46078f841474e
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_NET_MESSAGE
18 #define UT_NET_MESSAGE
20 class CUTNetMessage: public Test::Suite
22 public:
23 CUTNetMessage ()
25 TEST_ADD(CUTNetMessage::messageSwap);
26 TEST_ADD(CUTNetMessage::lockSubMEssage);
27 TEST_ADD(CUTNetMessage::lockSubMEssageWithLongName);
31 void lockSubMEssageWithLongName()
33 NLNET::CMessage master("BIG");
35 // serial some stuff
36 for (uint8 i=0; i<10; ++i)
38 master.serial(i);
41 uint32 sizes[4];
43 // serial 4 sub messages
44 for (uint i=0; i<4; ++i)
46 NLNET::CMessage sub(NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i));
48 for (uint8 j=0; j<i*4; ++j)
50 sub.serial(j);
53 string s("A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
54 sub.serial(s);
56 sizes[i] = sub.length();
58 master.serialMessage(sub);
61 // invert the message
62 master.invert();
64 // now, unpack and check
66 // read the first master data
67 for (uint8 i=0; i<10; ++i)
69 uint8 b;
70 master.serial(b);
72 TEST_ASSERT(b == i);
75 // unpack each sub message
76 for (uint i=0; i<4; ++i)
78 uint32 subSize;
79 master.serial(subSize);
81 master.lockSubMessage(subSize);
82 TEST_ASSERT(subSize == sizes[i]);
84 TEST_ASSERT(master.getName() == NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i));
85 TEST_ASSERT(master.length() == sizes[i]);
87 for (uint8 j=0; j<i*4; ++j)
89 uint8 b;
90 master.serial(b);
91 TEST_ASSERT(b == j);
94 string s;
95 master.serial(s);
96 TEST_ASSERT(s == "A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
98 TEST_ASSERT(master.getPos() == master.length());
100 master.unlockSubMessage();
103 // rewind the message
104 master.seek(master.getHeaderSize(), NLMISC::IStream::begin);
106 // read the first master data
107 for (uint8 i=0; i<10; ++i)
109 uint8 b;
110 master.serial(b);
112 TEST_ASSERT(b == i);
115 // assign from each sub message
116 for (uint i=0; i<4; ++i)
118 uint32 subSize;
119 master.serial(subSize);
121 master.lockSubMessage(subSize);
123 TEST_ASSERT(subSize == sizes[i]);
125 TEST_ASSERT(master.getName() == NLMISC::toString("A_VERY_LONG_SUB_MESSAGE_NAME_%u", i));
126 TEST_ASSERT(master.length() == sizes[i]);
128 NLNET::CMessage sub;
129 sub.assignFromSubMessage(master);
131 for (uint8 j=0; j<i*4; ++j)
133 uint8 b;
134 sub.serial(b);
135 TEST_ASSERT(b == j);
138 string s;
139 sub.serial(s);
140 TEST_ASSERT(s == "A VERY LONG MESSAGE THAT COULD BE A PROBLEM TO HANDLE");
142 TEST_ASSERT(sub.getPos() == sub.length());
144 master.unlockSubMessage();
149 void lockSubMEssage()
151 NLNET::CMessage master("BIG");
153 // serial some stuff
154 for (uint8 i=0; i<10; ++i)
156 master.serial(i);
159 sint32 sizes[4];
161 // serial 4 sub messages
162 for (uint i=0; i<4; ++i)
164 NLNET::CMessage sub(NLMISC::toString("SUB_%u", i));
166 for (uint8 j=0; j<i*4; ++j)
168 sub.serial(j);
171 string s("A MESSAGE");
172 sub.serial(s);
174 sizes[i] = sub.length();
176 master.serialMessage(sub);
179 // invert the message
180 master.invert();
182 // now, unpack and check
184 // read the first master data
185 for (uint8 i=0; i<10; ++i)
187 uint8 b;
188 master.serial(b);
190 TEST_ASSERT(b == i);
193 // unpack each sub message
194 for (uint i=0; i<4; ++i)
196 uint32 subSize;
197 master.serial(subSize);
199 master.lockSubMessage(subSize);
200 TEST_ASSERT(subSize == sizes[i]);
202 TEST_ASSERT(master.getName() == NLMISC::toString("SUB_%u", i));
203 TEST_ASSERT(master.length() == sizes[i]);
205 for (uint8 j=0; j<i*4; ++j)
207 uint8 b;
208 master.serial(b);
209 TEST_ASSERT(b == j);
212 string s;
213 master.serial(s);
214 TEST_ASSERT(s == "A MESSAGE");
216 TEST_ASSERT(master.getPos() == master.length());
218 master.unlockSubMessage();
221 // rewind the message
222 master.seek(master.getHeaderSize(), NLMISC::IStream::begin);
224 // read the first master data
225 for (uint8 i=0; i<10; ++i)
227 uint8 b;
228 master.serial(b);
230 TEST_ASSERT(b == i);
233 // assign from each sub message
234 for (uint i=0; i<4; ++i)
236 uint32 subSize;
237 master.serial(subSize);
239 master.lockSubMessage(subSize);
241 TEST_ASSERT(subSize == sizes[i]);
243 TEST_ASSERT(master.getName() == NLMISC::toString("SUB_%u", i));
244 TEST_ASSERT(master.length() == sizes[i]);
246 NLNET::CMessage sub;
247 sub.assignFromSubMessage(master);
249 for (uint8 j=0; j<i*4; ++j)
251 uint8 b;
252 sub.serial(b);
253 TEST_ASSERT(b == j);
256 string s;
257 sub.serial(s);
258 TEST_ASSERT(s == "A MESSAGE");
260 TEST_ASSERT(sub.getPos() == sub.length());
262 master.unlockSubMessage();
267 void messageSwap()
269 NLNET::CMessage msg2;
271 string s;
273 NLNET::CMessage msg1;
274 msg1.setType("NAME", NLNET::CMessage::Request);
276 s = "foo1";
277 msg1.serial(s);
278 s = "foo2";
279 msg1.serial(s);
280 s = "";
282 msg2.swap(msg1);
284 // check that ms1 is empty now
285 TEST_ASSERT(msg1.length() == 0);
286 TEST_ASSERT(!msg1.typeIsSet());
289 TEST_ASSERT(!msg2.isReading());
290 msg2.invert();
291 TEST_ASSERT(msg2.typeIsSet());
292 TEST_ASSERT(msg2.getName() == "NAME");
293 TEST_ASSERT(msg2.getType() == NLNET::CMessage::Request);
294 msg2.serial(s);
295 TEST_ASSERT(s == "foo1");
296 msg2.serial(s);
297 TEST_ASSERT(s == "foo2");
301 #endif