1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing System;
34ffe3c632Sopenharmony_ciusing System.IO;
35ffe3c632Sopenharmony_ciusing Google.Protobuf.TestProtos;
36ffe3c632Sopenharmony_ciusing Google.Protobuf.Buffers;
37ffe3c632Sopenharmony_ciusing NUnit.Framework;
38ffe3c632Sopenharmony_ci
39ffe3c632Sopenharmony_cinamespace Google.Protobuf
40ffe3c632Sopenharmony_ci{
41ffe3c632Sopenharmony_ci    public class CodedOutputStreamTest
42ffe3c632Sopenharmony_ci    {
43ffe3c632Sopenharmony_ci        /// <summary>
44ffe3c632Sopenharmony_ci        /// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
45ffe3c632Sopenharmony_ci        /// checks that the result matches the given bytes
46ffe3c632Sopenharmony_ci        /// </summary>
47ffe3c632Sopenharmony_ci        private static void AssertWriteVarint(byte[] data, ulong value)
48ffe3c632Sopenharmony_ci        {
49ffe3c632Sopenharmony_ci            // Only do 32-bit write if the value fits in 32 bits.
50ffe3c632Sopenharmony_ci            if ((value >> 32) == 0)
51ffe3c632Sopenharmony_ci            {
52ffe3c632Sopenharmony_ci                // CodedOutputStream
53ffe3c632Sopenharmony_ci                MemoryStream rawOutput = new MemoryStream();
54ffe3c632Sopenharmony_ci                CodedOutputStream output = new CodedOutputStream(rawOutput);
55ffe3c632Sopenharmony_ci                output.WriteRawVarint32((uint) value);
56ffe3c632Sopenharmony_ci                output.Flush();
57ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
58ffe3c632Sopenharmony_ci
59ffe3c632Sopenharmony_ci                // IBufferWriter
60ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
61ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
62ffe3c632Sopenharmony_ci                ctx.WriteUInt32((uint) value);
63ffe3c632Sopenharmony_ci                ctx.Flush();
64ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
65ffe3c632Sopenharmony_ci
66ffe3c632Sopenharmony_ci                // Also try computing size.
67ffe3c632Sopenharmony_ci                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value));
68ffe3c632Sopenharmony_ci            }
69ffe3c632Sopenharmony_ci
70ffe3c632Sopenharmony_ci            {
71ffe3c632Sopenharmony_ci                // CodedOutputStream
72ffe3c632Sopenharmony_ci                MemoryStream rawOutput = new MemoryStream();
73ffe3c632Sopenharmony_ci                CodedOutputStream output = new CodedOutputStream(rawOutput);
74ffe3c632Sopenharmony_ci                output.WriteRawVarint64(value);
75ffe3c632Sopenharmony_ci                output.Flush();
76ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
77ffe3c632Sopenharmony_ci
78ffe3c632Sopenharmony_ci                // IBufferWriter
79ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
80ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
81ffe3c632Sopenharmony_ci                ctx.WriteUInt64(value);
82ffe3c632Sopenharmony_ci                ctx.Flush();
83ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
84ffe3c632Sopenharmony_ci
85ffe3c632Sopenharmony_ci                // Also try computing size.
86ffe3c632Sopenharmony_ci                Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
87ffe3c632Sopenharmony_ci            }
88ffe3c632Sopenharmony_ci
89ffe3c632Sopenharmony_ci            // Try different buffer sizes.
90ffe3c632Sopenharmony_ci            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
91ffe3c632Sopenharmony_ci            {
92ffe3c632Sopenharmony_ci                // Only do 32-bit write if the value fits in 32 bits.
93ffe3c632Sopenharmony_ci                if ((value >> 32) == 0)
94ffe3c632Sopenharmony_ci                {
95ffe3c632Sopenharmony_ci                    MemoryStream rawOutput = new MemoryStream();
96ffe3c632Sopenharmony_ci                    CodedOutputStream output =
97ffe3c632Sopenharmony_ci                        new CodedOutputStream(rawOutput, bufferSize);
98ffe3c632Sopenharmony_ci                    output.WriteRawVarint32((uint) value);
99ffe3c632Sopenharmony_ci                    output.Flush();
100ffe3c632Sopenharmony_ci                    Assert.AreEqual(data, rawOutput.ToArray());
101ffe3c632Sopenharmony_ci
102ffe3c632Sopenharmony_ci                    var bufferWriter = new ArrayBufferWriter<byte>();
103ffe3c632Sopenharmony_ci                    bufferWriter.MaxGrowBy = bufferSize;
104ffe3c632Sopenharmony_ci                    WriteContext.Initialize(bufferWriter, out WriteContext ctx);
105ffe3c632Sopenharmony_ci                    ctx.WriteUInt32((uint) value);
106ffe3c632Sopenharmony_ci                    ctx.Flush();
107ffe3c632Sopenharmony_ci                    Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
108ffe3c632Sopenharmony_ci                }
109ffe3c632Sopenharmony_ci
110ffe3c632Sopenharmony_ci                {
111ffe3c632Sopenharmony_ci                    MemoryStream rawOutput = new MemoryStream();
112ffe3c632Sopenharmony_ci                    CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize);
113ffe3c632Sopenharmony_ci                    output.WriteRawVarint64(value);
114ffe3c632Sopenharmony_ci                    output.Flush();
115ffe3c632Sopenharmony_ci                    Assert.AreEqual(data, rawOutput.ToArray());
116ffe3c632Sopenharmony_ci
117ffe3c632Sopenharmony_ci                    var bufferWriter = new ArrayBufferWriter<byte>();
118ffe3c632Sopenharmony_ci                    bufferWriter.MaxGrowBy = bufferSize;
119ffe3c632Sopenharmony_ci                    WriteContext.Initialize(bufferWriter, out WriteContext ctx);
120ffe3c632Sopenharmony_ci                    ctx.WriteUInt64(value);
121ffe3c632Sopenharmony_ci                    ctx.Flush();
122ffe3c632Sopenharmony_ci                    Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
123ffe3c632Sopenharmony_ci                }
124ffe3c632Sopenharmony_ci
125ffe3c632Sopenharmony_ci            }
126ffe3c632Sopenharmony_ci        }
127ffe3c632Sopenharmony_ci
128ffe3c632Sopenharmony_ci        /// <summary>
129ffe3c632Sopenharmony_ci        /// Tests WriteRawVarint32() and WriteRawVarint64()
130ffe3c632Sopenharmony_ci        /// </summary>
131ffe3c632Sopenharmony_ci        [Test]
132ffe3c632Sopenharmony_ci        public void WriteVarint()
133ffe3c632Sopenharmony_ci        {
134ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0x00}, 0);
135ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0x01}, 1);
136ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0x7f}, 127);
137ffe3c632Sopenharmony_ci            // 14882
138ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0xa2, 0x74}, (0x22 << 0) | (0x74 << 7));
139ffe3c632Sopenharmony_ci            // 2961488830
140ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x0b},
141ffe3c632Sopenharmony_ci                              (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
142ffe3c632Sopenharmony_ci                              (0x0bL << 28));
143ffe3c632Sopenharmony_ci
144ffe3c632Sopenharmony_ci            // 64-bit
145ffe3c632Sopenharmony_ci            // 7256456126
146ffe3c632Sopenharmony_ci            AssertWriteVarint(new byte[] {0xbe, 0xf7, 0x92, 0x84, 0x1b},
147ffe3c632Sopenharmony_ci                              (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
148ffe3c632Sopenharmony_ci                              (0x1bL << 28));
149ffe3c632Sopenharmony_ci            // 41256202580718336
150ffe3c632Sopenharmony_ci            AssertWriteVarint(
151ffe3c632Sopenharmony_ci                new byte[] {0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49},
152ffe3c632Sopenharmony_ci                (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
153ffe3c632Sopenharmony_ci                (0x43UL << 28) | (0x49L << 35) | (0x24UL << 42) | (0x49UL << 49));
154ffe3c632Sopenharmony_ci            // 11964378330978735131
155ffe3c632Sopenharmony_ci            AssertWriteVarint(
156ffe3c632Sopenharmony_ci                new byte[] {0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01},
157ffe3c632Sopenharmony_ci                unchecked((ulong)
158ffe3c632Sopenharmony_ci                          ((0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
159ffe3c632Sopenharmony_ci                           (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
160ffe3c632Sopenharmony_ci                           (0x05L << 49) | (0x26L << 56) | (0x01L << 63))));
161ffe3c632Sopenharmony_ci        }
162ffe3c632Sopenharmony_ci
163ffe3c632Sopenharmony_ci        /// <summary>
164ffe3c632Sopenharmony_ci        /// Parses the given bytes using WriteRawLittleEndian32() and checks
165ffe3c632Sopenharmony_ci        /// that the result matches the given value.
166ffe3c632Sopenharmony_ci        /// </summary>
167ffe3c632Sopenharmony_ci        private static void AssertWriteLittleEndian32(byte[] data, uint value)
168ffe3c632Sopenharmony_ci        {
169ffe3c632Sopenharmony_ci            {
170ffe3c632Sopenharmony_ci                var rawOutput = new MemoryStream();
171ffe3c632Sopenharmony_ci                var output = new CodedOutputStream(rawOutput);
172ffe3c632Sopenharmony_ci                output.WriteRawLittleEndian32(value);
173ffe3c632Sopenharmony_ci                output.Flush();
174ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
175ffe3c632Sopenharmony_ci
176ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
177ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
178ffe3c632Sopenharmony_ci                ctx.WriteFixed32(value);
179ffe3c632Sopenharmony_ci                ctx.Flush();
180ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
181ffe3c632Sopenharmony_ci            }
182ffe3c632Sopenharmony_ci
183ffe3c632Sopenharmony_ci            // Try different buffer sizes.
184ffe3c632Sopenharmony_ci            for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
185ffe3c632Sopenharmony_ci            {
186ffe3c632Sopenharmony_ci                var rawOutput = new MemoryStream();
187ffe3c632Sopenharmony_ci                var output = new CodedOutputStream(rawOutput, bufferSize);
188ffe3c632Sopenharmony_ci                output.WriteRawLittleEndian32(value);
189ffe3c632Sopenharmony_ci                output.Flush();
190ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
191ffe3c632Sopenharmony_ci
192ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
193ffe3c632Sopenharmony_ci                bufferWriter.MaxGrowBy = bufferSize;
194ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
195ffe3c632Sopenharmony_ci                ctx.WriteFixed32(value);
196ffe3c632Sopenharmony_ci                ctx.Flush();
197ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
198ffe3c632Sopenharmony_ci            }
199ffe3c632Sopenharmony_ci        }
200ffe3c632Sopenharmony_ci
201ffe3c632Sopenharmony_ci        /// <summary>
202ffe3c632Sopenharmony_ci        /// Parses the given bytes using WriteRawLittleEndian64() and checks
203ffe3c632Sopenharmony_ci        /// that the result matches the given value.
204ffe3c632Sopenharmony_ci        /// </summary>
205ffe3c632Sopenharmony_ci        private static void AssertWriteLittleEndian64(byte[] data, ulong value)
206ffe3c632Sopenharmony_ci        {
207ffe3c632Sopenharmony_ci            {
208ffe3c632Sopenharmony_ci                var rawOutput = new MemoryStream();
209ffe3c632Sopenharmony_ci                var output = new CodedOutputStream(rawOutput);
210ffe3c632Sopenharmony_ci                output.WriteRawLittleEndian64(value);
211ffe3c632Sopenharmony_ci                output.Flush();
212ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
213ffe3c632Sopenharmony_ci
214ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
215ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
216ffe3c632Sopenharmony_ci                ctx.WriteFixed64(value);
217ffe3c632Sopenharmony_ci                ctx.Flush();
218ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
219ffe3c632Sopenharmony_ci            }
220ffe3c632Sopenharmony_ci
221ffe3c632Sopenharmony_ci            // Try different block sizes.
222ffe3c632Sopenharmony_ci            for (int blockSize = 1; blockSize <= 16; blockSize *= 2)
223ffe3c632Sopenharmony_ci            {
224ffe3c632Sopenharmony_ci                var rawOutput = new MemoryStream();
225ffe3c632Sopenharmony_ci                var output = new CodedOutputStream(rawOutput, blockSize);
226ffe3c632Sopenharmony_ci                output.WriteRawLittleEndian64(value);
227ffe3c632Sopenharmony_ci                output.Flush();
228ffe3c632Sopenharmony_ci                Assert.AreEqual(data, rawOutput.ToArray());
229ffe3c632Sopenharmony_ci
230ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
231ffe3c632Sopenharmony_ci                bufferWriter.MaxGrowBy = blockSize;
232ffe3c632Sopenharmony_ci                WriteContext.Initialize(bufferWriter, out WriteContext ctx);
233ffe3c632Sopenharmony_ci                ctx.WriteFixed64(value);
234ffe3c632Sopenharmony_ci                ctx.Flush();
235ffe3c632Sopenharmony_ci                Assert.AreEqual(data, bufferWriter.WrittenSpan.ToArray());
236ffe3c632Sopenharmony_ci            }
237ffe3c632Sopenharmony_ci        }
238ffe3c632Sopenharmony_ci
239ffe3c632Sopenharmony_ci        /// <summary>
240ffe3c632Sopenharmony_ci        /// Tests writeRawLittleEndian32() and writeRawLittleEndian64().
241ffe3c632Sopenharmony_ci        /// </summary>
242ffe3c632Sopenharmony_ci        [Test]
243ffe3c632Sopenharmony_ci        public void WriteLittleEndian()
244ffe3c632Sopenharmony_ci        {
245ffe3c632Sopenharmony_ci            AssertWriteLittleEndian32(new byte[] {0x78, 0x56, 0x34, 0x12}, 0x12345678);
246ffe3c632Sopenharmony_ci            AssertWriteLittleEndian32(new byte[] {0xf0, 0xde, 0xbc, 0x9a}, 0x9abcdef0);
247ffe3c632Sopenharmony_ci
248ffe3c632Sopenharmony_ci            AssertWriteLittleEndian64(
249ffe3c632Sopenharmony_ci                new byte[] {0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12},
250ffe3c632Sopenharmony_ci                0x123456789abcdef0L);
251ffe3c632Sopenharmony_ci            AssertWriteLittleEndian64(
252ffe3c632Sopenharmony_ci                new byte[] {0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a},
253ffe3c632Sopenharmony_ci                0x9abcdef012345678UL);
254ffe3c632Sopenharmony_ci        }
255ffe3c632Sopenharmony_ci
256ffe3c632Sopenharmony_ci        [Test]
257ffe3c632Sopenharmony_ci        public void WriteWholeMessage_VaryingBlockSizes()
258ffe3c632Sopenharmony_ci        {
259ffe3c632Sopenharmony_ci            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();
260ffe3c632Sopenharmony_ci
261ffe3c632Sopenharmony_ci            byte[] rawBytes = message.ToByteArray();
262ffe3c632Sopenharmony_ci
263ffe3c632Sopenharmony_ci            // Try different block sizes.
264ffe3c632Sopenharmony_ci            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
265ffe3c632Sopenharmony_ci            {
266ffe3c632Sopenharmony_ci                MemoryStream rawOutput = new MemoryStream();
267ffe3c632Sopenharmony_ci                CodedOutputStream output = new CodedOutputStream(rawOutput, blockSize);
268ffe3c632Sopenharmony_ci                message.WriteTo(output);
269ffe3c632Sopenharmony_ci                output.Flush();
270ffe3c632Sopenharmony_ci                Assert.AreEqual(rawBytes, rawOutput.ToArray());
271ffe3c632Sopenharmony_ci
272ffe3c632Sopenharmony_ci                var bufferWriter = new ArrayBufferWriter<byte>();
273ffe3c632Sopenharmony_ci                bufferWriter.MaxGrowBy = blockSize;
274ffe3c632Sopenharmony_ci                message.WriteTo(bufferWriter);
275ffe3c632Sopenharmony_ci                Assert.AreEqual(rawBytes, bufferWriter.WrittenSpan.ToArray());
276ffe3c632Sopenharmony_ci            }
277ffe3c632Sopenharmony_ci        }
278ffe3c632Sopenharmony_ci
279ffe3c632Sopenharmony_ci        [Test]
280ffe3c632Sopenharmony_ci        public void WriteContext_WritesWithFlushes()
281ffe3c632Sopenharmony_ci        {
282ffe3c632Sopenharmony_ci            TestAllTypes message = SampleMessages.CreateFullTestAllTypes();
283ffe3c632Sopenharmony_ci
284ffe3c632Sopenharmony_ci            MemoryStream expectedOutput = new MemoryStream();
285ffe3c632Sopenharmony_ci            CodedOutputStream output = new CodedOutputStream(expectedOutput);
286ffe3c632Sopenharmony_ci            output.WriteMessage(message);
287ffe3c632Sopenharmony_ci            output.Flush();
288ffe3c632Sopenharmony_ci            byte[] expectedBytes1 = expectedOutput.ToArray();
289ffe3c632Sopenharmony_ci
290ffe3c632Sopenharmony_ci            output.WriteMessage(message);
291ffe3c632Sopenharmony_ci            output.Flush();
292ffe3c632Sopenharmony_ci            byte[] expectedBytes2 = expectedOutput.ToArray();
293ffe3c632Sopenharmony_ci
294ffe3c632Sopenharmony_ci            var bufferWriter = new ArrayBufferWriter<byte>();
295ffe3c632Sopenharmony_ci            WriteContext.Initialize(bufferWriter, out WriteContext ctx);
296ffe3c632Sopenharmony_ci            ctx.WriteMessage(message);
297ffe3c632Sopenharmony_ci            ctx.Flush();
298ffe3c632Sopenharmony_ci            Assert.AreEqual(expectedBytes1, bufferWriter.WrittenSpan.ToArray());
299ffe3c632Sopenharmony_ci
300ffe3c632Sopenharmony_ci            ctx.WriteMessage(message);
301ffe3c632Sopenharmony_ci            ctx.Flush();
302ffe3c632Sopenharmony_ci            Assert.AreEqual(expectedBytes2, bufferWriter.WrittenSpan.ToArray());
303ffe3c632Sopenharmony_ci        }
304ffe3c632Sopenharmony_ci
305ffe3c632Sopenharmony_ci        [Test]
306ffe3c632Sopenharmony_ci        public void EncodeZigZag32()
307ffe3c632Sopenharmony_ci        {
308ffe3c632Sopenharmony_ci            Assert.AreEqual(0u, WritingPrimitives.EncodeZigZag32(0));
309ffe3c632Sopenharmony_ci            Assert.AreEqual(1u, WritingPrimitives.EncodeZigZag32(-1));
310ffe3c632Sopenharmony_ci            Assert.AreEqual(2u, WritingPrimitives.EncodeZigZag32(1));
311ffe3c632Sopenharmony_ci            Assert.AreEqual(3u, WritingPrimitives.EncodeZigZag32(-2));
312ffe3c632Sopenharmony_ci            Assert.AreEqual(0x7FFFFFFEu, WritingPrimitives.EncodeZigZag32(0x3FFFFFFF));
313ffe3c632Sopenharmony_ci            Assert.AreEqual(0x7FFFFFFFu, WritingPrimitives.EncodeZigZag32(unchecked((int) 0xC0000000)));
314ffe3c632Sopenharmony_ci            Assert.AreEqual(0xFFFFFFFEu, WritingPrimitives.EncodeZigZag32(0x7FFFFFFF));
315ffe3c632Sopenharmony_ci            Assert.AreEqual(0xFFFFFFFFu, WritingPrimitives.EncodeZigZag32(unchecked((int) 0x80000000)));
316ffe3c632Sopenharmony_ci        }
317ffe3c632Sopenharmony_ci
318ffe3c632Sopenharmony_ci        [Test]
319ffe3c632Sopenharmony_ci        public void EncodeZigZag64()
320ffe3c632Sopenharmony_ci        {
321ffe3c632Sopenharmony_ci            Assert.AreEqual(0u, WritingPrimitives.EncodeZigZag64(0));
322ffe3c632Sopenharmony_ci            Assert.AreEqual(1u, WritingPrimitives.EncodeZigZag64(-1));
323ffe3c632Sopenharmony_ci            Assert.AreEqual(2u, WritingPrimitives.EncodeZigZag64(1));
324ffe3c632Sopenharmony_ci            Assert.AreEqual(3u, WritingPrimitives.EncodeZigZag64(-2));
325ffe3c632Sopenharmony_ci            Assert.AreEqual(0x000000007FFFFFFEuL,
326ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0x000000003FFFFFFFUL)));
327ffe3c632Sopenharmony_ci            Assert.AreEqual(0x000000007FFFFFFFuL,
328ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0xFFFFFFFFC0000000UL)));
329ffe3c632Sopenharmony_ci            Assert.AreEqual(0x00000000FFFFFFFEuL,
330ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0x000000007FFFFFFFUL)));
331ffe3c632Sopenharmony_ci            Assert.AreEqual(0x00000000FFFFFFFFuL,
332ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0xFFFFFFFF80000000UL)));
333ffe3c632Sopenharmony_ci            Assert.AreEqual(0xFFFFFFFFFFFFFFFEL,
334ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0x7FFFFFFFFFFFFFFFUL)));
335ffe3c632Sopenharmony_ci            Assert.AreEqual(0xFFFFFFFFFFFFFFFFL,
336ffe3c632Sopenharmony_ci                            WritingPrimitives.EncodeZigZag64(unchecked((long) 0x8000000000000000UL)));
337ffe3c632Sopenharmony_ci        }
338ffe3c632Sopenharmony_ci
339ffe3c632Sopenharmony_ci        [Test]
340ffe3c632Sopenharmony_ci        public void RoundTripZigZag32()
341ffe3c632Sopenharmony_ci        {
342ffe3c632Sopenharmony_ci            // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
343ffe3c632Sopenharmony_ci            // were chosen semi-randomly via keyboard bashing.
344ffe3c632Sopenharmony_ci            Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(0)));
345ffe3c632Sopenharmony_ci            Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(1)));
346ffe3c632Sopenharmony_ci            Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(-1)));
347ffe3c632Sopenharmony_ci            Assert.AreEqual(14927, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(14927)));
348ffe3c632Sopenharmony_ci            Assert.AreEqual(-3612, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(-3612)));
349ffe3c632Sopenharmony_ci        }
350ffe3c632Sopenharmony_ci
351ffe3c632Sopenharmony_ci        [Test]
352ffe3c632Sopenharmony_ci        public void RoundTripZigZag64()
353ffe3c632Sopenharmony_ci        {
354ffe3c632Sopenharmony_ci            Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(0)));
355ffe3c632Sopenharmony_ci            Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(1)));
356ffe3c632Sopenharmony_ci            Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-1)));
357ffe3c632Sopenharmony_ci            Assert.AreEqual(14927, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(14927)));
358ffe3c632Sopenharmony_ci            Assert.AreEqual(-3612, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-3612)));
359ffe3c632Sopenharmony_ci
360ffe3c632Sopenharmony_ci            Assert.AreEqual(856912304801416L,
361ffe3c632Sopenharmony_ci                            ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(856912304801416L)));
362ffe3c632Sopenharmony_ci            Assert.AreEqual(-75123905439571256L,
363ffe3c632Sopenharmony_ci                            ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-75123905439571256L)));
364ffe3c632Sopenharmony_ci        }
365ffe3c632Sopenharmony_ci
366ffe3c632Sopenharmony_ci        [Test]
367ffe3c632Sopenharmony_ci        public void TestNegativeEnumNoTag()
368ffe3c632Sopenharmony_ci        {
369ffe3c632Sopenharmony_ci            Assert.AreEqual(10, CodedOutputStream.ComputeInt32Size(-2));
370ffe3c632Sopenharmony_ci            Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue));
371ffe3c632Sopenharmony_ci
372ffe3c632Sopenharmony_ci            byte[] bytes = new byte[10];
373ffe3c632Sopenharmony_ci            CodedOutputStream output = new CodedOutputStream(bytes);
374ffe3c632Sopenharmony_ci            output.WriteEnum((int) SampleEnum.NegativeValue);
375ffe3c632Sopenharmony_ci
376ffe3c632Sopenharmony_ci            Assert.AreEqual(0, output.SpaceLeft);
377ffe3c632Sopenharmony_ci            Assert.AreEqual("FE-FF-FF-FF-FF-FF-FF-FF-FF-01", BitConverter.ToString(bytes));
378ffe3c632Sopenharmony_ci        }
379ffe3c632Sopenharmony_ci
380ffe3c632Sopenharmony_ci        [Test]
381ffe3c632Sopenharmony_ci        public void TestCodedInputOutputPosition()
382ffe3c632Sopenharmony_ci        {
383ffe3c632Sopenharmony_ci            byte[] content = new byte[110];
384ffe3c632Sopenharmony_ci            for (int i = 0; i < content.Length; i++)
385ffe3c632Sopenharmony_ci                content[i] = (byte)i;
386ffe3c632Sopenharmony_ci
387ffe3c632Sopenharmony_ci            byte[] child = new byte[120];
388ffe3c632Sopenharmony_ci            {
389ffe3c632Sopenharmony_ci                MemoryStream ms = new MemoryStream(child);
390ffe3c632Sopenharmony_ci                CodedOutputStream cout = new CodedOutputStream(ms, 20);
391ffe3c632Sopenharmony_ci                // Field 11: numeric value: 500
392ffe3c632Sopenharmony_ci                cout.WriteTag(11, WireFormat.WireType.Varint);
393ffe3c632Sopenharmony_ci                Assert.AreEqual(1, cout.Position);
394ffe3c632Sopenharmony_ci                cout.WriteInt32(500);
395ffe3c632Sopenharmony_ci                Assert.AreEqual(3, cout.Position);
396ffe3c632Sopenharmony_ci                //Field 12: length delimited 120 bytes
397ffe3c632Sopenharmony_ci                cout.WriteTag(12, WireFormat.WireType.LengthDelimited);
398ffe3c632Sopenharmony_ci                Assert.AreEqual(4, cout.Position);
399ffe3c632Sopenharmony_ci                cout.WriteBytes(ByteString.CopyFrom(content));
400ffe3c632Sopenharmony_ci                Assert.AreEqual(115, cout.Position);
401ffe3c632Sopenharmony_ci                // Field 13: fixed numeric value: 501
402ffe3c632Sopenharmony_ci                cout.WriteTag(13, WireFormat.WireType.Fixed32);
403ffe3c632Sopenharmony_ci                Assert.AreEqual(116, cout.Position);
404ffe3c632Sopenharmony_ci                cout.WriteSFixed32(501);
405ffe3c632Sopenharmony_ci                Assert.AreEqual(120, cout.Position);
406ffe3c632Sopenharmony_ci                cout.Flush();
407ffe3c632Sopenharmony_ci            }
408ffe3c632Sopenharmony_ci
409ffe3c632Sopenharmony_ci            byte[] bytes = new byte[130];
410ffe3c632Sopenharmony_ci            {
411ffe3c632Sopenharmony_ci                CodedOutputStream cout = new CodedOutputStream(bytes);
412ffe3c632Sopenharmony_ci                // Field 1: numeric value: 500
413ffe3c632Sopenharmony_ci                cout.WriteTag(1, WireFormat.WireType.Varint);
414ffe3c632Sopenharmony_ci                Assert.AreEqual(1, cout.Position);
415ffe3c632Sopenharmony_ci                cout.WriteInt32(500);
416ffe3c632Sopenharmony_ci                Assert.AreEqual(3, cout.Position);
417ffe3c632Sopenharmony_ci                //Field 2: length delimited 120 bytes
418ffe3c632Sopenharmony_ci                cout.WriteTag(2, WireFormat.WireType.LengthDelimited);
419ffe3c632Sopenharmony_ci                Assert.AreEqual(4, cout.Position);
420ffe3c632Sopenharmony_ci                cout.WriteBytes(ByteString.CopyFrom(child));
421ffe3c632Sopenharmony_ci                Assert.AreEqual(125, cout.Position);
422ffe3c632Sopenharmony_ci                // Field 3: fixed numeric value: 500
423ffe3c632Sopenharmony_ci                cout.WriteTag(3, WireFormat.WireType.Fixed32);
424ffe3c632Sopenharmony_ci                Assert.AreEqual(126, cout.Position);
425ffe3c632Sopenharmony_ci                cout.WriteSFixed32(501);
426ffe3c632Sopenharmony_ci                Assert.AreEqual(130, cout.Position);
427ffe3c632Sopenharmony_ci                cout.Flush();
428ffe3c632Sopenharmony_ci            }
429ffe3c632Sopenharmony_ci            // Now test Input stream:
430ffe3c632Sopenharmony_ci            {
431ffe3c632Sopenharmony_ci                CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0, false);
432ffe3c632Sopenharmony_ci                Assert.AreEqual(0, cin.Position);
433ffe3c632Sopenharmony_ci                // Field 1:
434ffe3c632Sopenharmony_ci                uint tag = cin.ReadTag();
435ffe3c632Sopenharmony_ci                Assert.AreEqual(1, tag >> 3);
436ffe3c632Sopenharmony_ci                Assert.AreEqual(1, cin.Position);
437ffe3c632Sopenharmony_ci                Assert.AreEqual(500, cin.ReadInt32());
438ffe3c632Sopenharmony_ci                Assert.AreEqual(3, cin.Position);
439ffe3c632Sopenharmony_ci                //Field 2:
440ffe3c632Sopenharmony_ci                tag = cin.ReadTag();
441ffe3c632Sopenharmony_ci                Assert.AreEqual(2, tag >> 3);
442ffe3c632Sopenharmony_ci                Assert.AreEqual(4, cin.Position);
443ffe3c632Sopenharmony_ci                int childlen = cin.ReadLength();
444ffe3c632Sopenharmony_ci                Assert.AreEqual(120, childlen);
445ffe3c632Sopenharmony_ci                Assert.AreEqual(5, cin.Position);
446ffe3c632Sopenharmony_ci                int oldlimit = cin.PushLimit((int)childlen);
447ffe3c632Sopenharmony_ci                Assert.AreEqual(5, cin.Position);
448ffe3c632Sopenharmony_ci                // Now we are reading child message
449ffe3c632Sopenharmony_ci                {
450ffe3c632Sopenharmony_ci                    // Field 11: numeric value: 500
451ffe3c632Sopenharmony_ci                    tag = cin.ReadTag();
452ffe3c632Sopenharmony_ci                    Assert.AreEqual(11, tag >> 3);
453ffe3c632Sopenharmony_ci                    Assert.AreEqual(6, cin.Position);
454ffe3c632Sopenharmony_ci                    Assert.AreEqual(500, cin.ReadInt32());
455ffe3c632Sopenharmony_ci                    Assert.AreEqual(8, cin.Position);
456ffe3c632Sopenharmony_ci                    //Field 12: length delimited 120 bytes
457ffe3c632Sopenharmony_ci                    tag = cin.ReadTag();
458ffe3c632Sopenharmony_ci                    Assert.AreEqual(12, tag >> 3);
459ffe3c632Sopenharmony_ci                    Assert.AreEqual(9, cin.Position);
460ffe3c632Sopenharmony_ci                    ByteString bstr = cin.ReadBytes();
461ffe3c632Sopenharmony_ci                    Assert.AreEqual(110, bstr.Length);
462ffe3c632Sopenharmony_ci                    Assert.AreEqual((byte) 109, bstr[109]);
463ffe3c632Sopenharmony_ci                    Assert.AreEqual(120, cin.Position);
464ffe3c632Sopenharmony_ci                    // Field 13: fixed numeric value: 501
465ffe3c632Sopenharmony_ci                    tag = cin.ReadTag();
466ffe3c632Sopenharmony_ci                    Assert.AreEqual(13, tag >> 3);
467ffe3c632Sopenharmony_ci                    // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit
468ffe3c632Sopenharmony_ci                    Assert.AreEqual(121, cin.Position);
469ffe3c632Sopenharmony_ci                    Assert.AreEqual(501, cin.ReadSFixed32());
470ffe3c632Sopenharmony_ci                    Assert.AreEqual(125, cin.Position);
471ffe3c632Sopenharmony_ci                    Assert.IsTrue(cin.IsAtEnd);
472ffe3c632Sopenharmony_ci                }
473ffe3c632Sopenharmony_ci                cin.PopLimit(oldlimit);
474ffe3c632Sopenharmony_ci                Assert.AreEqual(125, cin.Position);
475ffe3c632Sopenharmony_ci                // Field 3: fixed numeric value: 501
476ffe3c632Sopenharmony_ci                tag = cin.ReadTag();
477ffe3c632Sopenharmony_ci                Assert.AreEqual(3, tag >> 3);
478ffe3c632Sopenharmony_ci                Assert.AreEqual(126, cin.Position);
479ffe3c632Sopenharmony_ci                Assert.AreEqual(501, cin.ReadSFixed32());
480ffe3c632Sopenharmony_ci                Assert.AreEqual(130, cin.Position);
481ffe3c632Sopenharmony_ci                Assert.IsTrue(cin.IsAtEnd);
482ffe3c632Sopenharmony_ci            }
483ffe3c632Sopenharmony_ci        }
484ffe3c632Sopenharmony_ci
485ffe3c632Sopenharmony_ci        [Test]
486ffe3c632Sopenharmony_ci        public void Dispose_DisposesUnderlyingStream()
487ffe3c632Sopenharmony_ci        {
488ffe3c632Sopenharmony_ci            var memoryStream = new MemoryStream();
489ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanWrite);
490ffe3c632Sopenharmony_ci            using (var cos = new CodedOutputStream(memoryStream))
491ffe3c632Sopenharmony_ci            {
492ffe3c632Sopenharmony_ci                cos.WriteRawBytes(new byte[] {0});
493ffe3c632Sopenharmony_ci                Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
494ffe3c632Sopenharmony_ci            }
495ffe3c632Sopenharmony_ci            Assert.AreEqual(1, memoryStream.ToArray().Length); // Flushed data from CodedOutputStream to MemoryStream
496ffe3c632Sopenharmony_ci            Assert.IsFalse(memoryStream.CanWrite); // Disposed
497ffe3c632Sopenharmony_ci        }
498ffe3c632Sopenharmony_ci
499ffe3c632Sopenharmony_ci        [Test]
500ffe3c632Sopenharmony_ci        public void Dispose_WithLeaveOpen()
501ffe3c632Sopenharmony_ci        {
502ffe3c632Sopenharmony_ci            var memoryStream = new MemoryStream();
503ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanWrite);
504ffe3c632Sopenharmony_ci            using (var cos = new CodedOutputStream(memoryStream, true))
505ffe3c632Sopenharmony_ci            {
506ffe3c632Sopenharmony_ci                cos.WriteRawBytes(new byte[] {0});
507ffe3c632Sopenharmony_ci                Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
508ffe3c632Sopenharmony_ci            }
509ffe3c632Sopenharmony_ci            Assert.AreEqual(1, memoryStream.Position); // Flushed data from CodedOutputStream to MemoryStream
510ffe3c632Sopenharmony_ci            Assert.IsTrue(memoryStream.CanWrite); // We left the stream open
511ffe3c632Sopenharmony_ci        }
512ffe3c632Sopenharmony_ci
513ffe3c632Sopenharmony_ci        [Test]
514ffe3c632Sopenharmony_ci        public void Dispose_FromByteArray()
515ffe3c632Sopenharmony_ci        {
516ffe3c632Sopenharmony_ci            var stream = new CodedOutputStream(new byte[10]);
517ffe3c632Sopenharmony_ci            stream.Dispose();
518ffe3c632Sopenharmony_ci        }
519ffe3c632Sopenharmony_ci    }
520ffe3c632Sopenharmony_ci}