test_data.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #msg.x = 0
  80. msg.y.append(0)
  81. msg.y.append(0)
  82. msg.y.append(0)
  83. #msg.z = 0
  84. #msg.y.append(pow(2, 32) - 1)
  85. #msg.y.append(pow(2, 32) - 1)
  86. #msg.y.append(pow(2, 32) - 1)
  87. #msg.x = 1
  88. #msg.y.append(1)
  89. #msg.y.append(1)
  90. #msg.y.append(1)
  91. #msg.z = 1
  92. #msg.x = pow(2, 32) - 1
  93. #msg.y.append(pow(2, 32) - 1)
  94. #msg.y.append(pow(2, 32) - 1)
  95. #msg.y.append(pow(2, 32) - 1)
  96. #msg.z = pow(2, 32) - 1
  97. str = ""
  98. msg_str = msg.SerializeToString()
  99. print(len(msg_str))
  100. print(msg_str)
  101. for x in msg_str:
  102. str += "0x{:02x}, ".format(x)
  103. print(str)
  104. print()
  105. def test_repeated_message():
  106. nmsg = rf.nested_message()
  107. nmsg.u = 1
  108. nmsg.v = 1
  109. msg = rf.repeated_message()
  110. msg.x = 0
  111. for i in range(3):
  112. nmsg = msg.y.add()
  113. nmsg.u = 1
  114. nmsg.v = 1
  115. msg.z = 0
  116. str = ""
  117. msg_str = msg.SerializeToString()
  118. print(len(msg_str))
  119. print(msg_str)
  120. for x in msg_str:
  121. str += "0x{:02x}, ".format(x)
  122. print(str)
  123. print()
  124. test_repeated_message()