Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // http://github.com/jskeet/dotnet-protobufs/ |
| 4 | // Original C++/Java/Python code: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 5 | // http://code.google.com/p/protobuf/ |
| 6 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions are |
| 9 | // met: |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 10 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 20 | // |
Jon Skeet | 60c059b | 2008-10-23 21:17:56 +0100 | [diff] [blame] | 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. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 32 | using System.IO; |
| 33 | using Google.ProtocolBuffers.TestProtos; |
| 34 | using NUnit.Framework; |
| 35 | |
| 36 | namespace Google.ProtocolBuffers { |
| 37 | [TestFixture] |
| 38 | public class CodedOutputStreamTest { |
| 39 | |
| 40 | /// <summary> |
| 41 | /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and |
| 42 | /// checks that the result matches the given bytes |
| 43 | /// </summary> |
| 44 | private static void AssertWriteVarint(byte[] data, ulong value) { |
| 45 | // Only do 32-bit write if the value fits in 32 bits. |
| 46 | if ((value >> 32) == 0) { |
| 47 | MemoryStream rawOutput = new MemoryStream(); |
| 48 | CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); |
| 49 | output.WriteRawVarint32((uint) value); |
| 50 | output.Flush(); |
| 51 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 52 | // Also try computing size. |
| 53 | Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value)); |
| 54 | } |
| 55 | |
| 56 | { |
| 57 | MemoryStream rawOutput = new MemoryStream(); |
| 58 | CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); |
| 59 | output.WriteRawVarint64(value); |
| 60 | output.Flush(); |
| 61 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 62 | |
| 63 | // Also try computing size. |
| 64 | Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value)); |
| 65 | } |
| 66 | |
| 67 | // Try different buffer sizes. |
| 68 | for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { |
| 69 | // Only do 32-bit write if the value fits in 32 bits. |
| 70 | if ((value >> 32) == 0) { |
| 71 | MemoryStream rawOutput = new MemoryStream(); |
| 72 | CodedOutputStream output = |
| 73 | CodedOutputStream.CreateInstance(rawOutput, bufferSize); |
| 74 | output.WriteRawVarint32((uint) value); |
| 75 | output.Flush(); |
| 76 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 77 | } |
| 78 | |
| 79 | { |
| 80 | MemoryStream rawOutput = new MemoryStream(); |
| 81 | CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, bufferSize); |
| 82 | output.WriteRawVarint64(value); |
| 83 | output.Flush(); |
| 84 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Tests WriteRawVarint32() and WriteRawVarint64() |
| 91 | /// </summary> |
| 92 | [Test] |
| 93 | public void WriteVarint() { |
| 94 | AssertWriteVarint(new byte[] {0x00}, 0); |
| 95 | AssertWriteVarint(new byte[] {0x01}, 1); |
| 96 | AssertWriteVarint(new byte[] {0x7f}, 127); |
| 97 | // 14882 |
| 98 | AssertWriteVarint(new byte[] {0xa2, 0x74}, (0x22 << 0) | (0x74 << 7)); |
| 99 | // 2961488830 |
| 100 | AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x0b}, |
| 101 | (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | |
| 102 | (0x0bL << 28)); |
| 103 | |
| 104 | // 64-bit |
| 105 | // 7256456126 |
| 106 | AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x1b}, |
| 107 | (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | |
| 108 | (0x1bL << 28)); |
| 109 | // 41256202580718336 |
| 110 | AssertWriteVarint( |
| 111 | new byte[] {0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49}, |
| 112 | (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | |
| 113 | (0x43UL << 28) | (0x49L << 35) | (0x24UL << 42) | (0x49UL << 49)); |
| 114 | // 11964378330978735131 |
| 115 | AssertWriteVarint( |
| 116 | new byte[] {0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01}, |
| 117 | unchecked((ulong) |
| 118 | ((0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | |
| 119 | (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) | |
| 120 | (0x05L << 49) | (0x26L << 56) | (0x01L << 63)))); |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Parses the given bytes using WriteRawLittleEndian32() and checks |
| 125 | /// that the result matches the given value. |
| 126 | /// </summary> |
| 127 | private static void AssertWriteLittleEndian32(byte[] data, uint value) { |
| 128 | MemoryStream rawOutput = new MemoryStream(); |
| 129 | CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); |
| 130 | output.WriteRawLittleEndian32(value); |
| 131 | output.Flush(); |
| 132 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 133 | |
| 134 | // Try different buffer sizes. |
| 135 | for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) { |
| 136 | rawOutput = new MemoryStream(); |
| 137 | output = CodedOutputStream.CreateInstance(rawOutput, bufferSize); |
| 138 | output.WriteRawLittleEndian32(value); |
| 139 | output.Flush(); |
| 140 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Parses the given bytes using WriteRawLittleEndian64() and checks |
| 146 | /// that the result matches the given value. |
| 147 | /// </summary> |
| 148 | private static void AssertWriteLittleEndian64(byte[] data, ulong value) { |
| 149 | MemoryStream rawOutput = new MemoryStream(); |
| 150 | CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput); |
| 151 | output.WriteRawLittleEndian64(value); |
| 152 | output.Flush(); |
| 153 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 154 | |
| 155 | // Try different block sizes. |
| 156 | for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { |
| 157 | rawOutput = new MemoryStream(); |
| 158 | output = CodedOutputStream.CreateInstance(rawOutput, blockSize); |
| 159 | output.WriteRawLittleEndian64(value); |
| 160 | output.Flush(); |
| 161 | Assert.AreEqual(data, rawOutput.ToArray()); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// <summary> |
| 166 | /// Tests writeRawLittleEndian32() and writeRawLittleEndian64(). |
| 167 | /// </summary> |
| 168 | [Test] |
| 169 | public void WriteLittleEndian() { |
| 170 | AssertWriteLittleEndian32(new byte[] {0x78, 0x56, 0x34, 0x12}, 0x12345678); |
| 171 | AssertWriteLittleEndian32(new byte[] {0xf0, 0xde, 0xbc, 0x9a}, 0x9abcdef0); |
| 172 | |
| 173 | AssertWriteLittleEndian64( |
| 174 | new byte[]{0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12}, |
| 175 | 0x123456789abcdef0L); |
| 176 | AssertWriteLittleEndian64( |
| 177 | new byte[]{0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a}, |
| 178 | 0x9abcdef012345678UL); |
| 179 | } |
| 180 | |
| 181 | [Test] |
| 182 | public void WriteWholeMessage() { |
| 183 | TestAllTypes message = TestUtil.GetAllSet(); |
| 184 | |
| 185 | byte[] rawBytes = message.ToByteArray(); |
| 186 | TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes); |
| 187 | |
| 188 | // Try different block sizes. |
| 189 | for (int blockSize = 1; blockSize < 256; blockSize *= 2) { |
| 190 | MemoryStream rawOutput = new MemoryStream(); |
| 191 | CodedOutputStream output = |
| 192 | CodedOutputStream.CreateInstance(rawOutput, blockSize); |
| 193 | message.WriteTo(output); |
| 194 | output.Flush(); |
| 195 | TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray()); |
| 196 | } |
| 197 | } |
| 198 | |
Jon Skeet | 25a2858 | 2009-02-18 16:06:22 +0000 | [diff] [blame^] | 199 | /// <summary> |
| 200 | /// Tests writing a whole message with every packed field type. Ensures the |
| 201 | /// wire format of packed fields is compatible with C++. |
| 202 | /// </summary> |
| 203 | [Test] |
| 204 | public void WriteWholePackedFieldsMessage() { |
| 205 | TestPackedTypes message = TestUtil.GetPackedSet(); |
| 206 | |
| 207 | byte[] rawBytes = message.ToByteArray(); |
| 208 | TestUtil.AssertEqualBytes(TestUtil.GetGoldenPackedFieldsMessage().ToByteArray(), |
| 209 | rawBytes); |
| 210 | } |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 211 | |
| 212 | [Test] |
| 213 | public void EncodeZigZag32() { |
| 214 | Assert.AreEqual(0, CodedOutputStream.EncodeZigZag32( 0)); |
| 215 | Assert.AreEqual(1, CodedOutputStream.EncodeZigZag32(-1)); |
| 216 | Assert.AreEqual(2, CodedOutputStream.EncodeZigZag32( 1)); |
| 217 | Assert.AreEqual(3, CodedOutputStream.EncodeZigZag32(-2)); |
| 218 | Assert.AreEqual(0x7FFFFFFE, CodedOutputStream.EncodeZigZag32(0x3FFFFFFF)); |
| 219 | Assert.AreEqual(0x7FFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0xC0000000))); |
| 220 | Assert.AreEqual(0xFFFFFFFE, CodedOutputStream.EncodeZigZag32(0x7FFFFFFF)); |
| 221 | Assert.AreEqual(0xFFFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0x80000000))); |
| 222 | } |
| 223 | |
| 224 | [Test] |
| 225 | public void EncodeZigZag64() { |
| 226 | Assert.AreEqual(0, CodedOutputStream.EncodeZigZag64( 0)); |
| 227 | Assert.AreEqual(1, CodedOutputStream.EncodeZigZag64(-1)); |
| 228 | Assert.AreEqual(2, CodedOutputStream.EncodeZigZag64( 1)); |
| 229 | Assert.AreEqual(3, CodedOutputStream.EncodeZigZag64(-2)); |
| 230 | Assert.AreEqual(0x000000007FFFFFFEL, |
| 231 | CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000003FFFFFFFUL))); |
| 232 | Assert.AreEqual(0x000000007FFFFFFFL, |
| 233 | CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFFC0000000UL))); |
| 234 | Assert.AreEqual(0x00000000FFFFFFFEL, |
| 235 | CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000007FFFFFFFUL))); |
| 236 | Assert.AreEqual(0x00000000FFFFFFFFL, |
| 237 | CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFF80000000UL))); |
| 238 | Assert.AreEqual(0xFFFFFFFFFFFFFFFEL, |
| 239 | CodedOutputStream.EncodeZigZag64(unchecked((long)0x7FFFFFFFFFFFFFFFUL))); |
| 240 | Assert.AreEqual(0xFFFFFFFFFFFFFFFFL, |
| 241 | CodedOutputStream.EncodeZigZag64(unchecked((long)0x8000000000000000UL))); |
| 242 | } |
| 243 | |
| 244 | [Test] |
| 245 | public void RoundTripZigZag32() { |
| 246 | // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1) |
| 247 | // were chosen semi-randomly via keyboard bashing. |
| 248 | Assert.AreEqual(0, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(0))); |
| 249 | Assert.AreEqual(1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(1))); |
| 250 | Assert.AreEqual(-1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-1))); |
| 251 | Assert.AreEqual(14927, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(14927))); |
| 252 | Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-3612))); |
| 253 | } |
| 254 | |
| 255 | [Test] |
| 256 | public void RoundTripZigZag64() { |
| 257 | Assert.AreEqual(0, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(0))); |
| 258 | Assert.AreEqual(1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(1))); |
| 259 | Assert.AreEqual(-1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-1))); |
| 260 | Assert.AreEqual(14927, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(14927))); |
| 261 | Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-3612))); |
| 262 | |
| 263 | Assert.AreEqual(856912304801416L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(856912304801416L))); |
| 264 | Assert.AreEqual(-75123905439571256L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-75123905439571256L))); |
| 265 | } |
| 266 | } |
| 267 | } |