google/protobuf/internal/wire_format_test.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 #
3 # Protocol Buffers - Google's data interchange format
4 # Copyright 2008 Google Inc. All rights reserved.
5 # https://developers.google.com/protocol-buffers/
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # * Neither the name of Google Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived from
19 # this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 """Test for google.protobuf.internal.wire_format."""
34 
35 __author__ = 'robinson@google.com (Will Robinson)'
36 
37 try:
38  import unittest2 as unittest #PY26
39 except ImportError:
40  import unittest
41 
42 from google.protobuf import message
43 from google.protobuf.internal import wire_format
44 
45 
46 class WireFormatTest(unittest.TestCase):
47 
48  def testPackTag(self):
49  field_number = 0xabc
50  tag_type = 2
51  self.assertEqual((field_number << 3) | tag_type,
52  wire_format.PackTag(field_number, tag_type))
53  PackTag = wire_format.PackTag
54  # Number too high.
55  self.assertRaises(message.EncodeError, PackTag, field_number, 6)
56  # Number too low.
57  self.assertRaises(message.EncodeError, PackTag, field_number, -1)
58 
59  def testUnpackTag(self):
60  # Test field numbers that will require various varint sizes.
61  for expected_field_number in (1, 15, 16, 2047, 2048):
62  for expected_wire_type in range(6): # Highest-numbered wiretype is 5.
63  field_number, wire_type = wire_format.UnpackTag(
64  wire_format.PackTag(expected_field_number, expected_wire_type))
65  self.assertEqual(expected_field_number, field_number)
66  self.assertEqual(expected_wire_type, wire_type)
67 
68  self.assertRaises(TypeError, wire_format.UnpackTag, None)
69  self.assertRaises(TypeError, wire_format.UnpackTag, 'abc')
70  self.assertRaises(TypeError, wire_format.UnpackTag, 0.0)
71  self.assertRaises(TypeError, wire_format.UnpackTag, object())
72 
73  def testZigZagEncode(self):
74  Z = wire_format.ZigZagEncode
75  self.assertEqual(0, Z(0))
76  self.assertEqual(1, Z(-1))
77  self.assertEqual(2, Z(1))
78  self.assertEqual(3, Z(-2))
79  self.assertEqual(4, Z(2))
80  self.assertEqual(0xfffffffe, Z(0x7fffffff))
81  self.assertEqual(0xffffffff, Z(-0x80000000))
82  self.assertEqual(0xfffffffffffffffe, Z(0x7fffffffffffffff))
83  self.assertEqual(0xffffffffffffffff, Z(-0x8000000000000000))
84 
85  self.assertRaises(TypeError, Z, None)
86  self.assertRaises(TypeError, Z, 'abcd')
87  self.assertRaises(TypeError, Z, 0.0)
88  self.assertRaises(TypeError, Z, object())
89 
90  def testZigZagDecode(self):
91  Z = wire_format.ZigZagDecode
92  self.assertEqual(0, Z(0))
93  self.assertEqual(-1, Z(1))
94  self.assertEqual(1, Z(2))
95  self.assertEqual(-2, Z(3))
96  self.assertEqual(2, Z(4))
97  self.assertEqual(0x7fffffff, Z(0xfffffffe))
98  self.assertEqual(-0x80000000, Z(0xffffffff))
99  self.assertEqual(0x7fffffffffffffff, Z(0xfffffffffffffffe))
100  self.assertEqual(-0x8000000000000000, Z(0xffffffffffffffff))
101 
102  self.assertRaises(TypeError, Z, None)
103  self.assertRaises(TypeError, Z, 'abcd')
104  self.assertRaises(TypeError, Z, 0.0)
105  self.assertRaises(TypeError, Z, object())
106 
107  def NumericByteSizeTestHelper(self, byte_size_fn, value, expected_value_size):
108  # Use field numbers that cause various byte sizes for the tag information.
109  for field_number, tag_bytes in ((15, 1), (16, 2), (2047, 2), (2048, 3)):
110  expected_size = expected_value_size + tag_bytes
111  actual_size = byte_size_fn(field_number, value)
112  self.assertEqual(expected_size, actual_size,
113  'byte_size_fn: %s, field_number: %d, value: %r\n'
114  'Expected: %d, Actual: %d'% (
115  byte_size_fn, field_number, value, expected_size, actual_size))
116 
118  # Test all numeric *ByteSize() functions.
119  NUMERIC_ARGS = [
120  # Int32ByteSize().
121  [wire_format.Int32ByteSize, 0, 1],
122  [wire_format.Int32ByteSize, 127, 1],
123  [wire_format.Int32ByteSize, 128, 2],
124  [wire_format.Int32ByteSize, -1, 10],
125  # Int64ByteSize().
126  [wire_format.Int64ByteSize, 0, 1],
127  [wire_format.Int64ByteSize, 127, 1],
128  [wire_format.Int64ByteSize, 128, 2],
129  [wire_format.Int64ByteSize, -1, 10],
130  # UInt32ByteSize().
131  [wire_format.UInt32ByteSize, 0, 1],
132  [wire_format.UInt32ByteSize, 127, 1],
133  [wire_format.UInt32ByteSize, 128, 2],
134  [wire_format.UInt32ByteSize, wire_format.UINT32_MAX, 5],
135  # UInt64ByteSize().
136  [wire_format.UInt64ByteSize, 0, 1],
137  [wire_format.UInt64ByteSize, 127, 1],
138  [wire_format.UInt64ByteSize, 128, 2],
139  [wire_format.UInt64ByteSize, wire_format.UINT64_MAX, 10],
140  # SInt32ByteSize().
141  [wire_format.SInt32ByteSize, 0, 1],
142  [wire_format.SInt32ByteSize, -1, 1],
143  [wire_format.SInt32ByteSize, 1, 1],
144  [wire_format.SInt32ByteSize, -63, 1],
145  [wire_format.SInt32ByteSize, 63, 1],
146  [wire_format.SInt32ByteSize, -64, 1],
147  [wire_format.SInt32ByteSize, 64, 2],
148  # SInt64ByteSize().
149  [wire_format.SInt64ByteSize, 0, 1],
150  [wire_format.SInt64ByteSize, -1, 1],
151  [wire_format.SInt64ByteSize, 1, 1],
152  [wire_format.SInt64ByteSize, -63, 1],
153  [wire_format.SInt64ByteSize, 63, 1],
154  [wire_format.SInt64ByteSize, -64, 1],
155  [wire_format.SInt64ByteSize, 64, 2],
156  # Fixed32ByteSize().
157  [wire_format.Fixed32ByteSize, 0, 4],
158  [wire_format.Fixed32ByteSize, wire_format.UINT32_MAX, 4],
159  # Fixed64ByteSize().
160  [wire_format.Fixed64ByteSize, 0, 8],
161  [wire_format.Fixed64ByteSize, wire_format.UINT64_MAX, 8],
162  # SFixed32ByteSize().
163  [wire_format.SFixed32ByteSize, 0, 4],
164  [wire_format.SFixed32ByteSize, wire_format.INT32_MIN, 4],
165  [wire_format.SFixed32ByteSize, wire_format.INT32_MAX, 4],
166  # SFixed64ByteSize().
167  [wire_format.SFixed64ByteSize, 0, 8],
168  [wire_format.SFixed64ByteSize, wire_format.INT64_MIN, 8],
169  [wire_format.SFixed64ByteSize, wire_format.INT64_MAX, 8],
170  # FloatByteSize().
171  [wire_format.FloatByteSize, 0.0, 4],
172  [wire_format.FloatByteSize, 1000000000.0, 4],
173  [wire_format.FloatByteSize, -1000000000.0, 4],
174  # DoubleByteSize().
175  [wire_format.DoubleByteSize, 0.0, 8],
176  [wire_format.DoubleByteSize, 1000000000.0, 8],
177  [wire_format.DoubleByteSize, -1000000000.0, 8],
178  # BoolByteSize().
179  [wire_format.BoolByteSize, False, 1],
180  [wire_format.BoolByteSize, True, 1],
181  # EnumByteSize().
182  [wire_format.EnumByteSize, 0, 1],
183  [wire_format.EnumByteSize, 127, 1],
184  [wire_format.EnumByteSize, 128, 2],
185  [wire_format.EnumByteSize, wire_format.UINT32_MAX, 5],
186  ]
187  for args in NUMERIC_ARGS:
188  self.NumericByteSizeTestHelper(*args)
189 
190  # Test strings and bytes.
191  for byte_size_fn in (wire_format.StringByteSize, wire_format.BytesByteSize):
192  # 1 byte for tag, 1 byte for length, 3 bytes for contents.
193  self.assertEqual(5, byte_size_fn(10, 'abc'))
194  # 2 bytes for tag, 1 byte for length, 3 bytes for contents.
195  self.assertEqual(6, byte_size_fn(16, 'abc'))
196  # 2 bytes for tag, 2 bytes for length, 128 bytes for contents.
197  self.assertEqual(132, byte_size_fn(16, 'a' * 128))
198 
199  # Test UTF-8 string byte size calculation.
200  # 1 byte for tag, 1 byte for length, 8 bytes for content.
201  self.assertEqual(10, wire_format.StringByteSize(
202  5, b'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'.decode('utf-8')))
203 
204  class MockMessage(object):
205  def __init__(self, byte_size):
206  self.byte_size = byte_size
207  def ByteSize(self):
208  return self.byte_size
209 
210  message_byte_size = 10
211  mock_message = MockMessage(byte_size=message_byte_size)
212  # Test groups.
213  # (2 * 1) bytes for begin and end tags, plus message_byte_size.
214  self.assertEqual(2 + message_byte_size,
215  wire_format.GroupByteSize(1, mock_message))
216  # (2 * 2) bytes for begin and end tags, plus message_byte_size.
217  self.assertEqual(4 + message_byte_size,
218  wire_format.GroupByteSize(16, mock_message))
219 
220  # Test messages.
221  # 1 byte for tag, plus 1 byte for length, plus contents.
222  self.assertEqual(2 + mock_message.byte_size,
223  wire_format.MessageByteSize(1, mock_message))
224  # 2 bytes for tag, plus 1 byte for length, plus contents.
225  self.assertEqual(3 + mock_message.byte_size,
226  wire_format.MessageByteSize(16, mock_message))
227  # 2 bytes for tag, plus 2 bytes for length, plus contents.
228  mock_message.byte_size = 128
229  self.assertEqual(4 + mock_message.byte_size,
230  wire_format.MessageByteSize(16, mock_message))
231 
232 
233  # Test message set item byte size.
234  # 4 bytes for tags, plus 1 byte for length, plus 1 byte for type_id,
235  # plus contents.
236  mock_message.byte_size = 10
237  self.assertEqual(mock_message.byte_size + 6,
238  wire_format.MessageSetItemByteSize(1, mock_message))
239 
240  # 4 bytes for tags, plus 2 bytes for length, plus 1 byte for type_id,
241  # plus contents.
242  mock_message.byte_size = 128
243  self.assertEqual(mock_message.byte_size + 7,
244  wire_format.MessageSetItemByteSize(1, mock_message))
245 
246  # 4 bytes for tags, plus 2 bytes for length, plus 2 byte for type_id,
247  # plus contents.
248  self.assertEqual(mock_message.byte_size + 8,
249  wire_format.MessageSetItemByteSize(128, mock_message))
250 
251  # Too-long varint.
252  self.assertRaises(message.EncodeError,
253  wire_format.UInt64ByteSize, 1, 1 << 128)
254 
255 
256 if __name__ == '__main__':
257  unittest.main()
google::protobuf.internal.wire_format_test.WireFormatTest.testByteSizeFunctions
def testByteSizeFunctions(self)
Definition: google/protobuf/internal/wire_format_test.py:117
google::protobuf.internal.wire_format_test.WireFormatTest.NumericByteSizeTestHelper
def NumericByteSizeTestHelper(self, byte_size_fn, value, expected_value_size)
Definition: google/protobuf/internal/wire_format_test.py:107
google::protobuf.internal.wire_format_test.WireFormatTest
Definition: google/protobuf/internal/wire_format_test.py:46
google::protobuf
Definition: data_proto2_to_proto3_util.h:12
google::protobuf.internal.wire_format_test.WireFormatTest.testUnpackTag
def testUnpackTag(self)
Definition: google/protobuf/internal/wire_format_test.py:59
google::protobuf.internal
Definition: python/google/protobuf/internal/__init__.py:1
google::protobuf.internal.python_message.ByteSize
ByteSize
Definition: python_message.py:1067
google::protobuf.internal.wire_format_test.WireFormatTest.testZigZagEncode
def testZigZagEncode(self)
Definition: google/protobuf/internal/wire_format_test.py:73
google::protobuf.internal.wire_format_test.WireFormatTest.testPackTag
def testPackTag(self)
Definition: google/protobuf/internal/wire_format_test.py:48
google::protobuf.internal.wire_format_test.WireFormatTest.byte_size
byte_size
Definition: google/protobuf/internal/wire_format_test.py:206
google::protobuf.internal.wire_format_test.WireFormatTest.testZigZagDecode
def testZigZagDecode(self)
Definition: google/protobuf/internal/wire_format_test.py:90
google::protobuf.internal.python_message.__init__
__init__
Definition: python_message.py:557


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02