test_data.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #
  2. # Copyright (C) 2020 Embedded AMS B.V. - All Rights Reserved
  3. #
  4. # This file is part of Embedded Proto.
  5. #
  6. # Embedded Proto is open source software: you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as published
  8. # by the Free Software Foundation, version 3 of the license.
  9. #
  10. # Embedded Proto is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Embedded Proto. If not, see <https://www.gnu.org/licenses/>.
  17. #
  18. # For commercial and closed source application please visit:
  19. # <https://EmbeddedProto.com/license/>.
  20. #
  21. # Embedded AMS B.V.
  22. # Info:
  23. # info at EmbeddedProto dot com
  24. #
  25. # Postal adress:
  26. # Johan Huizingalaan 763a
  27. # 1066 VH, Amsterdam
  28. # the Netherlands
  29. #
  30. from sys import path
  31. path.append('./build/python/')
  32. import simple_types_pb2 as st
  33. import nested_message_pb2 as nm
  34. import repeated_fields_pb2 as rf
  35. import oneof_fields_pb2 as of
  36. import file_to_include_pb2 as fti
  37. import include_other_files_pb2 as iof
  38. def test_simple_types():
  39. # A test function used to generate encoded data to test the implementation of the wireformatter
  40. # and header template.
  41. msg = st.Test_Simple_Types()
  42. # msg.a_int32 = -2147483648
  43. # msg.a_int64 = -9223372036854775808
  44. # msg.a_uint32 = 0
  45. # msg.a_uint64 = 0
  46. # msg.a_sint32 = -2147483648
  47. # msg.a_sint64 = -9223372036854775808
  48. # msg.a_bool = 0
  49. # msg.a_enum = 0
  50. # msg.a_fixed64 = 0
  51. # msg.a_sfixed64 = -9223372036854775808
  52. # msg.a_double = 0
  53. # msg.a_fixed32 = 0
  54. # msg.a_sfixed32 = -2147483648
  55. # msg.a_float = 0
  56. # msg.a_int32 = 1
  57. # msg.a_int64 = 1
  58. # msg.a_uint32 = 1
  59. # msg.a_uint64 = 1
  60. # msg.a_sint32 = 1
  61. # msg.a_sint64 = 1
  62. # msg.a_bool = 1
  63. # msg.a_enum = 1
  64. # msg.a_fixed64 = 1
  65. # msg.a_sfixed64 = 1
  66. # msg.a_double = 1
  67. # msg.a_fixed32 = 1
  68. # msg.a_sfixed32 = 1
  69. # msg.a_float = 1
  70. msg.a_double = pow(2, -1022)
  71. msg.a_float = pow(2, -126)
  72. str = ""
  73. msg_str = msg.SerializeToString()
  74. print(len(msg_str))
  75. print(msg_str)
  76. for x in msg_str:
  77. str += "0x{:02x}, ".format(x)
  78. print(str)
  79. print()
  80. x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  81. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  82. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
  83. msg2 = st.Test_Simple_Types()
  84. msg2.ParseFromString(x)
  85. print(msg2)
  86. def test_nested_message():
  87. msg = nm.message_b()
  88. # msg.u = 1.0
  89. # msg.v = 1.0
  90. # msg.nested_a.x = 1
  91. # msg.nested_a.y = 1.0
  92. # msg.nested_a.z = 1
  93. msg.u = 0 #pow(2, 1023)
  94. msg.v = 0 #pow(2, 1023)
  95. #msg.nested_a.x = 0#pow(2, 31) - 1
  96. #msg.nested_a.y = 0 #1.0
  97. #msg.nested_a.z = 0 #1
  98. str = ""
  99. msg_str = msg.SerializeToString()
  100. print(len(msg_str))
  101. print(msg_str)
  102. for x in msg_str:
  103. str += "0x{:02x}, ".format(x)
  104. print(str)
  105. print()
  106. x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  107. 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
  108. msg2 = nm.message_b()
  109. msg2.ParseFromString(x)
  110. print(msg2)
  111. def test_repeated_fields():
  112. msg = rf.repeated_fields()
  113. #msg.x = 0
  114. msg.y.append(0)
  115. msg.y.append(1)
  116. msg.y.append(0)
  117. #msg.z = 0
  118. #msg.y.append(pow(2, 32) - 1)
  119. #msg.y.append(pow(2, 32) - 1)
  120. #msg.y.append(pow(2, 32) - 1)
  121. #msg.x = 1
  122. #msg.y.append(1)
  123. #msg.y.append(1)
  124. #msg.y.append(1)
  125. #msg.z = 1
  126. #msg.x = pow(2, 32) - 1
  127. #msg.y.append(pow(2, 32) - 1)
  128. #msg.y.append(pow(2, 32) - 1)
  129. #msg.y.append(pow(2, 32) - 1)
  130. #msg.z = pow(2, 32) - 1
  131. str = ""
  132. msg_str = msg.SerializeToString()
  133. print(len(msg_str))
  134. print(msg_str)
  135. for x in msg_str:
  136. str += "0x{:02x}, ".format(x)
  137. print(str)
  138. print()
  139. def test_repeated_message():
  140. msg = rf.repeated_message()
  141. msg.a = 0
  142. for i in range(3):
  143. nmsg = msg.b.add()
  144. nmsg.u = 0
  145. nmsg.v = 0
  146. msg.c = 0
  147. msg.b[1].u = 1
  148. msg.b[1].v = 1
  149. str = ""
  150. msg_str = msg.SerializeToString()
  151. print(len(msg_str))
  152. print(msg_str)
  153. for x in msg_str:
  154. str += "0x{:02x}, ".format(x)
  155. print(str)
  156. print()
  157. def test_oneof_fields():
  158. msg = of.message_oneof()
  159. msg.msg_DEF.varD = 1
  160. msg.msg_DEF.varE = 22
  161. msg.msg_DEF.varF = 333
  162. str = ""
  163. msg_str = msg.SerializeToString()
  164. print(len(msg_str))
  165. print(msg_str)
  166. for x in msg_str:
  167. str += "0x{:02x}, ".format(x)
  168. print(str)
  169. print()
  170. def test_included_proto():
  171. msg = iof.IncludedMessages()
  172. msg.state = fti.StateA
  173. msg.msg.a = 1
  174. msg.msg.b = 1.0
  175. msg.rf.x = 1
  176. msg.rf.y.append(1)
  177. msg.rf.y.append(1)
  178. msg.rf.y.append(1)
  179. msg.rf.z = 1
  180. str = ""
  181. msg_str = msg.SerializeToString()
  182. print(len(msg_str))
  183. print(msg_str)
  184. for x in msg_str:
  185. str += "0x{:02x}, ".format(x)
  186. print(str)
  187. print()
  188. #test_repeated_fields()
  189. #test_repeated_message()
  190. #test_nested_message()
  191. test_oneof_fields()
  192. #test_included_proto()