public void EqualsAndHashCode()
        {
            TestAllTypes      a = TestUtil.GetAllSet();
            TestAllTypes      b = TestAllTypes.CreateBuilder().Build();
            TestAllTypes      c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
            TestAllTypes      d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
            TestAllExtensions e = TestUtil.GetAllExtensionsSet();
            TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
                                  .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();

            CheckEqualsIsConsistent(a);
            CheckEqualsIsConsistent(b);
            CheckEqualsIsConsistent(c);
            CheckEqualsIsConsistent(d);
            CheckEqualsIsConsistent(e);
            CheckEqualsIsConsistent(f);

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(a, f);

            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(b, f);

            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(c, f);

            CheckNotEqual(d, e);
            CheckNotEqual(d, f);

            CheckNotEqual(e, f);

            // Deserializing into the TestEmptyMessage such that every field is an UnknownFieldSet.Field
            TestEmptyMessage eUnknownFields = TestEmptyMessage.ParseFrom(e.ToByteArray());
            TestEmptyMessage fUnknownFields = TestEmptyMessage.ParseFrom(f.ToByteArray());

            CheckNotEqual(eUnknownFields, fUnknownFields);
            CheckEqualsIsConsistent(eUnknownFields);
            CheckEqualsIsConsistent(fUnknownFields);

            // Subseqent reconstitutions should be identical
            TestEmptyMessage eUnknownFields2 = TestEmptyMessage.ParseFrom(e.ToByteArray());

            CheckEqualsIsConsistent(eUnknownFields, eUnknownFields2);
        }
        public void RoundTrip_ExtensionGroups()
        {
            var message = new TestAllExtensions();

            message.SetExtension(UnittestExtensions.OptionalGroupExtension, new OptionalGroup_extension {
                A = 10
            });
            message.GetOrInitializeExtension(UnittestExtensions.RepeatedGroupExtension).AddRange(new[]
            {
                new RepeatedGroup_extension {
                    A = 10
                },
                new RepeatedGroup_extension {
                    A = 20
                },
                new RepeatedGroup_extension {
                    A = 30
                }
            });

            byte[]            bytes             = message.ToByteArray();
            TestAllExtensions extendable_parsed = TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry()
            {
                UnittestExtensions.OptionalGroupExtension, UnittestExtensions.RepeatedGroupExtension
            }).ParseFrom(bytes);

            Assert.AreEqual(message, extendable_parsed);
        }
Пример #3
0
        public void TestRecoverMissingExtensions()
        {
            const int optionalInt32 = 12345678;

            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            builder.SetExtension(Unittest.OptionalInt32Extension, optionalInt32);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.1);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.2);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.3);
            TestAllExtensions msg = builder.Build();

            Assert.IsTrue(msg.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(3, msg.GetExtensionCount(Unittest.RepeatedDoubleExtension));

            byte[]            bits = msg.ToByteArray();
            TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);

            Assert.IsFalse(copy.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(0, copy.GetExtensionCount(Unittest.RepeatedDoubleExtension));
            Assert.AreNotEqual(msg, copy);

            //Even though copy does not understand the typees they serialize correctly
            byte[] copybits = copy.ToByteArray();
            Assert.AreEqual(bits, copybits);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            Unittest.RegisterAllExtensions(registry);

            //Now we can take those copy bits and restore the full message with extensions
            copy = TestAllExtensions.ParseFrom(copybits, registry);
            Assert.IsTrue(copy.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(3, copy.GetExtensionCount(Unittest.RepeatedDoubleExtension));

            Assert.AreEqual(msg, copy);
            Assert.AreEqual(bits, copy.ToByteArray());

            //If we modify the object this should all continue to work as before
            copybits = copy.ToBuilder().Build().ToByteArray();
            Assert.AreEqual(bits, copybits);

            //If we replace extension the object this should all continue to work as before
            copybits = copy.ToBuilder()
                       .SetExtension(Unittest.OptionalInt32Extension, optionalInt32)
                       .Build().ToByteArray();
            Assert.AreEqual(bits, copybits);
        }
        public void RoundTrip_ParseUsingCodedInput()
        {
            var message = new TestAllExtensions();

            message.SetExtension(UnittestExtensions.OptionalBoolExtension, true);
            byte[] bytes = message.ToByteArray();
            using CodedInputStream input = new CodedInputStream(bytes);
            var parsed = TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry()
            {
                UnittestExtensions.OptionalBoolExtension
            }).ParseFrom(input);

            Assert.AreEqual(message, parsed);
        }
        public void TestMergeCodedInput()
        {
            var message = new TestAllExtensions();

            message.SetExtension(OptionalBoolExtension, true);
            var serialized = message.ToByteArray();

            var other = TestAllExtensions.Parser
                        .WithExtensionRegistry(new ExtensionRegistry()
            {
                OptionalBoolExtension
            })
                        .ParseFrom(serialized);

            Assert.AreEqual(message, other);
            Assert.AreEqual(message.CalculateSize(), other.CalculateSize());
        }
