test_data.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. import build.python.oneof_fields_pb2 as of
  5. def test_simple_types():
  6. # A test function used to generate encoded data to test the implementation of the wireformatter
  7. # and header template.
  8. msg = st.Test_Simple_Types()
  9. # msg.a_int32 = -2147483648
  10. # msg.a_int64 = -9223372036854775808
  11. # msg.a_uint32 = 0
  12. # msg.a_uint64 = 0
  13. # msg.a_sint32 = -2147483648
  14. # msg.a_sint64 = -9223372036854775808
  15. # msg.a_bool = 0
  16. # msg.a_enum = 0
  17. # msg.a_fixed64 = 0
  18. # msg.a_sfixed64 = -9223372036854775808
  19. # msg.a_double = 0
  20. # msg.a_fixed32 = 0
  21. # msg.a_sfixed32 = -2147483648
  22. # msg.a_float = 0
  23. # msg.a_int32 = 1
  24. # msg.a_int64 = 1
  25. # msg.a_uint32 = 1
  26. # msg.a_uint64 = 1
  27. # msg.a_sint32 = 1
  28. # msg.a_sint64 = 1
  29. # msg.a_bool = 1
  30. # msg.a_enum = 1
  31. # msg.a_fixed64 = 1
  32. # msg.a_sfixed64 = 1
  33. # msg.a_double = 1
  34. # msg.a_fixed32 = 1
  35. # msg.a_sfixed32 = 1
  36. # msg.a_float = 1
  37. msg.a_double = pow(2, -1022)
  38. msg.a_float = pow(2, -126)
  39. str = ""
  40. msg_str = msg.SerializeToString()
  41. print(len(msg_str))
  42. print(msg_str)
  43. for x in msg_str:
  44. str += "0x{:02x}, ".format(x)
  45. print(str)
  46. print()
  47. x = bytearray([0x08, 0x80, 0x80, 0x80, 0x80, 0x08,
  48. 0x10, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01,
  49. 0x28, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F,])
  50. msg2 = st.Test_Simple_Types()
  51. msg2.ParseFromString(x)
  52. print(msg2)
  53. def test_nested_message():
  54. msg = nm.message_b()
  55. # msg.u = 1.0
  56. # msg.v = 1.0
  57. # msg.nested_a.x = 1
  58. # msg.nested_a.y = 1.0
  59. # msg.nested_a.z = 1
  60. msg.u = 0 #pow(2, 1023)
  61. msg.v = 0 #pow(2, 1023)
  62. #msg.nested_a.x = 0#pow(2, 31) - 1
  63. #msg.nested_a.y = 0 #1.0
  64. #msg.nested_a.z = 0 #1
  65. str = ""
  66. msg_str = msg.SerializeToString()
  67. print(len(msg_str))
  68. print(msg_str)
  69. for x in msg_str:
  70. str += "0x{:02x}, ".format(x)
  71. print(str)
  72. print()
  73. x = bytearray([0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
  74. 0x3f, 0x1a, 0x09, 0x08, 0x01, 0x15, 0x00, 0x00, 0x80, 0x3f, 0x18, 0x02])
  75. msg2 = nm.message_b()
  76. msg2.ParseFromString(x)
  77. print(msg2)
  78. def test_repeated_fields():
  79. msg = rf.repeated_fields()
  80. #msg.x = 0
  81. msg.y.append(0)
  82. msg.y.append(1)
  83. msg.y.append(0)
  84. #msg.z = 0
  85. #msg.y.append(pow(2, 32) - 1)
  86. #msg.y.append(pow(2, 32) - 1)
  87. #msg.y.append(pow(2, 32) - 1)
  88. #msg.x = 1
  89. #msg.y.append(1)
  90. #msg.y.append(1)
  91. #msg.y.append(1)
  92. #msg.z = 1
  93. #msg.x = pow(2, 32) - 1
  94. #msg.y.append(pow(2, 32) - 1)
  95. #msg.y.append(pow(2, 32) - 1)
  96. #msg.y.append(pow(2, 32) - 1)
  97. #msg.z = pow(2, 32) - 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. def test_repeated_message():
  107. msg = rf.repeated_message()
  108. msg.x = 0
  109. for i in range(3):
  110. nmsg = msg.y.add()
  111. nmsg.u = 0
  112. nmsg.v = 0
  113. msg.z = 0
  114. msg.y[1].u = 1
  115. msg.y[1].v = 1
  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. def test_oneof_fields():
  125. msg = of.message_oneof()
  126. msg.a = 1
  127. msg.b = 1
  128. msg.x = 1
  129. msg.v = 1
  130. str = ""
  131. msg_str = msg.SerializeToString()
  132. print(len(msg_str))
  133. print(msg_str)
  134. for x in msg_str:
  135. str += "0x{:02x}, ".format(x)
  136. print(str)
  137. print()
  138. #test_repeated_fields()
  139. #test_repeated_message()
  140. #test_nested_message()
  141. test_oneof_fields()