diff options
author | Jon Skeet <jonskeet@google.com> | 2015-06-30 13:16:20 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-06-30 13:20:31 +0100 |
commit | f34d37a3d4d64621bc87aa0a65a05cab64062399 (patch) | |
tree | 416cceb9b343b21004b030deea069553644928d3 /csharp/src/ProtocolBuffers.Test | |
parent | b9d1d3891f4e68886398bbf0caf40229275a448a (diff) | |
download | protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.tar.gz protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.tar.bz2 protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.zip |
Tidying up and extra tests.
This is mostly just making things internal instead of public, removing and reordering a bunch of code in CodedInputStream/CodedOutputStream, and generally tidying up.
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test')
8 files changed, 589 insertions, 232 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/CodedInputStreamExtensions.cs b/csharp/src/ProtocolBuffers.Test/CodedInputStreamExtensions.cs new file mode 100644 index 00000000..408c7cb9 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/CodedInputStreamExtensions.cs @@ -0,0 +1,54 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using NUnit.Framework; + +namespace Google.Protobuf +{ + internal static class CodedInputStreamExtensions + { + public static void AssertNextTag(this CodedInputStream input, uint expectedTag) + { + uint tag; + Assert.IsTrue(input.ReadTag(out tag)); + Assert.AreEqual(expectedTag, tag); + } + + public static T ReadMessage<T>(this CodedInputStream stream, MessageParser<T> parser) + where T : IMessage<T> + { + var message = parser.CreateTemplate(); + stream.ReadMessage(message); + return message; + } + } +} diff --git a/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs b/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs index 47b5e0ac..a64994fd 100644 --- a/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs +++ b/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs @@ -35,10 +35,8 @@ #endregion
using System;
-using System.Collections.Generic;
using System.IO;
using Google.Protobuf.Collections;
-using Google.Protobuf.Descriptors;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
@@ -62,7 +60,7 @@ namespace Google.Protobuf }
/// <summary>
- /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and
+ /// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64()
/// </summary>
private static void AssertReadVarint(byte[] data, ulong value)
{
@@ -232,66 +230,26 @@ namespace Google.Protobuf Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, CodedInputStream.DecodeZigZag64(0xFFFFFFFFFFFFFFFEL));
Assert.AreEqual(unchecked((long) 0x8000000000000000L), CodedInputStream.DecodeZigZag64(0xFFFFFFFFFFFFFFFFL));
}
- /*
+
[Test]
- public void ReadWholeMessage()
+ public void ReadWholeMessage_VaryingBlockSizes()
{
- TestAllTypes message = TestUtil.GetAllSet();
+ TestAllTypes message = GeneratedMessageTest.GetSampleMessage();
byte[] rawBytes = message.ToByteArray();
- Assert.AreEqual(rawBytes.Length, message.SerializedSize);
- TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
- TestUtil.AssertAllFieldsSet(message2);
+ Assert.AreEqual(rawBytes.Length, message.CalculateSize());
+ TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(rawBytes);
+ Assert.AreEqual(message, message2);
// Try different block sizes.
for (int blockSize = 1; blockSize < 256; blockSize *= 2)
{
- message2 = TestAllTypes.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
- TestUtil.AssertAllFieldsSet(message2);
+ message2 = TestAllTypes.Parser.ParseFrom(new SmallBlockInputStream(rawBytes, blockSize));
+ Assert.AreEqual(message, message2);
}
}
-
+
[Test]
- public void SkipWholeMessage()
- {
- TestAllTypes message = TestUtil.GetAllSet();
- byte[] rawBytes = message.ToByteArray();
-
- // Create two parallel inputs. Parse one as unknown fields while using
- // skipField() to skip each field on the other. Expect the same tags.
- CodedInputStream input1 = CodedInputStream.CreateInstance(rawBytes);
- CodedInputStream input2 = CodedInputStream.CreateInstance(rawBytes);
- UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder();
-
- uint tag;
- string name;
- while (input1.ReadTag(out tag, out name))
- {
- uint tag2;
- Assert.IsTrue(input2.ReadTag(out tag2, out name));
- Assert.AreEqual(tag, tag2);
-
- unknownFields.MergeFieldFrom(tag, input1);
- input2.SkipField();
- }
- }*/
-
- /// <summary>
- /// Test that a bug in SkipRawBytes has been fixed: if the skip
- /// skips exactly up to a limit, this should bnot break things
- /// </summary>
- [Test]
- public void SkipRawBytesBug()
- {
- byte[] rawBytes = new byte[] {1, 2};
- CodedInputStream input = CodedInputStream.CreateInstance(rawBytes);
-
- int limit = input.PushLimit(1);
- input.SkipRawBytes(1);
- input.PopLimit(limit);
- Assert.AreEqual(2, input.ReadRawByte());
- }
- /*
public void ReadHugeBlob()
{
// Allocate and initialize a 1MB blob.
@@ -302,24 +260,15 @@ namespace Google.Protobuf }
// Make a message containing it.
- TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
- TestUtil.SetAllFields(builder);
- builder.SetOptionalBytes(ByteString.CopyFrom(blob));
- TestAllTypes message = builder.Build();
+ var message = new TestAllTypes { SingleBytes = ByteString.CopyFrom(blob) };
// 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());
+ TestAllTypes message2 = TestAllTypes.Parser.ParseFrom(message.ToByteString());
- 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);
- }*/
+ Assert.AreEqual(message, message2);
+ }
[Test]
public void ReadMaliciouslyLargeBlob()
@@ -461,85 +410,15 @@ namespace Google.Protobuf }
}
- enum TestNegEnum { None = 0, Value = -2 }
-
[Test]
public void TestNegativeEnum()
{
- byte[] bytes = new byte[10] { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
+ byte[] bytes = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01 };
CodedInputStream input = CodedInputStream.CreateInstance(bytes);
- Assert.AreEqual((int)TestNegEnum.Value, input.ReadEnum());
+ Assert.AreEqual((int)SampleEnum.NegativeValue, input.ReadEnum());
Assert.IsTrue(input.IsAtEnd);
}
- [Test]
- public void TestNegativeEnumPackedArray()
- {
- int arraySize = 1 + (10 * 5);
- int msgSize = 1 + 1 + arraySize;
- byte[] bytes = new byte[msgSize];
- CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
- // Length-delimited to show we want the packed representation
- uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited);
- output.WriteTag(tag);
- int size = 0;
- for (int i = 0; i >= -5; i--)
- {
- size += CodedOutputStream.ComputeEnumSize(i);
- }
- output.WriteRawVarint32((uint) size);
- for (int i = 0; i >= -5; i--)
- {
- output.WriteEnum(i);
- }
- Assert.AreEqual(0, output.SpaceLeft);
-
- CodedInputStream input = CodedInputStream.CreateInstance(bytes);
- Assert.IsTrue(input.ReadTag(out tag));
-
- RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
- values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int) x, x => (TestNegEnum) x));
-
- Assert.AreEqual(6, values.Count);
- Assert.AreEqual(TestNegEnum.None, values[0]);
- Assert.AreEqual(((TestNegEnum) (-1)), values[1]);
- Assert.AreEqual(TestNegEnum.Value, values[2]);
- Assert.AreEqual(((TestNegEnum)(-3)), values[3]);
- Assert.AreEqual(((TestNegEnum)(-4)), values[4]);
- Assert.AreEqual(((TestNegEnum)(-5)), values[5]);
- }
-
- [Test]
- public void TestNegativeEnumArray()
- {
- int arraySize = 1 + 1 + (11 * 5);
- int msgSize = arraySize;
- byte[] bytes = new byte[msgSize];
- CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
- uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint);
- for (int i = 0; i >= -5; i--)
- {
- output.WriteTag(tag);
- output.WriteEnum(i);
- }
-
- Assert.AreEqual(0, output.SpaceLeft);
-
- CodedInputStream input = CodedInputStream.CreateInstance(bytes);
- Assert.IsTrue(input.ReadTag(out tag));
-
- RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
- values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (TestNegEnum)x));
-
- Assert.AreEqual(6, values.Count);
- Assert.AreEqual(TestNegEnum.None, values[0]);
- Assert.AreEqual(((TestNegEnum)(-1)), values[1]);
- Assert.AreEqual(TestNegEnum.Value, values[2]);
- Assert.AreEqual(((TestNegEnum)(-3)), values[3]);
- Assert.AreEqual(((TestNegEnum)(-4)), values[4]);
- Assert.AreEqual(((TestNegEnum)(-5)), values[5]);
- }
-
//Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily
[Test]
public void TestSlowPathAvoidance()
diff --git a/csharp/src/ProtocolBuffers.Test/CodedOutputStreamTest.cs b/csharp/src/ProtocolBuffers.Test/CodedOutputStreamTest.cs index dd49e3d8..ab5dcbd6 100644 --- a/csharp/src/ProtocolBuffers.Test/CodedOutputStreamTest.cs +++ b/csharp/src/ProtocolBuffers.Test/CodedOutputStreamTest.cs @@ -35,9 +35,8 @@ #endregion
using System;
-using System.Collections.Generic;
using System.IO;
-using Google.Protobuf.Collections;
+using Google.Protobuf.TestProtos;
using NUnit.Framework;
namespace Google.Protobuf
@@ -195,42 +194,24 @@ namespace Google.Protobuf 0x9abcdef012345678UL);
}
- /*
[Test]
- public void WriteWholeMessage()
+ public void WriteWholeMessage_VaryingBlockSizes()
{
- TestAllTypes message = TestUtil.GetAllSet();
+ TestAllTypes message = GeneratedMessageTest.GetSampleMessage();
byte[] rawBytes = message.ToByteArray();
- TestUtil.AssertEqualBytes(TestUtil.GoldenMessage.ToByteArray(), rawBytes);
// Try different block sizes.
for (int blockSize = 1; blockSize < 256; blockSize *= 2)
{
MemoryStream rawOutput = new MemoryStream();
- CodedOutputStream output =
- CodedOutputStream.CreateInstance(rawOutput, blockSize);
+ CodedOutputStream output = CodedOutputStream.CreateInstance(rawOutput, blockSize);
message.WriteTo(output);
output.Flush();
- TestUtil.AssertEqualBytes(rawBytes, rawOutput.ToArray());
+ Assert.AreEqual(rawBytes, rawOutput.ToArray());
}
}
-
- /// <summary>
- /// Tests writing a whole message with every packed field type. Ensures the
- /// wire format of packed fields is compatible with C++.
- /// </summary>
- [Test]
- public void WriteWholePackedFieldsMessage()
- {
- TestPackedTypes message = TestUtil.GetPackedSet();
-
- byte[] rawBytes = message.ToByteArray();
- TestUtil.AssertEqualBytes(TestUtil.GetGoldenPackedFieldsMessage().ToByteArray(),
- rawBytes);
- }
- */
-
+
[Test]
public void EncodeZigZag32()
{
@@ -296,68 +277,16 @@ namespace Google.Protobuf public void TestNegativeEnumNoTag()
{
Assert.AreEqual(10, CodedOutputStream.ComputeInt32Size(-2));
- Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) TestNegEnum.Value));
+ Assert.AreEqual(10, CodedOutputStream.ComputeEnumSize((int) SampleEnum.NegativeValue));
byte[] bytes = new byte[10];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
- output.WriteEnum((int) TestNegEnum.Value);
+ output.WriteEnum((int) SampleEnum.NegativeValue);
Assert.AreEqual(0, output.SpaceLeft);
Assert.AreEqual("FE-FF-FF-FF-FF-FF-FF-FF-FF-01", BitConverter.ToString(bytes));
}
- enum TestNegEnum { None = 0, Value = -2 }
-
- /*
- [Test]
- public void TestNegativeEnumArrayPacked()
- {
- int arraySize = 1 + (10 * 5);
- int msgSize = 1 + 1 + arraySize;
- byte[] bytes = new byte[msgSize];
- CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
- output.WriteTag(8, WireFormat.WireType.LengthDelimited);
- output.WritePackedEnumArray(new RepeatedField<TestNegEnum> {
- 0, (TestNegEnum) (-1), TestNegEnum.Value, (TestNegEnum) (-3), (TestNegEnum) (-4), (TestNegEnum) (-5) });
-
- Assert.AreEqual(0, output.SpaceLeft);
-
- CodedInputStream input = CodedInputStream.CreateInstance(bytes);
- uint tag;
- Assert.IsTrue(input.ReadTag(out tag));
-
- List<int> values = new List<int>();
- input.ReadInt32Array(values);
-
- Assert.AreEqual(6, values.Count);
- for (int i = 0; i > -6; i--)
- Assert.AreEqual(i, values[Math.Abs(i)]);
- }
-
- [Test]
- public void TestNegativeEnumArray()
- {
- int arraySize = 1 + 1 + (11 * 5);
- int msgSize = arraySize;
- byte[] bytes = new byte[msgSize];
- CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
- output.WriteEnumArray(8, new RepeatedField<TestNegEnum> {
- 0, (TestNegEnum) (-1), TestNegEnum.Value, (TestNegEnum) (-3), (TestNegEnum) (-4), (TestNegEnum) (-5) });
- Assert.AreEqual(0, output.SpaceLeft);
-
- CodedInputStream input = CodedInputStream.CreateInstance(bytes);
- uint tag;
- Assert.IsTrue(input.ReadTag(out tag));
-
- List<int> values = new List<int>();
- input.ReadInt32Array(values);
-
- Assert.AreEqual(6, values.Count);
- for (int i = 0; i > -6; i--)
- Assert.AreEqual(i, values[Math.Abs(i)]);
- }
- */
-
[Test]
public void TestCodedInputOutputPosition()
{
@@ -407,7 +336,7 @@ namespace Google.Protobuf Assert.AreEqual(130, cout.Position);
cout.Flush();
}
- //Now test Input stream:
+ // Now test Input stream:
{
CodedInputStream cin = CodedInputStream.CreateInstance(new MemoryStream(bytes), new byte[50]);
uint tag;
@@ -420,8 +349,8 @@ namespace Google.Protobuf //Field 2:
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 2);
Assert.AreEqual(4, cin.Position);
- uint childlen = cin.ReadRawVarint32();
- Assert.AreEqual(120u, childlen);
+ int childlen = cin.ReadLength();
+ Assert.AreEqual(120, childlen);
Assert.AreEqual(5, cin.Position);
int oldlimit = cin.PushLimit((int)childlen);
Assert.AreEqual(5, cin.Position);
diff --git a/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs index 29945c36..988801b7 100644 --- a/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs +++ b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs @@ -1,5 +1,39 @@ -using System; +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +using System; using System.Collections.Generic; +using System.IO; +using System.Linq; using Google.Protobuf.TestProtos; using NUnit.Framework; @@ -89,5 +123,260 @@ namespace Google.Protobuf.Collections var clone = list.Clone(); clone[0] = 1; } + + [Test] + public void AddEntriesFrom_PackedInt32() + { + uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + var length = CodedOutputStream.ComputeInt32Size(10) + + CodedOutputStream.ComputeInt32Size(999) + + CodedOutputStream.ComputeInt32Size(-1000); + output.WriteTag(packedTag); + output.WriteRawVarint32((uint) length); + output.WriteInt32(10); + output.WriteInt32(999); + output.WriteInt32(-1000); + output.Flush(); + stream.Position = 0; + + // Deliberately "expecting" a non-packed tag, but we detect that the data is + // actually packed. + uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var field = new RepeatedField<int>(); + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(packedTag); + field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag)); + CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void AddEntriesFrom_NonPackedInt32() + { + uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + output.WriteTag(nonPackedTag); + output.WriteInt32(10); + output.WriteTag(nonPackedTag); + output.WriteInt32(999); + output.WriteTag(nonPackedTag); + output.WriteInt32(-1000); // Just for variety... + output.Flush(); + stream.Position = 0; + + // Deliberately "expecting" a packed tag, but we detect that the data is + // actually not packed. + uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var field = new RepeatedField<int>(); + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(nonPackedTag); + field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag)); + CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void AddEntriesFrom_String() + { + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + output.WriteTag(tag); + output.WriteString("Foo"); + output.WriteTag(tag); + output.WriteString(""); + output.WriteTag(tag); + output.WriteString("Bar"); + output.Flush(); + stream.Position = 0; + + var field = new RepeatedField<string>(); + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + field.AddEntriesFrom(input, FieldCodec.ForString(tag)); + CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void AddEntriesFrom_Message() + { + var message1 = new ForeignMessage { C = 2000 }; + var message2 = new ForeignMessage { C = -250 }; + + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + output.WriteTag(tag); + output.WriteMessage(message1); + output.WriteTag(tag); + output.WriteMessage(message2); + output.Flush(); + stream.Position = 0; + + var field = new RepeatedField<ForeignMessage>(); + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); + CollectionAssert.AreEqual(new[] { message1, message2}, field); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void WriteTo_PackedInt32() + { + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var field = new RepeatedField<int> { 10, 1000, 1000000 }; + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + field.WriteTo(output, FieldCodec.ForInt32(tag)); + output.Flush(); + stream.Position = 0; + + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + var length = input.ReadLength(); + Assert.AreEqual(10, input.ReadInt32()); + Assert.AreEqual(1000, input.ReadInt32()); + Assert.AreEqual(1000000, input.ReadInt32()); + Assert.IsTrue(input.IsAtEnd); + Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length); + } + + [Test] + public void WriteTo_NonPackedInt32() + { + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); + var field = new RepeatedField<int> { 10, 1000, 1000000}; + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + field.WriteTo(output, FieldCodec.ForInt32(tag)); + output.Flush(); + stream.Position = 0; + + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + Assert.AreEqual(10, input.ReadInt32()); + input.AssertNextTag(tag); + Assert.AreEqual(1000, input.ReadInt32()); + input.AssertNextTag(tag); + Assert.AreEqual(1000000, input.ReadInt32()); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void WriteTo_String() + { + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var field = new RepeatedField<string> { "Foo", "", "Bar" }; + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + field.WriteTo(output, FieldCodec.ForString(tag)); + output.Flush(); + stream.Position = 0; + + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + Assert.AreEqual("Foo", input.ReadString()); + input.AssertNextTag(tag); + Assert.AreEqual("", input.ReadString()); + input.AssertNextTag(tag); + Assert.AreEqual("Bar", input.ReadString()); + Assert.IsTrue(input.IsAtEnd); + } + + [Test] + public void WriteTo_Message() + { + var message1 = new ForeignMessage { C = 20 }; + var message2 = new ForeignMessage { C = 25 }; + uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); + var field = new RepeatedField<ForeignMessage> { message1, message2 }; + var stream = new MemoryStream(); + var output = CodedOutputStream.CreateInstance(stream); + field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); + output.Flush(); + stream.Position = 0; + + var input = CodedInputStream.CreateInstance(stream); + input.AssertNextTag(tag); + Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser)); + input.AssertNextTag(tag); + Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser)); + Assert.IsTrue(input.IsAtEnd); + } + + + [Test] + public void TestNegativeEnumArray() + { + int arraySize = 1 + 1 + (11 * 5); + int msgSize = arraySize; + byte[] bytes = new byte[msgSize]; + CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); + uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint); + for (int i = 0; i >= -5; i--) + { + output.WriteTag(tag); + output.WriteEnum(i); + } + + Assert.AreEqual(0, output.SpaceLeft); + + CodedInputStream input = CodedInputStream.CreateInstance(bytes); + Assert.IsTrue(input.ReadTag(out tag)); + + RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); + values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); + + Assert.AreEqual(6, values.Count); + Assert.AreEqual(SampleEnum.None, values[0]); + Assert.AreEqual(((SampleEnum)(-1)), values[1]); + Assert.AreEqual(SampleEnum.NegativeValue, values[2]); + Assert.AreEqual(((SampleEnum)(-3)), values[3]); + Assert.AreEqual(((SampleEnum)(-4)), values[4]); + Assert.AreEqual(((SampleEnum)(-5)), values[5]); + } + + + [Test] + public void TestNegativeEnumPackedArray() + { + int arraySize = 1 + (10 * 5); + int msgSize = 1 + 1 + arraySize; + byte[] bytes = new byte[msgSize]; + CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); + // Length-delimited to show we want the packed representation + uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited); + output.WriteTag(tag); + int size = 0; + for (int i = 0; i >= -5; i--) + { + size += CodedOutputStream.ComputeEnumSize(i); + } + output.WriteRawVarint32((uint)size); + for (int i = 0; i >= -5; i--) + { + output.WriteEnum(i); + } + Assert.AreEqual(0, output.SpaceLeft); + + CodedInputStream input = CodedInputStream.CreateInstance(bytes); + Assert.IsTrue(input.ReadTag(out tag)); + + RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); + values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); + + Assert.AreEqual(6, values.Count); + Assert.AreEqual(SampleEnum.None, values[0]); + Assert.AreEqual(((SampleEnum)(-1)), values[1]); + Assert.AreEqual(SampleEnum.NegativeValue, values[2]); + Assert.AreEqual(((SampleEnum)(-3)), values[3]); + Assert.AreEqual(((SampleEnum)(-4)), values[4]); + Assert.AreEqual(((SampleEnum)(-5)), values[5]); + } } } diff --git a/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs b/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs index 50141621..a14040d1 100644 --- a/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs +++ b/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs @@ -63,15 +63,21 @@ namespace Google.Protobuf }; [Test, TestCaseSource("Codecs")] - public void RoundTrip(ICodecTestData codec) + public void RoundTripWithTag(ICodecTestData codec) { - codec.TestRoundTrip(); + codec.TestRoundTripWithTag(); + } + + [Test, TestCaseSource("Codecs")] + public void RoundTripRaw(ICodecTestData codec) + { + codec.TestRoundTripRaw(); } [Test, TestCaseSource("Codecs")] public void CalculateSize(ICodecTestData codec) { - codec.TestCalculateSize(); + codec.TestCalculateSizeWithTag(); } [Test, TestCaseSource("Codecs")] @@ -82,8 +88,9 @@ namespace Google.Protobuf public interface ICodecTestData { - void TestRoundTrip(); - void TestCalculateSize(); + void TestRoundTripRaw(); + void TestRoundTripWithTag(); + void TestCalculateSizeWithTag(); void TestDefaultValue(); } @@ -100,7 +107,19 @@ namespace Google.Protobuf this.name = name; } - public void TestRoundTrip() + public void TestRoundTripRaw() + { + var stream = new MemoryStream(); + var codedOutput = CodedOutputStream.CreateInstance(stream); + codec.ValueWriter(codedOutput, sampleValue); + codedOutput.Flush(); + stream.Position = 0; + var codedInput = CodedInputStream.CreateInstance(stream); + Assert.AreEqual(sampleValue, codec.ValueReader(codedInput)); + Assert.IsTrue(codedInput.IsAtEnd); + } + + public void TestRoundTripWithTag() { var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); @@ -108,14 +127,12 @@ namespace Google.Protobuf codedOutput.Flush(); stream.Position = 0; var codedInput = CodedInputStream.CreateInstance(stream); - uint tag; - Assert.IsTrue(codedInput.ReadTag(out tag)); - Assert.AreEqual(codec.Tag, tag); + codedInput.AssertNextTag(codec.Tag); Assert.AreEqual(sampleValue, codec.Read(codedInput)); Assert.IsTrue(codedInput.IsAtEnd); } - public void TestCalculateSize() + public void TestCalculateSizeWithTag() { var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); @@ -126,6 +143,7 @@ namespace Google.Protobuf public void TestDefaultValue() { + // WriteTagAndValue ignores default values var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); codec.WriteTagAndValue(codedOutput, codec.DefaultValue); @@ -136,9 +154,20 @@ namespace Google.Protobuf { Assert.AreEqual(default(T), codec.DefaultValue); } - } - public string Description { get { return name; } } + // The plain ValueWriter/ValueReader delegates don't. + if (codec.DefaultValue != null) // This part isn't appropriate for message types. + { + codedOutput = CodedOutputStream.CreateInstance(stream); + codec.ValueWriter(codedOutput, codec.DefaultValue); + codedOutput.Flush(); + Assert.AreNotEqual(0, stream.Position); + Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); + stream.Position = 0; + var codedInput = CodedInputStream.CreateInstance(stream); + Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput)); + } + } public override string ToString() { diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs index f8668662..a094e46b 100644 --- a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs +++ b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs @@ -10,6 +10,61 @@ namespace Google.Protobuf /// </summary>
public class GeneratedMessageTest
{
+ /// <summary>
+ /// Returns a sample TestAllTypes with all fields populated
+ /// </summary>
+ public static TestAllTypes GetSampleMessage()
+ {
+ return new TestAllTypes
+ {
+ SingleBool = true,
+ SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
+ SingleDouble = 23.5,
+ SingleFixed32 = 23,
+ SingleFixed64 = 1234567890123,
+ SingleFloat = 12.25f,
+ SingleForeignEnum = ForeignEnum.FOREIGN_BAR,
+ SingleForeignMessage = new ForeignMessage { C = 10 },
+ SingleImportEnum = ImportEnum.IMPORT_BAZ,
+ SingleImportMessage = new ImportMessage { D = 20 },
+ SingleInt32 = 100,
+ SingleInt64 = 3210987654321,
+ SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO,
+ SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
+ SinglePublicImportMessage = new PublicImportMessage { E = 54 },
+ SingleSfixed32 = -123,
+ SingleSfixed64 = -12345678901234,
+ SingleSint32 = -456,
+ SingleSint64 = -12345678901235,
+ SingleString = "test",
+ SingleUint32 = uint.MaxValue,
+ SingleUint64 = ulong.MaxValue,
+ RepeatedBool = { true, false },
+ RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) },
+ RepeatedDouble = { -12.25, 23.5 },
+ RepeatedFixed32 = { uint.MaxValue, 23 },
+ RepeatedFixed64 = { ulong.MaxValue, 1234567890123 },
+ RepeatedFloat = { 100f, 12.25f },
+ RepeatedForeignEnum = { ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_BAR },
+ RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } },
+ RepeatedImportEnum = { ImportEnum.IMPORT_BAZ, ImportEnum.IMPORT_ENUM_UNSPECIFIED },
+ RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
+ RepeatedInt32 = { 100, 200 },
+ RepeatedInt64 = { 3210987654321, long.MaxValue },
+ RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAllTypes.Types.NestedEnum.NEG },
+ RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
+ RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
+ RepeatedSfixed32 = { -123, 123 },
+ RepeatedSfixed64 = { -12345678901234, 12345678901234 },
+ RepeatedSint32 = { -456, 100 },
+ RepeatedSint64 = { -12345678901235, 123 },
+ RepeatedString = { "foo", "bar" },
+ RepeatedUint32 = { uint.MaxValue, uint.MinValue },
+ RepeatedUint64 = { ulong.MaxValue, uint.MinValue },
+ OneofString = "Oneof string"
+ };
+ }
+
[Test]
public void EmptyMessageFieldDistinctFromMissingMessageField()
{
@@ -485,5 +540,83 @@ namespace Google.Protobuf Assert.Throws<InvalidOperationException>(() => frozen.RepeatedDouble.Add(0.0));
Assert.Throws<InvalidOperationException>(() => frozen.RepeatedNestedMessage.Add(new TestAllTypes.Types.NestedMessage()));
}
+
+ [Test]
+ public void OneofProperties()
+ {
+ // Switch the oneof case between each of the different options, and check everything behaves
+ // as expected in each case.
+ var message = new TestAllTypes();
+ Assert.AreEqual("", message.OneofString);
+ Assert.AreEqual(0, message.OneofUint32);
+ Assert.AreEqual(ByteString.Empty, message.OneofBytes);
+ Assert.IsNull(message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
+
+ message.OneofString = "sample";
+ Assert.AreEqual("sample", message.OneofString);
+ Assert.AreEqual(0, message.OneofUint32);
+ Assert.AreEqual(ByteString.Empty, message.OneofBytes);
+ Assert.IsNull(message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofString, message.OneofFieldCase);
+
+ var bytes = ByteString.CopyFrom(1, 2, 3);
+ message.OneofBytes = bytes;
+ Assert.AreEqual("", message.OneofString);
+ Assert.AreEqual(0, message.OneofUint32);
+ Assert.AreEqual(bytes, message.OneofBytes);
+ Assert.IsNull(message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofBytes, message.OneofFieldCase);
+
+ message.OneofUint32 = 20;
+ Assert.AreEqual("", message.OneofString);
+ Assert.AreEqual(20, message.OneofUint32);
+ Assert.AreEqual(ByteString.Empty, message.OneofBytes);
+ Assert.IsNull(message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message.OneofFieldCase);
+
+ var nestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 25 };
+ message.OneofNestedMessage = nestedMessage;
+ Assert.AreEqual("", message.OneofString);
+ Assert.AreEqual(0, message.OneofUint32);
+ Assert.AreEqual(ByteString.Empty, message.OneofBytes);
+ Assert.AreEqual(nestedMessage, message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofNestedMessage, message.OneofFieldCase);
+
+ message.ClearOneofField();
+ Assert.AreEqual("", message.OneofString);
+ Assert.AreEqual(0, message.OneofUint32);
+ Assert.AreEqual(ByteString.Empty, message.OneofBytes);
+ Assert.IsNull(message.OneofNestedMessage);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
+ }
+
+ [Test]
+ public void OneofSerialization_NonDefaultValue()
+ {
+ var message = new TestAllTypes();
+ message.OneofString = "this would take a bit of space";
+ message.OneofUint32 = 10;
+ var bytes = message.ToByteArray();
+ Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!
+
+ var message2 = TestAllTypes.Parser.ParseFrom(bytes);
+ Assert.AreEqual(message, message2);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
+ }
+
+ [Test]
+ public void OneofSerialization_DefaultValue()
+ {
+ var message = new TestAllTypes();
+ message.OneofString = "this would take a bit of space";
+ message.OneofUint32 = 0; // This is the default value for UInt32; normally wouldn't be serialized
+ var bytes = message.ToByteArray();
+ Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized
+
+ var message2 = TestAllTypes.Parser.ParseFrom(bytes);
+ Assert.AreEqual(message, message2);
+ Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, message2.OneofFieldCase);
+ }
}
}
diff --git a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj index 80b504aa..ae7d7575 100644 --- a/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj +++ b/csharp/src/ProtocolBuffers.Test/ProtocolBuffers.Test.csproj @@ -72,6 +72,7 @@ </ItemGroup>
<ItemGroup>
<Compile Include="ByteStringTest.cs" />
+ <Compile Include="CodedInputStreamExtensions.cs" />
<Compile Include="CodedInputStreamTest.cs" />
<Compile Include="CodedOutputStreamTest.cs" />
<Compile Include="EqualityTester.cs" />
@@ -79,6 +80,7 @@ <Compile Include="GeneratedMessageTest.cs" />
<Compile Include="Collections\MapFieldTest.cs" />
<Compile Include="Collections\RepeatedFieldTest.cs" />
+ <Compile Include="SampleEnum.cs" />
<Compile Include="TestProtos\MapUnittestProto3.cs" />
<Compile Include="TestProtos\UnittestImportProto3.cs" />
<Compile Include="TestProtos\UnittestImportPublicProto3.cs" />
diff --git a/csharp/src/ProtocolBuffers.Test/SampleEnum.cs b/csharp/src/ProtocolBuffers.Test/SampleEnum.cs new file mode 100644 index 00000000..001f9b08 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/SampleEnum.cs @@ -0,0 +1,42 @@ +#region Copyright notice and license +// Protocol Buffers - Google's data interchange format +// Copyright 2015 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#endregion + +namespace Google.Protobuf +{ + // Just a sample enum with positive and negative values to be used in tests. + internal enum SampleEnum + { + NegativeValue = -2, + None = 0, + PositiveValue = 3 + } +}
\ No newline at end of file |