test_data.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 address:
  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. import string_bytes_pb2 as sb
  39. def test_simple_types():
  40. # A test function used to generate encoded data to test the implementation of the wireformatter
  41. # and header template.
  42. msg = st.Test_Simple_Types()
  43. # msg.a_int32 = -2147483648
  44. # msg.a_int64 = -9223372036854775808
  45. # msg.a_uint32 = 0
  46. # msg.a_uint64 = 0
  47. # msg.a_sint32 = -2147483648
  48. # msg.a_sint64 = -9223372036854775808
  49. # msg.a_bool = 0
  50. # msg.a_enum = 0
  51. # msg.a_fixed64 = 0
  52. # msg.a_sfixed64 = -9223372036854775808
  53. # msg.a_double = 0
  54. # msg.a_fixed32 = 0
  55. # msg.a_sfixed32 = -2147483648
  56. # msg.a_float = 0
  57. # msg.a_int32 = 1
  58. # msg.a_int64 = 1
  59. # msg.a_uint32 = 1
  60. # msg.a_uint64 = 1
  61. # msg.a_sint32 = 1
  62. # msg.a_sint64 = 1
  63. # msg.a_bool = 1
  64. # msg.a_enum = 1
  65. # msg.a_fixed64 = 1
  66. # msg.a_sfixed64 = 1
  67. # msg.a_double = 1
  68. # msg.a_fixed32 = 1
  69. # msg.a_sfixed32 = 1
  70. # msg.a_float = 1
  71. msg.a_double = pow(2, -1022)
  72. msg.a_float = pow(2, -126)
  73. str = ""
  74. msg_str = msg.SerializeToString()
  75. print(len(msg_str))
  76. print(msg_str)
  77. for x in msg_str:
  78. str += "0x{:02x}, ".format(x)
  79. print(str)
  80. print()
  81. x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  82. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  83. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
  84. msg2 = st.Test_Simple_Types()
  85. msg2.ParseFromString(x)
  86. print(msg2)
  87. def test_nested_message():
  88. msg = nm.message_b()
  89. #msg.u = 1.0
  90. #msg.v = 1
  91. #msg.nested_a.x.append(1)
  92. #msg.nested_a.y = 1.0
  93. #msg.nested_a.z = 1
  94. msg.u = 1.7976931348623157e+308 # Max double 1.7976931348623157e+308
  95. msg.v = pow(2, 31) - 1 # Max int32
  96. msg.nested_a.x.append(pow(2, 31) - 1) # Max int32
  97. msg.nested_a.y = 3.40282347e+38 # Max float
  98. msg.nested_a.z = 9223372036854775807 # Max sint64
  99. str = ""
  100. msg_str = msg.SerializeToString()
  101. print(len(msg_str))
  102. print(msg_str)
  103. for x in msg_str:
  104. str += "0x{:02x}, ".format(x)
  105. print(str)
  106. print()
  107. x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  108. 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
  109. msg2 = nm.message_b()
  110. msg2.ParseFromString(x)
  111. print(msg2)
  112. def test_repeated_fields():
  113. msg = rf.repeated_fields()
  114. #msg.x = 0
  115. msg.y.append(0)
  116. msg.y.append(1)
  117. msg.y.append(0)
  118. #msg.z = 0
  119. #msg.y.append(pow(2, 32) - 1)
  120. #msg.y.append(pow(2, 32) - 1)
  121. #msg.y.append(pow(2, 32) - 1)
  122. #msg.x = 1
  123. #msg.y.append(1)
  124. #msg.y.append(1)
  125. #msg.y.append(1)
  126. #msg.z = 1
  127. #msg.x = pow(2, 32) - 1
  128. #msg.y.append(pow(2, 32) - 1)
  129. #msg.y.append(pow(2, 32) - 1)
  130. #msg.y.append(pow(2, 32) - 1)
  131. #msg.z = pow(2, 32) - 1
  132. str = ""
  133. msg_str = msg.SerializeToString()
  134. print(len(msg_str))
  135. print(msg_str)
  136. for x in msg_str:
  137. str += "0x{:02x}, ".format(x)
  138. print(str)
  139. print()
  140. def test_repeated_message():
  141. msg = rf.repeated_message()
  142. msg.a = 0
  143. for i in range(3):
  144. nmsg = msg.b.add()
  145. nmsg.u = 0
  146. nmsg.v = 0
  147. msg.c = 0
  148. msg.b[1].u = 1
  149. msg.b[1].v = 1
  150. str = ""
  151. msg_str = msg.SerializeToString()
  152. print(len(msg_str))
  153. print(msg_str)
  154. for x in msg_str:
  155. str += "0x{:02x}, ".format(x)
  156. print(str)
  157. print()
  158. def test_string():
  159. msg = sb.text()
  160. msg.txt = "Foo bar"
  161. str = ""
  162. msg_str = msg.SerializeToString()
  163. print(len(msg_str))
  164. print(msg_str)
  165. for x in msg_str:
  166. str += "0x{:02x}, ".format(x)
  167. print(str)
  168. print()
  169. def test_bytes():
  170. msg = sb.raw_bytes()
  171. msg.b = b'\x01\x02\x03\x00'
  172. str = ""
  173. msg_str = msg.SerializeToString()
  174. print(len(msg_str))
  175. print(msg_str)
  176. for x in msg_str:
  177. str += "0x{:02x}, ".format(x)
  178. print(str)
  179. print()
  180. def test_repeated_string_bytes():
  181. msg = sb.repeated_string_bytes()
  182. msg.array_of_txt.append("Foo bar 1")
  183. msg.array_of_txt.append("")
  184. msg.array_of_txt.append("Foo bar 3")
  185. str = ""
  186. msg_str = msg.SerializeToString()
  187. print(len(msg_str))
  188. print(msg_str)
  189. for x in msg_str:
  190. str += "0x{:02x}, ".format(x)
  191. print(str)
  192. print()
  193. def test_oneof_fields():
  194. msg = of.message_oneof()
  195. msg.msg_DEF.varD = 1
  196. msg.msg_DEF.varE = 22
  197. msg.msg_DEF.varF = 333
  198. str = ""
  199. msg_str = msg.SerializeToString()
  200. print(len(msg_str))
  201. print(msg_str)
  202. for x in msg_str:
  203. str += "0x{:02x}, ".format(x)
  204. print(str)
  205. print()
  206. def test_included_proto():
  207. msg = iof.IncludedMessages()
  208. msg.state = fti.StateA
  209. msg.msg.a = 1
  210. msg.msg.b = 1.0
  211. msg.rf.x = 1
  212. msg.rf.y.append(1)
  213. msg.rf.y.append(1)
  214. msg.rf.y.append(1)
  215. msg.rf.z = 1
  216. str = ""
  217. msg_str = msg.SerializeToString()
  218. print(len(msg_str))
  219. print(msg_str)
  220. for x in msg_str:
  221. str += "0x{:02x}, ".format(x)
  222. print(str)
  223. print()
  224. #test_repeated_fields()
  225. #test_repeated_message()
  226. #test_string()
  227. #test_bytes()
  228. test_repeated_string_bytes()
  229. #test_nested_message()
  230. #test_oneof_fields()
  231. #test_included_proto()