/[resiprocate]/main/rutil/test/testParseBuffer.cxx
ViewVC logotype

Contents of /main/rutil/test/testParseBuffer.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9119 - (show annotations) (download)
Mon Apr 25 17:43:24 2011 UTC (8 years, 6 months ago) by bcampen
File MIME type: text/plain
File size: 12661 byte(s)
Clearing up warnings (there are a couple that still remain on OS X
due to gcc bug #37016).

1 #include "rutil/ParseBuffer.hxx"
2 #include <string.h>
3 #include <assert.h>
4 #include "rutil/Logger.hxx"
5
6 using namespace resip;
7
8 #define RESIPROCATE_SUBSYSTEM Subsystem::TEST
9
10 int
11 main(int argc, char** argv)
12 {
13 Log::initialize(Log::Cout, argc > 1 ? Log::toLevel(argv[1]) : Log::Info, argv[0]);
14
15 {
16 const char buf[] = "/home/jason/test";
17 ParseBuffer pb(buf);
18 pb.skipToEnd();
19 pb.skipBackToChar('/');
20 assert(!pb.bof());
21 Data result(pb.position());
22 assert(result == "test");
23 }
24
25 {
26 const char buf[] = "test";
27 ParseBuffer pb(buf);
28 pb.skipToEnd();
29 pb.skipBackToChar('/');
30 assert(pb.bof());
31 Data result(pb.position());
32 assert(result == "test");
33 }
34
35 {
36 const char buf[] = "/test";
37 ParseBuffer pb(buf);
38 pb.skipToEnd();
39 pb.skipBackToChar('/');
40 Data result(pb.position());
41 assert(result == "test");
42 }
43
44 {
45 const char buf[] = "!^.*$!sip:user@example.com!";
46 ParseBuffer pb(buf, strlen(buf));
47
48 const char delim = buf[0];
49 const char* start = pb.skipChar(delim);
50 std::cerr << "start=" << start << std::endl;
51 pb.skipToChar(delim);
52
53 Data e1;
54 pb.data(e1, start);
55 std::cerr << "e1=" << e1 << std::endl;
56 assert(e1 == "^.*$");
57
58 start = pb.skipChar(delim);
59 Data e2;
60 pb.skipToChar(delim);
61 pb.data(e2, start);
62 std::cerr << "e2=" << e2 << std::endl;
63 assert(e2 == "sip:user@example.com");
64
65 start = pb.skipChar(delim);
66 Data e3;
67 pb.data(e3, start);
68 std::cerr << "e3=" << e3 << std::endl;
69 assert(e3.empty());
70 }
71
72 {
73 const char buf[] = "Ducky%20%26";
74 ParseBuffer pb(buf, strlen(buf));
75
76 const char* start = pb.skipWhitespace();
77 pb.skipToEnd();
78
79 Data target;
80 pb.dataUnescaped(target, start);
81
82 assert(target == "Ducky &");
83 }
84
85 {
86 const char buf[] = " \r\t\r\n\t !";
87 ParseBuffer pb(buf, strlen(buf));
88
89 pb.skipWhitespace();
90 assert(*pb.position() == '!');
91 }
92
93 {
94 const char buf[] = "asdfa1234123edfdf213ref@!\t \r\t\r\n\t ";
95 ParseBuffer pb(buf, strlen(buf));
96
97 pb.skipNonWhitespace();
98 assert(*pb.position() == '\t');
99 }
100
101 {
102 std::cerr << "!! Test position" << std::endl;
103 char buf[] = "Here is a buffer with some stuff.";
104 ParseBuffer pb(buf, strlen(buf));
105
106 pb.skipToChars("buff");
107 *pb.position();
108
109 pb.skipToEnd();
110
111 do
112 {
113 try
114 {
115 *pb.position();
116 assert(false);
117 }
118 catch (BaseException& e)
119 {
120 break;
121 }
122 } while (false);
123 }
124
125 {
126 std::cerr << "!! Test fail one line" << std::endl;
127
128 char buf[] = "Here is a \t buffer with some stuff.";
129 ParseBuffer pb(buf, strlen(buf));
130
131 do
132 {
133 try
134 {
135 pb.skipChars("Here isn't a");
136 }
137 catch (ParseException& e)
138 {
139 //std::cerr<< e << std::endl;
140 break;
141 }
142 assert(0);
143 } while (false);
144 }
145
146 {
147 std::cerr << "!! Test fail multiline" << std::endl;
148
149 const Data test("Test input");
150 char buf[] = "Here is a \r\n buffer with \r\nsome stuff.";
151 ParseBuffer pb(buf, strlen(buf), test);
152
153 do
154 {
155 try
156 {
157 pb.skipToChars("buff");
158 pb.skipChars("buff");
159 pb.skipChar('g');
160 }
161 catch (ParseException& e)
162 {
163 //using namespace std;
164 //cerr << e << endl;
165 //cerr << '\'' << e.getMessage() << '\'' << endl;
166 //cerr << '\'' << test << '\'' << endl;
167 break;
168 }
169 assert(0);
170 } while (false);
171 }
172
173 {
174 char buf[] = "Content-Languages: English, \r\n French , \r\n\t LISP \r \n \n\r \r\n\r\n";
175 ParseBuffer pb(buf, strlen(buf));
176 pb.skipToTermCRLF();
177 pb.skipChars("\r\n");
178 pb.skipChars("\r\n");
179 pb.assertEof();
180 }
181
182 {
183 char buf[] = "Content-Languages: English, \r\n French , \r\n\t LISP \r \n \n\r \r\n\r\n";
184 ParseBuffer pb(buf, strlen(buf));
185 pb.skipToChars("French");
186 pb.skipN(strlen("French"));
187 pb.skipWhitespace();
188 pb.skipChar(',');
189 pb.skipLWS();
190 std::cerr << pb.position();
191 pb.skipChars("LISP");
192 }
193
194 {
195 char buf[] = "123456789";
196 ParseBuffer pb(buf, strlen(buf));
197 pb.skipN(9);
198 assert(pb.eof());
199 }
200
201 {
202 char buf[] = "123456789";
203 ParseBuffer pb(buf, strlen(buf));
204 try
205 {
206 char foo = *pb.skipN(9);
207 (void)foo;
208 assert(0);
209 }
210 catch (ParseException& e)
211 {}
212 }
213
214 {
215 char buf[] = "Here is a \t buffer with some stuff.";
216 ParseBuffer pb(buf, strlen(buf));
217 pb.skipToChars("some");
218 std::cerr << pb.position() << std::endl;
219 pb.skipChars("some stu");
220 }
221
222 {
223 char buf[] = "Here is asom \t buffer with some stuff.";
224 ParseBuffer pb(buf, strlen(buf));
225 pb.skipToChars("some");
226 pb.skipChars("some stuf");
227 }
228
229 {
230 char buf[] = "Here is asom \t buffer with som stuff.";
231 ParseBuffer pb(buf, strlen(buf));
232 pb.skipToChars("some");
233 pb.assertEof();
234 }
235
236 {
237 char buf[] = "Here is a \t buffer with some stuff.";
238 ParseBuffer pb(buf, strlen(buf));
239 pb.skipToChars(Data("some"));
240 pb.skipChars("some stuf");
241 }
242
243 {
244 char buf[] = "Here is asom \t buffer with some stuff.";
245 ParseBuffer pb(buf, strlen(buf));
246 pb.skipToChars(Data("some"));
247 pb.skipChars("some stuf");
248 }
249
250 {
251 char buf[] = "Here is asom \t buffer with som stuff.";
252 ParseBuffer pb(buf, strlen(buf));
253 pb.skipToChars(Data("some"));
254 pb.assertEof();
255 }
256
257 {
258 char buf[] = "Here is a \t buffer with some stuff.";
259 ParseBuffer pb(buf, strlen(buf));
260
261 pb.skipChars("Here is a");
262 }
263
264 {
265 char buf[] = "Here is a \t buffer with some stuff.";
266 ParseBuffer pb(buf, strlen(buf));
267
268 do
269 {
270 try
271 {
272 pb.skipChars("Here isn't a");
273 }
274 catch (ParseException& e)
275 {
276 break;
277 }
278 assert(0);
279 } while (false);
280 }
281
282 {
283 char buf[] = "Here is a buf.";
284 ParseBuffer pb(buf, strlen(buf));
285
286 do
287 {
288 try
289 {
290 pb.skipChars("Here is a ");
291 pb.skipChars("buffer");
292 }
293 catch (ParseException& e)
294 {
295 break;
296 }
297 assert(0);
298 } while (false);
299 }
300
301 {
302 const char* buf = "Here is a \t buffer with some stuff.";
303
304 ParseBuffer pb(buf, strlen(buf));
305
306 assert(!pb.eof());
307 assert(pb.position() == buf);
308
309 pb.skipWhitespace();
310 assert(pb.position() == buf);
311
312 pb.skipNonWhitespace();
313 assert(*pb.position() == ' ');
314 }
315
316 {
317 char buf[] = " \t buffer with some stuff.";
318 ParseBuffer pb(buf, strlen(buf));
319
320 pb.reset(pb.end());
321 pb.skipBackToChar('s');
322 pb.skipBackChar();
323 pb.skipBackToChar('s');
324 pb.skipBackChar('s');
325
326 assert(Data(pb.position(), 4) == "some");
327 }
328
329 {
330 char buf[] = "buffer with some stuff.";
331 ParseBuffer pb(buf, strlen(buf));
332
333 pb.reset(pb.end());
334 pb.skipBackToChar('q');
335 assert(pb.bof());
336 pb.skipChar('b');
337 }
338
339 {
340 char buf[] = " \t buffer with some stuff.";
341 ParseBuffer pb(buf, strlen(buf));
342
343 pb.skipWhitespace();
344 assert(*pb.position() == 'b');
345
346 pb.skipToChar('s');
347 assert(*pb.position() == 's');
348 pb.skipNonWhitespace();
349 pb.skipWhitespace();
350 pb.skipNonWhitespace();
351 assert(pb.eof());
352 }
353
354 {
355 char buf[] = "jhsj:!hskd;|.";
356 ParseBuffer pb(buf, strlen(buf));
357
358 pb.skipToOneOf(":@");
359 assert(*pb.position() == ':');
360 }
361
362 {
363 char buf[] = "user@host:port";
364 ParseBuffer pb(buf, strlen(buf));
365
366 pb.skipToOneOf(":@");
367 assert(*pb.position() == '@');
368 }
369
370 {
371 char buf[] = "jhsjfhskd;|.";
372 ParseBuffer pb(buf, strlen(buf));
373
374 pb.skipToOneOf(".|;");
375 assert(*pb.position() == ';');
376 }
377
378 {
379 char buf[] = "\" \\\"Q \t buffer with some stuff.\"Z";
380 ParseBuffer pb(buf, strlen(buf));
381 pb.skipWhitespace();
382 pb.skipToChar('"');
383 pb.skipChar();
384 pb.skipToEndQuote();
385 assert(*pb.position() == '"');
386 pb.skipChar();
387 assert(*pb.position() == 'Z');
388 }
389
390 {
391 char buf[] = "17 ";
392 ParseBuffer pb(buf, strlen(buf));
393 assert(pb.integer() == 17);
394 }
395
396 {
397 char buf[] = "-17";
398 ParseBuffer pb(buf, strlen(buf));
399 assert(pb.integer() == -17);
400 }
401
402 {
403 char buf[] = "999999999999999999999999999 ";
404 ParseBuffer pb(buf, strlen(buf));
405 try
406 {
407 pb.integer();
408 assert(0);
409 }
410 catch(ParseException& e)
411 {}
412 }
413
414 {
415 char buf[] = "-999999999999999999999999999 ";
416 ParseBuffer pb(buf, strlen(buf));
417 try
418 {
419 pb.integer();
420 assert(0);
421 }
422 catch(ParseException& e)
423 {}
424 }
425
426 #ifndef WIN32
427 {
428 char buf[] = "2890844526";
429 ParseBuffer pb(buf, strlen(buf));
430 assert(pb.uInt64() == 2890844526UL);
431 }
432 #endif
433
434 #ifndef RESIP_FIXED_POINT
435 {
436 char buf[] = "17.71";
437 ParseBuffer pb(buf, strlen(buf));
438 float val = pb.floatVal();
439 assert(val > 17.70 && val < 17.72);
440 }
441 #endif
442
443 {
444 char buf[] = "token another token";
445 ParseBuffer pb(buf, strlen(buf));
446 const char *start = pb.position();
447 pb.skipToChar(' ');
448 pb.skipChar(' ');
449 Data t;
450 // make t share memry with buf
451 pb.data(t, start);
452 assert(t.data() == buf);
453 // assign copies
454 Data t1 = t;
455 assert(t1.data() != buf);
456 // assign copies
457 t = t1;
458 assert(t.data() != buf);
459
460 start = pb.position();
461 pb.skipToChar(' ');
462 pb.skipChar(' ');
463 Data t2;
464 pb.data(t2, start);
465 // should survive scope exit
466 }
467
468 std::cerr << "All OK" << std::endl;
469 return 0;
470 }
471
472 /* ====================================================================
473 * The Vovida Software License, Version 1.0
474 *
475 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
476 *
477 * Redistribution and use in source and binary forms, with or without
478 * modification, are permitted provided that the following conditions
479 * are met:
480 *
481 * 1. Redistributions of source code must retain the above copyright
482 * notice, this list of conditions and the following disclaimer.
483 *
484 * 2. Redistributions in binary form must reproduce the above copyright
485 * notice, this list of conditions and the following disclaimer in
486 * the documentation and/or other materials provided with the
487 * distribution.
488 *
489 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
490 * and "Vovida Open Communication Application Library (VOCAL)" must
491 * not be used to endorse or promote products derived from this
492 * software without prior written permission. For written
493 * permission, please contact vocal@vovida.org.
494 *
495 * 4. Products derived from this software may not be called "VOCAL", nor
496 * may "VOCAL" appear in their name, without prior written
497 * permission of Vovida Networks, Inc.
498 *
499 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
500 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
501 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
502 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
503 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
504 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
505 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
506 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
507 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
508 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
509 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
510 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
511 * DAMAGE.
512 *
513 * ====================================================================
514 *
515 * This software consists of voluntary contributions made by Vovida
516 * Networks, Inc. and many individuals on behalf of Vovida Networks,
517 * Inc. For more information on Vovida Networks, Inc., please see
518 * <http://www.vovida.org/>.
519 *
520 */

Properties

Name Value
svn:eol-style native
svn:mime-type text/plain

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27