Пример #6
0
        public void TestDefaultValueRoundTrip()
        {
            var message = new TestAllExtensions();

            message.SetExtension(OptionalBoolExtension, false);
            Assert.IsFalse(message.GetExtension(OptionalBoolExtension));
            Assert.IsTrue(message.HasExtension(OptionalBoolExtension));

            var bytes    = message.ToByteArray();
            var registry = new ExtensionRegistry {
                OptionalBoolExtension
            };
            var parsed = TestAllExtensions.Parser.WithExtensionRegistry(registry).ParseFrom(bytes);

            Assert.IsFalse(parsed.GetExtension(OptionalBoolExtension));
            Assert.IsTrue(parsed.HasExtension(OptionalBoolExtension));
        }
Пример #7
0
        public void TestMergeCodedInput()
        {
            var message = new TestAllExtensions();

            message.SetExtension(OptionalBoolExtension, true);
            var serialized = message.ToByteArray();

            MessageParsingHelpers.AssertReadingMessage(
                TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry()
            {
                OptionalBoolExtension
            }),
                serialized,
                other =>
            {
                Assert.AreEqual(message, other);
                Assert.AreEqual(message.CalculateSize(), other.CalculateSize());
            });
        }
Пример #8
0
        public void TryMergeFieldFrom_CodedInputStream()
        {
            var message = new TestAllExtensions();

            message.SetExtension(OptionalStringExtension, "abcd");

            var input = new CodedInputStream(message.ToByteArray());

            input.ExtensionRegistry = new ExtensionRegistry {
                OptionalStringExtension
            };
            input.ReadTag(); // TryMergeFieldFrom expects that a tag was just read and will inspect the LastTag value

            ExtensionSet <TestAllExtensions> extensionSet = null;

            // test the legacy overload of TryMergeFieldFrom that takes a CodedInputStream
            Assert.IsTrue(ExtensionSet.TryMergeFieldFrom(ref extensionSet, input));
            Assert.AreEqual("abcd", ExtensionSet.Get(ref extensionSet, OptionalStringExtension));
        }
