CodedOutputStreamTest.cs 10 KB

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