Exemplo n.º 1
0
 public void RoundTripZigZag32()
 {
     // Some easier-to-verify round-trip tests.  The inputs (other than 0, 1, -1)
     // were chosen semi-randomly via keyboard bashing.
     Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(0)));
     Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(1)));
     Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(-1)));
     Assert.AreEqual(14927, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(14927)));
     Assert.AreEqual(-3612, ParsingPrimitives.DecodeZigZag32(WritingPrimitives.EncodeZigZag32(-3612)));
 }
Exemplo n.º 2
0
 public void EncodeZigZag32()
 {
     Assert.AreEqual(0u, WritingPrimitives.EncodeZigZag32(0));
     Assert.AreEqual(1u, WritingPrimitives.EncodeZigZag32(-1));
     Assert.AreEqual(2u, WritingPrimitives.EncodeZigZag32(1));
     Assert.AreEqual(3u, WritingPrimitives.EncodeZigZag32(-2));
     Assert.AreEqual(0x7FFFFFFEu, WritingPrimitives.EncodeZigZag32(0x3FFFFFFF));
     Assert.AreEqual(0x7FFFFFFFu, WritingPrimitives.EncodeZigZag32(unchecked ((int)0xC0000000)));
     Assert.AreEqual(0xFFFFFFFEu, WritingPrimitives.EncodeZigZag32(0x7FFFFFFF));
     Assert.AreEqual(0xFFFFFFFFu, WritingPrimitives.EncodeZigZag32(unchecked ((int)0x80000000)));
 }
Exemplo n.º 3
0
        public void RoundTripZigZag64()
        {
            Assert.AreEqual(0, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(0)));
            Assert.AreEqual(1, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(1)));
            Assert.AreEqual(-1, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-1)));
            Assert.AreEqual(14927, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(14927)));
            Assert.AreEqual(-3612, ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-3612)));

            Assert.AreEqual(856912304801416L,
                            ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(856912304801416L)));
            Assert.AreEqual(-75123905439571256L,
                            ParsingPrimitives.DecodeZigZag64(WritingPrimitives.EncodeZigZag64(-75123905439571256L)));
        }
