CodedOutputStreamTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System.IO;
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc.
  4. // http://code.google.com/p/protobuf/
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. using Google.ProtocolBuffers.TestProtos;
  18. using NUnit.Framework;
  19. namespace Google.ProtocolBuffers {
  20. [TestFixture]
  21. public class CodedOutputStreamTest {
  22. private static void AssertEqualBytes(byte[] a, byte[] b) {
  23. Assert.AreEqual(ByteString.CopyFrom(a), ByteString.CopyFrom(b));
  24. }
  25. /// <summary>
  26. /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
  27. /// checks that the result matches the given bytes
  28. /// </summary>
  29. private static void AssertWriteVarint(byte[] data, ulong value) {
  30. // Only do 32-bit write if the value fits in 32 bits.
  31. if ((value >> 32) == 0) {
  32. MemoryStream rawOutput = new MemoryStream();
  33. CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
  34. output.WriteRawVarint32((uint) value);
  35. output.Flush();
  36. Assert.AreEqual(data, rawOutput.ToArray());
  37. // Also try computing size.
  38. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value));
  39. }
  40. {
  41. MemoryStream rawOutput = new MemoryStream();
  42. CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
  43. output.WriteRawVarint64(value);
  44. output.Flush();
  45. Assert.AreEqual(data, rawOutput.ToArray());
  46. // Also try computing size.
  47. Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
  48. }
  49. // Try different buffer sizes.
  50. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) {
  51. // Only do 32-bit write if the value fits in 32 bits.
  52. if ((value >> 32) == 0) {
  53. MemoryStream rawOutput = new MemoryStream();
  54. CodedOutputStream output =
  55. CodedOutputStream.CreateInstance(rawOutput, bufferSize);
  56. output.WriteRawVarint32((uint) value);
  57. output.Flush();
  58. Assert.AreEqual(data, rawOutput.ToArray());
  59. }
  60. {
  61. MemoryStream rawOutput = new MemoryStream();
  62. CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
  63. output.WriteRawVarint64(value);
  64. output.Flush();
  65. Assert.AreEqual(data, rawOutput.ToArray());
  66. }
  67. }
  68. }
  69. /// <summary>
  70. /// Tests WriteRawVarint32() and WriteRawVarint64()
  71. /// </summary>
  72. [Test]
  73. public void WriteVarint() {
  74. AssertWriteVarint(new byte[] {0x00}, 0);
  75. AssertWriteVarint(new byte[] {0x01}, 1);
  76. AssertWriteVarint(new byte[] {0x7f}, 127);
  77. // 14882
  78. AssertWriteVarint(new byte[] {0xa2, 0x74}, (0x22 << 0) | (0x74 << 7));
  79. // 2961488830
  80. AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x0b},
  81. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  82. (0x0bL << 28));
  83. // 64-bit
  84. // 7256456126
  85. AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x1b},
  86. (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
  87. (0x1bL << 28));
  88. // 41256202580718336
  89. AssertWriteVarint(
  90. new byte[] {0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
  91. (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
  92. (0x43UL << 28) | (0x49L << 35) | (0x24UL << 42) | (0x49UL << 49));
  93. // 11964378330978735131
  94. AssertWriteVarint(
  95. new byte[] {0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
  96. unchecked((ulong)
  97. ((0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
  98. (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
  99. (0x05L << 49) | (0x26L << 56) | (0x01L << 63))));
  100. }
  101. /// <summary>
  102. /// Parses the given bytes using WriteRawLittleEndian32() and checks
  103. /// that the result matches the given value.
  104. /// </summary>
  105. private static void AssertWriteLittleEndian32(byte[] data, uint value) {
  106. MemoryStream rawOutput = new MemoryStream();
  107. CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
  108. output.WriteRawLittleEndian32(value);
  109. output.Flush();
  110. Assert.AreEqual(data, rawOutput.ToArray());
  111. // Try different buffer sizes.
  112. for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2) {
  113. rawOutput = new MemoryStream();
  114. output = CodedOutputStream.CreateInstance(rawOutput, bufferSize);
  115. output.WriteRawLittleEndian32(value);
  116. output.Flush();
  117. Assert.AreEqual(data, rawOutput.ToArray());
  118. }
  119. }
  120. /// <summary>
  121. /// Parses the given bytes using WriteRawLittleEndian64() and checks
  122. /// that the result matches the given value.
  123. /// </summary>
  124. private static void AssertWriteLittleEndian64(byte[] data, ulong value) {
  125. MemoryStream rawOutput = new MemoryStream();
  126. CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput);
  127. output.WriteRawLittleEndian64(value);
  128. output.Flush();
  129. Assert.AreEqual(data, rawOutput.ToArray());
  130. // Try different block sizes.
  131. for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
  132. rawOutput = new MemoryStream();
  133. output = CodedOutputStream.CreateInstance(rawOutput, blockSize);
  134. output.WriteRawLittleEndian64(value);
  135. output.Flush();
  136. Assert.AreEqual(data, rawOutput.ToArray());
  137. }
  138. }
  139. /// <summary>
  140. /// Tests writeRawLittleEndian32() and writeRawLittleEndian64().
  141. /// </summary>
  142. [Test]
  143. public void WriteLittleEndian() {
  144. AssertWriteLittleEndian32(new byte[] {0x78, 0x56, 0x34, 0x12}, 0x12345678);
  145. AssertWriteLittleEndian32(new byte[] {0xf0, 0xde, 0xbc, 0x9a}, 0x9abcdef0);
  146. AssertWriteLittleEndian64(
  147. new byte[]{0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12},
  148. 0x123456789abcdef0L);
  149. AssertWriteLittleEndian64(
  150. new byte[]{0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a},
  151. 0x9abcdef012345678UL);
  152. }
  153. [Test]
  154. public void WriteWholeMessage() {
  155. TestAllTypes message = TestUtil.GetAllSet();
  156. byte[] rawBytes = message.ToByteArray();
  157. AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);
  158. // Try different block sizes.
  159. for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
  160. MemoryStream rawOutput = new MemoryStream();
  161. CodedOutputStream output =
  162. CodedOutputStream.CreateInstance(rawOutput, blockSize);
  163. message.WriteTo(output);
  164. output.Flush();
  165. AssertEqualBytes(rawBytes, rawOutput.ToArray());
  166. }
  167. }
  168. [Test]
  169. public void EncodeZigZag32() {
  170. Assert.AreEqual(0, CodedOutputStream.EncodeZigZag32( 0));
  171. Assert.AreEqual(1, CodedOutputStream.EncodeZigZag32(-1));
  172. Assert.AreEqual(2, CodedOutputStream.EncodeZigZag32( 1));
  173. Assert.AreEqual(3, CodedOutputStream.EncodeZigZag32(-2));
  174. Assert.AreEqual(0x7FFFFFFE, CodedOutputStream.EncodeZigZag32(0x3FFFFFFF));
  175. Assert.AreEqual(0x7FFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0xC0000000)));
  176. Assert.AreEqual(0xFFFFFFFE, CodedOutputStream.EncodeZigZag32(0x7FFFFFFF));
  177. Assert.AreEqual(0xFFFFFFFF, CodedOutputStream.EncodeZigZag32(unchecked((int)0x80000000)));
  178. }
  179. [Test]
  180. public void EncodeZigZag64() {
  181. Assert.AreEqual(0, CodedOutputStream.EncodeZigZag64( 0));
  182. Assert.AreEqual(1, CodedOutputStream.EncodeZigZag64(-1));
  183. Assert.AreEqual(2, CodedOutputStream.EncodeZigZag64( 1));
  184. Assert.AreEqual(3, CodedOutputStream.EncodeZigZag64(-2));
  185. Assert.AreEqual(0x000000007FFFFFFEL,
  186. CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000003FFFFFFFUL)));
  187. Assert.AreEqual(0x000000007FFFFFFFL,
  188. CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFFC0000000UL)));
  189. Assert.AreEqual(0x00000000FFFFFFFEL,
  190. CodedOutputStream.EncodeZigZag64(unchecked((long)0x000000007FFFFFFFUL)));
  191. Assert.AreEqual(0x00000000FFFFFFFFL,
  192. CodedOutputStream.EncodeZigZag64(unchecked((long)0xFFFFFFFF80000000UL)));
  193. Assert.AreEqual(0xFFFFFFFFFFFFFFFEL,
  194. CodedOutputStream.EncodeZigZag64(unchecked((long)0x7FFFFFFFFFFFFFFFUL)));
  195. Assert.AreEqual(0xFFFFFFFFFFFFFFFFL,
  196. CodedOutputStream.EncodeZigZag64(unchecked((long)0x8000000000000000UL)));
  197. }
  198. [Test]
  199. public void RoundTripZigZag32() {
  200. // Some easier-to-verify round-trip tests. The inputs (other than 0, 1, -1)
  201. // were chosen semi-randomly via keyboard bashing.
  202. Assert.AreEqual(0, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(0)));
  203. Assert.AreEqual(1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(1)));
  204. Assert.AreEqual(-1, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-1)));
  205. Assert.AreEqual(14927, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(14927)));
  206. Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag32(CodedOutputStream.EncodeZigZag32(-3612)));
  207. }
  208. [Test]
  209. public void RoundTripZigZag64() {
  210. Assert.AreEqual(0, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(0)));
  211. Assert.AreEqual(1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(1)));
  212. Assert.AreEqual(-1, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-1)));
  213. Assert.AreEqual(14927, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(14927)));
  214. Assert.AreEqual(-3612, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-3612)));
  215. Assert.AreEqual(856912304801416L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(856912304801416L)));
  216. Assert.AreEqual(-75123905439571256L, CodedInputStream.DecodeZigZag64(CodedOutputStream.EncodeZigZag64(-75123905439571256L)));
  217. }
  218. }
  219. }