Commit b9d1d389 authored by Jon Skeet's avatar Jon Skeet

Tests changed enough to build

parent f2a27cc2
...@@ -479,22 +479,34 @@ namespace Google.Protobuf ...@@ -479,22 +479,34 @@ namespace Google.Protobuf
int msgSize = 1 + 1 + arraySize; int msgSize = 1 + 1 + arraySize;
byte[] bytes = new byte[msgSize]; byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
output.WriteTag(8, WireFormat.WireType.LengthDelimited); // Length-delimited to show we want the packed representation
output.WritePackedInt32Array(new RepeatedField<int> { 0, -1, -2, -3, -4, -5 }); 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); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes); CodedInputStream input = CodedInputStream.CreateInstance(bytes);
uint tag;
Assert.IsTrue(input.ReadTag(out tag)); Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>(); RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
input.ReadEnumArray(values); values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int) x, x => (TestNegEnum) x));
Assert.AreEqual(6, values.Count); Assert.AreEqual(6, values.Count);
Assert.AreEqual(TestNegEnum.None, values[0]); Assert.AreEqual(TestNegEnum.None, values[0]);
Assert.AreEqual(((TestNegEnum) (-1)), values[1]);
Assert.AreEqual(TestNegEnum.Value, values[2]); Assert.AreEqual(TestNegEnum.Value, values[2]);
// TODO(jonskeet): Test unknown value preservation Assert.AreEqual(((TestNegEnum)(-3)), values[3]);
Assert.AreEqual(((TestNegEnum)(-4)), values[4]);
Assert.AreEqual(((TestNegEnum)(-5)), values[5]);
} }
[Test] [Test]
...@@ -504,21 +516,28 @@ namespace Google.Protobuf ...@@ -504,21 +516,28 @@ namespace Google.Protobuf
int msgSize = arraySize; int msgSize = arraySize;
byte[] bytes = new byte[msgSize]; byte[] bytes = new byte[msgSize];
CodedOutputStream output = CodedOutputStream.CreateInstance(bytes); CodedOutputStream output = CodedOutputStream.CreateInstance(bytes);
output.WriteInt32Array(8, new RepeatedField<int> { 0, -1, -2, -3, -4, -5 }); 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); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = CodedInputStream.CreateInstance(bytes); CodedInputStream input = CodedInputStream.CreateInstance(bytes);
uint tag;
Assert.IsTrue(input.ReadTag(out tag)); Assert.IsTrue(input.ReadTag(out tag));
RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>(); RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
input.ReadEnumArray(values); values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (TestNegEnum)x));
Assert.AreEqual(6, values.Count); Assert.AreEqual(6, values.Count);
Assert.AreEqual(TestNegEnum.None, values[0]); Assert.AreEqual(TestNegEnum.None, values[0]);
Assert.AreEqual(((TestNegEnum)(-1)), values[1]);
Assert.AreEqual(TestNegEnum.Value, values[2]); Assert.AreEqual(TestNegEnum.Value, values[2]);
// TODO(jonskeet): Test unknown value preservation 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 //Issue 71: CodedInputStream.ReadBytes go to slow path unnecessarily
......
...@@ -308,6 +308,7 @@ namespace Google.Protobuf ...@@ -308,6 +308,7 @@ namespace Google.Protobuf
enum TestNegEnum { None = 0, Value = -2 } enum TestNegEnum { None = 0, Value = -2 }
/*
[Test] [Test]
public void TestNegativeEnumArrayPacked() public void TestNegativeEnumArrayPacked()
{ {
...@@ -355,6 +356,7 @@ namespace Google.Protobuf ...@@ -355,6 +356,7 @@ namespace Google.Protobuf
for (int i = 0; i > -6; i--) for (int i = 0; i > -6; i--)
Assert.AreEqual(i, values[Math.Abs(i)]); Assert.AreEqual(i, values[Math.Abs(i)]);
} }
*/
[Test] [Test]
public void TestCodedInputOutputPosition() public void TestCodedInputOutputPosition()
......
...@@ -104,7 +104,7 @@ namespace Google.Protobuf ...@@ -104,7 +104,7 @@ namespace Google.Protobuf
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = CodedOutputStream.CreateInstance(stream);
codec.Write(codedOutput, sampleValue); codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush(); codedOutput.Flush();
stream.Position = 0; stream.Position = 0;
var codedInput = CodedInputStream.CreateInstance(stream); var codedInput = CodedInputStream.CreateInstance(stream);
...@@ -119,19 +119,19 @@ namespace Google.Protobuf ...@@ -119,19 +119,19 @@ namespace Google.Protobuf
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = CodedOutputStream.CreateInstance(stream);
codec.Write(codedOutput, sampleValue); codec.WriteTagAndValue(codedOutput, sampleValue);
codedOutput.Flush(); codedOutput.Flush();
Assert.AreEqual(stream.Position, codec.CalculateSize(sampleValue)); Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue));
} }
public void TestDefaultValue() public void TestDefaultValue()
{ {
var stream = new MemoryStream(); var stream = new MemoryStream();
var codedOutput = CodedOutputStream.CreateInstance(stream); var codedOutput = CodedOutputStream.CreateInstance(stream);
codec.Write(codedOutput, codec.DefaultValue); codec.WriteTagAndValue(codedOutput, codec.DefaultValue);
codedOutput.Flush(); codedOutput.Flush();
Assert.AreEqual(0, stream.Position); Assert.AreEqual(0, stream.Position);
Assert.AreEqual(0, codec.CalculateSize(codec.DefaultValue)); Assert.AreEqual(0, codec.CalculateSizeWithTag(codec.DefaultValue));
if (typeof(T).IsValueType) if (typeof(T).IsValueType)
{ {
Assert.AreEqual(default(T), codec.DefaultValue); Assert.AreEqual(default(T), codec.DefaultValue);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment