| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- 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
- 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_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_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 = nm.message_b()
- # msg.u = 1.0
- # msg.v = 1.0
- # msg.nested_a.x = 1
- # msg.nested_a.y = 1.0
- # msg.nested_a.z = 1
- msg.u = 0 #pow(2, 1023)
- msg.v = 0 #pow(2, 1023)
- #msg.nested_a.x = 0#pow(2, 31) - 1
- #msg.nested_a.y = 0 #1.0
- #msg.nested_a.z = 0 #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()
- x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
- 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
- msg2 = nm.message_b()
- 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()
- def test_oneof_fields():
- msg = of.message_oneof()
- msg.a = 1
- msg.b = 1
- msg.x = 1
- msg.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()
- 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
- 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()
- #test_repeated_fields()
- #test_repeated_message()
- #test_nested_message()
- #test_oneof_fields()
- test_included_proto()
|