test_data.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import build.python.simple_types_pb2 as st
  2. import build.python.nested_message_pb2 as nm
  3. def test_simple_types():
  4. # A test function used to generate encoded data to test the implementation of the wireformatter
  5. # and header template.
  6. msg = st.Test_Simple_Types()
  7. # msg.a_int32 = -2147483648
  8. # msg.a_int64 = -9223372036854775808
  9. # msg.a_uint32 = 0
  10. # msg.a_uint64 = 0
  11. # msg.a_sint32 = -2147483648
  12. # msg.a_sint64 = -9223372036854775808
  13. # msg.a_bool = 0
  14. # msg.a_enum = 0
  15. # msg.a_fixed64 = 0
  16. # msg.a_sfixed64 = -9223372036854775808
  17. # msg.a_double = 0
  18. # msg.a_fixed32 = 0
  19. # msg.a_sfixed32 = -2147483648
  20. # msg.a_float = 0
  21. # msg.a_int32 = 1
  22. # msg.a_int64 = 1
  23. # msg.a_uint32 = 1
  24. # msg.a_uint64 = 1
  25. # msg.a_sint32 = 1
  26. # msg.a_sint64 = 1
  27. # msg.a_bool = 1
  28. # msg.a_enum = 1
  29. # msg.a_fixed64 = 1
  30. # msg.a_sfixed64 = 1
  31. # msg.a_double = 1
  32. # msg.a_fixed32 = 1
  33. # msg.a_sfixed32 = 1
  34. # msg.a_float = 1
  35. msg.a_double = pow(2, -1022)
  36. msg.a_float = pow(2, -126)
  37. str = ""
  38. msg_str = msg.SerializeToString()
  39. print(len(msg_str))
  40. print(msg_str)
  41. for x in msg_str:
  42. str += "0x{:02x}, ".format(x)
  43. print(str)
  44. print()
  45. x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  46. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  47. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
  48. msg2 = st.Test_Simple_Types()
  49. msg2.ParseFromString(x)
  50. print(msg2)
  51. def test_nested_message():
  52. msg = nm.message_b()
  53. # msg.u = 1.0
  54. # msg.v = 1.0
  55. # msg.nested_a.x = 1
  56. # msg.nested_a.y = 1.0
  57. # msg.nested_a.z = 1
  58. msg.u = 0 #pow(2, 1023)
  59. msg.v = 0 #pow(2, 1023)
  60. msg.nested_a.x = pow(2, 31) - 1
  61. msg.nested_a.y = 0 #1.0
  62. msg.nested_a.z = 0 #1
  63. str = ""
  64. msg_str = msg.SerializeToString()
  65. print(len(msg_str))
  66. print(msg_str)
  67. for x in msg_str:
  68. str += "0x{:02x}, ".format(x)
  69. print(str)
  70. print()
  71. x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  72. 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
  73. msg2 = nm.message_b()
  74. msg2.ParseFromString(x)
  75. print(msg2)
  76. test_nested_message()