Пример #9
0
        public void ExtensionWriterTest()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder()
                                                .SetExtension(Unittest.DefaultBoolExtension, true)
                                                .SetExtension(Unittest.DefaultBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(Unittest.DefaultCordExtension, "123")
                                                .SetExtension(Unittest.DefaultDoubleExtension, 123)
                                                .SetExtension(Unittest.DefaultFixed32Extension, 123u)
                                                .SetExtension(Unittest.DefaultFixed64Extension, 123u)
                                                .SetExtension(Unittest.DefaultFloatExtension, 123)
                                                .SetExtension(Unittest.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(Unittest.DefaultImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(Unittest.DefaultInt32Extension, 123)
                                                .SetExtension(Unittest.DefaultInt64Extension, 123)
                                                .SetExtension(Unittest.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(Unittest.DefaultSfixed32Extension, 123)
                                                .SetExtension(Unittest.DefaultSfixed64Extension, 123)
                                                .SetExtension(Unittest.DefaultSint32Extension, 123)
                                                .SetExtension(Unittest.DefaultSint64Extension, 123)
                                                .SetExtension(Unittest.DefaultStringExtension, "123")
                                                .SetExtension(Unittest.DefaultStringPieceExtension, "123")
                                                .SetExtension(Unittest.DefaultUint32Extension, 123u)
                                                .SetExtension(Unittest.DefaultUint64Extension, 123u)
                                                //Optional
                                                .SetExtension(Unittest.OptionalBoolExtension, true)
                                                .SetExtension(Unittest.OptionalBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(Unittest.OptionalCordExtension, "123")
                                                .SetExtension(Unittest.OptionalDoubleExtension, 123)
                                                .SetExtension(Unittest.OptionalFixed32Extension, 123u)
                                                .SetExtension(Unittest.OptionalFixed64Extension, 123u)
                                                .SetExtension(Unittest.OptionalFloatExtension, 123)
                                                .SetExtension(Unittest.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(Unittest.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(Unittest.OptionalInt32Extension, 123)
                                                .SetExtension(Unittest.OptionalInt64Extension, 123)
                                                .SetExtension(Unittest.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(Unittest.OptionalSfixed32Extension, 123)
                                                .SetExtension(Unittest.OptionalSfixed64Extension, 123)
                                                .SetExtension(Unittest.OptionalSint32Extension, 123)
                                                .SetExtension(Unittest.OptionalSint64Extension, 123)
                                                .SetExtension(Unittest.OptionalStringExtension, "123")
                                                .SetExtension(Unittest.OptionalStringPieceExtension, "123")
                                                .SetExtension(Unittest.OptionalUint32Extension, 123u)
                                                .SetExtension(Unittest.OptionalUint64Extension, 123u)
                                                //Repeated
                                                .AddExtension(Unittest.RepeatedBoolExtension, true)
                                                .AddExtension(Unittest.RepeatedBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .AddExtension(Unittest.RepeatedCordExtension, "123")
                                                .AddExtension(Unittest.RepeatedDoubleExtension, 123)
                                                .AddExtension(Unittest.RepeatedFixed32Extension, 123u)
                                                .AddExtension(Unittest.RepeatedFixed64Extension, 123u)
                                                .AddExtension(Unittest.RepeatedFloatExtension, 123)
                                                .AddExtension(Unittest.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .AddExtension(Unittest.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .AddExtension(Unittest.RepeatedInt32Extension, 123)
                                                .AddExtension(Unittest.RepeatedInt64Extension, 123)
                                                .AddExtension(Unittest.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .AddExtension(Unittest.RepeatedSfixed32Extension, 123)
                                                .AddExtension(Unittest.RepeatedSfixed64Extension, 123)
                                                .AddExtension(Unittest.RepeatedSint32Extension, 123)
                                                .AddExtension(Unittest.RepeatedSint64Extension, 123)
                                                .AddExtension(Unittest.RepeatedStringExtension, "123")
                                                .AddExtension(Unittest.RepeatedStringPieceExtension, "123")
                                                .AddExtension(Unittest.RepeatedUint32Extension, 123u)
                                                .AddExtension(Unittest.RepeatedUint64Extension, 123u)
            ;
            TestAllExtensions msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            Unittest.RegisterAllExtensions(registry);

            TestAllExtensions.Builder copyBuilder = TestAllExtensions.CreateBuilder().MergeFrom(msg.ToByteArray(),
                                                                                                registry);
            TestAllExtensions copy = copyBuilder.Build();

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());

            Assert.AreEqual(true, copy.GetExtension(Unittest.DefaultBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(Unittest.DefaultBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultCordExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(Unittest.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.DefaultImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.DefaultNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultStringExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(Unittest.OptionalBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(Unittest.OptionalBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalCordExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(Unittest.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.OptionalImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.OptionalNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalStringExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(Unittest.RepeatedBoolExtension, 0));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(Unittest.RepeatedBytesExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedCordExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedDoubleExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedFixed32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedFixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedFloatExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ,
                            copy.GetExtension(Unittest.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.RepeatedImportEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedInt32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedInt64Extension, 0));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSint32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSint64Extension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedStringExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedStringPieceExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedUint32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedUint64Extension, 0));
        }