-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathTest.cpp
177 lines (151 loc) · 4.98 KB
/
Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* COPYRIGHT NOTICE
* Copyright (C) 2015, Jhuster, All Rights Reserved
* Author: Jhuster([email protected])
*
* https://github.com/Jhuster/TLV
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*/
#include <iostream>
#include <string>
#include "TlvBox.h"
using namespace tlv;
#define TEST_TYPE_0 0x00
#define TEST_TYPE_1 0x01
#define TEST_TYPE_2 0x02
#define TEST_TYPE_3 0x03
#define TEST_TYPE_4 0x04
#define TEST_TYPE_5 0x05
#define TEST_TYPE_6 0x06
#define TEST_TYPE_7 0x07
#define TEST_TYPE_8 0x08
#define TEST_TYPE_9 0x09
#define TEST_TYPE_a 0x0a
int main(int argc, char const *argv[])
{
TlvBox box;
box.PutBoolValue(TEST_TYPE_0,true);
box.PutCharValue(TEST_TYPE_1, 'x');
box.PutShortValue(TEST_TYPE_2,(short)2);
box.PutIntValue(TEST_TYPE_3,(int)3);
box.PutLongValue(TEST_TYPE_4,(long)4);
box.PutFloatValue(TEST_TYPE_5,(float)5.67);
box.PutDoubleValue(TEST_TYPE_6,(double)8.91);
box.PutStringValue(TEST_TYPE_7, (char *)"hello world !");
std::string name = "name";
box.PutStringValue(TEST_TYPE_8, name);
unsigned char array[6] = {1,2,3,4,5,6};
box.PutBytesValue(TEST_TYPE_9,array,6);
if(!box.Serialize()) {
std::cout << "box Serialize Failed !\n";
return -1;
}
std::cout << "box Serialize Success, " << box.GetSerializedBytes() << " bytes \n";
TlvBox boxes;
boxes.PutObjectValue(TEST_TYPE_a, box);
if(!boxes.Serialize()) {
std::cout << "boxes Serialize Failed !\n";
return -1;
}
std::cout << "boxes Serialize Success, " << boxes.GetSerializedBytes() << " bytes \n";
TlvBox parsedBoxes;
if(!parsedBoxes.Parse(boxes.GetSerializedBuffer(),boxes.GetSerializedBytes())) {
std::cout << "boxes Parse Failed !\n";
return -1;
}
std::cout << "boxes Parse Success, " << parsedBoxes.GetSerializedBytes() << " bytes \n";
TlvBox parsedBox;
if(!parsedBoxes.GetObjectValue(TEST_TYPE_a,parsedBox)) {
std::cout << "GetObjectValue Failed !\n";
return -1;
}
std::cout << "box Parse Success, " << parsedBox.GetSerializedBytes() << " bytes \n";
{
bool value;
if(!parsedBox.GetBoolValue(TEST_TYPE_0,value)) {
std::cout << "GetBoolValue Failed !\n";
return -1;
}
std::cout << "GetBoolValue Success " << value << std::endl;
}
{
char value;
if(!parsedBox.GetCharValue(TEST_TYPE_1,value)) {
std::cout << "GetCharValue Failed !\n";
return -1;
}
std::cout << "GetCharValue Success " << value << std::endl;
}
{
short value;
if(!parsedBox.GetShortValue(TEST_TYPE_2,value)) {
std::cout << "GetShortValue Failed !\n";
return -1;
}
std::cout << "GetShortValue Success " << value << std::endl;
}
{
int value;
if(!parsedBox.GetIntValue(TEST_TYPE_3,value)) {
std::cout << "GetIntValue Failed !\n";
return -1;
}
std::cout << "GetIntValue Success " << value << std::endl;
}
{
long value;
if(!parsedBox.GetLongValue(TEST_TYPE_4,value)) {
std::cout << "GetLongValue Failed !\n";
return -1;
}
std::cout << "GetLongValue Success " << value << std::endl;
}
{
float value;
if(!parsedBox.GetFloatValue(TEST_TYPE_5,value)) {
std::cout << "PutFloatValue Failed !\n";
return -1;
}
std::cout << "PutFloatValue Success " << value << std::endl;
}
{
double value;
if(!parsedBox.GetDoubleValue(TEST_TYPE_6,value)) {
std::cout << "GetDoubleValue Failed !\n";
return -1;
}
std::cout << "GetDoubleValue Success " << value << std::endl;
}
{
char value[128]; int length = 128;
if(!parsedBox.GetStringValue(TEST_TYPE_7,value,length)) {
std::cout << "GetStringValue Failed !\n";
return -1;
}
std::cout << "GetStringValue Success " << value << std::endl;
}
{
std::string value;
if(!parsedBox.GetStringValue(TEST_TYPE_8,value)) {
std::cout << "GetStringValue Failed !\n";
return -1;
}
std::cout << "GetStringValue Success " << value << std::endl;
}
{
unsigned char value[128]; int length = 128;
if(!parsedBox.GetBytesValue(TEST_TYPE_9,value,length)) {
std::cout << "GetBytesValue Failed !\n";
return -1;
}
std::cout << "GetBytesValue Success ";
for(int i=0; i<length; i++) {
std::cout << (int)value[i] << "--";
}
std::cout << std::endl;
}
return 0;
}