-
Notifications
You must be signed in to change notification settings - Fork 39
/
test_data.py
373 lines (298 loc) · 8.85 KB
/
test_data.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#
# Copyright (C) 2020-2024 Embedded AMS B.V. - All Rights Reserved
#
# This file is part of Embedded Proto.
#
# Embedded Proto is open source 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 3 of the license.
#
# Embedded Proto is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
#
# For commercial and closed source application please visit:
# <https://EmbeddedProto.com/license/>.
#
# Embedded AMS B.V.
# Info:
# info at EmbeddedProto dot com
#
# Postal address:
# Atoomweg 2
# 1627 LE, Hoorn
# the Netherlands
#
from sys import path
path.append('./build/python/')
import simple_types_pb2 as st
import nested_message_pb2 as nm
import repeated_fields_pb2 as rf
import oneof_fields_pb2 as of
import file_to_include_pb2 as fti
import include_other_files_pb2 as iof
from subfolder import file_to_include_from_subfolder_pb2 as ftis
import string_bytes_pb2 as sb
import optional_fields_pb2 as op
def test_simple_types():
# A test function used to generate encoded data to test the implementation of the wireformatter
# and header template.
msg = st.Test_Simple_Types()
# msg.a_int32 = -2147483648
# msg.a_int64 = -9223372036854775808
# msg.a_uint32 = 0
# msg.a_uint64 = 0
# msg.a_sint32 = -2147483648
# msg.a_sint64 = -9223372036854775808
# msg.a_bool = 0
# msg.a_enum = 0
# msg.a_fixed64 = 0
# msg.a_sfixed64 = -9223372036854775808
# msg.a_double = 0
# msg.a_fixed32 = 0
# msg.a_sfixed32 = -2147483648
# msg.a_float = 0
# msg.a_nested_enum = st.Test_Simple_Types.NE_A
# msg.a_int32 = 1
# msg.a_int64 = 1
# msg.a_uint32 = 1
# msg.a_uint64 = 1
# msg.a_sint32 = 1
# msg.a_sint64 = 1
# msg.a_bool = 1
# msg.a_enum = 1
# msg.a_fixed64 = 1
# msg.a_sfixed64 = 1
# msg.a_double = 1
# msg.a_fixed32 = 1
# msg.a_sfixed32 = 1
# msg.a_float = 1
msg.a_nested_enum = st.Test_Simple_Types.NE_B
#msg.a_double = pow(2, -1022)
#msg.a_float = pow(2, -126)
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
msg2 = st.Test_Simple_Types()
msg2.ParseFromString(x)
print(msg2)
def test_nested_message():
#msg_b = nm.message_b()
#msg_b.u = 1.0
#msg_b.v = 1
#msg_b.nested_a.x.append(1)
#msg_b.nested_a.y = 1.0
#msg_b.nested_a.z = 1
#msg_b.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
#msg_b.v = pow(2, 31) - 1 # Max int32
#msg_b.nested_a.x.append(pow(2, 31) - 1) # Max int32
#msg_b.nested_a.y = 3.40282347e+38 # Max float
#msg_b.nested_a.z = 9223372036854775807 # Max sint64
msg_c = nm.message_c()
msg_c.nested_b.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
msg_c.nested_b.v = pow(2, 31) - 1 # Max int32
msg_c.nested_b.nested_a.x.append(pow(2, 31) - 1) # Max int32
msg_c.nested_b.nested_a.y = 3.40282347e+38 # Max float
msg_c.nested_b.nested_a.z = 9223372036854775807 # Max sint64
msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
msg_c.nested_d.d.append(pow(2, 32) - 1) # Max uint32
msg_c.nested_g.g = pow(2, 31) - 1 # Max int32
msg = msg_c
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
# Setup for message c
x = bytes([0x0A, 0x28, # Tag and Size on nested b
0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0x7F, # u
0x12, 0x17, # Tag and size of nested a
0x0A, 0x05, # Tag and size of x
0xFF, 0xFF, 0xFF, 0xFF, 0x07, # x
0x15, 0xFF, 0xFF, 0x7F, 0x7F, # y
0x18, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, # z
0x18, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, # v
0x12, 0x0C, # Tag and size of nested d
0x0A, 0x0A, # Length of d
0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, # Value(s) of d
0x1A, 0x06, # Tag and size of nested g
0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, # Value g
])
msg2 = nm.message_c()
msg2.ParseFromString(x)
print(msg2)
def test_repeated_fields():
msg = rf.repeated_fields()
#msg.x = 0
msg.y.append(0)
msg.y.append(1)
msg.y.append(0)
#msg.z = 0
#msg.y.append(pow(2, 32) - 1)
#msg.y.append(pow(2, 32) - 1)
#msg.y.append(pow(2, 32) - 1)
#msg.x = 1
#msg.y.append(1)
#msg.y.append(1)
#msg.y.append(1)
#msg.z = 1
#msg.x = pow(2, 32) - 1
#msg.y.append(pow(2, 32) - 1)
#msg.y.append(pow(2, 32) - 1)
#msg.y.append(pow(2, 32) - 1)
#msg.z = pow(2, 32) - 1
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_repeated_message():
msg = rf.repeated_message()
msg.a = 0
for i in range(3):
nmsg = msg.b.add()
nmsg.u = 0
nmsg.v = 0
msg.c = 0
msg.b[1].u = 1
msg.b[1].v = 1
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
msg_enum = rf.repeated_enum()
msg_enum.enum_values.append(rf.SomeEnum.SE_A)
msg_enum.enum_values.append(rf.SomeEnum.SE_B)
msg_enum.enum_values.append(rf.SomeEnum.SE_C)
str = ""
msg_str = msg_enum.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_string():
msg = sb.text()
msg.txt = "Foo bar"
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_bytes():
msg = sb.raw_bytes()
msg.b = b'\x01\x02\x03\x00'
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_repeated_string_bytes():
msg = sb.repeated_string_bytes()
msg.array_of_txt.append("Foo bar 1")
msg.array_of_txt.append("")
msg.array_of_txt.append("Foo bar 3")
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_oneof_fields():
# msg = of.message_oneof()
# msg.msg_DEF.varD = 1
# msg.msg_DEF.varE = 22
# msg.msg_DEF.varF = 333
msg = of.string_bytes_oneof()
#msg.name = ""
msg.data = bytes()
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_included_proto():
msg = iof.IncludedMessages()
msg.state = fti.StateA
msg.msg.a = 1
msg.msg.b = 1.0
msg.rf.x = 1
msg.rf.y.append(1)
msg.rf.y.append(1)
msg.rf.y.append(1)
msg.rf.z = 1
msg.sub_msg.val = 1
msg.sub_msg.array.append(1)
msg.sub_msg.array.append(1)
msg.sub_msg.ne = ftis.other_folder_msg.NE_B
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
def test_optional_empty():
msg = op.optional_fields()
msg.b = 0
msg.y = 0.0
#msg.pos.xpos = 0.0
msg.state = op.states.A
#msg.bytes_array = bytes()
#msg.str = ""
str = ""
msg_str = msg.SerializeToString()
print(len(msg_str))
print(msg_str)
for x in msg_str:
str += "0x{:02x}, ".format(x)
print(str)
print()
msg2 = op.optional_fields()
msg2.ParseFromString(bytes(msg_str))
print(msg2)
#test_simple_types()
#test_repeated_fields()
#test_repeated_message()
#test_string()
#test_bytes()
#test_repeated_string_bytes()
#test_nested_message()
#test_oneof_fields()
#test_included_proto()
test_optional_empty()