Exemplo n.º 4
0
 public void EncodeZigZag64()
 {
     Assert.AreEqual(0u, WritingPrimitives.EncodeZigZag64(0));
     Assert.AreEqual(1u, WritingPrimitives.EncodeZigZag64(-1));
     Assert.AreEqual(2u, WritingPrimitives.EncodeZigZag64(1));
     Assert.AreEqual(3u, WritingPrimitives.EncodeZigZag64(-2));
     Assert.AreEqual(0x000000007FFFFFFEuL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0x000000003FFFFFFFUL)));
     Assert.AreEqual(0x000000007FFFFFFFuL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0xFFFFFFFFC0000000UL)));
     Assert.AreEqual(0x00000000FFFFFFFEuL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0x000000007FFFFFFFUL)));
     Assert.AreEqual(0x00000000FFFFFFFFuL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0xFFFFFFFF80000000UL)));
     Assert.AreEqual(0xFFFFFFFFFFFFFFFEL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0x7FFFFFFFFFFFFFFFUL)));
     Assert.AreEqual(0xFFFFFFFFFFFFFFFFL,
                     WritingPrimitives.EncodeZigZag64(unchecked ((long)0x8000000000000000UL)));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Writes an already-encoded tag.
        /// </summary>
        /// <param name="tag">The encoded tag</param>
        public void WriteTag(uint tag)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteTag(ref span, ref state, tag);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Writes a length (in bytes) for length-delimited data.
        /// </summary>
        /// <remarks>
        /// This method simply writes a rawint, but exists for clarity in calling code.
        /// </remarks>
        /// <param name="length">Length value, in bytes.</param>
        public void WriteLength(int length)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteLength(ref span, ref state, length);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Encodes and writes a tag.
        /// </summary>
        /// <param name="fieldNumber">The number of the field to write the tag for</param>
        /// <param name="type">The wire format type of the tag to write</param>
        public void WriteTag(int fieldNumber, WireFormat.WireType type)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteTag(ref span, ref state, fieldNumber, type);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Write a byte string, without a tag, to the stream.
 /// The data is length-prefixed.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteBytes(ByteString value)
 {
     WritingPrimitives.WriteBytes(ref buffer, ref state, value);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Writes an sint64 value, without a tag.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteSInt64(long value)
 {
     WritingPrimitives.WriteSInt64(ref buffer, ref state, value);
 }
Exemplo n.º 10
0
        /// <summary>
        /// Writes a 32 bit value as a varint. The fast route is taken when
        /// there's enough buffer space left to whizz through without checking
        /// for each byte; otherwise, we resort to calling WriteRawByte each time.
        /// </summary>
        internal void WriteRawVarint32(uint value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteRawVarint32(ref span, ref state, value);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Writes out part of an array of bytes.
        /// </summary>
        internal void WriteRawBytes(byte[] value, int offset, int length)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteRawBytes(ref span, ref state, value, offset, length);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Writes a double field value, without a tag, to the stream.
        /// </summary>
        /// <param name="value">The value to write</param>
        public void WriteDouble(double value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteDouble(ref span, ref state, value);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Writes a float field value, without a tag, to the stream.
        /// </summary>
        /// <param name="value">The value to write</param>
        public void WriteFloat(float value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteFloat(ref span, ref state, value);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Writes the given two-byte tag.
 /// </summary>
 /// <param name="b1">The first byte of the encoded tag</param>
 /// <param name="b2">The second byte of the encoded tag</param>
 public void WriteRawTag(byte b1, byte b2)
 {
     WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2);
 }
Exemplo n.º 15
0
 /// <summary>
 /// Writes the given five-byte tag.
 /// </summary>
 /// <param name="b1">The first byte of the encoded tag</param>
 /// <param name="b2">The second byte of the encoded tag</param>
 /// <param name="b3">The third byte of the encoded tag</param>
 /// <param name="b4">The fourth byte of the encoded tag</param>
 /// <param name="b5">The fifth byte of the encoded tag</param>
 public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
 {
     WritingPrimitives.WriteRawTag(ref buffer, ref state, b1, b2, b3, b4, b5);
 }
Exemplo n.º 16
0
 /// <summary>
 /// Writes an already-encoded tag.
 /// </summary>
 /// <param name="tag">The encoded tag</param>
 public void WriteTag(uint tag)
 {
     WritingPrimitives.WriteTag(ref buffer, ref state, tag);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Encodes and writes a tag.
 /// </summary>
 /// <param name="fieldNumber">The number of the field to write the tag for</param>
 /// <param name="type">The wire format type of the tag to write</param>
 public void WriteTag(int fieldNumber, WireFormat.WireType type)
 {
     WritingPrimitives.WriteTag(ref buffer, ref state, fieldNumber, type);
 }
Exemplo n.º 18
0
 /// <summary>
 /// Writes a length (in bytes) for length-delimited data.
 /// </summary>
 /// <remarks>
 /// This method simply writes a rawint, but exists for clarity in calling code.
 /// </remarks>
 /// <param name="length">Length value, in bytes.</param>
 public void WriteLength(int length)
 {
     WritingPrimitives.WriteLength(ref buffer, ref state, length);
 }
Exemplo n.º 19
0
        /// <summary>
        /// Writes the given single-byte tag directly to the stream.
        /// </summary>
        /// <param name="b1">The encoded tag</param>
        public void WriteRawTag(byte b1)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteRawTag(ref span, ref state, b1);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Writes a bool field value, without a tag, to the stream.
        /// </summary>
        /// <param name="value">The value to write</param>
        public void WriteBool(bool value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteBool(ref span, ref state, value);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Writes the given five-byte tag directly to the stream.
        /// </summary>
        /// <param name="b1">The first byte of the encoded tag</param>
        /// <param name="b2">The second byte of the encoded tag</param>
        /// <param name="b3">The third byte of the encoded tag</param>
        /// <param name="b4">The fourth byte of the encoded tag</param>
        /// <param name="b5">The fifth byte of the encoded tag</param>
        public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteRawTag(ref span, ref state, b1, b2, b3, b4, b5);
        }
Exemplo n.º 22
0
 /// <summary>
 /// Writes an enum value, without a tag.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteEnum(int value)
 {
     WritingPrimitives.WriteEnum(ref buffer, ref state, value);
 }
Exemplo n.º 23
0
        internal void WriteRawLittleEndian64(ulong value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteRawLittleEndian64(ref span, ref state, value);
        }
Exemplo n.º 24
0
 /// <summary>
 /// Writes a bool field value, without a tag.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteBool(bool value)
 {
     WritingPrimitives.WriteBool(ref buffer, ref state, value);
 }
Exemplo n.º 25
0
 public static void WriteMessage(ref WriteContext ctx, IMessage value)
 {
     WritingPrimitives.WriteLength(ref ctx.buffer, ref ctx.state, value.CalculateSize());
     WriteRawMessage(ref ctx, value);
 }
Exemplo n.º 26
0
        /// <summary>
        /// Writes an sint64 value, without a tag, to the stream.
        /// </summary>
        /// <param name="value">The value to write</param>
        public void WriteSInt64(long value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteSInt64(ref span, ref state, value);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Write a byte string, without a tag, to the stream.
        /// The data is length-prefixed.
        /// </summary>
        /// <param name="value">The value to write</param>
        public void WriteBytes(ByteString value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteBytes(ref span, ref state, value);
        }
Exemplo n.º 28
0
 /// <summary>
 /// Writes an sint32 value, without a tag.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteSInt32(int value)
 {
     WritingPrimitives.WriteSInt32(ref buffer, ref state, value);
 }
Exemplo n.º 29
0
        /// <summary>
        /// Writes an sfixed32 value, without a tag, to the stream.
        /// </summary>
        /// <param name="value">The value to write.</param>
        public void WriteSFixed32(int value)
        {
            var span = new Span <byte>(buffer);

            WritingPrimitives.WriteSFixed32(ref span, ref state, value);
        }
Exemplo n.º 30
0
 /// <summary>
 /// Writes a fixed32 field value, without a tag.
 /// </summary>
 /// <param name="value">The value to write</param>
 public void WriteFixed32(uint value)
 {
     WritingPrimitives.WriteFixed32(ref buffer, ref state, value);
 }