1ffe3c632Sopenharmony_ci#region Copyright notice and license 2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 3ffe3c632Sopenharmony_ci// Copyright 2015 Google Inc. All rights reserved. 4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 5ffe3c632Sopenharmony_ci// 6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 8ffe3c632Sopenharmony_ci// met: 9ffe3c632Sopenharmony_ci// 10ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 12ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 15ffe3c632Sopenharmony_ci// distribution. 16ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 18ffe3c632Sopenharmony_ci// this software without specific prior written permission. 19ffe3c632Sopenharmony_ci// 20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31ffe3c632Sopenharmony_ci#endregion 32ffe3c632Sopenharmony_ci 33ffe3c632Sopenharmony_ciusing System; 34ffe3c632Sopenharmony_ciusing System.IO; 35ffe3c632Sopenharmony_ciusing Google.Protobuf.TestProtos; 36ffe3c632Sopenharmony_ciusing NUnit.Framework; 37ffe3c632Sopenharmony_ciusing System.Collections; 38ffe3c632Sopenharmony_ciusing System.Collections.Generic; 39ffe3c632Sopenharmony_ciusing System.Linq; 40ffe3c632Sopenharmony_ciusing Google.Protobuf.WellKnownTypes; 41ffe3c632Sopenharmony_ci 42ffe3c632Sopenharmony_cinamespace Google.Protobuf 43ffe3c632Sopenharmony_ci{ 44ffe3c632Sopenharmony_ci /// <summary> 45ffe3c632Sopenharmony_ci /// Tests around the generated TestAllTypes message. 46ffe3c632Sopenharmony_ci /// </summary> 47ffe3c632Sopenharmony_ci public class GeneratedMessageTest 48ffe3c632Sopenharmony_ci { 49ffe3c632Sopenharmony_ci [Test] 50ffe3c632Sopenharmony_ci public void EmptyMessageFieldDistinctFromMissingMessageField() 51ffe3c632Sopenharmony_ci { 52ffe3c632Sopenharmony_ci // This demonstrates what we're really interested in... 53ffe3c632Sopenharmony_ci var message1 = new TestAllTypes { SingleForeignMessage = new ForeignMessage() }; 54ffe3c632Sopenharmony_ci var message2 = new TestAllTypes(); // SingleForeignMessage is null 55ffe3c632Sopenharmony_ci EqualityTester.AssertInequality(message1, message2); 56ffe3c632Sopenharmony_ci } 57ffe3c632Sopenharmony_ci 58ffe3c632Sopenharmony_ci [Test] 59ffe3c632Sopenharmony_ci public void DefaultValues() 60ffe3c632Sopenharmony_ci { 61ffe3c632Sopenharmony_ci // Single fields 62ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 63ffe3c632Sopenharmony_ci Assert.AreEqual(false, message.SingleBool); 64ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.SingleBytes); 65ffe3c632Sopenharmony_ci Assert.AreEqual(0.0, message.SingleDouble); 66ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.SingleFixed32); 67ffe3c632Sopenharmony_ci Assert.AreEqual(0L, message.SingleFixed64); 68ffe3c632Sopenharmony_ci Assert.AreEqual(0.0f, message.SingleFloat); 69ffe3c632Sopenharmony_ci Assert.AreEqual(ForeignEnum.ForeignUnspecified, message.SingleForeignEnum); 70ffe3c632Sopenharmony_ci Assert.IsNull(message.SingleForeignMessage); 71ffe3c632Sopenharmony_ci Assert.AreEqual(ImportEnum.Unspecified, message.SingleImportEnum); 72ffe3c632Sopenharmony_ci Assert.IsNull(message.SingleImportMessage); 73ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.SingleInt32); 74ffe3c632Sopenharmony_ci Assert.AreEqual(0L, message.SingleInt64); 75ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.Types.NestedEnum.Unspecified, message.SingleNestedEnum); 76ffe3c632Sopenharmony_ci Assert.IsNull(message.SingleNestedMessage); 77ffe3c632Sopenharmony_ci Assert.IsNull(message.SinglePublicImportMessage); 78ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.SingleSfixed32); 79ffe3c632Sopenharmony_ci Assert.AreEqual(0L, message.SingleSfixed64); 80ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.SingleSint32); 81ffe3c632Sopenharmony_ci Assert.AreEqual(0L, message.SingleSint64); 82ffe3c632Sopenharmony_ci Assert.AreEqual("", message.SingleString); 83ffe3c632Sopenharmony_ci Assert.AreEqual(0U, message.SingleUint32); 84ffe3c632Sopenharmony_ci Assert.AreEqual(0UL, message.SingleUint64); 85ffe3c632Sopenharmony_ci 86ffe3c632Sopenharmony_ci // Repeated fields 87ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedBool.Count); 88ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedBytes.Count); 89ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedDouble.Count); 90ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedFixed32.Count); 91ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedFixed64.Count); 92ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedFloat.Count); 93ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedForeignEnum.Count); 94ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedForeignMessage.Count); 95ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedImportEnum.Count); 96ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedImportMessage.Count); 97ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedNestedEnum.Count); 98ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedNestedMessage.Count); 99ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedPublicImportMessage.Count); 100ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedSfixed32.Count); 101ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedSfixed64.Count); 102ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedSint32.Count); 103ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedSint64.Count); 104ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedString.Count); 105ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedUint32.Count); 106ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.RepeatedUint64.Count); 107ffe3c632Sopenharmony_ci 108ffe3c632Sopenharmony_ci // Oneof fields 109ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); 110ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 111ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 112ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 113ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 114ffe3c632Sopenharmony_ci } 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci [Test] 117ffe3c632Sopenharmony_ci public void NullStringAndBytesRejected() 118ffe3c632Sopenharmony_ci { 119ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 120ffe3c632Sopenharmony_ci Assert.Throws<ArgumentNullException>(() => message.SingleString = null); 121ffe3c632Sopenharmony_ci Assert.Throws<ArgumentNullException>(() => message.OneofString = null); 122ffe3c632Sopenharmony_ci Assert.Throws<ArgumentNullException>(() => message.SingleBytes = null); 123ffe3c632Sopenharmony_ci Assert.Throws<ArgumentNullException>(() => message.OneofBytes = null); 124ffe3c632Sopenharmony_ci } 125ffe3c632Sopenharmony_ci 126ffe3c632Sopenharmony_ci [Test] 127ffe3c632Sopenharmony_ci public void RoundTrip_Empty() 128ffe3c632Sopenharmony_ci { 129ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 130ffe3c632Sopenharmony_ci // Without setting any values, there's nothing to write. 131ffe3c632Sopenharmony_ci byte[] bytes = message.ToByteArray(); 132ffe3c632Sopenharmony_ci Assert.AreEqual(0, bytes.Length); 133ffe3c632Sopenharmony_ci TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); 134ffe3c632Sopenharmony_ci Assert.AreEqual(message, parsed); 135ffe3c632Sopenharmony_ci } 136ffe3c632Sopenharmony_ci 137ffe3c632Sopenharmony_ci [Test] 138ffe3c632Sopenharmony_ci public void RoundTrip_SingleValues() 139ffe3c632Sopenharmony_ci { 140ffe3c632Sopenharmony_ci var message = new TestAllTypes 141ffe3c632Sopenharmony_ci { 142ffe3c632Sopenharmony_ci SingleBool = true, 143ffe3c632Sopenharmony_ci SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), 144ffe3c632Sopenharmony_ci SingleDouble = 23.5, 145ffe3c632Sopenharmony_ci SingleFixed32 = 23, 146ffe3c632Sopenharmony_ci SingleFixed64 = 1234567890123, 147ffe3c632Sopenharmony_ci SingleFloat = 12.25f, 148ffe3c632Sopenharmony_ci SingleForeignEnum = ForeignEnum.ForeignBar, 149ffe3c632Sopenharmony_ci SingleForeignMessage = new ForeignMessage { C = 10 }, 150ffe3c632Sopenharmony_ci SingleImportEnum = ImportEnum.ImportBaz, 151ffe3c632Sopenharmony_ci SingleImportMessage = new ImportMessage { D = 20 }, 152ffe3c632Sopenharmony_ci SingleInt32 = 100, 153ffe3c632Sopenharmony_ci SingleInt64 = 3210987654321, 154ffe3c632Sopenharmony_ci SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, 155ffe3c632Sopenharmony_ci SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, 156ffe3c632Sopenharmony_ci SinglePublicImportMessage = new PublicImportMessage { E = 54 }, 157ffe3c632Sopenharmony_ci SingleSfixed32 = -123, 158ffe3c632Sopenharmony_ci SingleSfixed64 = -12345678901234, 159ffe3c632Sopenharmony_ci SingleSint32 = -456, 160ffe3c632Sopenharmony_ci SingleSint64 = -12345678901235, 161ffe3c632Sopenharmony_ci SingleString = "test", 162ffe3c632Sopenharmony_ci SingleUint32 = uint.MaxValue, 163ffe3c632Sopenharmony_ci SingleUint64 = ulong.MaxValue 164ffe3c632Sopenharmony_ci }; 165ffe3c632Sopenharmony_ci 166ffe3c632Sopenharmony_ci byte[] bytes = message.ToByteArray(); 167ffe3c632Sopenharmony_ci TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); 168ffe3c632Sopenharmony_ci Assert.AreEqual(message, parsed); 169ffe3c632Sopenharmony_ci } 170ffe3c632Sopenharmony_ci 171ffe3c632Sopenharmony_ci [Test] 172ffe3c632Sopenharmony_ci public void RoundTrip_RepeatedValues() 173ffe3c632Sopenharmony_ci { 174ffe3c632Sopenharmony_ci var message = new TestAllTypes 175ffe3c632Sopenharmony_ci { 176ffe3c632Sopenharmony_ci RepeatedBool = { true, false }, 177ffe3c632Sopenharmony_ci RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) }, 178ffe3c632Sopenharmony_ci RepeatedDouble = { -12.25, 23.5 }, 179ffe3c632Sopenharmony_ci RepeatedFixed32 = { uint.MaxValue, 23 }, 180ffe3c632Sopenharmony_ci RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, 181ffe3c632Sopenharmony_ci RepeatedFloat = { 100f, 12.25f }, 182ffe3c632Sopenharmony_ci RepeatedForeignEnum = { ForeignEnum.ForeignFoo, ForeignEnum.ForeignBar }, 183ffe3c632Sopenharmony_ci RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } }, 184ffe3c632Sopenharmony_ci RepeatedImportEnum = { ImportEnum.ImportBaz, ImportEnum.Unspecified }, 185ffe3c632Sopenharmony_ci RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } }, 186ffe3c632Sopenharmony_ci RepeatedInt32 = { 100, 200 }, 187ffe3c632Sopenharmony_ci RepeatedInt64 = { 3210987654321, long.MaxValue }, 188ffe3c632Sopenharmony_ci RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg }, 189ffe3c632Sopenharmony_ci RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, 190ffe3c632Sopenharmony_ci RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } }, 191ffe3c632Sopenharmony_ci RepeatedSfixed32 = { -123, 123 }, 192ffe3c632Sopenharmony_ci RepeatedSfixed64 = { -12345678901234, 12345678901234 }, 193ffe3c632Sopenharmony_ci RepeatedSint32 = { -456, 100 }, 194ffe3c632Sopenharmony_ci RepeatedSint64 = { -12345678901235, 123 }, 195ffe3c632Sopenharmony_ci RepeatedString = { "foo", "bar" }, 196ffe3c632Sopenharmony_ci RepeatedUint32 = { uint.MaxValue, uint.MinValue }, 197ffe3c632Sopenharmony_ci RepeatedUint64 = { ulong.MaxValue, uint.MinValue } 198ffe3c632Sopenharmony_ci }; 199ffe3c632Sopenharmony_ci 200ffe3c632Sopenharmony_ci byte[] bytes = message.ToByteArray(); 201ffe3c632Sopenharmony_ci TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); 202ffe3c632Sopenharmony_ci Assert.AreEqual(message, parsed); 203ffe3c632Sopenharmony_ci } 204ffe3c632Sopenharmony_ci 205ffe3c632Sopenharmony_ci // Note that not every map within map_unittest_proto3 is used. They all go through very 206ffe3c632Sopenharmony_ci // similar code paths. The fact that all maps are present is validation that we have codecs 207ffe3c632Sopenharmony_ci // for every type. 208ffe3c632Sopenharmony_ci [Test] 209ffe3c632Sopenharmony_ci public void RoundTrip_Maps() 210ffe3c632Sopenharmony_ci { 211ffe3c632Sopenharmony_ci var message = new TestMap 212ffe3c632Sopenharmony_ci { 213ffe3c632Sopenharmony_ci MapBoolBool = { 214ffe3c632Sopenharmony_ci { false, true }, 215ffe3c632Sopenharmony_ci { true, false } 216ffe3c632Sopenharmony_ci }, 217ffe3c632Sopenharmony_ci MapInt32Bytes = { 218ffe3c632Sopenharmony_ci { 5, ByteString.CopyFrom(6, 7, 8) }, 219ffe3c632Sopenharmony_ci { 25, ByteString.CopyFrom(1, 2, 3, 4, 5) }, 220ffe3c632Sopenharmony_ci { 10, ByteString.Empty } 221ffe3c632Sopenharmony_ci }, 222ffe3c632Sopenharmony_ci MapInt32ForeignMessage = { 223ffe3c632Sopenharmony_ci { 0, new ForeignMessage { C = 10 } }, 224ffe3c632Sopenharmony_ci { 5, new ForeignMessage() }, 225ffe3c632Sopenharmony_ci }, 226ffe3c632Sopenharmony_ci MapInt32Enum = { 227ffe3c632Sopenharmony_ci { 1, MapEnum.Bar }, 228ffe3c632Sopenharmony_ci { 2000, MapEnum.Foo } 229ffe3c632Sopenharmony_ci } 230ffe3c632Sopenharmony_ci }; 231ffe3c632Sopenharmony_ci 232ffe3c632Sopenharmony_ci byte[] bytes = message.ToByteArray(); 233ffe3c632Sopenharmony_ci TestMap parsed = TestMap.Parser.ParseFrom(bytes); 234ffe3c632Sopenharmony_ci Assert.AreEqual(message, parsed); 235ffe3c632Sopenharmony_ci } 236ffe3c632Sopenharmony_ci 237ffe3c632Sopenharmony_ci [Test] 238ffe3c632Sopenharmony_ci public void MapWithEmptyEntry() 239ffe3c632Sopenharmony_ci { 240ffe3c632Sopenharmony_ci var message = new TestMap 241ffe3c632Sopenharmony_ci { 242ffe3c632Sopenharmony_ci MapInt32Bytes = { { 0, ByteString.Empty } } 243ffe3c632Sopenharmony_ci }; 244ffe3c632Sopenharmony_ci 245ffe3c632Sopenharmony_ci byte[] bytes = message.ToByteArray(); 246ffe3c632Sopenharmony_ci Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), length of entry (0; 1 byte) 247ffe3c632Sopenharmony_ci 248ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(bytes); 249ffe3c632Sopenharmony_ci Assert.AreEqual(1, parsed.MapInt32Bytes.Count); 250ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, parsed.MapInt32Bytes[0]); 251ffe3c632Sopenharmony_ci } 252ffe3c632Sopenharmony_ci 253ffe3c632Sopenharmony_ci [Test] 254ffe3c632Sopenharmony_ci public void MapWithOnlyValue() 255ffe3c632Sopenharmony_ci { 256ffe3c632Sopenharmony_ci // Hand-craft the stream to contain a single entry with just a value. 257ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 258ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 259ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited); 260ffe3c632Sopenharmony_ci var nestedMessage = new ForeignMessage { C = 20 }; 261ffe3c632Sopenharmony_ci // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage) 262ffe3c632Sopenharmony_ci output.WriteLength(2 + nestedMessage.CalculateSize()); 263ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.LengthDelimited); 264ffe3c632Sopenharmony_ci output.WriteMessage(nestedMessage); 265ffe3c632Sopenharmony_ci output.Flush(); 266ffe3c632Sopenharmony_ci 267ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 268ffe3c632Sopenharmony_ci Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]); 269ffe3c632Sopenharmony_ci } 270ffe3c632Sopenharmony_ci 271ffe3c632Sopenharmony_ci [Test] 272ffe3c632Sopenharmony_ci public void MapWithOnlyKey_PrimitiveValue() 273ffe3c632Sopenharmony_ci { 274ffe3c632Sopenharmony_ci // Hand-craft the stream to contain a single entry with just a key. 275ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 276ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 277ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32DoubleFieldNumber, WireFormat.WireType.LengthDelimited); 278ffe3c632Sopenharmony_ci int key = 10; 279ffe3c632Sopenharmony_ci output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); 280ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 281ffe3c632Sopenharmony_ci output.WriteInt32(key); 282ffe3c632Sopenharmony_ci output.Flush(); 283ffe3c632Sopenharmony_ci 284ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 285ffe3c632Sopenharmony_ci Assert.AreEqual(0.0, parsed.MapInt32Double[key]); 286ffe3c632Sopenharmony_ci } 287ffe3c632Sopenharmony_ci 288ffe3c632Sopenharmony_ci [Test] 289ffe3c632Sopenharmony_ci public void MapWithOnlyKey_MessageValue() 290ffe3c632Sopenharmony_ci { 291ffe3c632Sopenharmony_ci // Hand-craft the stream to contain a single entry with just a key. 292ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 293ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 294ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireFormat.WireType.LengthDelimited); 295ffe3c632Sopenharmony_ci int key = 10; 296ffe3c632Sopenharmony_ci output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); 297ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 298ffe3c632Sopenharmony_ci output.WriteInt32(key); 299ffe3c632Sopenharmony_ci output.Flush(); 300ffe3c632Sopenharmony_ci 301ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 302ffe3c632Sopenharmony_ci Assert.AreEqual(new ForeignMessage(), parsed.MapInt32ForeignMessage[key]); 303ffe3c632Sopenharmony_ci } 304ffe3c632Sopenharmony_ci 305ffe3c632Sopenharmony_ci [Test] 306ffe3c632Sopenharmony_ci public void MapIgnoresExtraFieldsWithinEntryMessages() 307ffe3c632Sopenharmony_ci { 308ffe3c632Sopenharmony_ci // Hand-craft the stream to contain a single entry with three fields 309ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 310ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 311ffe3c632Sopenharmony_ci 312ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 313ffe3c632Sopenharmony_ci 314ffe3c632Sopenharmony_ci var key = 10; // Field 1 315ffe3c632Sopenharmony_ci var value = 20; // Field 2 316ffe3c632Sopenharmony_ci var extra = 30; // Field 3 317ffe3c632Sopenharmony_ci 318ffe3c632Sopenharmony_ci // Each field can be represented in a single byte, with a single byte tag. 319ffe3c632Sopenharmony_ci // Total message size: 6 bytes. 320ffe3c632Sopenharmony_ci output.WriteLength(6); 321ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 322ffe3c632Sopenharmony_ci output.WriteInt32(key); 323ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 324ffe3c632Sopenharmony_ci output.WriteInt32(value); 325ffe3c632Sopenharmony_ci output.WriteTag(3, WireFormat.WireType.Varint); 326ffe3c632Sopenharmony_ci output.WriteInt32(extra); 327ffe3c632Sopenharmony_ci output.Flush(); 328ffe3c632Sopenharmony_ci 329ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 330ffe3c632Sopenharmony_ci Assert.AreEqual(value, parsed.MapInt32Int32[key]); 331ffe3c632Sopenharmony_ci } 332ffe3c632Sopenharmony_ci 333ffe3c632Sopenharmony_ci [Test] 334ffe3c632Sopenharmony_ci public void MapFieldOrderIsIrrelevant() 335ffe3c632Sopenharmony_ci { 336ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 337ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 338ffe3c632Sopenharmony_ci 339ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 340ffe3c632Sopenharmony_ci 341ffe3c632Sopenharmony_ci var key = 10; 342ffe3c632Sopenharmony_ci var value = 20; 343ffe3c632Sopenharmony_ci 344ffe3c632Sopenharmony_ci // Each field can be represented in a single byte, with a single byte tag. 345ffe3c632Sopenharmony_ci // Total message size: 4 bytes. 346ffe3c632Sopenharmony_ci output.WriteLength(4); 347ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 348ffe3c632Sopenharmony_ci output.WriteInt32(value); 349ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 350ffe3c632Sopenharmony_ci output.WriteInt32(key); 351ffe3c632Sopenharmony_ci output.Flush(); 352ffe3c632Sopenharmony_ci 353ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 354ffe3c632Sopenharmony_ci Assert.AreEqual(value, parsed.MapInt32Int32[key]); 355ffe3c632Sopenharmony_ci } 356ffe3c632Sopenharmony_ci 357ffe3c632Sopenharmony_ci [Test] 358ffe3c632Sopenharmony_ci public void MapNonContiguousEntries() 359ffe3c632Sopenharmony_ci { 360ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 361ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 362ffe3c632Sopenharmony_ci 363ffe3c632Sopenharmony_ci // Message structure: 364ffe3c632Sopenharmony_ci // Entry for MapInt32Int32 365ffe3c632Sopenharmony_ci // Entry for MapStringString 366ffe3c632Sopenharmony_ci // Entry for MapInt32Int32 367ffe3c632Sopenharmony_ci 368ffe3c632Sopenharmony_ci // First entry 369ffe3c632Sopenharmony_ci var key1 = 10; 370ffe3c632Sopenharmony_ci var value1 = 20; 371ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 372ffe3c632Sopenharmony_ci output.WriteLength(4); 373ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 374ffe3c632Sopenharmony_ci output.WriteInt32(key1); 375ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 376ffe3c632Sopenharmony_ci output.WriteInt32(value1); 377ffe3c632Sopenharmony_ci 378ffe3c632Sopenharmony_ci // Second entry 379ffe3c632Sopenharmony_ci var key2 = "a"; 380ffe3c632Sopenharmony_ci var value2 = "b"; 381ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited); 382ffe3c632Sopenharmony_ci output.WriteLength(6); // 3 bytes per entry: tag, size, character 383ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.LengthDelimited); 384ffe3c632Sopenharmony_ci output.WriteString(key2); 385ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.LengthDelimited); 386ffe3c632Sopenharmony_ci output.WriteString(value2); 387ffe3c632Sopenharmony_ci 388ffe3c632Sopenharmony_ci // Third entry 389ffe3c632Sopenharmony_ci var key3 = 15; 390ffe3c632Sopenharmony_ci var value3 = 25; 391ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 392ffe3c632Sopenharmony_ci output.WriteLength(4); 393ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 394ffe3c632Sopenharmony_ci output.WriteInt32(key3); 395ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 396ffe3c632Sopenharmony_ci output.WriteInt32(value3); 397ffe3c632Sopenharmony_ci 398ffe3c632Sopenharmony_ci output.Flush(); 399ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 400ffe3c632Sopenharmony_ci var expected = new TestMap 401ffe3c632Sopenharmony_ci { 402ffe3c632Sopenharmony_ci MapInt32Int32 = { { key1, value1 }, { key3, value3 } }, 403ffe3c632Sopenharmony_ci MapStringString = { { key2, value2 } } 404ffe3c632Sopenharmony_ci }; 405ffe3c632Sopenharmony_ci Assert.AreEqual(expected, parsed); 406ffe3c632Sopenharmony_ci } 407ffe3c632Sopenharmony_ci 408ffe3c632Sopenharmony_ci [Test] 409ffe3c632Sopenharmony_ci public void DuplicateKeys_LastEntryWins() 410ffe3c632Sopenharmony_ci { 411ffe3c632Sopenharmony_ci var memoryStream = new MemoryStream(); 412ffe3c632Sopenharmony_ci var output = new CodedOutputStream(memoryStream); 413ffe3c632Sopenharmony_ci 414ffe3c632Sopenharmony_ci var key = 10; 415ffe3c632Sopenharmony_ci var value1 = 20; 416ffe3c632Sopenharmony_ci var value2 = 30; 417ffe3c632Sopenharmony_ci 418ffe3c632Sopenharmony_ci // First entry 419ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 420ffe3c632Sopenharmony_ci output.WriteLength(4); 421ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 422ffe3c632Sopenharmony_ci output.WriteInt32(key); 423ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 424ffe3c632Sopenharmony_ci output.WriteInt32(value1); 425ffe3c632Sopenharmony_ci 426ffe3c632Sopenharmony_ci // Second entry - same key, different value 427ffe3c632Sopenharmony_ci output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); 428ffe3c632Sopenharmony_ci output.WriteLength(4); 429ffe3c632Sopenharmony_ci output.WriteTag(1, WireFormat.WireType.Varint); 430ffe3c632Sopenharmony_ci output.WriteInt32(key); 431ffe3c632Sopenharmony_ci output.WriteTag(2, WireFormat.WireType.Varint); 432ffe3c632Sopenharmony_ci output.WriteInt32(value2); 433ffe3c632Sopenharmony_ci output.Flush(); 434ffe3c632Sopenharmony_ci 435ffe3c632Sopenharmony_ci var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); 436ffe3c632Sopenharmony_ci Assert.AreEqual(value2, parsed.MapInt32Int32[key]); 437ffe3c632Sopenharmony_ci } 438ffe3c632Sopenharmony_ci 439ffe3c632Sopenharmony_ci [Test] 440ffe3c632Sopenharmony_ci public void CloneSingleNonMessageValues() 441ffe3c632Sopenharmony_ci { 442ffe3c632Sopenharmony_ci var original = new TestAllTypes 443ffe3c632Sopenharmony_ci { 444ffe3c632Sopenharmony_ci SingleBool = true, 445ffe3c632Sopenharmony_ci SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), 446ffe3c632Sopenharmony_ci SingleDouble = 23.5, 447ffe3c632Sopenharmony_ci SingleFixed32 = 23, 448ffe3c632Sopenharmony_ci SingleFixed64 = 1234567890123, 449ffe3c632Sopenharmony_ci SingleFloat = 12.25f, 450ffe3c632Sopenharmony_ci SingleInt32 = 100, 451ffe3c632Sopenharmony_ci SingleInt64 = 3210987654321, 452ffe3c632Sopenharmony_ci SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, 453ffe3c632Sopenharmony_ci SingleSfixed32 = -123, 454ffe3c632Sopenharmony_ci SingleSfixed64 = -12345678901234, 455ffe3c632Sopenharmony_ci SingleSint32 = -456, 456ffe3c632Sopenharmony_ci SingleSint64 = -12345678901235, 457ffe3c632Sopenharmony_ci SingleString = "test", 458ffe3c632Sopenharmony_ci SingleUint32 = uint.MaxValue, 459ffe3c632Sopenharmony_ci SingleUint64 = ulong.MaxValue 460ffe3c632Sopenharmony_ci }; 461ffe3c632Sopenharmony_ci var clone = original.Clone(); 462ffe3c632Sopenharmony_ci Assert.AreNotSame(original, clone); 463ffe3c632Sopenharmony_ci Assert.AreEqual(original, clone); 464ffe3c632Sopenharmony_ci // Just as a single example 465ffe3c632Sopenharmony_ci clone.SingleInt32 = 150; 466ffe3c632Sopenharmony_ci Assert.AreNotEqual(original, clone); 467ffe3c632Sopenharmony_ci } 468ffe3c632Sopenharmony_ci 469ffe3c632Sopenharmony_ci [Test] 470ffe3c632Sopenharmony_ci public void CloneRepeatedNonMessageValues() 471ffe3c632Sopenharmony_ci { 472ffe3c632Sopenharmony_ci var original = new TestAllTypes 473ffe3c632Sopenharmony_ci { 474ffe3c632Sopenharmony_ci RepeatedBool = { true, false }, 475ffe3c632Sopenharmony_ci RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6) }, 476ffe3c632Sopenharmony_ci RepeatedDouble = { -12.25, 23.5 }, 477ffe3c632Sopenharmony_ci RepeatedFixed32 = { uint.MaxValue, 23 }, 478ffe3c632Sopenharmony_ci RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, 479ffe3c632Sopenharmony_ci RepeatedFloat = { 100f, 12.25f }, 480ffe3c632Sopenharmony_ci RepeatedInt32 = { 100, 200 }, 481ffe3c632Sopenharmony_ci RepeatedInt64 = { 3210987654321, long.MaxValue }, 482ffe3c632Sopenharmony_ci RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg }, 483ffe3c632Sopenharmony_ci RepeatedSfixed32 = { -123, 123 }, 484ffe3c632Sopenharmony_ci RepeatedSfixed64 = { -12345678901234, 12345678901234 }, 485ffe3c632Sopenharmony_ci RepeatedSint32 = { -456, 100 }, 486ffe3c632Sopenharmony_ci RepeatedSint64 = { -12345678901235, 123 }, 487ffe3c632Sopenharmony_ci RepeatedString = { "foo", "bar" }, 488ffe3c632Sopenharmony_ci RepeatedUint32 = { uint.MaxValue, uint.MinValue }, 489ffe3c632Sopenharmony_ci RepeatedUint64 = { ulong.MaxValue, uint.MinValue } 490ffe3c632Sopenharmony_ci }; 491ffe3c632Sopenharmony_ci 492ffe3c632Sopenharmony_ci var clone = original.Clone(); 493ffe3c632Sopenharmony_ci Assert.AreNotSame(original, clone); 494ffe3c632Sopenharmony_ci Assert.AreEqual(original, clone); 495ffe3c632Sopenharmony_ci // Just as a single example 496ffe3c632Sopenharmony_ci clone.RepeatedDouble.Add(25.5); 497ffe3c632Sopenharmony_ci Assert.AreNotEqual(original, clone); 498ffe3c632Sopenharmony_ci } 499ffe3c632Sopenharmony_ci 500ffe3c632Sopenharmony_ci [Test] 501ffe3c632Sopenharmony_ci public void CloneSingleMessageField() 502ffe3c632Sopenharmony_ci { 503ffe3c632Sopenharmony_ci var original = new TestAllTypes 504ffe3c632Sopenharmony_ci { 505ffe3c632Sopenharmony_ci SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } 506ffe3c632Sopenharmony_ci }; 507ffe3c632Sopenharmony_ci 508ffe3c632Sopenharmony_ci var clone = original.Clone(); 509ffe3c632Sopenharmony_ci Assert.AreNotSame(original, clone); 510ffe3c632Sopenharmony_ci Assert.AreNotSame(original.SingleNestedMessage, clone.SingleNestedMessage); 511ffe3c632Sopenharmony_ci Assert.AreEqual(original, clone); 512ffe3c632Sopenharmony_ci 513ffe3c632Sopenharmony_ci clone.SingleNestedMessage.Bb = 30; 514ffe3c632Sopenharmony_ci Assert.AreNotEqual(original, clone); 515ffe3c632Sopenharmony_ci } 516ffe3c632Sopenharmony_ci 517ffe3c632Sopenharmony_ci [Test] 518ffe3c632Sopenharmony_ci public void CloneRepeatedMessageField() 519ffe3c632Sopenharmony_ci { 520ffe3c632Sopenharmony_ci var original = new TestAllTypes 521ffe3c632Sopenharmony_ci { 522ffe3c632Sopenharmony_ci RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 20 } } 523ffe3c632Sopenharmony_ci }; 524ffe3c632Sopenharmony_ci 525ffe3c632Sopenharmony_ci var clone = original.Clone(); 526ffe3c632Sopenharmony_ci Assert.AreNotSame(original, clone); 527ffe3c632Sopenharmony_ci Assert.AreNotSame(original.RepeatedNestedMessage, clone.RepeatedNestedMessage); 528ffe3c632Sopenharmony_ci Assert.AreNotSame(original.RepeatedNestedMessage[0], clone.RepeatedNestedMessage[0]); 529ffe3c632Sopenharmony_ci Assert.AreEqual(original, clone); 530ffe3c632Sopenharmony_ci 531ffe3c632Sopenharmony_ci clone.RepeatedNestedMessage[0].Bb = 30; 532ffe3c632Sopenharmony_ci Assert.AreNotEqual(original, clone); 533ffe3c632Sopenharmony_ci } 534ffe3c632Sopenharmony_ci 535ffe3c632Sopenharmony_ci [Test] 536ffe3c632Sopenharmony_ci public void CloneOneofField() 537ffe3c632Sopenharmony_ci { 538ffe3c632Sopenharmony_ci var original = new TestAllTypes 539ffe3c632Sopenharmony_ci { 540ffe3c632Sopenharmony_ci OneofNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 } 541ffe3c632Sopenharmony_ci }; 542ffe3c632Sopenharmony_ci 543ffe3c632Sopenharmony_ci var clone = original.Clone(); 544ffe3c632Sopenharmony_ci Assert.AreNotSame(original, clone); 545ffe3c632Sopenharmony_ci Assert.AreEqual(original, clone); 546ffe3c632Sopenharmony_ci 547ffe3c632Sopenharmony_ci // We should have cloned the message 548ffe3c632Sopenharmony_ci original.OneofNestedMessage.Bb = 30; 549ffe3c632Sopenharmony_ci Assert.AreNotEqual(original, clone); 550ffe3c632Sopenharmony_ci } 551ffe3c632Sopenharmony_ci 552ffe3c632Sopenharmony_ci [Test] 553ffe3c632Sopenharmony_ci public void OneofProperties() 554ffe3c632Sopenharmony_ci { 555ffe3c632Sopenharmony_ci // Switch the oneof case between each of the different options, and check everything behaves 556ffe3c632Sopenharmony_ci // as expected in each case. 557ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 558ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 559ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 560ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 561ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 562ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); 563ffe3c632Sopenharmony_ci 564ffe3c632Sopenharmony_ci message.OneofString = "sample"; 565ffe3c632Sopenharmony_ci Assert.AreEqual("sample", message.OneofString); 566ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 567ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 568ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 569ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message.OneofFieldCase); 570ffe3c632Sopenharmony_ci 571ffe3c632Sopenharmony_ci var bytes = ByteString.CopyFrom(1, 2, 3); 572ffe3c632Sopenharmony_ci message.OneofBytes = bytes; 573ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 574ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 575ffe3c632Sopenharmony_ci Assert.AreEqual(bytes, message.OneofBytes); 576ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 577ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofBytes, message.OneofFieldCase); 578ffe3c632Sopenharmony_ci 579ffe3c632Sopenharmony_ci message.OneofUint32 = 20; 580ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 581ffe3c632Sopenharmony_ci Assert.AreEqual(20, message.OneofUint32); 582ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 583ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 584ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase); 585ffe3c632Sopenharmony_ci 586ffe3c632Sopenharmony_ci var nestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 25 }; 587ffe3c632Sopenharmony_ci message.OneofNestedMessage = nestedMessage; 588ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 589ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 590ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 591ffe3c632Sopenharmony_ci Assert.AreEqual(nestedMessage, message.OneofNestedMessage); 592ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofNestedMessage, message.OneofFieldCase); 593ffe3c632Sopenharmony_ci 594ffe3c632Sopenharmony_ci message.ClearOneofField(); 595ffe3c632Sopenharmony_ci Assert.AreEqual("", message.OneofString); 596ffe3c632Sopenharmony_ci Assert.AreEqual(0, message.OneofUint32); 597ffe3c632Sopenharmony_ci Assert.AreEqual(ByteString.Empty, message.OneofBytes); 598ffe3c632Sopenharmony_ci Assert.IsNull(message.OneofNestedMessage); 599ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); 600ffe3c632Sopenharmony_ci } 601ffe3c632Sopenharmony_ci 602ffe3c632Sopenharmony_ci [Test] 603ffe3c632Sopenharmony_ci public void Oneof_DefaultValuesNotEqual() 604ffe3c632Sopenharmony_ci { 605ffe3c632Sopenharmony_ci var message1 = new TestAllTypes { OneofString = "" }; 606ffe3c632Sopenharmony_ci var message2 = new TestAllTypes { OneofUint32 = 0 }; 607ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message1.OneofFieldCase); 608ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase); 609ffe3c632Sopenharmony_ci Assert.AreNotEqual(message1, message2); 610ffe3c632Sopenharmony_ci } 611ffe3c632Sopenharmony_ci 612ffe3c632Sopenharmony_ci [Test] 613ffe3c632Sopenharmony_ci public void OneofSerialization_NonDefaultValue() 614ffe3c632Sopenharmony_ci { 615ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 616ffe3c632Sopenharmony_ci message.OneofString = "this would take a bit of space"; 617ffe3c632Sopenharmony_ci message.OneofUint32 = 10; 618ffe3c632Sopenharmony_ci var bytes = message.ToByteArray(); 619ffe3c632Sopenharmony_ci Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string! 620ffe3c632Sopenharmony_ci 621ffe3c632Sopenharmony_ci var message2 = TestAllTypes.Parser.ParseFrom(bytes); 622ffe3c632Sopenharmony_ci Assert.AreEqual(message, message2); 623ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase); 624ffe3c632Sopenharmony_ci } 625ffe3c632Sopenharmony_ci 626ffe3c632Sopenharmony_ci [Test] 627ffe3c632Sopenharmony_ci public void OneofSerialization_DefaultValue() 628ffe3c632Sopenharmony_ci { 629ffe3c632Sopenharmony_ci var message = new TestAllTypes(); 630ffe3c632Sopenharmony_ci message.OneofString = "this would take a bit of space"; 631ffe3c632Sopenharmony_ci message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized 632ffe3c632Sopenharmony_ci var bytes = message.ToByteArray(); 633ffe3c632Sopenharmony_ci Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized 634ffe3c632Sopenharmony_ci 635ffe3c632Sopenharmony_ci var message2 = TestAllTypes.Parser.ParseFrom(bytes); 636ffe3c632Sopenharmony_ci Assert.AreEqual(message, message2); 637ffe3c632Sopenharmony_ci Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase); 638ffe3c632Sopenharmony_ci } 639ffe3c632Sopenharmony_ci 640ffe3c632Sopenharmony_ci [Test] 641ffe3c632Sopenharmony_ci public void DiscardUnknownFields_RealDataStillRead() 642ffe3c632Sopenharmony_ci { 643ffe3c632Sopenharmony_ci var message = SampleMessages.CreateFullTestAllTypes(); 644ffe3c632Sopenharmony_ci var stream = new MemoryStream(); 645ffe3c632Sopenharmony_ci var output = new CodedOutputStream(stream); 646ffe3c632Sopenharmony_ci var unusedFieldNumber = 23456; 647ffe3c632Sopenharmony_ci Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber)); 648ffe3c632Sopenharmony_ci output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited); 649ffe3c632Sopenharmony_ci output.WriteString("ignore me"); 650ffe3c632Sopenharmony_ci message.WriteTo(output); 651ffe3c632Sopenharmony_ci output.Flush(); 652ffe3c632Sopenharmony_ci 653ffe3c632Sopenharmony_ci stream.Position = 0; 654ffe3c632Sopenharmony_ci var parsed = TestAllTypes.Parser.ParseFrom(stream); 655ffe3c632Sopenharmony_ci // TODO(jieluo): Add test back after DiscardUnknownFields is supported 656ffe3c632Sopenharmony_ci // Assert.AreEqual(message, parsed); 657ffe3c632Sopenharmony_ci } 658ffe3c632Sopenharmony_ci 659ffe3c632Sopenharmony_ci [Test] 660ffe3c632Sopenharmony_ci public void DiscardUnknownFields_AllTypes() 661ffe3c632Sopenharmony_ci { 662ffe3c632Sopenharmony_ci // Simple way of ensuring we can skip all kinds of fields. 663ffe3c632Sopenharmony_ci var data = SampleMessages.CreateFullTestAllTypes().ToByteArray(); 664ffe3c632Sopenharmony_ci var empty = Empty.Parser.ParseFrom(data); 665ffe3c632Sopenharmony_ci // TODO(jieluo): Add test back after DiscardUnknownField is supported. 666ffe3c632Sopenharmony_ci // Assert.AreEqual(new Empty(), empty); 667ffe3c632Sopenharmony_ci } 668ffe3c632Sopenharmony_ci 669ffe3c632Sopenharmony_ci // This was originally seen as a conformance test failure. 670ffe3c632Sopenharmony_ci [Test] 671ffe3c632Sopenharmony_ci public void TruncatedMessageFieldThrows() 672ffe3c632Sopenharmony_ci { 673ffe3c632Sopenharmony_ci // 130, 3 is the message tag 674ffe3c632Sopenharmony_ci // 1 is the data length - but there's no data. 675ffe3c632Sopenharmony_ci var data = new byte[] { 130, 3, 1 }; 676ffe3c632Sopenharmony_ci Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(data)); 677ffe3c632Sopenharmony_ci } 678ffe3c632Sopenharmony_ci 679ffe3c632Sopenharmony_ci /// <summary> 680ffe3c632Sopenharmony_ci /// Demonstrates current behaviour with an extraneous end group tag - see issue 688 681ffe3c632Sopenharmony_ci /// for details; we may want to change this. 682ffe3c632Sopenharmony_ci /// </summary> 683ffe3c632Sopenharmony_ci [Test] 684ffe3c632Sopenharmony_ci public void ExtraEndGroupThrows() 685ffe3c632Sopenharmony_ci { 686ffe3c632Sopenharmony_ci var message = SampleMessages.CreateFullTestAllTypes(); 687ffe3c632Sopenharmony_ci var stream = new MemoryStream(); 688ffe3c632Sopenharmony_ci var output = new CodedOutputStream(stream); 689ffe3c632Sopenharmony_ci 690ffe3c632Sopenharmony_ci output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.WireType.Fixed32); 691ffe3c632Sopenharmony_ci output.WriteFixed32(123); 692ffe3c632Sopenharmony_ci output.WriteTag(100, WireFormat.WireType.EndGroup); 693ffe3c632Sopenharmony_ci 694ffe3c632Sopenharmony_ci output.Flush(); 695ffe3c632Sopenharmony_ci 696ffe3c632Sopenharmony_ci stream.Position = 0; 697ffe3c632Sopenharmony_ci Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Parser.ParseFrom(stream)); 698ffe3c632Sopenharmony_ci } 699ffe3c632Sopenharmony_ci 700ffe3c632Sopenharmony_ci [Test] 701ffe3c632Sopenharmony_ci public void CustomDiagnosticMessage_DirectToStringCall() 702ffe3c632Sopenharmony_ci { 703ffe3c632Sopenharmony_ci var message = new ForeignMessage { C = 31 }; 704ffe3c632Sopenharmony_ci Assert.AreEqual("{ \"c\": 31, \"@cInHex\": \"1f\" }", message.ToString()); 705ffe3c632Sopenharmony_ci Assert.AreEqual("{ \"c\": 31 }", JsonFormatter.Default.Format(message)); 706ffe3c632Sopenharmony_ci } 707ffe3c632Sopenharmony_ci 708ffe3c632Sopenharmony_ci [Test] 709ffe3c632Sopenharmony_ci public void CustomDiagnosticMessage_Nested() 710ffe3c632Sopenharmony_ci { 711ffe3c632Sopenharmony_ci var message = new TestAllTypes { SingleForeignMessage = new ForeignMessage { C = 16 } }; 712ffe3c632Sopenharmony_ci Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString()); 713ffe3c632Sopenharmony_ci Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonFormatter.Default.Format(message)); 714ffe3c632Sopenharmony_ci } 715ffe3c632Sopenharmony_ci 716ffe3c632Sopenharmony_ci [Test] 717ffe3c632Sopenharmony_ci public void CustomDiagnosticMessage_DirectToTextWriterCall() 718ffe3c632Sopenharmony_ci { 719ffe3c632Sopenharmony_ci var message = new ForeignMessage { C = 31 }; 720ffe3c632Sopenharmony_ci var writer = new StringWriter(); 721ffe3c632Sopenharmony_ci JsonFormatter.Default.Format(message, writer); 722ffe3c632Sopenharmony_ci Assert.AreEqual("{ \"c\": 31 }", writer.ToString()); 723ffe3c632Sopenharmony_ci } 724ffe3c632Sopenharmony_ci } 725ffe3c632Sopenharmony_ci} 726