1//
2// VarTest.cpp
3//
4// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "VarTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Exception.h"
15#include "Poco/Dynamic/Var.h"
16#include "Poco/Bugcheck.h"
17#include "Poco/Dynamic/Struct.h"
18#include "Poco/Dynamic/Pair.h"
19#include <map>
20#include <utility>
21
22
23#if defined(_MSC_VER) && _MSC_VER < 1400
24 #pragma warning(disable:4800)//forcing value to bool 'true' or 'false'
25#endif
26
27
28using namespace Poco;
29using namespace Poco::Dynamic;
30
31
32class Dummy
33{
34public:
35 Dummy(): _val(0)
36 {
37 }
38
39 Dummy(int val): _val(val)
40 {
41 }
42
43 operator int () const
44 {
45 return _val;
46 }
47
48 bool operator == (int i)
49 {
50 return i == _val;
51 }
52
53private:
54 int _val;
55};
56
57
58VarTest::VarTest(const std::string& rName): CppUnit::TestCase(rName)
59{
60}
61
62
63VarTest::~VarTest()
64{
65}
66
67
68void VarTest::testInt8()
69{
70 Poco::Int8 src = 32;
71 Var a1 = src;
72
73 assertTrue (a1.type() == typeid(Poco::Int8));
74
75 std::string s1;
76 Poco::Int8 s2;
77 Poco::Int16 s3;
78 Poco::Int32 s4;
79 Poco::Int64 s5;
80 Poco::UInt8 s6;
81 Poco::UInt16 s7;
82 Poco::UInt32 s8;
83 Poco::UInt64 s9;
84 float s10;
85 double s11;
86 bool s12;
87 char s13;
88 a1.convert(s1);
89 a1.convert(s2);
90 a1.convert(s3);
91 a1.convert(s4);
92 a1.convert(s5);
93 a1.convert(s6);
94 a1.convert(s7);
95 a1.convert(s8);
96 a1.convert(s9);
97 a1.convert(s10);
98 a1.convert(s11);
99 a1.convert(s12);
100 a1.convert(s13);
101 Int64 s14;
102 UInt64 s15;
103 a1.convert(s14);
104 a1.convert(s15);
105 assertTrue (s14 == 32);
106 assertTrue (s15 == 32);
107 assertTrue (s1 == "32");
108 assertTrue (s2 == 32);
109 assertTrue (s3 == 32);
110 assertTrue (s4 == 32);
111 assertTrue (s5 == 32);
112 assertTrue (s6 == 32);
113 assertTrue (s7 == 32);
114 assertTrue (s8 == 32);
115 assertTrue (s9 == 32);
116 assertTrue (s10 == 32.0f);
117 assertTrue (s11 == 32.0);
118 assertTrue (s12);
119 assertTrue (s13 == ' ');
120 Var a2(a1);
121 std::string t2;
122 a2.convert(t2);
123 assertTrue (s1 == t2);
124
125 Int8 value = a1.extract<Int8>();
126 assertTrue (value == 32);
127
128 try
129 {
130 Int16 value2; value2 = a1.extract<Int16>();
131 fail("bad cast - must throw");
132 }
133 catch (Poco::BadCastException&) { }
134
135 try
136 {
137 Poco::UInt8 usrc = 200;
138 a1 = usrc;
139 src = a1;
140 fail("out of range - must throw");
141 }
142 catch (Poco::RangeException&)
143 {
144 a1 = src;
145 }
146
147 Var a3 = a1 + 1;
148 assertTrue (a3 == 33);
149 a3 = a1 - 1;
150 assertTrue (a3 == 31);
151 a3 += 1;
152 assertTrue (a3 == 32);
153 a3 -= 1;
154 assertTrue (a3 == 31);
155 a3 = a1 / 2;
156 assertTrue (a3 == 16);
157 a3 = a1 * 2;
158 assertTrue (a3 == 64);
159 a3 /= 2;
160 assertTrue (a3 == 32);
161 a3 *= 2;
162 assertTrue (a3 == 64);
163}
164
165
166void VarTest::testInt16()
167{
168 Poco::Int16 src = 32;
169 Var a1 = src;
170
171 assertTrue (a1.type() == typeid(Poco::Int16));
172
173 std::string s1;
174 Poco::Int8 s2;
175 Poco::Int16 s3;
176 Poco::Int32 s4;
177 Poco::Int64 s5;
178 Poco::UInt8 s6;
179 Poco::UInt16 s7;
180 Poco::UInt32 s8;
181 Poco::UInt64 s9;
182 float s10;
183 double s11;
184 bool s12;
185 char s13;
186 a1.convert(s1);
187 a1.convert(s2);
188 a1.convert(s3);
189 a1.convert(s4);
190 a1.convert(s5);
191 a1.convert(s6);
192 a1.convert(s7);
193 a1.convert(s8);
194 a1.convert(s9);
195 a1.convert(s10);
196 a1.convert(s11);
197 a1.convert(s12);
198 a1.convert(s13);
199
200 Int64 s14;
201 UInt64 s15;
202 a1.convert(s14);
203 a1.convert(s15);
204 assertTrue (s14 == 32);
205 assertTrue (s15 == 32);
206 assertTrue (s1 == "32");
207 assertTrue (s2 == 32);
208 assertTrue (s3 == 32);
209 assertTrue (s4 == 32);
210 assertTrue (s5 == 32);
211 assertTrue (s6 == 32);
212 assertTrue (s7 == 32);
213 assertTrue (s8 == 32);
214 assertTrue (s9 == 32);
215 assertTrue (s10 == 32.0f);
216 assertTrue (s11 == 32.0);
217 assertTrue (s12);
218 assertTrue (s13 == ' ');
219 Var a2(a1);
220 std::string t2;
221 a2.convert(t2);
222 assertTrue (s1 == t2);
223
224 Int16 value = a1.extract<Int16>();
225 assertTrue (value == 32);
226
227 try
228 {
229 Int32 value2; value2 = a1.extract<Int32>();
230 fail("bad cast - must throw");
231 }
232 catch (Poco::BadCastException&)
233 {
234 }
235
236 Var a3 = a1 + 1;
237 assertTrue (a3 == 33);
238 a3 = a1 - 1;
239 assertTrue (a3 == 31);
240 a3 += 1;
241 assertTrue (a3 == 32);
242 a3 -= 1;
243 assertTrue (a3 == 31);
244 a3 = a1 / 2;
245 assertTrue (a3 == 16);
246 a3 = a1 * 2;
247 assertTrue (a3 == 64);
248 a3 /= 2;
249 assertTrue (a3 == 32);
250 a3 *= 2;
251 assertTrue (a3 == 64);
252}
253
254
255void VarTest::testInt32()
256{
257 Poco::Int32 src = 32;
258 Var a1 = src;
259
260 assertTrue (a1.type() == typeid(Poco::Int32));
261
262 std::string s1;
263 Poco::Int8 s2;
264 Poco::Int16 s3;
265 Poco::Int32 s4;
266 Poco::Int64 s5;
267 Poco::UInt8 s6;
268 Poco::UInt16 s7;
269 Poco::UInt32 s8;
270 Poco::UInt64 s9;
271 float s10;
272 double s11;
273 bool s12;
274 char s13;
275 a1.convert(s1);
276 a1.convert(s2);
277 a1.convert(s3);
278 a1.convert(s4);
279 a1.convert(s5);
280 a1.convert(s6);
281 a1.convert(s7);
282 a1.convert(s8);
283 a1.convert(s9);
284 a1.convert(s10);
285 a1.convert(s11);
286 a1.convert(s12);
287 a1.convert(s13);
288
289 Int64 s14;
290 UInt64 s15;
291 a1.convert(s14);
292 a1.convert(s15);
293 assertTrue (s14 == 32);
294 assertTrue (s15 == 32);
295 assertTrue (s1 == "32");
296 assertTrue (s2 == 32);
297 assertTrue (s3 == 32);
298 assertTrue (s4 == 32);
299 assertTrue (s5 == 32);
300 assertTrue (s6 == 32);
301 assertTrue (s7 == 32);
302 assertTrue (s8 == 32);
303 assertTrue (s9 == 32);
304 assertTrue (s10 == 32.0f);
305 assertTrue (s11 == 32.0);
306 assertTrue (s12);
307 assertTrue (s13 == ' ');
308 Var a2(a1);
309 std::string t2;
310 a2.convert(t2);
311 assertTrue (s1 == t2);
312
313 Int32 value = a1.extract<Int32>();
314 assertTrue (value == 32);
315
316 try
317 {
318 Int16 value2; value2 = a1.extract<Int16>();
319 fail("bad cast - must throw");
320 }
321 catch (Poco::BadCastException&)
322 {
323 }
324
325 Var a3 = a1 + 1;
326 assertTrue (a3 == 33);
327 a3 = a1 - 1;
328 assertTrue (a3 == 31);
329 a3 += 1;
330 assertTrue (a3 == 32);
331 a3 -= 1;
332 assertTrue (a3 == 31);
333 a3 = a1 / 2;
334 assertTrue (a3 == 16);
335 a3 = a1 * 2;
336 assertTrue (a3 == 64);
337 a3 /= 2;
338 assertTrue (a3 == 32);
339 a3 *= 2;
340 assertTrue (a3 == 64);
341}
342
343
344void VarTest::testInt64()
345{
346 Poco::Int64 src = 32;
347 Var a1 = src;
348
349 assertTrue (a1.type() == typeid(Poco::Int64));
350
351 std::string s1;
352 Poco::Int8 s2;
353 Poco::Int16 s3;
354 Poco::Int32 s4;
355 Poco::Int64 s5;
356 Poco::UInt8 s6;
357 Poco::UInt16 s7;
358 Poco::UInt32 s8;
359 Poco::UInt64 s9;
360 float s10;
361 double s11;
362 bool s12;
363 char s13;
364 a1.convert(s1);
365 a1.convert(s2);
366 a1.convert(s3);
367 a1.convert(s4);
368 a1.convert(s5);
369 a1.convert(s6);
370 a1.convert(s7);
371 a1.convert(s8);
372 a1.convert(s9);
373 a1.convert(s10);
374 a1.convert(s11);
375 a1.convert(s12);
376 a1.convert(s13);
377
378 Int64 s14;
379 UInt64 s15;
380 a1.convert(s14);
381 a1.convert(s15);
382 assertTrue (s14 == 32);
383 assertTrue (s15 == 32);
384 assertTrue (s1 == "32");
385 assertTrue (s2 == 32);
386 assertTrue (s3 == 32);
387 assertTrue (s4 == 32);
388 assertTrue (s5 == 32);
389 assertTrue (s6 == 32);
390 assertTrue (s7 == 32);
391 assertTrue (s8 == 32);
392 assertTrue (s9 == 32);
393 assertTrue (s10 == 32.0f);
394 assertTrue (s11 == 32.0);
395 assertTrue (s12);
396 assertTrue (s13 == ' ');
397 Var a2(a1);
398 std::string t2;
399 a2.convert(t2);
400 assertTrue (s1 == t2);
401
402 Int64 value = a1.extract<Int64>();
403 assertTrue (value == 32);
404
405 try
406 {
407 Int16 value2; value2 = a1.extract<Int16>();
408 fail("bad cast - must throw");
409 }
410 catch (Poco::BadCastException&)
411 {
412 }
413
414 Var a3 = a1 + 1;
415 assertTrue (a3 == 33);
416 a3 = a1 - 1;
417 assertTrue (a3 == 31);
418 a3 += 1;
419 assertTrue (a3 == 32);
420 a3 -= 1;
421 assertTrue (a3 == 31);
422 a3 = a1 / 2;
423 assertTrue (a3 == 16);
424 a3 = a1 * 2;
425 assertTrue (a3 == 64);
426 a3 /= 2;
427 assertTrue (a3 == 32);
428 a3 *= 2;
429 assertTrue (a3 == 64);
430}
431
432
433void VarTest::testUInt8()
434{
435 Poco::UInt8 src = 32;
436 Var a1 = src;
437
438 assertTrue (a1.type() == typeid(Poco::UInt8));
439
440 std::string s1;
441 Poco::Int8 s2;
442 Poco::Int16 s3;
443 Poco::Int32 s4;
444 Poco::Int64 s5;
445 Poco::UInt8 s6;
446 Poco::UInt16 s7;
447 Poco::UInt32 s8;
448 Poco::UInt64 s9;
449 float s10;
450 double s11;
451 bool s12;
452 char s13;
453 a1.convert(s1);
454 a1.convert(s2);
455 a1.convert(s3);
456 a1.convert(s4);
457 a1.convert(s5);
458 a1.convert(s6);
459 a1.convert(s7);
460 a1.convert(s8);
461 a1.convert(s9);
462 a1.convert(s10);
463 a1.convert(s11);
464 a1.convert(s12);
465 a1.convert(s13);
466
467 Int64 s14;
468 UInt64 s15;
469 a1.convert(s14);
470 a1.convert(s15);
471 assertTrue (s14 == 32);
472 assertTrue (s15 == 32);
473 assertTrue (s1 == "32");
474 assertTrue (s2 == 32);
475 assertTrue (s3 == 32);
476 assertTrue (s4 == 32);
477 assertTrue (s5 == 32);
478 assertTrue (s6 == 32);
479 assertTrue (s7 == 32);
480 assertTrue (s8 == 32);
481 assertTrue (s9 == 32);
482 assertTrue (s10 == 32.0f);
483 assertTrue (s11 == 32.0);
484 assertTrue (s12);
485 assertTrue (s13 == ' ');
486 Var a2(a1);
487 std::string t2;
488 a2.convert(t2);
489 assertTrue (s1 == t2);
490
491 UInt8 value = a1.extract<UInt8>();
492 assertTrue (value == 32);
493
494 try
495 {
496 Int16 value2; value2 = a1.extract<Int16>();
497 fail("bad cast - must throw");
498 }
499 catch (Poco::BadCastException&)
500 {
501 }
502
503 Var a3 = a1 + 1;
504 assertTrue (a3 == 33);
505 a3 = a1 - 1;
506 assertTrue (a3 == 31);
507 a3 += 1;
508 assertTrue (a3 == 32);
509 a3 -= 1;
510 assertTrue (a3 == 31);
511 a3 = a1 / 2;
512 assertTrue (a3 == 16);
513 a3 = a1 * 2;
514 assertTrue (a3 == 64);
515 a3 /= 2;
516 assertTrue (a3 == 32);
517 a3 *= 2;
518 assertTrue (a3 == 64);
519}
520
521
522void VarTest::testUInt16()
523{
524 Poco::UInt16 src = 32;
525 Var a1 = src;
526
527 assertTrue (a1.type() == typeid(Poco::UInt16));
528
529 std::string s1;
530 Poco::Int8 s2;
531 Poco::Int16 s3;
532 Poco::Int32 s4;
533 Poco::Int64 s5;
534 Poco::UInt8 s6;
535 Poco::UInt16 s7;
536 Poco::UInt32 s8;
537 Poco::UInt64 s9;
538 float s10;
539 double s11;
540 bool s12;
541 char s13;
542 a1.convert(s1);
543 a1.convert(s2);
544 a1.convert(s3);
545 a1.convert(s4);
546 a1.convert(s5);
547 a1.convert(s6);
548 a1.convert(s7);
549 a1.convert(s8);
550 a1.convert(s9);
551 a1.convert(s10);
552 a1.convert(s11);
553 a1.convert(s12);
554 a1.convert(s13);
555 Int64 s14;
556 UInt64 s15;
557 a1.convert(s14);
558 a1.convert(s15);
559 assertTrue (s14 == 32);
560 assertTrue (s15 == 32);
561 assertTrue (s1 == "32");
562 assertTrue (s2 == 32);
563 assertTrue (s3 == 32);
564 assertTrue (s4 == 32);
565 assertTrue (s5 == 32);
566 assertTrue (s6 == 32);
567 assertTrue (s7 == 32);
568 assertTrue (s8 == 32);
569 assertTrue (s9 == 32);
570 assertTrue (s10 == 32.0f);
571 assertTrue (s11 == 32.0);
572 assertTrue (s12);
573 assertTrue (s13 == ' ');
574 Var a2(a1);
575 std::string t2;
576 a2.convert(t2);
577 assertTrue (s1 == t2);
578
579 UInt16 value = a1.extract<UInt16>();
580 assertTrue (value == 32);
581
582 try
583 {
584 Int16 value2; value2 = a1.extract<Int16>();
585 fail("bad cast - must throw");
586 }
587 catch (Poco::BadCastException&)
588 {
589 }
590
591 Var a3 = a1 + 1;
592 assertTrue (a3 == 33);
593 a3 = a1 - 1;
594 assertTrue (a3 == 31);
595 a3 += 1;
596 assertTrue (a3 == 32);
597 a3 -= 1;
598 assertTrue (a3 == 31);
599 a3 = a1 / 2;
600 assertTrue (a3 == 16);
601 a3 = a1 * 2;
602 assertTrue (a3 == 64);
603 a3 /= 2;
604 assertTrue (a3 == 32);
605 a3 *= 2;
606 assertTrue (a3 == 64);
607}
608
609
610void VarTest::testUInt32()
611{
612 Poco::UInt32 src = 32;
613 Var a1 = src;
614
615 assertTrue (a1.type() == typeid(Poco::UInt32));
616
617 std::string s1;
618 Poco::Int8 s2;
619 Poco::Int16 s3;
620 Poco::Int32 s4;
621 Poco::Int64 s5;
622 Poco::UInt8 s6;
623 Poco::UInt16 s7;
624 Poco::UInt32 s8;
625 Poco::UInt64 s9;
626 float s10;
627 double s11;
628 bool s12;
629 char s13;
630 a1.convert(s1);
631 a1.convert(s2);
632 a1.convert(s3);
633 a1.convert(s4);
634 a1.convert(s5);
635 a1.convert(s6);
636 a1.convert(s7);
637 a1.convert(s8);
638 a1.convert(s9);
639 a1.convert(s10);
640 a1.convert(s11);
641 a1.convert(s12);
642 a1.convert(s13);
643 Int64 s14;
644 UInt64 s15;
645 a1.convert(s14);
646 a1.convert(s15);
647 assertTrue (s14 == 32);
648 assertTrue (s15 == 32);
649 assertTrue (s1 == "32");
650 assertTrue (s2 == 32);
651 assertTrue (s3 == 32);
652 assertTrue (s4 == 32);
653 assertTrue (s5 == 32);
654 assertTrue (s6 == 32);
655 assertTrue (s7 == 32);
656 assertTrue (s8 == 32);
657 assertTrue (s9 == 32);
658 assertTrue (s10 == 32.0f);
659 assertTrue (s11 == 32.0);
660 assertTrue (s12);
661 assertTrue (s13 == ' ');
662 Var a2(a1);
663 std::string t2;
664 a2.convert(t2);
665 assertTrue (s1 == t2);
666
667 UInt32 value = a1.extract<UInt32>();
668 assertTrue (value == 32);
669
670 try
671 {
672 Int16 value2; value2 = a1.extract<Int16>();
673 fail("bad cast - must throw");
674 }
675 catch (Poco::BadCastException&)
676 {
677 }
678
679 Var a3 = a1 + 1;
680 assertTrue (a3 == 33);
681 a3 = a1 - 1;
682 assertTrue (a3 == 31);
683 a3 += 1;
684 assertTrue (a3 == 32);
685 a3 -= 1;
686 assertTrue (a3 == 31);
687 a3 = a1 / 2;
688 assertTrue (a3 == 16);
689 a3 = a1 * 2;
690 assertTrue (a3 == 64);
691 a3 /= 2;
692 assertTrue (a3 == 32);
693 a3 *= 2;
694 assertTrue (a3 == 64);
695}
696
697
698void VarTest::testUInt64()
699{
700 Poco::UInt64 src = 32;
701 Var a1 = src;
702
703 assertTrue (a1.type() == typeid(Poco::UInt64));
704
705 std::string s1;
706 Poco::Int8 s2;
707 Poco::Int16 s3;
708 Poco::Int32 s4;
709 Poco::Int64 s5;
710 Poco::UInt8 s6;
711 Poco::UInt16 s7;
712 Poco::UInt32 s8;
713 Poco::UInt64 s9;
714 float s10;
715 double s11;
716 bool s12;
717 char s13;
718 a1.convert(s1);
719 a1.convert(s2);
720 a1.convert(s3);
721 a1.convert(s4);
722 a1.convert(s5);
723 a1.convert(s6);
724 a1.convert(s7);
725 a1.convert(s8);
726 a1.convert(s9);
727 a1.convert(s10);
728 a1.convert(s11);
729 a1.convert(s12);
730 a1.convert(s13);
731 Int64 s14;
732 UInt64 s15;
733 a1.convert(s14);
734 a1.convert(s15);
735 assertTrue (s14 == 32);
736 assertTrue (s15 == 32);
737 assertTrue (s1 == "32");
738 assertTrue (s2 == 32);
739 assertTrue (s3 == 32);
740 assertTrue (s4 == 32);
741 assertTrue (s5 == 32);
742 assertTrue (s6 == 32);
743 assertTrue (s7 == 32);
744 assertTrue (s8 == 32);
745 assertTrue (s9 == 32);
746 assertTrue (s10 == 32.0f);
747 assertTrue (s11 == 32.0);
748 assertTrue (s12);
749 assertTrue (s13 == ' ');
750 Var a2(a1);
751 std::string t2;
752 a2.convert(t2);
753 assertTrue (s1 == t2);
754
755 UInt64 value = a1.extract<UInt64>();
756 assertTrue (value == 32);
757
758 try
759 {
760 Int16 value2; value2 = a1.extract<Int16>();
761 fail("bad cast - must throw");
762 }
763 catch (Poco::BadCastException&)
764 {
765 }
766
767 Var a3 = a1 + 1;
768 assertTrue (a3 == 33);
769 a3 = a1 - 1;
770 assertTrue (a3 == 31);
771 a3 += 1;
772 assertTrue (a3 == 32);
773 a3 -= 1;
774 assertTrue (a3 == 31);
775 a3 = a1 / 2;
776 assertTrue (a3 == 16);
777 a3 = a1 * 2;
778 assertTrue (a3 == 64);
779 a3 /= 2;
780 assertTrue (a3 == 32);
781 a3 *= 2;
782 assertTrue (a3 == 64);
783}
784
785
786void VarTest::testBool()
787{
788 bool src = true;
789 Var a1 = src;
790
791 assertTrue (a1.type() == typeid(bool));
792
793 std::string s1;
794 Poco::Int8 s2;
795 Poco::Int16 s3;
796 Poco::Int32 s4;
797 Poco::Int64 s5;
798 Poco::UInt8 s6;
799 Poco::UInt16 s7;
800 Poco::UInt32 s8;
801 Poco::UInt64 s9;
802 float s10;
803 double s11;
804 bool s12;
805 char s13;
806 a1.convert(s1);
807 a1.convert(s2);
808 a1.convert(s3);
809 a1.convert(s4);
810 a1.convert(s5);
811 a1.convert(s6);
812 a1.convert(s7);
813 a1.convert(s8);
814 a1.convert(s9);
815 a1.convert(s10);
816 a1.convert(s11);
817 a1.convert(s12);
818 a1.convert(s13);
819 Int64 s14;
820 UInt64 s15;
821 a1.convert(s14);
822 a1.convert(s15);
823 assertTrue (s14 == 1);
824 assertTrue (s15 == 1);
825 assertTrue (s1 == "true");
826 assertTrue (s2 == 1);
827 assertTrue (s3 == 1);
828 assertTrue (s4 == 1);
829 assertTrue (s5 == 1);
830 assertTrue (s6 == 1);
831 assertTrue (s7 == 1);
832 assertTrue (s8 == 1);
833 assertTrue (s9 == 1);
834 assertTrue (s10 == 1.0f);
835 assertTrue (s11 == 1.0);
836 assertTrue (s12);
837 assertTrue (s13 == '\x1');
838 Var a2(a1);
839 std::string t2;
840 a2.convert(t2);
841 assertTrue (s1 == t2);
842
843 bool value = a1.extract<bool>();
844 assertTrue (value);
845
846 try
847 {
848 Int16 value2; value2 = a1.extract<Int16>();
849 fail("bad cast - must throw");
850 }
851 catch (Poco::BadCastException&)
852 {
853 }
854}
855
856
857void VarTest::testChar()
858{
859 char src = ' ';
860 Var a1 = src;
861
862 assertTrue (a1.type() == typeid(char));
863
864 std::string s1;
865 Poco::Int8 s2;
866 Poco::Int16 s3;
867 Poco::Int32 s4;
868 Poco::Int64 s5;
869 Poco::UInt8 s6;
870 Poco::UInt16 s7;
871 Poco::UInt32 s8;
872 Poco::UInt64 s9;
873 float s10;
874 double s11;
875 bool s12;
876 char s13;
877 a1.convert(s1);
878 a1.convert(s2);
879 a1.convert(s3);
880 a1.convert(s4);
881 a1.convert(s5);
882 a1.convert(s6);
883 a1.convert(s7);
884 a1.convert(s8);
885 a1.convert(s9);
886 a1.convert(s10);
887 a1.convert(s11);
888 a1.convert(s12);
889 a1.convert(s13);
890 Int64 s14;
891 UInt64 s15;
892 a1.convert(s14);
893 a1.convert(s15);
894 assertTrue (s14 == 32);
895 assertTrue (s15 == 32);
896 assertTrue (s1 == " ");
897 assertTrue (s2 == 32);
898 assertTrue (s3 == 32);
899 assertTrue (s4 == 32);
900 assertTrue (s5 == 32);
901 assertTrue (s6 == 32);
902 assertTrue (s7 == 32);
903 assertTrue (s8 == 32);
904 assertTrue (s9 == 32);
905 assertTrue (s10 == 32.0f);
906 assertTrue (s11 == 32.0);
907 assertTrue (s12);
908 assertTrue (s13 == ' ');
909 Var a2(a1);
910 std::string t2;
911 a2.convert(t2);
912 assertTrue (s1 == t2);
913
914 char value = a1.extract<char>();
915 assertTrue (value == ' ');
916
917 try
918 {
919 Int16 value2; value2 = a1.extract<Int16>();
920 fail("bad cast - must throw");
921 }
922 catch (Poco::BadCastException&)
923 {
924 }
925}
926
927
928void VarTest::testFloat()
929{
930 Var any("0");
931 float f = any;
932
933 float src = 32.0f;
934 Var a1 = src;
935
936 assertTrue (a1.type() == typeid(float));
937
938 std::string s1;
939 Poco::Int8 s2;
940 Poco::Int16 s3;
941 Poco::Int32 s4;
942 Poco::Int64 s5;
943 Poco::UInt8 s6;
944 Poco::UInt16 s7;
945 Poco::UInt32 s8;
946 Poco::UInt64 s9;
947 float s10;
948 double s11;
949 bool s12;
950 char s13;
951 a1.convert(s1);
952 a1.convert(s2);
953 a1.convert(s3);
954 a1.convert(s4);
955 a1.convert(s5);
956 a1.convert(s6);
957 a1.convert(s7);
958 a1.convert(s8);
959 a1.convert(s9);
960 a1.convert(s10);
961 a1.convert(s11);
962 a1.convert(s12);
963 a1.convert(s13);
964 Int64 s14;
965 UInt64 s15;
966 a1.convert(s14);
967 a1.convert(s15);
968 assertTrue (s14 == 32);
969 assertTrue (s15 == 32);
970 assertTrue (s1 == "32");
971 assertTrue (s2 == 32);
972 assertTrue (s3 == 32);
973 assertTrue (s4 == 32);
974 assertTrue (s5 == 32);
975 assertTrue (s6 == 32);
976 assertTrue (s7 == 32);
977 assertTrue (s8 == 32);
978 assertTrue (s9 == 32);
979 assertTrue (s10 == 32.0f);
980 assertTrue (s11 == 32.0);
981 assertTrue (s12);
982 assertTrue (s13 == ' ');
983 Var a2(a1);
984 std::string t2;
985 a2.convert(t2);
986 assertTrue (s1 == t2);
987
988 float value = a1.extract<float>();
989 assertTrue (value == 32.0f);
990
991 try
992 {
993 Int16 value2; value2 = a1.extract<Int16>();
994 fail("bad cast - must throw");
995 }
996 catch (Poco::BadCastException&)
997 {
998 }
999
1000 Var a3 = a1 + 1.0f;
1001 assertTrue (a3 == 33.0f);
1002 a3 = a1 - 1.0f;
1003 assertTrue (a3 == 31.0f);
1004 a3 += 1.0f;
1005 assertTrue (a3 == 32.0f);
1006 a3 -= 1.0f;
1007 assertTrue (a3 == 31.0f);
1008 a3 = a1 / 2.0f;
1009 assertTrue (a3 == 16.0f);
1010 a3 = a1 * 2.0f;
1011 assertTrue (a3 == 64.0f);
1012 a3 /= 2.0f;
1013 assertTrue (a3 == 32.0f);
1014 a3 *= 2.0f;
1015 assertTrue (a3 == 64.0f);
1016}
1017
1018
1019void VarTest::testDouble()
1020{
1021 double d = 0;
1022 Var v(d);
1023 float f = v;
1024
1025 double src = 32.0;
1026 Var a1 = src;
1027
1028 assertTrue (a1.type() == typeid(double));
1029
1030 std::string s1;
1031 Poco::Int8 s2;
1032 Poco::Int16 s3;
1033 Poco::Int32 s4;
1034 Poco::Int64 s5;
1035 Poco::UInt8 s6;
1036 Poco::UInt16 s7;
1037 Poco::UInt32 s8;
1038 Poco::UInt64 s9;
1039 float s10;
1040 double s11;
1041 bool s12;
1042 char s13;
1043 a1.convert(s1);
1044 a1.convert(s2);
1045 a1.convert(s3);
1046 a1.convert(s4);
1047 a1.convert(s5);
1048 a1.convert(s6);
1049 a1.convert(s7);
1050 a1.convert(s8);
1051 a1.convert(s9);
1052 a1.convert(s10);
1053 a1.convert(s11);
1054 a1.convert(s12);
1055 a1.convert(s13);
1056 Int64 s14;
1057 UInt64 s15;
1058 a1.convert(s14);
1059 a1.convert(s15);
1060 assertTrue (s14 == 32);
1061 assertTrue (s15 == 32);
1062 assertTrue (s1 == "32");
1063 assertTrue (s2 == 32);
1064 assertTrue (s3 == 32);
1065 assertTrue (s4 == 32);
1066 assertTrue (s5 == 32);
1067 assertTrue (s6 == 32);
1068 assertTrue (s7 == 32);
1069 assertTrue (s8 == 32);
1070 assertTrue (s9 == 32);
1071 assertTrue (s10 == 32.0f);
1072 assertTrue (s11 == 32.0);
1073 assertTrue (s12);
1074 assertTrue (s13 == ' ');
1075 Var a2(a1);
1076 std::string t2;
1077 a2.convert(t2);
1078 assertTrue (s1 == t2);
1079
1080 double value = a1.extract<double>();
1081 assertTrue (value == 32.0);
1082
1083 try
1084 {
1085 Int16 value2; value2 = a1.extract<Int16>();
1086 fail("bad cast - must throw");
1087 }
1088 catch (Poco::BadCastException&)
1089 {
1090 }
1091
1092
1093 Var a3 = a1 + 1.0;
1094 assertTrue (a3 == 33.0);
1095 a3 = a1 - 1.0;
1096 assertTrue (a3 == 31.0);
1097 a3 += 1.0;
1098 assertTrue (a3 == 32.0);
1099 a3 -= 1.0;
1100 assertTrue (a3 == 31.0);
1101 a3 = a1 / 2.0;
1102 assertTrue (a3 == 16.0);
1103 a3 = a1 * 2.0;
1104 assertTrue (a3 == 64.0);
1105 a3 /= 2.0;
1106 assertTrue (a3 == 32.0);
1107 a3 *= 2.0;
1108 assertTrue (a3 == 64.0);
1109}
1110
1111
1112void VarTest::testString()
1113{
1114 Var a1("32");
1115
1116 assertTrue (a1.type() == typeid(std::string));
1117
1118 std::string s1;
1119 Poco::Int8 s2;
1120 Poco::Int16 s3;
1121 Poco::Int32 s4;
1122 Poco::Int64 s5;
1123 Poco::UInt8 s6;
1124 Poco::UInt16 s7;
1125 Poco::UInt32 s8;
1126 Poco::UInt64 s9;
1127 float s10;
1128 double s11;
1129 bool s12 = false;
1130 char s13;
1131 a1.convert(s1);
1132 a1.convert(s2);
1133 a1.convert(s3);
1134 a1.convert(s4);
1135 a1.convert(s5);
1136 a1.convert(s6);
1137 a1.convert(s7);
1138 a1.convert(s8);
1139 a1.convert(s9);
1140 a1.convert(s10);
1141 a1.convert(s11);
1142 a1.convert(s12);
1143 a1.convert(s13);
1144 Int64 s14;
1145 UInt64 s15;
1146 a1.convert(s14);
1147 a1.convert(s15);
1148 assertTrue (s14 == 32);
1149 assertTrue (s15 == 32);
1150 assertTrue (s1 == "32");
1151 assertTrue (s2 == 32);
1152 assertTrue (s3 == 32);
1153 assertTrue (s4 == 32);
1154 assertTrue (s5 == 32);
1155 assertTrue (s6 == 32);
1156 assertTrue (s7 == 32);
1157 assertTrue (s8 == 32);
1158 assertTrue (s9 == 32);
1159 assertTrue (s10 == 32.0f);
1160 assertTrue (s11 == 32.0);
1161 assertTrue (s12);
1162 assertTrue (s13 == '3');
1163
1164 const std::string& value = a1.extract<std::string>();
1165 assertTrue (value == "32");
1166
1167 try
1168 {
1169 Int16 value2; value2 = a1.extract<Int16>();
1170 fail("bad cast - must throw");
1171 }
1172 catch (Poco::BadCastException&)
1173 {
1174 }
1175
1176 Var a4(123);
1177 std::string s("456");
1178 Var a5 = a4 + s;
1179 assertTrue (a5 == "123456");
1180 a4 += s;
1181 assertTrue (a4 == "123456");
1182 Var a6 = a4 + "789";
1183 assertTrue (a6 == "123456789");
1184 a4 += "789";
1185 assertTrue (a4 == "123456789");
1186
1187 a4 = "";
1188 assertTrue (!a4);
1189 a4 = "0";
1190 assertTrue (!a4);
1191 a4 = "FaLsE";
1192 assertTrue (!a4);
1193}
1194
1195
1196void VarTest::testLong()
1197{
1198#ifndef POCO_LONG_IS_64_BIT
1199 long src = 32;
1200 Var a1 = src;
1201
1202 assertTrue (a1.type() == typeid(long));
1203
1204 std::string s1;
1205 Poco::Int8 s2;
1206 Poco::Int16 s3;
1207 Poco::Int32 s4;
1208 Poco::Int64 s5;
1209 Poco::UInt8 s6;
1210 Poco::UInt16 s7;
1211 Poco::UInt32 s8;
1212 Poco::UInt64 s9;
1213 float s10;
1214 double s11;
1215 bool s12;
1216 char s13;
1217 a1.convert(s1);
1218 a1.convert(s2);
1219 a1.convert(s3);
1220 a1.convert(s4);
1221 a1.convert(s5);
1222 a1.convert(s6);
1223 a1.convert(s7);
1224 a1.convert(s8);
1225 a1.convert(s9);
1226 a1.convert(s10);
1227 a1.convert(s11);
1228 a1.convert(s12);
1229 a1.convert(s13);
1230 long s14;
1231 unsigned long s15;
1232 a1.convert(s14);
1233 a1.convert(s15);
1234 assertTrue (s14 == 32);
1235 assertTrue (s15 == 32);
1236 assertTrue (s1 == "32");
1237 assertTrue (s2 == 32);
1238 assertTrue (s3 == 32);
1239 assertTrue (s4 == 32);
1240 assertTrue (s5 == 32);
1241 assertTrue (s6 == 32);
1242 assertTrue (s7 == 32);
1243 assertTrue (s8 == 32);
1244 assertTrue (s9 == 32);
1245 assertTrue (s10 == 32.0f);
1246 assertTrue (s11 == 32.0);
1247 assertTrue (s12);
1248 assertTrue (s13 == ' ');
1249 Var a2(a1);
1250 std::string t2;
1251 a2.convert(t2);
1252 assertTrue (s1 == t2);
1253
1254 long value = a1.extract<long>();
1255 assertTrue (value == 32);
1256
1257 try
1258 {
1259 Int16 value2; value2 = a1.extract<Int16>();
1260 fail("bad cast - must throw");
1261 }
1262 catch (Poco::BadCastException&)
1263 {
1264 }
1265
1266 Var a3 = a1 + 1;
1267 assertTrue (a3 == 33);
1268 a3 = a1 - 1;
1269 assertTrue (a3 == 31);
1270 a3 += 1;
1271 assertTrue (a3 == 32);
1272 a3 -= 1;
1273 assertTrue (a3 == 31);
1274 a3 = a1 / 2;
1275 assertTrue (a3 == 16);
1276 a3 = a1 * 2;
1277 assertTrue (a3 == 64);
1278 a3 /= 2;
1279 assertTrue (a3 == 32);
1280 a3 *= 2;
1281 assertTrue (a3 == 64);
1282#endif // POCO_LONG_IS_64_BIT
1283}
1284
1285
1286void VarTest::testULong()
1287{
1288#ifndef POCO_LONG_IS_64_BIT
1289 unsigned long src = 32;
1290 Var a1 = src;
1291
1292 assertTrue (a1.type() == typeid(unsigned long));
1293
1294 std::string s1;
1295 Poco::Int8 s2;
1296 Poco::Int16 s3;
1297 Poco::Int32 s4;
1298 Poco::Int64 s5;
1299 Poco::UInt8 s6;
1300 Poco::UInt16 s7;
1301 Poco::UInt32 s8;
1302 Poco::UInt64 s9;
1303 float s10;
1304 double s11;
1305 bool s12;
1306 char s13;
1307 a1.convert(s1);
1308 a1.convert(s2);
1309 a1.convert(s3);
1310 a1.convert(s4);
1311 a1.convert(s5);
1312 a1.convert(s6);
1313 a1.convert(s7);
1314 a1.convert(s8);
1315 a1.convert(s9);
1316 a1.convert(s10);
1317 a1.convert(s11);
1318 a1.convert(s12);
1319 a1.convert(s13);
1320 long s14;
1321 unsigned long s15;
1322 a1.convert(s14);
1323 a1.convert(s15);
1324 assertTrue (s14 == 32);
1325 assertTrue (s15 == 32);
1326 assertTrue (s1 == "32");
1327 assertTrue (s2 == 32);
1328 assertTrue (s3 == 32);
1329 assertTrue (s4 == 32);
1330 assertTrue (s5 == 32);
1331 assertTrue (s6 == 32);
1332 assertTrue (s7 == 32);
1333 assertTrue (s8 == 32);
1334 assertTrue (s9 == 32);
1335 assertTrue (s10 == 32.0f);
1336 assertTrue (s11 == 32.0);
1337 assertTrue (s12);
1338 assertTrue (s13 == ' ');
1339 Var a2(a1);
1340 std::string t2;
1341 a2.convert(t2);
1342 assertTrue (s1 == t2);
1343
1344 unsigned long value = a1.extract<unsigned long>();
1345 assertTrue (value == 32);
1346
1347 try
1348 {
1349 Int16 value2; value2 = a1.extract<Int16>();
1350 fail("bad cast - must throw");
1351 }
1352 catch (Poco::BadCastException&)
1353 {
1354 }
1355
1356 Var a3 = a1 + 1;
1357 assertTrue (a3 == 33);
1358 a3 = a1 - 1;
1359 assertTrue (a3 == 31);
1360 a3 += 1;
1361 assertTrue (a3 == 32);
1362 a3 -= 1;
1363 assertTrue (a3 == 31);
1364 a3 = a1 / 2;
1365 assertTrue (a3 == 16);
1366 a3 = a1 * 2;
1367 assertTrue (a3 == 64);
1368 a3 /= 2;
1369 assertTrue (a3 == 32);
1370 a3 *= 2;
1371 assertTrue (a3 == 64);
1372#endif // POCO_LONG_IS_64_BIT
1373}
1374
1375
1376void VarTest::testUDT()
1377{
1378 Dummy d0;
1379 assertTrue (d0 == 0);
1380
1381 Dummy d(1);
1382 Var da = d;
1383 assertTrue (da.extract<Dummy>() == 1);
1384
1385 Dummy d1 = d;
1386 Var da1 = d1;
1387 assertTrue (da1.extract<Dummy>() == 1);
1388
1389 try
1390 {
1391 float f = da1;
1392 fail ("must fail");
1393 }
1394 catch (BadCastException&) { }
1395}
1396
1397
1398void VarTest::testConversionOperator()
1399{
1400 Var any("42");
1401 int i = any;
1402 assertTrue (i == 42);
1403 assertTrue (any == i);
1404
1405 any = 123;
1406 std::string s1 = any.convert<std::string>();
1407 assertTrue (s1 == "123");
1408 assertTrue (s1 == any);
1409 assertTrue (any == s1);
1410 assertTrue ("123" == any);
1411
1412 any = 321;
1413 s1 = any.convert<std::string>();
1414 assertTrue (s1 == "321");
1415
1416 any = "456";
1417 assertTrue (any == "456");
1418 assertTrue ("456" == any);
1419
1420 any = 789;
1421 std::string s2 = any.convert<std::string>();
1422 assertTrue (s2 == "789");
1423 assertTrue (s2 == any);
1424 assertTrue (any == s2);
1425 assertTrue ("789" == any);
1426
1427 Var any2 = "1.5";
1428 double d = any2;
1429 assertTrue (d == 1.5);
1430 assertTrue (any2 == d);
1431}
1432
1433
1434void VarTest::testComparisonOperators()
1435{
1436 Var any1 = 1;
1437 Var any2 = "1";
1438 assertTrue (any1 == any2);
1439 assertTrue (any1 == 1);
1440 assertTrue (1 == any1);
1441 assertTrue (any1 == "1");
1442 assertTrue ("1" == any1);
1443 assertTrue (any1 <= 1);
1444 assertTrue (1 >= any1);
1445 assertTrue (any1 <= 2);
1446 assertTrue (2 >= any1);
1447 assertTrue (any1 < 2);
1448 assertTrue (2 > any1);
1449 assertTrue (any1 > 0);
1450 assertTrue (0 < any1);
1451 assertTrue (any1 >= 1);
1452 assertTrue (1 <= any1);
1453 assertTrue (any1 >= 0);
1454 assertTrue (0 <= any1);
1455
1456#if !defined(POCO_LONG_IS_64_BIT)
1457
1458 any1 = 1L;
1459 assertTrue (any1 == any2);
1460 assertTrue (any1 == 1L);
1461 assertTrue (1L == any1);
1462 assertTrue (any1 == "1");
1463 assertTrue ("1" == any1);
1464 assertTrue (any1 != 2L);
1465 assertTrue (2L != any1);
1466 assertTrue (any1 != "2");
1467 assertTrue ("2" != any1);
1468 assertTrue (any1 <= 1L);
1469 assertTrue (1L >= any1);
1470 assertTrue (any1 <= 2L);
1471 assertTrue (2L >= any1);
1472 assertTrue (any1 < 2L);
1473 assertTrue (2L > any1);
1474 assertTrue (any1 > 0);
1475 assertTrue (0 < any1);
1476 assertTrue (any1 >= 1L);
1477 assertTrue (1L <= any1);
1478 assertTrue (any1 >= 0);
1479 assertTrue (0 <= any1);
1480
1481#endif // !defined(POCO_LONG_IS_64_BIT)
1482
1483 any1 = 0x31;
1484 assertTrue (any1 == '1');
1485 assertTrue ('1' == any1);
1486 assertTrue (any1 <= '1');
1487 assertTrue ('1' >= any1);
1488 assertTrue (any1 <= '2');
1489 assertTrue ('2' >= any1);
1490 assertTrue (any1 < '2');
1491 assertTrue ('2' > any1);
1492 assertTrue (any1 > 0);
1493 assertTrue (0 < any1);
1494 assertTrue (any1 >= '1');
1495 assertTrue ('1' <= any1);
1496 assertTrue (any1 >= 0);
1497 assertTrue (0 <= any1);
1498
1499 any1 = "2";
1500 assertTrue (any1 != any2);
1501 assertTrue (any1 != 1);
1502 assertTrue (1 != any1);
1503 assertTrue (any1 != "1");
1504 assertTrue ("1" != any1);
1505
1506 any1 = 1.5;
1507 assertTrue (any1 == 1.5);
1508 assertTrue (1.5 == any1);
1509 assertTrue (any1 == "1.5");
1510 assertTrue ("1.5" == any1);
1511 assertTrue (any1 != 2.5);
1512 assertTrue (2.5 != any1);
1513 assertTrue (any1 != "2.5");
1514 assertTrue ("2.5" != any1);
1515 assertTrue (any1 <= 1.5);
1516 assertTrue (1.5 >= any1);
1517 assertTrue (any1 <= 2.5);
1518 assertTrue (2.5 >= any1);
1519 assertTrue (any1 < 2.5);
1520 assertTrue (2.5 > any1);
1521 assertTrue (any1 > 0);
1522 assertTrue (0 < any1);
1523 assertTrue (any1 >= 1.5);
1524 assertTrue (1.5 <= any1);
1525 assertTrue (any1 >= 0);
1526 assertTrue (0 <= any1);
1527
1528 any1 = 1.5f;
1529 assertTrue (any1 == 1.5f);
1530 assertTrue (1.5f == any1);
1531 assertTrue (any1 == "1.5");
1532 assertTrue ("1.5" == any1);
1533 assertTrue (any1 != 2.5f);
1534 assertTrue (2.5f != any1);
1535 assertTrue (any1 != "2.5");
1536 assertTrue ("2.5" != any1);
1537 assertTrue (any1 <= 1.5f);
1538 assertTrue (1.5f >= any1);
1539 assertTrue (any1 <= 2.5f);
1540 assertTrue (2.5f >= any1);
1541 assertTrue (any1 < 2.5f);
1542 assertTrue (2.5f > any1);
1543 assertTrue (any1 > 0);
1544 assertTrue (0 < any1);
1545 assertTrue (any1 >= 1.5f);
1546 assertTrue (1.5f <= any1);
1547 assertTrue (any1 >= 0);
1548 assertTrue (0 <= any1);
1549}
1550
1551
1552void VarTest::testArithmeticOperators()
1553{
1554 Var any1 = 1;
1555 Var any2 = 2;
1556 Var any3 = any1 + any2;
1557 assertTrue (any3 == 3);
1558 int i = 1;
1559 i += any1;
1560 assertTrue (2 == i);
1561
1562 any1 = 3;
1563 assertTrue ((5 - any1) == 2);
1564 any2 = 5;
1565 any3 = any2 - any1;
1566 assertTrue (any3 == 2);
1567 any3 -= 1;
1568 assertTrue (any3 == 1);
1569 i = 5;
1570 i -= any1;
1571 assertTrue (2 == i);
1572
1573 any1 = 3;
1574 assertTrue ((5 * any1) == 15);
1575 any2 = 5;
1576 any3 = any1 * any2;
1577 assertTrue (any3 == 15);
1578 any3 *= 3;
1579 assertTrue (any3 == 45);
1580 i = 5;
1581 i *= any1;
1582 assertTrue (15 == i);
1583
1584 any1 = 3;
1585 assertTrue ((9 / any1) == 3);
1586 any2 = 9;
1587 any3 = any2 / any1;
1588 assertTrue (any3 == 3);
1589 any3 /= 3;
1590 assertTrue (any3 == 1);
1591 i = 9;
1592 i /= any1;
1593 assertTrue (3 == i);
1594
1595 any1 = 1.0f;
1596 any2 = .5f;
1597 any3 = .0f;
1598 any3 = any1 + any2;
1599 assertTrue (any3 == 1.5f);
1600 any3 += .5f;
1601 assertTrue (any3 == 2.0f);
1602
1603 any1 = 1.0;
1604 any2 = .5;
1605 any3 = 0.0;
1606 any3 = any1 + any2;
1607 assertTrue (any3 == 1.5);
1608 any3 += .5;
1609 assertTrue (any3 == 2.0);
1610
1611 any1 = 1;
1612 any2 = "2";
1613 any3 = any1 + any2;
1614 assertTrue (any3 == 3);
1615 any2 = "4";
1616 any3 += any2;
1617 assertTrue (any3 == 7);
1618 assertTrue (1 + any3 == 8);
1619
1620 any1 = "123";
1621 any2 = "456";
1622 any3 = any1 + any2;
1623 assertTrue (any3 == "123456");
1624 any2 = "789";
1625 any3 += any2;
1626 assertTrue (any3 == "123456789");
1627 assertTrue (("xyz" + any3) == "xyz123456789");
1628
1629 try { any3 = any1 - any2; fail ("must fail"); }
1630 catch (InvalidArgumentException&){}
1631
1632 try { any3 -= any2; fail ("must fail"); }
1633 catch (InvalidArgumentException&){}
1634
1635 try { any3 = any1 * any2; fail ("must fail"); }
1636 catch (InvalidArgumentException&){}
1637
1638 try { any3 *= any2; fail ("must fail"); }
1639 catch (InvalidArgumentException&){}
1640
1641 try { any3 = any1 / any2; fail ("must fail"); }
1642 catch (InvalidArgumentException&){}
1643
1644 try { any3 /= any2; fail ("must fail"); }
1645 catch (InvalidArgumentException&){}
1646
1647 any1 = 10;
1648
1649 assertTrue (any1++ == 10);
1650 assertTrue (any1 == 11);
1651 assertTrue (++any1 == 12);
1652
1653 assertTrue (any1-- == 12);
1654 assertTrue (any1 == 11);
1655 assertTrue (--any1 == 10);
1656
1657 any1 = 1.23;
1658
1659 try { ++any1; fail ("must fail"); }
1660 catch (InvalidArgumentException&){}
1661
1662 try { any1++; fail ("must fail"); }
1663 catch (InvalidArgumentException&){}
1664
1665 try { --any1; fail ("must fail"); }
1666 catch (InvalidArgumentException&){}
1667
1668 try { any1--; fail ("must fail"); }
1669 catch (InvalidArgumentException&){}
1670}
1671
1672
1673void VarTest::testLimitsInt()
1674{
1675 testLimitsSigned<Int16, Int8>();
1676 testLimitsSigned<Int32, Int8>();
1677 testLimitsSigned<Int64, Int8>();
1678 testLimitsFloatToInt<float, Int8>();
1679 testLimitsFloatToInt<double, Int8>();
1680
1681 testLimitsSigned<Int32, Int16>();
1682 testLimitsSigned<Int64, Int16>();
1683 testLimitsFloatToInt<float, Int16>();
1684 testLimitsFloatToInt<double, Int16>();
1685
1686 testLimitsSigned<Int64, Int32>();
1687 testLimitsFloatToInt<float, Int32>();
1688 testLimitsFloatToInt<double, Int32>();
1689
1690 testLimitsSignedUnsigned<Int8, UInt8>();
1691 testLimitsSignedUnsigned<Int16, UInt8>();
1692 testLimitsSignedUnsigned<Int32, UInt8>();
1693 testLimitsSignedUnsigned<Int64, UInt8>();
1694 testLimitsFloatToInt<float, UInt8>();
1695 testLimitsFloatToInt<double, UInt8>();
1696
1697 testLimitsSignedUnsigned<Int8, UInt16>();
1698 testLimitsSignedUnsigned<Int16, UInt16>();
1699 testLimitsSignedUnsigned<Int32, UInt16>();
1700 testLimitsSignedUnsigned<Int64, UInt16>();
1701 testLimitsFloatToInt<float, UInt16>();
1702 testLimitsFloatToInt<double, UInt16>();
1703
1704 testLimitsSignedUnsigned<Int8, UInt32>();
1705 testLimitsSignedUnsigned<Int16, UInt32>();
1706 testLimitsSignedUnsigned<Int32, UInt32>();
1707 testLimitsSignedUnsigned<Int64, UInt32>();
1708 testLimitsFloatToInt<float, UInt32>();
1709 testLimitsFloatToInt<double, UInt32>();
1710
1711 testLimitsSignedUnsigned<Int8, UInt64>();
1712 testLimitsSignedUnsigned<Int16, UInt64>();
1713 testLimitsSignedUnsigned<Int32, UInt64>();
1714 testLimitsSignedUnsigned<Int64, UInt64>();
1715 testLimitsFloatToInt<float, UInt64>();
1716 testLimitsFloatToInt<double, UInt64>();
1717
1718
1719 testLimitsUnsigned<UInt16, UInt8>();
1720 testLimitsUnsigned<UInt32, UInt8>();
1721 testLimitsUnsigned<UInt64, UInt8>();
1722
1723 testLimitsUnsigned<UInt32, UInt16>();
1724 testLimitsUnsigned<UInt64, UInt16>();
1725
1726 testLimitsUnsigned<UInt64, UInt32>();
1727}
1728
1729
1730void VarTest::testLimitsFloat()
1731{
1732 if (std::numeric_limits<double>::max() != std::numeric_limits<float>::max())
1733 {
1734 double iMin = -1 * std::numeric_limits<float>::max();
1735 Var da = iMin * 10;
1736 try { float f; f = da; fail("must fail"); }
1737 catch (RangeException&) {}
1738
1739 double iMax = std::numeric_limits<float>::max();
1740 da = iMax * 10;
1741 try { float f; f = da; fail("must fail"); }
1742 catch (RangeException&) {}
1743 }
1744}
1745
1746
1747void VarTest::testCtor()
1748{
1749 // this is mainly to test a reported compiler error with assignment on HP aCC.
1750 // (SF# 1733964)
1751
1752 Var a1(42);
1753 Var a2(a1);
1754 Var a3;
1755
1756 a3 = a1;
1757
1758 assertTrue (a2 == 42);
1759 assertTrue (a3 == 42);
1760}
1761
1762
1763void VarTest::testIsStruct()
1764{
1765 std::string s1("string");
1766 Poco::Int8 s2(-23);
1767 Poco::Int16 s3(-33);
1768 Poco::Int32 s4(-388);
1769 Poco::Int64 s5(-23823838);
1770 Poco::UInt8 s6(32u);
1771 Poco::UInt16 s7(16000u);
1772 Poco::UInt32 s8(334234u);
1773 Poco::UInt64 s9(2328328382u);
1774 float s10(13.333f);
1775 double s11(13.555);
1776 bool s12(true);
1777 char s13('c');
1778 long s14(232323);
1779 unsigned long s15(21233232u);
1780 std::vector<Var> s16;
1781 Struct<std::string> s17;
1782 Struct<int> s18;
1783
1784 Var d1(s1);
1785 Var d2(s2);
1786 Var d3(s3);
1787 Var d4(s4);
1788 Var d5(s5);
1789 Var d6(s6);
1790 Var d7(s7);
1791 Var d8(s8);
1792 Var d9(s9);
1793 Var d10(s10);
1794 Var d11(s11);
1795 Var d12(s12);
1796 Var d13(s13);
1797 Var d14(s14);
1798 Var d15(s15);
1799 Var d16(s16);
1800 Var d17(s17);
1801 Var d18(s18);
1802
1803 assertTrue (!d1.isStruct());
1804 assertTrue (!d2.isStruct());
1805 assertTrue (!d3.isStruct());
1806 assertTrue (!d4.isStruct());
1807 assertTrue (!d5.isStruct());
1808 assertTrue (!d6.isStruct());
1809 assertTrue (!d7.isStruct());
1810 assertTrue (!d8.isStruct());
1811 assertTrue (!d9.isStruct());
1812 assertTrue (!d10.isStruct());
1813 assertTrue (!d11.isStruct());
1814 assertTrue (!d12.isStruct());
1815 assertTrue (!d13.isStruct());
1816 assertTrue (!d14.isStruct());
1817 assertTrue (!d15.isStruct());
1818 assertTrue (!d16.isStruct());
1819 assertTrue (d17.isStruct());
1820 assertTrue (d18.isStruct());
1821}
1822
1823
1824void VarTest::testIsArray()
1825{
1826 std::string s1("string");
1827 Poco::Int8 s2(-23);
1828 Poco::Int16 s3(-33);
1829 Poco::Int32 s4(-388);
1830 Poco::Int64 s5(-23823838);
1831 Poco::UInt8 s6(32u);
1832 Poco::UInt16 s7(16000u);
1833 Poco::UInt32 s8(334234u);
1834 Poco::UInt64 s9(2328328382u);
1835 float s10(13.333f);
1836 double s11(13.555);
1837 bool s12(true);
1838 char s13('c');
1839 Int64 s14(232323);
1840 UInt64 s15(21233232u);
1841 std::vector<Var> s16;
1842 DynamicStruct s17;
1843
1844 Var d0;
1845 Var d1(s1);
1846 Var d2(s2);
1847 Var d3(s3);
1848 Var d4(s4);
1849 Var d5(s5);
1850 Var d6(s6);
1851 Var d7(s7);
1852 Var d8(s8);
1853 Var d9(s9);
1854 Var d10(s10);
1855 Var d11(s11);
1856 Var d12(s12);
1857 Var d13(s13);
1858 Var d14(s14);
1859 Var d15(s15);
1860 Var d16(s16);
1861 Var d17(s17);
1862
1863 assertTrue (!d0.isArray());
1864 assertTrue (!d1.isArray());
1865 assertTrue (!d2.isArray());
1866 assertTrue (!d3.isArray());
1867 assertTrue (!d4.isArray());
1868 assertTrue (!d5.isArray());
1869 assertTrue (!d6.isArray());
1870 assertTrue (!d7.isArray());
1871 assertTrue (!d8.isArray());
1872 assertTrue (!d9.isArray());
1873 assertTrue (!d10.isArray());
1874 assertTrue (!d11.isArray());
1875 assertTrue (!d12.isArray());
1876 assertTrue (!d13.isArray());
1877 assertTrue (!d14.isArray());
1878 assertTrue (!d15.isArray());
1879 assertTrue (d16.isArray());
1880 assertTrue (!d17.isArray());
1881}
1882
1883
1884void VarTest::testArrayIdxOperator()
1885{
1886 std::string s1("string");
1887 Poco::Int8 s2(-23);
1888 Poco::Int16 s3(-33);
1889 Poco::Int32 s4(-388);
1890 Poco::Int64 s5(-23823838);
1891 Poco::UInt8 s6(32u);
1892 Poco::UInt16 s7(16000u);
1893 Poco::UInt32 s8(334234u);
1894 Poco::UInt64 s9(2328328382u);
1895 float s10(13.333f);
1896 double s11(13.555);
1897 bool s12(true);
1898 char s13('c');
1899 long s14(232323);
1900 unsigned long s15(21233232u);
1901 std::vector<Var> s16;
1902 s16.push_back(s1);
1903 s16.push_back(s2);
1904 DynamicStruct s17;
1905
1906 Var d1(s1);
1907 Var d2(s2);
1908 Var d3(s3);
1909 Var d4(s4);
1910 Var d5(s5);
1911 Var d6(s6);
1912 Var d7(s7);
1913 Var d8(s8);
1914 Var d9(s9);
1915 Var d10(s10);
1916 Var d11(s11);
1917 Var d12(s12);
1918 Var d13(s13);
1919 Var d14(s14);
1920 Var d15(s15);
1921 Var d16(s16);
1922 Var d17(s17);
1923
1924 testGetIdxMustThrow(d1, 0);
1925 testGetIdxNoThrow(d2, 0);
1926 testGetIdxNoThrow(d3, 0);
1927 testGetIdxNoThrow(d4, 0);
1928 testGetIdxNoThrow(d5, 0);
1929 testGetIdxNoThrow(d6, 0);
1930 testGetIdxNoThrow(d7, 0);
1931 testGetIdxNoThrow(d8, 0);
1932 testGetIdxNoThrow(d9, 0);
1933 testGetIdxNoThrow(d10, 0);
1934 testGetIdxNoThrow(d11, 0);
1935 testGetIdxNoThrow(d12, 0);
1936 testGetIdxNoThrow(d13, 0);
1937 testGetIdxNoThrow(d14, 0);
1938 testGetIdxNoThrow(d15, 0);
1939 testGetIdx(d16, 0, s1);
1940 testGetIdx(d16, 1, s2);
1941
1942 testGetIdxMustThrow(d1, 1);
1943 testGetIdxMustThrow(d2, 1);
1944 testGetIdxMustThrow(d3, 1);
1945 testGetIdxMustThrow(d4, 1);
1946 testGetIdxMustThrow(d5, 1);
1947 testGetIdxMustThrow(d6, 1);
1948 testGetIdxMustThrow(d7, 1);
1949 testGetIdxMustThrow(d8, 1);
1950 testGetIdxMustThrow(d9, 1);
1951 testGetIdxMustThrow(d10, 1);
1952 testGetIdxMustThrow(d11, 1);
1953 testGetIdxMustThrow(d12, 1);
1954 testGetIdxMustThrow(d13, 1);
1955 testGetIdxMustThrow(d14, 1);
1956 testGetIdxMustThrow(d15, 1);
1957 testGetIdxMustThrow(d17, 1);
1958}
1959
1960
1961void VarTest::testDynamicStructBasics()
1962{
1963 DynamicStruct aStruct;
1964 assertTrue (aStruct.empty());
1965 assertTrue (aStruct.size() == 0);
1966 assertTrue (aStruct.members().empty());
1967
1968 aStruct.insert("First Name", "Little");
1969 assertTrue (!aStruct.empty());
1970 assertTrue (aStruct.size() == 1);
1971 assertTrue (*(aStruct.members().begin()) == "First Name");
1972 assertTrue (aStruct["First Name"] == "Little");
1973 aStruct.insert("Last Name", "POCO");
1974 assertTrue (aStruct.members().size() == 2);
1975 aStruct.erase("First Name");
1976 assertTrue (aStruct.size() == 1);
1977 assertTrue (*(aStruct.members().begin()) == "Last Name");
1978 aStruct.clear();
1979 assertTrue (aStruct.size() == 0);
1980}
1981
1982
1983void VarTest::testOrderedDynamicStructBasics()
1984{
1985#ifdef POCO_ENABLE_CPP11
1986 OrderedDynamicStruct aStruct;
1987 assertTrue(aStruct.empty());
1988 assertTrue(aStruct.size() == 0);
1989 assertTrue(aStruct.members().empty());
1990
1991 aStruct.insert("First Name", "Little");
1992 assertTrue(!aStruct.empty());
1993 assertTrue(aStruct.size() == 1);
1994 assertTrue(*(aStruct.members().begin()) == "First Name");
1995 assertTrue(aStruct["First Name"] == "Little");
1996 aStruct.insert("Last Name", "POCO");
1997 assertTrue(aStruct.members().size() == 2);
1998 aStruct.erase("First Name");
1999 assertTrue(aStruct.size() == 1);
2000 assertTrue(*(aStruct.members().begin()) == "Last Name");
2001 aStruct.insert("Age", 1);
2002 assertTrue(aStruct["Age"] == 1);
2003 assertTrue(aStruct.members().size() == 2);
2004 assertTrue(*(aStruct.members().begin()) == "Last Name");
2005 aStruct.clear();
2006 assertTrue(aStruct.size() == 0);
2007#endif // POCO_ENABLE_CPP11
2008}
2009
2010
2011void VarTest::testDynamicStructString()
2012{
2013 DynamicStruct aStruct;
2014 aStruct["First Name"] = "Junior";
2015 aStruct["Last Name"] = "POCO";
2016 Var a1(aStruct);
2017 assertTrue (a1["First Name"] == "Junior");
2018 assertTrue (a1["Last Name"] == "POCO");
2019 a1["First Name"] = "Senior";
2020 assertTrue (a1["First Name"] == "Senior");
2021 testGetIdxMustThrow(a1, 0);
2022
2023 Struct<std::string> s1;
2024 s1["1"] = 1;
2025 s1["2"] = 2;
2026 s1["3"] = 3;
2027
2028 Struct<std::string> s2(s1);
2029 assertTrue (s2["1"] == 1);
2030 assertTrue (s2["2"] == 2);
2031 assertTrue (s2["3"] == 3);
2032
2033 std::map<std::string, int> m1;
2034 m1["1"] = 1;
2035 m1["2"] = 2;
2036 m1["3"] = 3;
2037
2038 Struct<std::string> m2(m1);
2039 assertTrue (m2["1"] == 1);
2040 assertTrue (m2["2"] == 2);
2041 assertTrue (m2["3"] == 3);
2042}
2043
2044
2045void VarTest::testOrderedDynamicStructString()
2046{
2047#ifdef POCO_ENABLE_CPP11
2048 OrderedDynamicStruct aStruct;
2049 aStruct["First Name"] = "Junior";
2050 aStruct["Last Name"] = "POCO";
2051 Var a1(aStruct);
2052 assertTrue(a1["First Name"] == "Junior");
2053 assertTrue(a1["Last Name"] == "POCO");
2054 a1["First Name"] = "Senior";
2055 assertTrue(a1["First Name"] == "Senior");
2056 testGetIdxMustThrow(a1, 0);
2057
2058 typedef Struct<std::string, OrderedMap<std::string, Var>, OrderedSet<std::string> > OrderedStruct;
2059 OrderedStruct s1;
2060 s1["1"] = 1;
2061 s1["2"] = 2;
2062 s1["3"] = 3;
2063
2064 OrderedStruct s2(s1);
2065 assertTrue(s2["1"] == 1);
2066 assertTrue(s2["2"] == 2);
2067 assertTrue(s2["3"] == 3);
2068
2069 OrderedMap<std::string, int> m1;
2070 m1["2"] = 2;
2071 m1["1"] = 1;
2072 m1["3"] = 3;
2073 assertTrue (m1.begin()->first == "2");
2074 assertTrue(m1.begin()->second == 2);
2075
2076 OrderedStruct m2(m1);
2077 assertTrue(m2["1"] == 1);
2078 assertTrue(m2["2"] == 2);
2079 assertTrue(m2["3"] == 3);
2080#endif // POCO_ENABLE_CPP11
2081}
2082
2083
2084void VarTest::testDynamicStructInt()
2085{
2086 Dynamic::Struct<int> aStruct;
2087 aStruct[0] = "Junior";
2088 aStruct[1] = "POCO";
2089 aStruct[2] = 10;
2090 Var a1(aStruct);
2091 assertTrue (a1[0]== "Junior");
2092 assertTrue (a1[1]== "POCO");
2093 assertTrue (a1[2]== 10);
2094 a1[0] = "Senior";
2095 assertTrue (a1[0] == "Senior");
2096
2097 Struct<int> s1;
2098 s1[1] = "1";
2099 s1[2] = "2";
2100 s1[3] = "3";
2101
2102 Struct<int> s2(s1);
2103 assertTrue (s2[1] == "1");
2104 assertTrue (s2[2] == "2");
2105 assertTrue (s2[3] == "3");
2106
2107 std::map<int, std::string> m1;
2108 m1[1] = "1";
2109 m1[2] = "2";
2110 m1[3] = "3";
2111
2112 Struct<int> m2(m1);
2113 assertTrue (m2[1] == "1");
2114 assertTrue (m2[2] == "2");
2115 assertTrue (m2[3] == "3");
2116}
2117
2118
2119void VarTest::testOrderedDynamicStructInt()
2120{
2121#ifdef POCO_ENABLE_CPP11
2122 typedef Struct<int, OrderedMap<int, Var>, OrderedSet<int> > OrderedStruct;
2123 OrderedStruct aStruct;
2124 aStruct[0] = "POCO";
2125 aStruct[1] = "Junior";
2126 aStruct[2] = 100;
2127 aStruct[3] = 10;
2128
2129 Var a1(aStruct);
2130 assertTrue(a1[0] == "POCO");
2131 assertTrue(a1[1] == "Junior");
2132 assertTrue(a1[2] == 100);
2133 assertTrue(a1[3] == 10);
2134 a1[0] = "Senior";
2135 assertTrue(a1[0] == "Senior");
2136
2137 OrderedStruct s1;
2138 s1[1] = "1";
2139 s1[2] = "2";
2140 s1[3] = "3";
2141
2142 OrderedStruct s2(s1);
2143 assertTrue(s2[1] == "1");
2144 assertTrue(s2[2] == "2");
2145 assertTrue(s2[3] == "3");
2146
2147 OrderedMap<int, std::string> m1;
2148 m1[1] = "2";
2149 m1[2] = "1";
2150 m1[3] = "3";
2151
2152 OrderedStruct m2(m1);
2153 assertTrue(m2[1] == "2");
2154 assertTrue(m2[2] == "1");
2155 assertTrue(m2[3] == "3");
2156#endif // POCO_ENABLE_CPP11
2157}
2158
2159
2160void VarTest::testDynamicPair()
2161{
2162 Pair<int> aPair;
2163 assertTrue (0 == aPair.first());
2164 try
2165 {
2166 std::string s = aPair.second().convert<std::string>();
2167 fail ("must fail");
2168 }
2169 catch (InvalidAccessException&) { }
2170
2171 Var va(aPair);
2172 assertTrue ("{ \"0\" : null }" == va.convert<std::string>());
2173 assertTrue (aPair.toString() == va.convert<std::string>());
2174
2175 aPair = Pair<int>(4, "123");
2176 assertTrue ("123" == aPair.second());
2177
2178 va = aPair;
2179 assertTrue ("{ \"4\" : \"123\" }" == va.convert<std::string>());
2180 assertTrue (aPair.toString() == va.convert<std::string>());
2181
2182 int i = 1;
2183 std::string s = "2";
2184 Pair<int> iPair(i, s);
2185 assertTrue (1 == iPair.first());
2186 assertTrue ("2" == iPair.second());
2187
2188 Pair<std::string> sPair(s, i);
2189 assertTrue ("2" == sPair.first());
2190 assertTrue (1 == sPair.second());
2191
2192 std::pair<int, std::string> p = std::make_pair(i, s);
2193 Pair<int> pPair(p);
2194 assertTrue (1 == pPair.first());
2195 assertTrue ("2" == pPair.second());
2196
2197 Var vp(pPair);
2198 assertTrue ("{ \"1\" : \"2\" }" == vp.convert<std::string>());
2199 assertTrue (pPair.toString() == vp.convert<std::string>());
2200
2201 Var vs(sPair);
2202 assertTrue ("{ \"2\" : 1 }" == vs.convert<std::string>());
2203 assertTrue (sPair.toString() == vs.convert<std::string>());
2204}
2205
2206
2207void VarTest::testArrayToString()
2208{
2209 std::string s1("string");
2210 Poco::Int8 s2(23);
2211 std::vector<Var> s16;
2212 s16.push_back(s1);
2213 s16.push_back(s2);
2214 Var a1(s16);
2215 std::string res = a1.convert<std::string>();
2216 std::string expected("[ \"string\", 23 ]");
2217 assertTrue (res == expected);
2218}
2219
2220
2221void VarTest::testArrayToStringEscape()
2222{
2223 std::string s1("\"quoted string\"");
2224 Poco::Int8 s2(23);
2225 std::vector<Var> s16;
2226 s16.push_back(s1);
2227 s16.push_back(s2);
2228 Var a1(s16);
2229 std::string res = a1.convert<std::string>();
2230 std::string expected("[ \"\\\"quoted string\\\"\", 23 ]");
2231 assertTrue (res == expected);
2232}
2233
2234
2235void VarTest::testStructToString()
2236{
2237 DynamicStruct aStruct;
2238 aStruct["First Name"] = "Junior";
2239 aStruct["Last Name"] = "POCO";
2240 aStruct["Age"] = 1;
2241 Var a1(aStruct);
2242 std::string res = a1.convert<std::string>();
2243 std::string expected = "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }";
2244 assertTrue (res == expected);
2245 assertTrue (aStruct.toString() == res);
2246}
2247
2248
2249void VarTest::testOrderedStructToString()
2250{
2251#ifdef POCO_ENABLE_CPP11
2252 OrderedDynamicStruct aStruct;
2253 aStruct["First Name"] = "Junior";
2254 aStruct["Last Name"] = "POCO";
2255 aStruct["Age"] = 1;
2256 Var a1(aStruct);
2257 std::string res = a1.convert<std::string>();
2258 std::string expected = "{ \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\", \"Age\" : 1 }";
2259 assertTrue(res == expected);
2260 assertTrue(aStruct.toString() == res);
2261#endif // POCO_ENABLE_CPP11
2262}
2263
2264
2265void VarTest::testStructToStringEscape()
2266{
2267 DynamicStruct aStruct;
2268 aStruct["Value"] = "Value with \" and \n";
2269 Var a1(aStruct);
2270 std::string res = a1.convert<std::string>();
2271 std::string expected = "{ \"Value\" : \"Value with \\\" and \\n\" }";
2272 assertTrue (res == expected);
2273 assertTrue (aStruct.toString() == res);
2274}
2275
2276
2277void VarTest::testArrayOfStructsToString()
2278{
2279 std::vector<Var> s16;
2280 DynamicStruct aStruct;
2281 aStruct["First Name"] = "Junior";
2282 aStruct["Last Name"] = "POCO";
2283 aStruct["Age"] = 1;
2284 s16.push_back(aStruct);
2285 aStruct["First Name"] = "Senior";
2286 aStruct["Last Name"] = "POCO";
2287 aStruct["Age"] = 100;
2288 s16.push_back(aStruct);
2289 std::vector<Var> s16Cpy = s16;
2290 // recursive arrays!
2291 s16Cpy.push_back(s16);
2292 s16.push_back(s16Cpy);
2293 Var a1(s16);
2294 std::string res = a1.convert<std::string>();
2295 std::string expected = "[ "
2296 "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
2297 "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, "
2298 "[ "
2299 "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
2300 "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" }, "
2301 "[ "
2302 "{ \"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : \"POCO\" }, "
2303 "{ \"Age\" : 100, \"First Name\" : \"Senior\", \"Last Name\" : \"POCO\" } "
2304 "] ] ]";
2305
2306 assertTrue (res == expected);
2307 assertTrue (a1.toString() == res);
2308}
2309
2310
2311void VarTest::testStructWithArraysToString()
2312{
2313 std::string s1("string");
2314 Poco::Int8 s2(23);
2315 std::vector<Var> s16;
2316 s16.push_back(s1);
2317 s16.push_back(s2);
2318 Var a1(s16);
2319 DynamicStruct addr;
2320 addr["Number"] = 4;
2321 addr["Street"] = "Unknown";
2322 addr["Country"] = "Carinthia";
2323 DynamicStruct aStruct;
2324 aStruct["First Name"] = "Junior";
2325 aStruct["Last Name"] = a1;
2326 aStruct["Age"] = 1;
2327 aStruct["Address"] = addr;
2328 Var a2(aStruct);
2329 std::string res = a2.convert<std::string>();
2330 std::string expected = "{ \"Address\" : { \"Country\" : \"Carinthia\", \"Number\" : 4, \"Street\" : \"Unknown\" }, "
2331 "\"Age\" : 1, \"First Name\" : \"Junior\", \"Last Name\" : [ \"string\", 23 ] }";
2332
2333 assertTrue (res == expected);
2334 assertTrue (aStruct.toString() == res);
2335}
2336
2337
2338void VarTest::testJSONDeserializeString()
2339{
2340 Var a("test");
2341 std::string tst = Var::toString(a);
2342 Var b = Var::parse(tst);
2343 assertTrue (b.convert<std::string>() == "test");
2344
2345 Var c('c');
2346 tst = Var::toString(c);
2347 Var b2 = Var::parse(tst);
2348 char cc = b2.convert<char>();
2349 assertTrue (cc == 'c');
2350
2351 tst = "{ \"a\" : \"1\", \"b\" : \"2\" \n}";
2352 a = Var::parse(tst);
2353 assertTrue (a.toString() == "{ \"a\" : \"1\", \"b\" : \"2\" }");
2354
2355 tst = "{ \"a\" : \"1\", \"b\" : \"2\"\n}";
2356 a = Var::parse(tst);
2357 assertTrue (a.toString() == "{ \"a\" : \"1\", \"b\" : \"2\" }");
2358}
2359
2360
2361void VarTest::testJSONDeserializePrimitives()
2362{
2363 Poco::Int8 i8(-12);
2364 Poco::UInt16 u16(2345);
2365 Poco::Int32 i32(-24343);
2366 Poco::UInt64 u64(1234567890);
2367 u64 *= u64;
2368 bool b = false;
2369 float f = 3.1415f;
2370 double d = 3.1415;
2371
2372 std::string s8 = Var::toString(i8);
2373 std::string s16 = Var::toString(u16);
2374 std::string s32 = Var::toString(i32);
2375 std::string s64 = Var::toString(u64);
2376 std::string sb = Var::toString(b);
2377 std::string sf = Var::toString(f);
2378 std::string sd = Var::toString(d);
2379 Var a8 = Var::parse(s8);
2380 Var a16 = Var::parse(s16);
2381 Var a32 = Var::parse(s32);
2382 Var a64 = Var::parse(s64);
2383 Var ab = Var::parse(sb);
2384 Var af = Var::parse(sf);
2385 Var ad = Var::parse(sd);
2386 assertTrue (a8 == i8);
2387 assertTrue (a16 == u16);
2388 assertTrue (a32 == i32);
2389 assertTrue (a64 == u64);
2390 assertTrue (ab == b);
2391 assertTrue (af == f);
2392 assertTrue (ad == d);
2393}
2394
2395
2396void VarTest::testJSONDeserializeArray()
2397{
2398 Poco::Int8 i8(-12);
2399 Poco::UInt16 u16(2345);
2400 Poco::Int32 i32(-24343);
2401 Poco::UInt64 u64(1234567890);
2402 u64 *= u64;
2403 bool b = false;
2404 float f = 3.1415f;
2405 double d = 3.1415;
2406 std::string s("test string");
2407 char c('x');
2408 std::vector<Var> aVec;
2409 aVec.push_back(i8);
2410 aVec.push_back(u16);
2411 aVec.push_back(i32);
2412 aVec.push_back(u64);
2413 aVec.push_back(b);
2414 aVec.push_back(f);
2415 aVec.push_back(d);
2416 aVec.push_back(s);
2417 aVec.push_back(c);
2418
2419 std::string sVec = Var::toString(aVec);
2420 Var a = Var::parse(sVec);
2421 assertTrue (a[0] == i8);
2422 assertTrue (a[1] == u16);
2423 assertTrue (a[2] == i32);
2424 assertTrue (a[3] == u64);
2425 assertTrue (a[4] == b);
2426 assertTrue (a[5] == f);
2427 assertTrue (a[6] == d);
2428 assertTrue (a[7] == s);
2429 assertTrue (a[8] == c);
2430}
2431
2432
2433void VarTest::testJSONDeserializeComplex()
2434{
2435 Poco::Int8 i8(-12);
2436 Poco::UInt16 u16(2345);
2437 Poco::Int32 i32(-24343);
2438 Poco::UInt64 u64(1234567890);
2439 u64 *= u64;
2440 bool b = false;
2441 float f = 3.1415f;
2442 double d = 3.1415;
2443 std::string s("test string");
2444 char c('x');
2445 DynamicStruct aStr;
2446 aStr["i8"] = i8;
2447 aStr["u16"] = u16;
2448 aStr["i32"] = i32;
2449 aStr["u64"] = u64;
2450 aStr["b"] = b;
2451 aStr["f"] = f;
2452 aStr["d"] = d;
2453 aStr["s"] = s;
2454 aStr["c"] = c;
2455 std::vector<Var> aVec;
2456 aVec.push_back(i8);
2457 aVec.push_back(u16);
2458 aVec.push_back(i32);
2459 aVec.push_back(u64);
2460 aVec.push_back(b);
2461 aVec.push_back(f);
2462 aVec.push_back(d);
2463 aVec.push_back(s);
2464 aVec.push_back(c);
2465 aVec.push_back(aStr);
2466 aStr["vec"] = aVec;
2467
2468 std::string sStr = Var::toString(aStr);
2469 Var a = Var::parse(sStr);
2470 assertTrue (a.isStruct());
2471 assertTrue (aStr["i8"] == i8);
2472 assertTrue (aStr["u16"] == u16);
2473 assertTrue (aStr["i32"] == i32);
2474 assertTrue (aStr["u64"] == u64);
2475 assertTrue (aStr["b"] == b);
2476 assertTrue (aStr["f"] == f);
2477 assertTrue (aStr["d"] == d);
2478 assertTrue (aStr["s"] == s);
2479 assertTrue (aStr["c"] == c);
2480 Var vecRet = a["vec"];
2481 assertTrue (vecRet.isArray());
2482 assertTrue (vecRet[0] == i8);
2483 assertTrue (vecRet[1] == u16);
2484 assertTrue (vecRet[2] == i32);
2485 assertTrue (vecRet[3] == u64);
2486 assertTrue (vecRet[4] == b);
2487 assertTrue (vecRet[5] == f);
2488 assertTrue (vecRet[6] == d);
2489 assertTrue (vecRet[7] == s);
2490 assertTrue (vecRet[8] == c);
2491 Var strRet = vecRet[9];
2492 assertTrue (strRet.isStruct());
2493}
2494
2495
2496void VarTest::testJSONDeserializeStruct()
2497{
2498 Poco::Int8 i8(-12);
2499 Poco::UInt16 u16(2345);
2500 Poco::Int32 i32(-24343);
2501 Poco::UInt64 u64(1234567890);
2502 u64 *= u64;
2503 bool b = false;
2504 float f = 3.1415f;
2505 double d = 3.1415;
2506 std::string s("test string");
2507 char c('x');
2508 DynamicStruct aStr;
2509 aStr["i8"] = i8;
2510 aStr["u16"] = u16;
2511 aStr["i32"] = i32;
2512 aStr["u64"] = u64;
2513 aStr["b"] = b;
2514 aStr["f"] = f;
2515 aStr["d"] = d;
2516 aStr["s"] = s;
2517 aStr["c"] = c;
2518
2519 std::string sStr = Var::toString(aStr);
2520 Var a = Var::parse(sStr);
2521 assertTrue (a["i8"] == i8);
2522 assertTrue (a["u16"] == u16);
2523 assertTrue (a["i32"] == i32);
2524 assertTrue (a["u64"] == u64);
2525 assertTrue (a["b"] == b);
2526 assertTrue (a["f"] == f);
2527 assertTrue (a["d"] == d);
2528 assertTrue (a["s"] == s);
2529 assertTrue (a["c"] == c);
2530}
2531
2532
2533void VarTest::testJSONRoundtripStruct()
2534{
2535 Poco::Int64 i64(-1234567890);
2536 Poco::UInt64 u64(1234567890);
2537 u64 *= u64;
2538 bool b = false;
2539 double d = 3.1415;
2540 std::string s("test string");
2541 DynamicStruct aStr;
2542 aStr["i64"] = i64;
2543 aStr["u64"] = u64;
2544 aStr["b"] = b;
2545 aStr["d"] = d;
2546 aStr["s"] = s;
2547
2548 std::string sStr = Var::toString(aStr);
2549 Var a = Var::parse(sStr);
2550 assertTrue (a["i64"].isInteger());
2551 assertTrue (!a["u64"].isSigned());
2552 assertTrue (a["b"].isBoolean());
2553 assertTrue (a["d"].isNumeric());
2554 assertTrue (a["s"].isString());
2555
2556 std::string serialized = Var::toString(a);
2557 assertTrue (sStr == serialized);
2558}
2559
2560
2561void VarTest::testDate()
2562{
2563 Poco::DateTime dtNow(2007, 3, 13, 8, 12, 15);
2564
2565 Poco::Timestamp tsNow = dtNow.timestamp();
2566 Poco::LocalDateTime ldtNow(dtNow.timestamp());
2567 Var dt(dtNow);
2568 assertTrue (dt.isDate());
2569 assertTrue (dt.isTime());
2570 assertTrue (dt.isDateTime());
2571 Var ts(tsNow);
2572 assertTrue (ts.isDate());
2573 assertTrue (ts.isTime());
2574 assertTrue (ts.isDateTime());
2575 Var ldt(ldtNow);
2576 assertTrue (ldt.isDate());
2577 assertTrue (ldt.isTime());
2578 assertTrue (ldt.isDateTime());
2579 Var dtStr(dt.convert<std::string>());
2580 Var tsStr(ts.convert<std::string>());
2581 Var ldtStr(ldt.convert<std::string>());
2582 DateTime dtRes = dtStr.convert<DateTime>();
2583 LocalDateTime ldtRes = ldtStr.convert<LocalDateTime>();
2584 Timestamp tsRes = tsStr.convert<Timestamp>();
2585 assertTrue (dtNow == dtRes);
2586 assertTrue (ldtNow == ldtRes);
2587 assertTrue (tsNow == tsRes);
2588}
2589
2590
2591void VarTest::testGetIdxNoThrow(Var& a1, std::vector<Var>::size_type n)
2592{
2593 Var val1 = a1[n];
2594}
2595
2596
2597void VarTest::testGetIdxMustThrow(Var& a1, std::vector<Var>::size_type n)
2598{
2599 try
2600 {
2601 Var val1 = a1[n];
2602 fail("bad cast - must throw");
2603 val1 = 0; // silence the compiler
2604 }
2605 catch (Poco::InvalidAccessException&)
2606 {
2607 }
2608 catch (Poco::RangeException&)
2609 {
2610 }
2611
2612 try
2613 {
2614 const Var& c1 = a1;
2615 const Var& cval1 = c1[n];
2616 fail("bad const cast - must throw");
2617 assertTrue (cval1 == c1); // silence the compiler
2618 }
2619 catch (Poco::InvalidAccessException&)
2620 {
2621 }
2622 catch (Poco::RangeException&)
2623 {
2624 }
2625}
2626
2627
2628void VarTest::testEmpty()
2629{
2630 Var da;
2631 assertTrue (da.isEmpty());
2632 assertTrue (da.type() == typeid(void));
2633 assertTrue (!da.isArray());
2634 assertTrue (!da.isInteger());
2635 assertTrue (!da.isNumeric());
2636 assertTrue (!da.isSigned());
2637 assertTrue (!da.isString());
2638 assertTrue (da == da);
2639 assertTrue (!(da != da));
2640
2641 assertTrue (da != Var(1));
2642 assertTrue (!(da == Var(1)));
2643 assertTrue (Var(1) != da);
2644 assertTrue (!(Var(1) == da));
2645
2646 da = "123";
2647 int i = da.convert<int>();
2648 assertTrue (123 == i);
2649 std::string s = da.extract<std::string>();
2650 assertTrue ("123" == s);
2651 assertTrue (!da.isEmpty());
2652 da.empty();
2653 assertTrue (da.isEmpty());
2654 assertTrue (da.type() == typeid(void));
2655 assertTrue (!da.isArray());
2656 assertTrue (!da.isInteger());
2657 assertTrue (!da.isNumeric());
2658 assertTrue (!da.isSigned());
2659 assertTrue (!da.isString());
2660 assertTrue (da == da);
2661 assertTrue (!(da != da));
2662
2663 assertTrue (da != "");
2664 assertTrue ("" != da);
2665 assertTrue (!(da == ""));
2666 assertTrue (!("" == da));
2667
2668 testEmptyComparisons<unsigned char>();
2669 testEmptyComparisons<char>();
2670 testEmptyComparisons<Poco::UInt8>();
2671 testEmptyComparisons<Poco::Int8>();
2672 testEmptyComparisons<Poco::UInt16>();
2673 testEmptyComparisons<Poco::Int16>();
2674 testEmptyComparisons<Poco::UInt32>();
2675 testEmptyComparisons<Poco::Int32>();
2676 testEmptyComparisons<Poco::UInt64>();
2677 testEmptyComparisons<Poco::Int64>();
2678#ifndef POCO_LONG_IS_64_BIT
2679 testEmptyComparisons<unsigned long>();
2680 testEmptyComparisons<long>();
2681#endif
2682 testEmptyComparisons<float>();
2683 testEmptyComparisons<double>();
2684
2685 try
2686 {
2687 int i = da;
2688 fail ("must fail");
2689 } catch (InvalidAccessException&) { }
2690
2691 try
2692 {
2693 int i = da.extract<int>();
2694 fail ("must fail");
2695 } catch (InvalidAccessException&) { }
2696}
2697
2698
2699void VarTest::testIterator()
2700{
2701 Var da;
2702 assertTrue (da.isEmpty());
2703 assertTrue (da.begin() == da.end());
2704
2705 da = 1;
2706 assertTrue (!da.isEmpty());
2707 assertTrue (da == 1);
2708 assertTrue (da[0] == 1);
2709 try
2710 {
2711 da[1] = 2;
2712 }
2713 catch (RangeException&) {}
2714 assertTrue (da.begin() != da.end());
2715
2716 Var::Iterator it = da.begin();
2717 Var::Iterator end = da.end();
2718 assertTrue (it != end);
2719 assertTrue (++it == end);
2720 assertTrue (--it == da.begin());
2721 it++;
2722 assertTrue (it == end);
2723 try
2724 {
2725 ++it;
2726 fail ("must fail");
2727 }
2728 catch (RangeException&) {}
2729 assertTrue (it == end);
2730
2731 da = "abc";
2732 assertTrue (da.size() == 3);
2733 assertTrue (!da.isArray());
2734 assertTrue (da.isString());
2735 //assertTrue (da[0] == 'a');
2736 assertTrue (da.at(0) == 'a');
2737 //assertTrue (da[1] = 'b');
2738 assertTrue (da.at(1) == 'b');
2739 //assertTrue (da[2] = 'c');
2740 assertTrue (da.at(2) == 'c');
2741
2742 da.at(0) = 'b';
2743 assertTrue (da.at(0) == 'b');
2744 // TODO: allow treatment of strings like arrays
2745 //da[1] = 'c';
2746 da.at(1) = 'c';
2747 assertTrue (da.at(1) == 'c');
2748 //da[2] = 'a';
2749 da.at(2) = 'a';
2750 assertTrue (da.at(2) == 'a');
2751
2752 it = da.begin();
2753 end = da.end();
2754 assertTrue (it != end);
2755 assertTrue (++it != end);
2756 assertTrue (--it == da.begin());
2757
2758 testContainerIterator<std::vector<Var> >();
2759 testContainerIterator<std::list<Var> >();
2760 testContainerIterator<std::deque<Var> >();
2761}
2762
2763
2764void VarTest::setUp()
2765{
2766}
2767
2768
2769void VarTest::tearDown()
2770{
2771}
2772
2773
2774CppUnit::Test* VarTest::suite()
2775{
2776 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("VarTest");
2777
2778 CppUnit_addTest(pSuite, VarTest, testInt8);
2779 CppUnit_addTest(pSuite, VarTest, testInt16);
2780 CppUnit_addTest(pSuite, VarTest, testInt32);
2781 CppUnit_addTest(pSuite, VarTest, testInt64);
2782 CppUnit_addTest(pSuite, VarTest, testUInt8);
2783 CppUnit_addTest(pSuite, VarTest, testUInt16);
2784 CppUnit_addTest(pSuite, VarTest, testUInt32);
2785 CppUnit_addTest(pSuite, VarTest, testUInt64);
2786 CppUnit_addTest(pSuite, VarTest, testBool);
2787 CppUnit_addTest(pSuite, VarTest, testChar);
2788 CppUnit_addTest(pSuite, VarTest, testFloat);
2789 CppUnit_addTest(pSuite, VarTest, testDouble);
2790 CppUnit_addTest(pSuite, VarTest, testLong);
2791 CppUnit_addTest(pSuite, VarTest, testULong);
2792 CppUnit_addTest(pSuite, VarTest, testString);
2793 CppUnit_addTest(pSuite, VarTest, testUDT);
2794 CppUnit_addTest(pSuite, VarTest, testConversionOperator);
2795 CppUnit_addTest(pSuite, VarTest, testComparisonOperators);
2796 CppUnit_addTest(pSuite, VarTest, testArithmeticOperators);
2797 CppUnit_addTest(pSuite, VarTest, testLimitsInt);
2798 CppUnit_addTest(pSuite, VarTest, testLimitsFloat);
2799 CppUnit_addTest(pSuite, VarTest, testCtor);
2800 CppUnit_addTest(pSuite, VarTest, testIsStruct);
2801 CppUnit_addTest(pSuite, VarTest, testIsArray);
2802 CppUnit_addTest(pSuite, VarTest, testArrayIdxOperator);
2803 CppUnit_addTest(pSuite, VarTest, testDynamicPair);
2804 CppUnit_addTest(pSuite, VarTest, testDynamicStructBasics);
2805 CppUnit_addTest(pSuite, VarTest, testOrderedDynamicStructBasics);
2806 CppUnit_addTest(pSuite, VarTest, testDynamicStructString);
2807 CppUnit_addTest(pSuite, VarTest, testOrderedDynamicStructString);
2808 CppUnit_addTest(pSuite, VarTest, testDynamicStructInt);
2809 CppUnit_addTest(pSuite, VarTest, testOrderedDynamicStructInt);
2810 CppUnit_addTest(pSuite, VarTest, testArrayToString);
2811 CppUnit_addTest(pSuite, VarTest, testArrayToStringEscape);
2812 CppUnit_addTest(pSuite, VarTest, testStructToString);
2813 CppUnit_addTest(pSuite, VarTest, testOrderedStructToString);
2814 CppUnit_addTest(pSuite, VarTest, testStructToStringEscape);
2815 CppUnit_addTest(pSuite, VarTest, testArrayOfStructsToString);
2816 CppUnit_addTest(pSuite, VarTest, testStructWithArraysToString);
2817 CppUnit_addTest(pSuite, VarTest, testJSONDeserializeString);
2818 CppUnit_addTest(pSuite, VarTest, testJSONDeserializePrimitives);
2819 CppUnit_addTest(pSuite, VarTest, testJSONDeserializeArray);
2820 CppUnit_addTest(pSuite, VarTest, testJSONDeserializeStruct);
2821 CppUnit_addTest(pSuite, VarTest, testJSONDeserializeComplex);
2822 CppUnit_addTest(pSuite, VarTest, testJSONRoundtripStruct);
2823 CppUnit_addTest(pSuite, VarTest, testDate);
2824 CppUnit_addTest(pSuite, VarTest, testEmpty);
2825 CppUnit_addTest(pSuite, VarTest, testIterator);
2826
2827 return pSuite;
2828}
2829