Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers.Test/CodedOutputStreamTest.cs @ 4095

Last change on this file since 4095 was 3857, checked in by abeham, 14 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 11.9 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System.IO;
36using Google.ProtocolBuffers.TestProtos;
37using NUnit.Framework;
38
39namespace Google.ProtocolBuffers {
40  [TestFixture]
41  public class CodedOutputStreamTest {
42
43    /// <summary>
44    /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
45    /// checks that the result matches the given bytes
46    /// </summary>
47    private static void AssertWriteVarint(byte[] data, ulong value) {
48      // Only do 32-bit write if the value fits in 32 bits.
49      if ((value >> 32) == 0) {
50        MemoryStream rawOutput = new MemoryStream();
51        CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
52        output.WriteRawVarint32((uint) value);
53        output.Flush();
54        Assert.AreEqual(data, rawOutput.ToArray());
55        // Also try computing size.
56        Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value));
57      }
58
59      {
60        MemoryStream rawOutput = new MemoryStream();
61        CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
62        output.WriteRawVarint64(value);
63        output.Flush();
64        Assert.AreEqual(data, rawOutput.ToArray());
65
66        // Also try computing size.
67        Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
68      }
69
70      // Try different buffer sizes.
71      for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) {
72        // Only do 32-bit write if the value fits in 32 bits.
73        if ((value >> 32) == 0) {
74          MemoryStream rawOutput = new MemoryStream();
75          CodedOutputStream output =
76            CodedOutputStream.CreateInstance(rawOutput, bufferSize);
77          output.WriteRawVarint32((uint) value);
78          output.Flush();
79          Assert.AreEqual(data, rawOutput.ToArray());
80        }
81
82        {
83          MemoryStream rawOutput = new MemoryStream();
84          CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
85          output.WriteRawVarint64(value);
86          output.Flush();
87          Assert.AreEqual(data, rawOutput.ToArray());
88        }
89      }
90    }
91
92    /// <summary>
93    /// Tests WriteRawVarint32() and WriteRawVarint64()
94    /// </summary>
95    [Test]
96    public void WriteVarint() {
97      AssertWriteVarint(new byte[] {0x00}, 0);
98      AssertWriteVarint(new byte[] {0x01}, 1);
99      AssertWriteVarint(new byte[] {0x7f}, 127);
100      // 14882
101      AssertWriteVarint(new byte[] {0xa2, 0x74}, (0x22 << 0) | (0x74 << 7));
102      // 2961488830
103      AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x0b},
104        (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
105        (0x0bL << 28));
106
107      // 64-bit
108      // 7256456126
109      AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x1b},
110        (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
111        (0x1bL << 28));
112      // 41256202580718336
113      AssertWriteVarint(
114        new byte[] {0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
115        (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
116        (0x43UL << 28) | (0x49L << 35) | (0x24UL << 42) | (0x49UL << 49));
117      // 11964378330978735131
118      AssertWriteVarint(
119        new byte[] {0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
120        unchecked((ulong)
121        ((0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
122        (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
123        (0x05L << 49) | (0x26L << 56) | (0x01L << 63))));
124    }
125
126    /// <summary>
127    /// Parses the given bytes using WriteRawLittleEndian32() and checks
128    /// that the result matches the given value.
129    /// </summary>
130    private static void AssertWriteLittleEndian32(byte[] data, uint value) {
131      MemoryStream rawOutput = new MemoryStream();
132      CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
133      output.WriteRawLittleEndian32(value);
134      output.Flush();
135      Assert.AreEqual(data, rawOutput.ToArray());
136
137      // Try different buffer sizes.
138      for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) {
139        rawOutput = new MemoryStream();
140        output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
141        output.WriteRawLittleEndian32(value);
142        output.Flush();
143        Assert.AreEqual(data, rawOutput.ToArray());
144      }
145    }
146
147    /// <summary>
148    /// Parses the given bytes using WriteRawLittleEndian64() and checks
149    /// that the result matches the given value.
150    /// </summary>
151    private static void AssertWriteLittleEndian64(byte[] data, ulong value) {
152      MemoryStream rawOutput = new MemoryStream();
153      CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
154      output.WriteRawLittleEndian64(value);
155      output.Flush();
156      Assert.AreEqual(data, rawOutput.ToArray());
157
158      // Try different block sizes.
159      for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
160        rawOutput = new MemoryStream();
161        output = CodedOutputStream.CreateInstance(rawOutput, blockSize);
162        output.WriteRawLittleEndian64(value);
163        output.Flush();
164        Assert.AreEqual(data, rawOutput.ToArray());
165      }
166    }
167
168    /// <summary>
169    /// Tests writeRawLittleEndian32() and writeRawLittleEndian64().
170    /// </summary>
171    [Test]
172    public void WriteLittleEndian() {
173      AssertWriteLittleEndian32(new byte[] {0x78, 0x56, 0x34, 0x12}, 0x12345678);
174      AssertWriteLittleEndian32(new byte[] {0xf0, 0xde, 0xbc, 0x9a}, 0x9abcdef0);
175
176      AssertWriteLittleEndian64(
177        new byte[]{0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12},
178        0x123456789abcdef0L);
179      AssertWriteLittleEndian64(
180        new byte[]{0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a},
181        0x9abcdef012345678UL);
182    }
183
184    [Test]
185    public void WriteWholeMessage() {
186      TestAllTypes message = TestUtil.GetAllSet();
187
188      byte[] rawBytes = message.ToByteArray();
189      TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);
190
191      // Try different block sizes.
192      for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
193        MemoryStream rawOutput = new MemoryStream();
194        CodedOutputStream output =
195          CodedOutputStream.CreateInstance(rawOutput, blockSize);
196        message.WriteTo(output);
197        output.Flush();
198        TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray());
199      }
200    }
201
202    /// <summary>
203    /// Tests writing a whole message with every packed field type. Ensures the
204    /// wire format of packed fields is compatible with C++.
205    /// </summary>
206    [Test]
207    public void WriteWholePackedFieldsMessage() {
208      TestPackedTypes message = TestUtil.GetPackedSet();
209
210      byte[] rawBytes = message.ToByteArray();
211      TestUtil.AssertEqualBytes(TestUtil.GetGoldenPackedFieldsMessage().ToByteArray(),
212                       rawBytes);
213    }
214
215    [Test]
216    public void EncodeZigZag32() {
217      Assert.AreEqual(0, CodedOutputStream.EncodeZigZag32( 0));
218      Assert.AreEqual(1, CodedOutputStream.EncodeZigZag32(-1));
219      Assert.AreEqual(2, CodedOutputStream.EncodeZigZag32( 1));
220      Assert.AreEqual(3, CodedOutputStream.EncodeZigZag32(-2));
221      Assert.AreEqual(0x7FFFFFFE, CodedOutputStream.EncodeZigZag32(0x3FFFFFFF));
222      Assert.AreEqual(0x7FFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0xC0000000)));
223      Assert.AreEqual(0xFFFFFFFE, CodedOutputStream.EncodeZigZag32(0x7FFFFFFF));
224      Assert.AreEqual(0xFFFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0x80000000)));
225    }
226
227    [Test]
228    public void EncodeZigZag64() {
229      Assert.AreEqual(0, CodedOutputStream.EncodeZigZag64( 0));
230      Assert.AreEqual(1, CodedOutputStream.EncodeZigZag64(-1));
231      Assert.AreEqual(2, CodedOutputStream.EncodeZigZag64( 1));
232      Assert.AreEqual(3, CodedOutputStream.EncodeZigZag64(-2));
233      Assert.AreEqual(0x000000007FFFFFFEL,
234                   CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000003FFFFFFFUL)));
235      Assert.AreEqual(0x000000007FFFFFFFL,
236                   CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFFC0000000UL)));
237      Assert.AreEqual(0x00000000FFFFFFFEL,
238                   CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000007FFFFFFFUL)));
239      Assert.AreEqual(0x00000000FFFFFFFFL,
240                   CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFF80000000UL)));
241      Assert.AreEqual(0xFFFFFFFFFFFFFFFEL,
242                   CodedOutputStream.EncodeZigZag64(unchecked((long)0x7FFFFFFFFFFFFFFFUL)));
243      Assert.AreEqual(0xFFFFFFFFFFFFFFFFL,
244                   CodedOutputStream.EncodeZigZag64(unchecked((long)0x8000000000000000UL)));
245    }
246
247    [Test]
248    public void RoundTripZigZag32() {
249      // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
250      // were chosen semi-randomly via keyboard bashing.
251      Assert.AreEqual(0, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(0)));
252      Assert.AreEqual(1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(1)));
253      Assert.AreEqual(-1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-1)));
254      Assert.AreEqual(14927, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(14927)));
255      Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-3612)));
256    }
257     
258    [Test]
259    public void RoundTripZigZag64() {
260      Assert.AreEqual(0, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(0)));
261      Assert.AreEqual(1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(1)));
262      Assert.AreEqual(-1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-1)));
263      Assert.AreEqual(14927, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(14927)));
264      Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-3612)));
265
266      Assert.AreEqual(856912304801416L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(856912304801416L)));
267      Assert.AreEqual(-75123905439571256L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-75123905439571256L)));
268    }
269  }
270}
Note: See TracBrowser for help on using the repository browser.