source: source/ariba/utility/serialization/TestSerialization.cpp@ 6938

Last change on this file since 6938 was 6922, checked in by mies, 15 years ago

replaced deprecated hash_map, hash_set classes with boost
added unsigned serialization
fixed more warnings
included -Wall to Makefile.am

File size: 3.6 KB
Line 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#include "TestSerialization.h"
40#include "Data.hpp"
41#include "DataStream.hpp"
42#include "Serialization.hpp"
43
44using_serialization;
45
46class Aclass: public VSerializeable { VSERIALIZEABLE;
47public:
48 uint32_t x;
49 uint8_t y;
50 int8_t z;
51 bool b;
52 char* text;
53 std::string str;
54 std::vector<uint8_t> v;
55 Aclass* vclass;
56 bool vcls;
57 uint8_t* static_array;
58
59 Aclass(bool vcls = false) {
60 text = new char[80];
61 static_array = new uint8_t[20];
62 for (size_t i=0; i<20; i++) static_array[i] = i;
63 strcpy(text, "Hallo!");
64 str = "std::string:)";
65 x = 0x01020304;
66 y = 0xF;
67 z = -2;
68 b = 0;
69 this->vcls = vcls;
70 v.push_back(0xA0);
71 v.push_back(0xB0);
72 v.push_back(0xC0);
73 v.push_back(0xD0);
74 if (vcls) vclass = new Aclass();
75 else vclass = NULL;
76 }
77
78 void clean() {
79 text = NULL;
80 str = "";
81 x = 0;
82 y = 0;
83 b = 1;
84 z = 0;
85 v.clear();
86 vclass = NULL;
87 }
88
89 void view(bool ret = true) {
90 printf("obj=[%08X, %1X, %d, %d, '%s', '%s' ", x, y, z, b, text, str.c_str());
91 for (size_t i = 0; i < v.size(); i++)
92 printf("%02X ", v[i]);
93 if (vclass != NULL) vclass->view(false);
94 printf("]");
95 if (ret) printf("\n");
96 }
97};
98
99sznBeginDefault( Aclass, X ){
100 double xpos;
101 X && x && b && I(y,6) && T(text) && T(str) && A(v,4) && I(z) && vcls;
102 X && static_A(static_array,20);
103 X && static_A( (uint8_t*)&xpos, 8 );
104 if (vcls) X && VO(vclass);
105}sznEnd()
106
107vsznDefault( Aclass );
108
109int test_serialization() {
110 using namespace std;
111
112 Aclass a(true);
113 Data data = data_serialize(a);
114 a.view();
115 cout << "length=" << data_length(a) / 8 << endl;
116 cout << "data=" << data << endl;
117
118 Aclass b(true);
119 b.clean();
120 b.view();
121 data_deserialize(b, data);
122 b.view();
123
124 VSerializeable *c = &b;
125 cout << "length=" << data_length(c) / 8 << endl;
126 cout << "data=" << (data = data_serialize(c)) << endl;
127 Aclass d;
128 d.clean();
129 data_deserialize(&d, data);
130 d.view();
131 cout << "--- test successful." << endl;
132
133 return 0;
134}
135
136
Note: See TracBrowser for help on using the repository browser.