test_data.py 2.6 KB

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