CodedOutputStreamTest.cs 11 KB

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