1ffe3c632Sopenharmony_ciusing Google.Protobuf.TestProtos.Proto2;
2ffe3c632Sopenharmony_ciusing NUnit.Framework;
3ffe3c632Sopenharmony_ci
4ffe3c632Sopenharmony_ciusing static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
5ffe3c632Sopenharmony_ci
6ffe3c632Sopenharmony_cinamespace Google.Protobuf
7ffe3c632Sopenharmony_ci{
8ffe3c632Sopenharmony_ci    public class ExtensionSetTest
9ffe3c632Sopenharmony_ci    {
10ffe3c632Sopenharmony_ci        [Test]
11ffe3c632Sopenharmony_ci        public void EmptyExtensionSet()
12ffe3c632Sopenharmony_ci        {
13ffe3c632Sopenharmony_ci            ExtensionSet<TestAllExtensions> extensions = new ExtensionSet<TestAllExtensions>();
14ffe3c632Sopenharmony_ci            Assert.AreEqual(0, extensions.CalculateSize());
15ffe3c632Sopenharmony_ci        }
16ffe3c632Sopenharmony_ci
17ffe3c632Sopenharmony_ci        [Test]
18ffe3c632Sopenharmony_ci        public void MergeExtensionSet()
19ffe3c632Sopenharmony_ci        {
20ffe3c632Sopenharmony_ci            ExtensionSet<TestAllExtensions> extensions = null;
21ffe3c632Sopenharmony_ci            ExtensionSet.Set(ref extensions, OptionalBoolExtension, true);
22ffe3c632Sopenharmony_ci
23ffe3c632Sopenharmony_ci            ExtensionSet<TestAllExtensions> other = null;
24ffe3c632Sopenharmony_ci
25ffe3c632Sopenharmony_ci            Assert.IsFalse(ExtensionSet.Has(ref other, OptionalBoolExtension));
26ffe3c632Sopenharmony_ci            ExtensionSet.MergeFrom(ref other, extensions);
27ffe3c632Sopenharmony_ci            Assert.IsTrue(ExtensionSet.Has(ref other, OptionalBoolExtension));
28ffe3c632Sopenharmony_ci        }
29ffe3c632Sopenharmony_ci
30ffe3c632Sopenharmony_ci        [Test]
31ffe3c632Sopenharmony_ci        public void TestMergeCodedInput()
32ffe3c632Sopenharmony_ci        {
33ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
34ffe3c632Sopenharmony_ci            message.SetExtension(OptionalBoolExtension, true);
35ffe3c632Sopenharmony_ci            var serialized = message.ToByteArray();
36ffe3c632Sopenharmony_ci
37ffe3c632Sopenharmony_ci            MessageParsingHelpers.AssertWritingMessage(message);
38ffe3c632Sopenharmony_ci
39ffe3c632Sopenharmony_ci            MessageParsingHelpers.AssertReadingMessage(
40ffe3c632Sopenharmony_ci                TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { OptionalBoolExtension }),
41ffe3c632Sopenharmony_ci                serialized,
42ffe3c632Sopenharmony_ci                other =>
43ffe3c632Sopenharmony_ci                {
44ffe3c632Sopenharmony_ci                    Assert.AreEqual(message, other);
45ffe3c632Sopenharmony_ci                    Assert.AreEqual(message.CalculateSize(), other.CalculateSize());
46ffe3c632Sopenharmony_ci                });
47ffe3c632Sopenharmony_ci        }
48ffe3c632Sopenharmony_ci
49ffe3c632Sopenharmony_ci        [Test]
50ffe3c632Sopenharmony_ci        public void TestMergeMessage()
51ffe3c632Sopenharmony_ci        {
52ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
53ffe3c632Sopenharmony_ci            message.SetExtension(OptionalBoolExtension, true);
54ffe3c632Sopenharmony_ci
55ffe3c632Sopenharmony_ci            var other = new TestAllExtensions();
56ffe3c632Sopenharmony_ci
57ffe3c632Sopenharmony_ci            Assert.AreNotEqual(message, other);
58ffe3c632Sopenharmony_ci            Assert.AreNotEqual(message.CalculateSize(), other.CalculateSize());
59ffe3c632Sopenharmony_ci
60ffe3c632Sopenharmony_ci            other.MergeFrom(message);
61ffe3c632Sopenharmony_ci
62ffe3c632Sopenharmony_ci            Assert.AreEqual(message, other);
63ffe3c632Sopenharmony_ci            Assert.AreEqual(message.CalculateSize(), other.CalculateSize());
64ffe3c632Sopenharmony_ci        }
65ffe3c632Sopenharmony_ci
66ffe3c632Sopenharmony_ci        [Test]
67ffe3c632Sopenharmony_ci        public void TryMergeFieldFrom_CodedInputStream()
68ffe3c632Sopenharmony_ci        {
69ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
70ffe3c632Sopenharmony_ci            message.SetExtension(OptionalStringExtension, "abcd");
71ffe3c632Sopenharmony_ci
72ffe3c632Sopenharmony_ci            var input = new CodedInputStream(message.ToByteArray());
73ffe3c632Sopenharmony_ci            input.ExtensionRegistry = new ExtensionRegistry() { OptionalStringExtension };
74ffe3c632Sopenharmony_ci            input.ReadTag(); // TryMergeFieldFrom expects that a tag was just read and will inspect the LastTag value
75ffe3c632Sopenharmony_ci
76ffe3c632Sopenharmony_ci            ExtensionSet<TestAllExtensions> extensionSet = null;
77ffe3c632Sopenharmony_ci            // test the legacy overload of TryMergeFieldFrom that takes a CodedInputStream
78ffe3c632Sopenharmony_ci            Assert.IsTrue(ExtensionSet.TryMergeFieldFrom(ref extensionSet, input));
79ffe3c632Sopenharmony_ci            Assert.AreEqual("abcd", ExtensionSet.Get(ref extensionSet, OptionalStringExtension));
80ffe3c632Sopenharmony_ci        }
81ffe3c632Sopenharmony_ci
82ffe3c632Sopenharmony_ci        [Test]
83ffe3c632Sopenharmony_ci        public void TestEquals()
84ffe3c632Sopenharmony_ci        {
85ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
86ffe3c632Sopenharmony_ci            message.SetExtension(OptionalBoolExtension, true);
87ffe3c632Sopenharmony_ci
88ffe3c632Sopenharmony_ci            var other = new TestAllExtensions();
89ffe3c632Sopenharmony_ci
90ffe3c632Sopenharmony_ci            Assert.AreNotEqual(message, other);
91ffe3c632Sopenharmony_ci            Assert.AreNotEqual(message.CalculateSize(), other.CalculateSize());
92ffe3c632Sopenharmony_ci
93ffe3c632Sopenharmony_ci            other.SetExtension(OptionalBoolExtension, true);
94ffe3c632Sopenharmony_ci
95ffe3c632Sopenharmony_ci            Assert.AreEqual(message, other);
96ffe3c632Sopenharmony_ci            Assert.AreEqual(message.CalculateSize(), other.CalculateSize());
97ffe3c632Sopenharmony_ci        }
98ffe3c632Sopenharmony_ci
99ffe3c632Sopenharmony_ci        [Test]
100ffe3c632Sopenharmony_ci        public void TestHashCode()
101ffe3c632Sopenharmony_ci        {
102ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
103ffe3c632Sopenharmony_ci            var hashCode = message.GetHashCode();
104ffe3c632Sopenharmony_ci
105ffe3c632Sopenharmony_ci            message.SetExtension(OptionalBoolExtension, true);
106ffe3c632Sopenharmony_ci
107ffe3c632Sopenharmony_ci            Assert.AreNotEqual(hashCode, message.GetHashCode());
108ffe3c632Sopenharmony_ci        }
109ffe3c632Sopenharmony_ci
110ffe3c632Sopenharmony_ci        [Test]
111ffe3c632Sopenharmony_ci        public void TestClone()
112ffe3c632Sopenharmony_ci        {
113ffe3c632Sopenharmony_ci            var message = new TestAllExtensions();
114ffe3c632Sopenharmony_ci            message.SetExtension(OptionalBoolExtension, true);
115ffe3c632Sopenharmony_ci
116ffe3c632Sopenharmony_ci            var other = message.Clone();
117ffe3c632Sopenharmony_ci
118ffe3c632Sopenharmony_ci            Assert.AreEqual(message, other);
119ffe3c632Sopenharmony_ci            Assert.AreEqual(message.CalculateSize(), message.CalculateSize());
120ffe3c632Sopenharmony_ci        }
121ffe3c632Sopenharmony_ci    }
122ffe3c632Sopenharmony_ci}
123