Initial import of v2.0.0beta
[protobuf.git] / python / google / protobuf / internal / output_stream_test.py
blob026f6161523105828ceb755127c68ad74927ae5d
1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc.
3 # http://code.google.com/p/protobuf/
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Test for google.protobuf.internal.output_stream."""
19 __author__ = 'robinson@google.com (Will Robinson)'
21 import unittest
22 from google.protobuf import message
23 from google.protobuf.internal import output_stream
24 from google.protobuf.internal import wire_format
27 class OutputStreamTest(unittest.TestCase):
29 def setUp(self):
30 self.stream = output_stream.OutputStream()
32 def testAppendRawBytes(self):
33 # Empty string.
34 self.stream.AppendRawBytes('')
35 self.assertEqual('', self.stream.ToString())
37 # Nonempty string.
38 self.stream.AppendRawBytes('abc')
39 self.assertEqual('abc', self.stream.ToString())
41 # Ensure that we're actually appending.
42 self.stream.AppendRawBytes('def')
43 self.assertEqual('abcdef', self.stream.ToString())
45 def AppendNumericTestHelper(self, append_fn, values_and_strings):
46 """For each (value, expected_string) pair in values_and_strings,
47 calls an OutputStream.Append*(value) method on an OutputStream and ensures
48 that the string written to that stream matches expected_string.
50 Args:
51 append_fn: Unbound OutputStream method that takes an integer or
52 long value as input.
53 values_and_strings: Iterable of (value, expected_string) pairs.
54 """
55 for conversion in (int, long):
56 for value, string in values_and_strings:
57 stream = output_stream.OutputStream()
58 expected_string = ''
59 append_fn(stream, conversion(value))
60 expected_string += string
61 self.assertEqual(expected_string, stream.ToString())
63 def AppendOverflowTestHelper(self, append_fn, value):
64 """Calls an OutputStream.Append*(value) method and asserts
65 that the method raises message.EncodeError.
67 Args:
68 append_fn: Unbound OutputStream method that takes an integer or
69 long value as input.
70 value: Value to pass to append_fn which should cause an
71 message.EncodeError.
72 """
73 stream = output_stream.OutputStream()
74 self.assertRaises(message.EncodeError, append_fn, stream, value)
76 def testAppendLittleEndian32(self):
77 append_fn = output_stream.OutputStream.AppendLittleEndian32
78 values_and_expected_strings = [
79 (0, '\x00\x00\x00\x00'),
80 (1, '\x01\x00\x00\x00'),
81 ((1 << 32) - 1, '\xff\xff\xff\xff'),
83 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
85 self.AppendOverflowTestHelper(append_fn, 1 << 32)
86 self.AppendOverflowTestHelper(append_fn, -1)
88 def testAppendLittleEndian64(self):
89 append_fn = output_stream.OutputStream.AppendLittleEndian64
90 values_and_expected_strings = [
91 (0, '\x00\x00\x00\x00\x00\x00\x00\x00'),
92 (1, '\x01\x00\x00\x00\x00\x00\x00\x00'),
93 ((1 << 64) - 1, '\xff\xff\xff\xff\xff\xff\xff\xff'),
95 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
97 self.AppendOverflowTestHelper(append_fn, 1 << 64)
98 self.AppendOverflowTestHelper(append_fn, -1)
100 def testAppendVarint32(self):
101 append_fn = output_stream.OutputStream.AppendVarint32
102 values_and_expected_strings = [
103 (0, '\x00'),
104 (1, '\x01'),
105 (127, '\x7f'),
106 (128, '\x80\x01'),
107 (-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
108 (wire_format.INT32_MAX, '\xff\xff\xff\xff\x07'),
109 (wire_format.INT32_MIN, '\x80\x80\x80\x80\xf8\xff\xff\xff\xff\x01'),
111 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
113 self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MAX + 1)
114 self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MIN - 1)
116 def testAppendVarUInt32(self):
117 append_fn = output_stream.OutputStream.AppendVarUInt32
118 values_and_expected_strings = [
119 (0, '\x00'),
120 (1, '\x01'),
121 (127, '\x7f'),
122 (128, '\x80\x01'),
123 (wire_format.UINT32_MAX, '\xff\xff\xff\xff\x0f'),
125 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
127 self.AppendOverflowTestHelper(append_fn, -1)
128 self.AppendOverflowTestHelper(append_fn, wire_format.UINT32_MAX + 1)
130 def testAppendVarint64(self):
131 append_fn = output_stream.OutputStream.AppendVarint64
132 values_and_expected_strings = [
133 (0, '\x00'),
134 (1, '\x01'),
135 (127, '\x7f'),
136 (128, '\x80\x01'),
137 (-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
138 (wire_format.INT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\x7f'),
139 (wire_format.INT64_MIN, '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01'),
141 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
143 self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MAX + 1)
144 self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MIN - 1)
146 def testAppendVarUInt64(self):
147 append_fn = output_stream.OutputStream.AppendVarUInt64
148 values_and_expected_strings = [
149 (0, '\x00'),
150 (1, '\x01'),
151 (127, '\x7f'),
152 (128, '\x80\x01'),
153 (wire_format.UINT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
155 self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
157 self.AppendOverflowTestHelper(append_fn, -1)
158 self.AppendOverflowTestHelper(append_fn, wire_format.UINT64_MAX + 1)
161 if __name__ == '__main__':
162 unittest.main()