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 DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();

            reflectionTester.AssertAllFieldsSetViaReflection(copy);

            // Oneof
            FieldDescriptor bytesField =
                TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
            FieldDescriptor uint32Field =
                TestAllTypes.Descriptor.FindFieldByName("oneof_uint32");

            Assert.True(copy.HasField(bytesField));
            Assert.False(copy.HasField(uint32Field));

            DynamicMessage.Builder dynamicBuilder = DynamicMessage.CreateBuilder(message);
            dynamicBuilder[uint32Field] = 123U;
            DynamicMessage copy2 = dynamicBuilder.Build();

            Assert.IsFalse(copy2.HasField(bytesField));
            Assert.IsTrue(copy2.HasField(uint32Field));
            Assert.AreEqual(123U, copy2[uint32Field]);
        }
        public void ToBuilder()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertAllFieldsSet(message.ToBuilder().Build());
        }
        public void ReflectionGetters()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            reflectionTester.AssertAllFieldsSetViaReflection(message);
        }
        public void RepeatedSetters()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestUtil.ModifyRepeatedFields(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertRepeatedFieldsModified(message);
        }
Exemple #6
0
        public void DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();

            reflectionTester.AssertAllFieldsSetViaReflection(copy);
        }
Exemple #7
0
        public void DynamicMessageParsing()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            ByteString rawBytes = message.ToByteString();

            IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);

            reflectionTester.AssertAllFieldsSetViaReflection(message2);
        }