public void TestNegativeEnumArray()
        {
            int arraySize = 1 + 1 + (11 * 5);
            int msgSize   = arraySize;

            byte[]            bytes  = new byte[msgSize];
            CodedOutputStream output = new CodedOutputStream(bytes);
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);

            for (int i = 0; i >= -5; i--)
            {
                output.WriteTag(tag);
                output.WriteEnum(i);
            }

            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = new CodedInputStream(bytes);

            tag = input.ReadTag();

            RepeatedField <SampleEnum> values = new RepeatedField <SampleEnum>();

            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }
        public void AddEntriesFrom_Message()
        {
            var message1 = new ForeignMessage {
                C = 2000
            };
            var message2 = new ForeignMessage {
                C = -250
            };

            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteMessage(message1);
            output.WriteTag(tag);
            output.WriteMessage(message2);
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField <ForeignMessage>();
            var input = new CodedInputStream(stream);

            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser));
            CollectionAssert.AreEqual(new[] { message1, message2 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
        public void AddEntriesFrom_NonPackedInt32()
        {
            uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint);
            var  stream       = new MemoryStream();
            var  output       = new CodedOutputStream(stream);

            output.WriteTag(nonPackedTag);
            output.WriteInt32(10);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(999);
            output.WriteTag(nonPackedTag);
            output.WriteInt32(-1000); // Just for variety...
            output.Flush();
            stream.Position = 0;

            // Deliberately "expecting" a packed tag, but we detect that the data is
            // actually not packed.
            uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  field     = new RepeatedField <int>();
            var  input     = new CodedInputStream(stream);

            input.AssertNextTag(nonPackedTag);
            field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag));
            CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
示例#4
0
        public void TestNegativeEnumPackedArray()
        {
            int arraySize = 1 + (10 * 5);
            int msgSize   = 1 + 1 + arraySize;

            byte[]            bytes  = new byte[msgSize];
            CodedOutputStream output = new CodedOutputStream(bytes);
            // Length-delimited to show we want the packed representation
            uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);

            output.WriteTag(tag);
            int size = 0;

            for (int i = 0; i >= -5; i--)
            {
                size += CodedOutputStream.ComputeEnumSize(i);
            }
            output.WriteRawVarint32((uint)size);
            for (int i = 0; i >= -5; i--)
            {
                output.WriteEnum(i);
            }
            Assert.AreEqual(0, output.SpaceLeft);

            CodedInputStream input = new CodedInputStream(bytes);

            tag = input.ReadTag();

            RepeatedField <SampleEnum> values = new RepeatedField <SampleEnum>();

            values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

            Assert.AreEqual(6, values.Count);
            Assert.AreEqual(SampleEnum.None, values[0]);
            Assert.AreEqual(((SampleEnum)(-1)), values[1]);
            Assert.AreEqual(SampleEnum.NegativeValue, values[2]);
            Assert.AreEqual(((SampleEnum)(-3)), values[3]);
            Assert.AreEqual(((SampleEnum)(-4)), values[4]);
            Assert.AreEqual(((SampleEnum)(-5)), values[5]);
        }
示例#5
0
        public void AddEntriesFrom_String()
        {
            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteString("Foo");
            output.WriteTag(tag);
            output.WriteString("");
            output.WriteTag(tag);
            output.WriteString("Bar");
            output.Flush();
            stream.Position = 0;

            var field = new RepeatedField <string>();
            var input = new CodedInputStream(stream);

            input.AssertNextTag(tag);
            field.AddEntriesFrom(input, FieldCodec.ForString(tag));
            CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field);
            Assert.IsTrue(input.IsAtEnd);
        }
示例#6
0
        public void TestPackedRepeatedFieldCollectionNotAllocatedWhenLengthExceedsRemainingData()
        {
            uint tag    = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited);
            var  codec  = FieldCodec.ForFixed32(tag);
            var  stream = new MemoryStream();
            var  output = new CodedOutputStream(stream);

            output.WriteTag(tag);
            output.WriteString("A long string");
            output.WriteTag(codec.Tag);
            output.WriteRawVarint32((uint)codec.FixedSize);
            // Note that there is no content for the packed field.
            // The field length exceeds the remaining length of the buffer.
            output.Flush();
            stream.Position = 0;

            var sequence = ReadOnlySequenceFactory.CreateWithContent(stream.ToArray());

            ParseContext.Initialize(sequence, out ParseContext ctx);

            ctx.ReadTag();
            ctx.ReadString();
            ctx.ReadTag();
            var field = new RepeatedField <uint>();

            try
            {
                field.AddEntriesFrom(ref ctx, codec);
                Assert.Fail();
            }
            catch (InvalidProtocolBufferException)
            {
            }

            // Collection was not pre-initialized
            Assert.AreEqual(0, field.Count);
        }