OLD | NEW |
1 #! /usr/bin/python | 1 #! /usr/bin/env python |
2 # | 2 # |
3 # Protocol Buffers - Google's data interchange format | 3 # Protocol Buffers - Google's data interchange format |
4 # Copyright 2008 Google Inc. All rights reserved. | 4 # Copyright 2008 Google Inc. All rights reserved. |
5 # http://code.google.com/p/protobuf/ | 5 # https://developers.google.com/protocol-buffers/ |
6 # | 6 # |
7 # Redistribution and use in source and binary forms, with or without | 7 # Redistribution and use in source and binary forms, with or without |
8 # modification, are permitted provided that the following conditions are | 8 # modification, are permitted provided that the following conditions are |
9 # met: | 9 # met: |
10 # | 10 # |
11 # * Redistributions of source code must retain the above copyright | 11 # * Redistributions of source code must retain the above copyright |
12 # notice, this list of conditions and the following disclaimer. | 12 # notice, this list of conditions and the following disclaimer. |
13 # * Redistributions in binary form must reproduce the above | 13 # * Redistributions in binary form must reproduce the above |
14 # copyright notice, this list of conditions and the following disclaimer | 14 # copyright notice, this list of conditions and the following disclaimer |
15 # in the documentation and/or other materials provided with the | 15 # in the documentation and/or other materials provided with the |
(...skipping 11 matching lines...) Expand all Loading... |
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | 32 |
33 """Test for google.protobuf.internal.wire_format.""" | 33 """Test for google.protobuf.internal.wire_format.""" |
34 | 34 |
35 __author__ = 'robinson@google.com (Will Robinson)' | 35 __author__ = 'robinson@google.com (Will Robinson)' |
36 | 36 |
37 import unittest | 37 try: |
| 38 import unittest2 as unittest |
| 39 except ImportError: |
| 40 import unittest |
38 from google.protobuf import message | 41 from google.protobuf import message |
39 from google.protobuf.internal import wire_format | 42 from google.protobuf.internal import wire_format |
40 | 43 |
41 | 44 |
42 class WireFormatTest(unittest.TestCase): | 45 class WireFormatTest(unittest.TestCase): |
43 | 46 |
44 def testPackTag(self): | 47 def testPackTag(self): |
45 field_number = 0xabc | 48 field_number = 0xabc |
46 tag_type = 2 | 49 tag_type = 2 |
47 self.assertEqual((field_number << 3) | tag_type, | 50 self.assertEqual((field_number << 3) | tag_type, |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 # 1 byte for tag, 1 byte for length, 3 bytes for contents. | 191 # 1 byte for tag, 1 byte for length, 3 bytes for contents. |
189 self.assertEqual(5, byte_size_fn(10, 'abc')) | 192 self.assertEqual(5, byte_size_fn(10, 'abc')) |
190 # 2 bytes for tag, 1 byte for length, 3 bytes for contents. | 193 # 2 bytes for tag, 1 byte for length, 3 bytes for contents. |
191 self.assertEqual(6, byte_size_fn(16, 'abc')) | 194 self.assertEqual(6, byte_size_fn(16, 'abc')) |
192 # 2 bytes for tag, 2 bytes for length, 128 bytes for contents. | 195 # 2 bytes for tag, 2 bytes for length, 128 bytes for contents. |
193 self.assertEqual(132, byte_size_fn(16, 'a' * 128)) | 196 self.assertEqual(132, byte_size_fn(16, 'a' * 128)) |
194 | 197 |
195 # Test UTF-8 string byte size calculation. | 198 # Test UTF-8 string byte size calculation. |
196 # 1 byte for tag, 1 byte for length, 8 bytes for content. | 199 # 1 byte for tag, 1 byte for length, 8 bytes for content. |
197 self.assertEqual(10, wire_format.StringByteSize( | 200 self.assertEqual(10, wire_format.StringByteSize( |
198 5, unicode('\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82', 'utf-8'))) | 201 5, b'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'.decode('utf-8'))) |
199 | 202 |
200 class MockMessage(object): | 203 class MockMessage(object): |
201 def __init__(self, byte_size): | 204 def __init__(self, byte_size): |
202 self.byte_size = byte_size | 205 self.byte_size = byte_size |
203 def ByteSize(self): | 206 def ByteSize(self): |
204 return self.byte_size | 207 return self.byte_size |
205 | 208 |
206 message_byte_size = 10 | 209 message_byte_size = 10 |
207 mock_message = MockMessage(byte_size=message_byte_size) | 210 mock_message = MockMessage(byte_size=message_byte_size) |
208 # Test groups. | 211 # Test groups. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 self.assertEqual(mock_message.byte_size + 8, | 247 self.assertEqual(mock_message.byte_size + 8, |
245 wire_format.MessageSetItemByteSize(128, mock_message)) | 248 wire_format.MessageSetItemByteSize(128, mock_message)) |
246 | 249 |
247 # Too-long varint. | 250 # Too-long varint. |
248 self.assertRaises(message.EncodeError, | 251 self.assertRaises(message.EncodeError, |
249 wire_format.UInt64ByteSize, 1, 1 << 128) | 252 wire_format.UInt64ByteSize, 1, 1 << 128) |
250 | 253 |
251 | 254 |
252 if __name__ == '__main__': | 255 if __name__ == '__main__': |
253 unittest.main() | 256 unittest.main() |
OLD | NEW |