test_data.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. from sys import path
  2. path.append('./build/python/')
  3. import simple_types_pb2 as st
  4. import nested_message_pb2 as nm
  5. import repeated_fields_pb2 as rf
  6. import oneof_fields_pb2 as of
  7. import file_to_include_pb2 as fti
  8. import include_other_files_pb2 as iof
  9. def test_simple_types():
  10. # A test function used to generate encoded data to test the implementation of the wireformatter
  11. # and header template.
  12. msg = st.Test_Simple_Types()
  13. # msg.a_int32 = -2147483648
  14. # msg.a_int64 = -9223372036854775808
  15. # msg.a_uint32 = 0
  16. # msg.a_uint64 = 0
  17. # msg.a_sint32 = -2147483648
  18. # msg.a_sint64 = -9223372036854775808
  19. # msg.a_bool = 0
  20. # msg.a_enum = 0
  21. # msg.a_fixed64 = 0
  22. # msg.a_sfixed64 = -9223372036854775808
  23. # msg.a_double = 0
  24. # msg.a_fixed32 = 0
  25. # msg.a_sfixed32 = -2147483648
  26. # msg.a_float = 0
  27. # msg.a_int32 = 1
  28. # msg.a_int64 = 1
  29. # msg.a_uint32 = 1
  30. # msg.a_uint64 = 1
  31. # msg.a_sint32 = 1
  32. # msg.a_sint64 = 1
  33. # msg.a_bool = 1
  34. # msg.a_enum = 1
  35. # msg.a_fixed64 = 1
  36. # msg.a_sfixed64 = 1
  37. # msg.a_double = 1
  38. # msg.a_fixed32 = 1
  39. # msg.a_sfixed32 = 1
  40. # msg.a_float = 1
  41. msg.a_double = pow(2, -1022)
  42. msg.a_float = pow(2, -126)
  43. str = ""
  44. msg_str = msg.SerializeToString()
  45. print(len(msg_str))
  46. print(msg_str)
  47. for x in msg_str:
  48. str += "0x{:02x}, ".format(x)
  49. print(str)
  50. print()
  51. x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  52. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  53. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
  54. msg2 = st.Test_Simple_Types()
  55. msg2.ParseFromString(x)
  56. print(msg2)
  57. def test_nested_message():
  58. msg = nm.message_b()
  59. # msg.u = 1.0
  60. # msg.v = 1.0
  61. # msg.nested_a.x = 1
  62. # msg.nested_a.y = 1.0
  63. # msg.nested_a.z = 1
  64. msg.u = 0 #pow(2, 1023)
  65. msg.v = 0 #pow(2, 1023)
  66. #msg.nested_a.x = 0#pow(2, 31) - 1
  67. #msg.nested_a.y = 0 #1.0
  68. #msg.nested_a.z = 0 #1
  69. str = ""
  70. msg_str = msg.SerializeToString()
  71. print(len(msg_str))
  72. print(msg_str)
  73. for x in msg_str:
  74. str += "0x{:02x}, ".format(x)
  75. print(str)
  76. print()
  77. x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  78. 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
  79. msg2 = nm.message_b()
  80. msg2.ParseFromString(x)
  81. print(msg2)
  82. def test_repeated_fields():
  83. msg = rf.repeated_fields()
  84. #msg.x = 0
  85. msg.y.append(0)
  86. msg.y.append(1)
  87. msg.y.append(0)
  88. #msg.z = 0
  89. #msg.y.append(pow(2, 32) - 1)
  90. #msg.y.append(pow(2, 32) - 1)
  91. #msg.y.append(pow(2, 32) - 1)
  92. #msg.x = 1
  93. #msg.y.append(1)
  94. #msg.y.append(1)
  95. #msg.y.append(1)
  96. #msg.z = 1
  97. #msg.x = pow(2, 32) - 1
  98. #msg.y.append(pow(2, 32) - 1)
  99. #msg.y.append(pow(2, 32) - 1)
  100. #msg.y.append(pow(2, 32) - 1)
  101. #msg.z = pow(2, 32) - 1
  102. str = ""
  103. msg_str = msg.SerializeToString()
  104. print(len(msg_str))
  105. print(msg_str)
  106. for x in msg_str:
  107. str += "0x{:02x}, ".format(x)
  108. print(str)
  109. print()
  110. def test_repeated_message():
  111. msg = rf.repeated_message()
  112. msg.a = 0
  113. for i in range(3):
  114. nmsg = msg.b.add()
  115. nmsg.u = 0
  116. nmsg.v = 0
  117. msg.c = 0
  118. msg.b[1].u = 1
  119. msg.b[1].v = 1
  120. str = ""
  121. msg_str = msg.SerializeToString()
  122. print(len(msg_str))
  123. print(msg_str)
  124. for x in msg_str:
  125. str += "0x{:02x}, ".format(x)
  126. print(str)
  127. print()
  128. def test_oneof_fields():
  129. msg = of.message_oneof()
  130. msg.a = 1
  131. msg.b = 1
  132. msg.x = 1
  133. msg.v = 1
  134. str = ""
  135. msg_str = msg.SerializeToString()
  136. print(len(msg_str))
  137. print(msg_str)
  138. for x in msg_str:
  139. str += "0x{:02x}, ".format(x)
  140. print(str)
  141. print()
  142. def test_included_proto():
  143. msg = iof.IncludedMessages()
  144. msg.state = fti.StateA
  145. msg.msg.a = 1
  146. msg.msg.b = 1.0
  147. msg.rf.x = 1
  148. msg.rf.y.append(1)
  149. msg.rf.y.append(1)
  150. msg.rf.y.append(1)
  151. msg.rf.z = 1
  152. str = ""
  153. msg_str = msg.SerializeToString()
  154. print(len(msg_str))
  155. print(msg_str)
  156. for x in msg_str:
  157. str += "0x{:02x}, ".format(x)
  158. print(str)
  159. print()
  160. #test_repeated_fields()
  161. #test_repeated_message()
  162. #test_nested_message()
  163. #test_oneof_fields()
  164. test_included_proto()