public void Serialization()
        {
            IMessage abstractMessage = new AbstractMessageWrapper(TestUtil.GetAllSet());

            TestUtil.AssertAllFieldsSet(TestAllTypes.ParseFrom(abstractMessage.ToByteString()));
            Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), abstractMessage.ToByteString());
        }
예제 #2
0
        public void ManyMessagesShouldNotTriggerSizeAlert()
        {
            int messageSize = TestUtil.GetAllSet().SerializedSize;
            // Enough messages to trigger the alert unless we've reset the size
            // Note that currently we need to make this big enough to copy two whole buffers,
            // as otherwise when we refill the buffer the second type, the alert triggers instantly.
            int correctCount = (CodedInputStream.BufferSize * 2) / messageSize + 1;

            using (MemoryStream stream = new MemoryStream())
            {
                MessageStreamWriter <TestAllTypes> writer = new MessageStreamWriter <TestAllTypes>(stream);
                for (int i = 0; i < correctCount; i++)
                {
                    writer.Write(TestUtil.GetAllSet());
                }
                writer.Flush();

                stream.Position = 0;

                int count = 0;
                foreach (var message in MessageStreamIterator <TestAllTypes> .FromStreamProvider(() => stream)
                         .WithSizeLimit(CodedInputStream.BufferSize * 2))
                {
                    count++;
                    TestUtil.AssertAllFieldsSet(message);
                }
                Assert.AreEqual(correctCount, count);
            }
        }
        public void Copy()
        {
            AbstractMessageWrapper message =
                new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder()).MergeFrom(TestUtil.GetAllSet()).Build();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
예제 #4
0
        public void ReadHugeBlob()
        {
            // Allocate and initialize a 1MB blob.
            byte[] blob = new byte[1 << 20];
            for (int i = 0; i < blob.Length; i++)
            {
                blob[i] = (byte)i;
            }

            // Make a message containing it.
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            builder.SetOptionalBytes(ByteString.CopyFrom(blob));
            TestAllTypes message = builder.Build();

            // Serialize and parse it.  Make sure to parse from an InputStream, not
            // directly from a ByteString, so that CodedInputStream uses buffered
            // reading.
            TestAllTypes message2 = TestAllTypes.ParseFrom(message.ToByteString().CreateCodedInput());

            Assert.AreEqual(message.OptionalBytes, message2.OptionalBytes);

            // Make sure all the other fields were parsed correctly.
            TestAllTypes message3 = TestAllTypes.CreateBuilder(message2)
                                    .SetOptionalBytes(TestUtil.GetAllSet().OptionalBytes)
                                    .Build();

            TestUtil.AssertAllFieldsSet(message3);
        }
        public void Parsing()
        {
            IBuilder builder = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder());
            AbstractMessageWrapper message = (AbstractMessageWrapper)builder.WeakMergeFrom(TestUtil.GetAllSet().ToByteString()).WeakBuild();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
예제 #6
0
        public void ToBuilder()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertAllFieldsSet(message.ToBuilder().Build());
        }
예제 #7
0
        public void ReflectionSetters()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            reflectionTester.SetAllFieldsViaReflection(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertAllFieldsSet(message);
        }
 public void Parse()
 {
     TestUtil.TestInMultipleCultures(() => {
         TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
         TextFormat.Merge(AllFieldsSetText, builder);
         TestUtil.AssertAllFieldsSet(builder.Build());
     });
 }
예제 #9
0
        public void Serialization()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            ByteString rawBytes = message.ToByteString();

            Assert.AreEqual(rawBytes.Length, message.SerializedSize);

            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);
        }
예제 #10
0
        public void SerializeExtensions()
        {
            // TestAllTypes and TestAllExtensions should have compatible wire formats,
            // so if we serialize a TestAllExtensions then parse it as TestAllTypes
            // it should work.
            TestAllExtensions message  = TestUtil.GetAllExtensionsSet();
            ByteString        rawBytes = message.ToByteString();

            Assert.AreEqual(rawBytes.Length, message.SerializedSize);

            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);
        }
예제 #11
0
        public void DynamicMessageSerialization()
        {
            IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);

            reflectionTester.SetAllFieldsViaReflection(builder);
            IMessage message = builder.WeakBuild();

            ByteString   rawBytes = message.ToByteString();
            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);

            // In fact, the serialized forms should be exactly the same, byte-for-byte.
            Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
        }
예제 #12
0
        public void SerializeDelimited()
        {
            MemoryStream stream = new MemoryStream();

            TestUtil.GetAllSet().WriteDelimitedTo(stream);
            stream.WriteByte(12);
            TestUtil.GetPackedSet().WriteDelimitedTo(stream);
            stream.WriteByte(34);

            stream.Position = 0;

            TestUtil.AssertAllFieldsSet(TestAllTypes.ParseDelimitedFrom(stream));
            Assert.AreEqual(12, stream.ReadByte());
            TestUtil.AssertPackedFieldsSet(TestPackedTypes.ParseDelimitedFrom(stream));
            Assert.AreEqual(34, stream.ReadByte());
            Assert.AreEqual(-1, stream.ReadByte());
        }
예제 #13
0
        public void ReadWholeMessage()
        {
            TestAllTypes message = TestUtil.GetAllSet();

            byte[] rawBytes = message.ToByteArray();
            Assert.AreEqual(rawBytes.Length, message.SerializedSize);
            TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);

            TestUtil.AssertAllFieldsSet(message2);

            // Try different block sizes.
            for (int blockSize = 1; blockSize < 256; blockSize *= 2)
            {
                message2 = TestAllTypes.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
                TestUtil.AssertAllFieldsSet(message2);
            }
        }
        public void ParseKnownAndUnknown()
        {
            // Test mixing known and unknown fields when parsing.

            UnknownFieldSet fields =
                UnknownFieldSet.CreateBuilder(unknownFields)
                .AddField(123456,
                          UnknownField.CreateBuilder().AddVarint(654321).Build())
                .Build();

            ByteString   data        = fields.ToByteString();
            TestAllTypes destination = TestAllTypes.ParseFrom(data);

            TestUtil.AssertAllFieldsSet(destination);
            Assert.AreEqual(1, destination.UnknownFields.FieldDictionary.Count);

            UnknownField field = destination.UnknownFields[123456];

            Assert.AreEqual(1, field.VarintList.Count);
            Assert.AreEqual(654321, (long)field.VarintList[0]);
        }
예제 #15
0
 public void ParseReader()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
     TestUtil.AssertAllFieldsSet(builder.Build());
 }