Commit 1872d080 authored by Jon Skeet's avatar Jon Skeet

Fixed bug spotted by danielr

parent ad74853e
...@@ -155,8 +155,9 @@ namespace Google.ProtocolBuffers.ProtoGen { ...@@ -155,8 +155,9 @@ namespace Google.ProtocolBuffers.ProtoGen {
writer.WriteLine("size += dataSize;"); writer.WriteLine("size += dataSize;");
int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber); int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber);
if (Descriptor.IsPacked) { if (Descriptor.IsPacked) {
writer.WriteLine("size += {0};", tagSize); writer.WriteLine("if ({0}_.Count != 0) {{", Name);
writer.WriteLine("size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);"); writer.WriteLine(" size += {0} + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);", tagSize);
writer.WriteLine("}");
} else { } else {
writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name); writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name);
} }
......
...@@ -452,5 +452,11 @@ namespace Google.ProtocolBuffers { ...@@ -452,5 +452,11 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(UnitTestProtoFile.RepeatedNestedMessageExtensionFieldNumber, 48); Assert.AreEqual(UnitTestProtoFile.RepeatedNestedMessageExtensionFieldNumber, 48);
Assert.AreEqual(UnitTestProtoFile.RepeatedNestedEnumExtensionFieldNumber, 51); Assert.AreEqual(UnitTestProtoFile.RepeatedNestedEnumExtensionFieldNumber, 51);
} }
[Test]
public void EmptyPackedValue() {
TestPackedTypes empty = new TestPackedTypes.Builder().Build();
Assert.AreEqual(0, empty.SerializedSize);
}
} }
} }
...@@ -12765,8 +12765,9 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12765,8 +12765,9 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedInt32_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedInt32MemoizedSerializedSize = dataSize; packedInt32MemoizedSerializedSize = dataSize;
} }
{ {
...@@ -12775,8 +12776,9 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12775,8 +12776,9 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedInt64_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedInt64MemoizedSerializedSize = dataSize; packedInt64MemoizedSerializedSize = dataSize;
} }
{ {
...@@ -12785,8 +12787,9 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12785,8 +12787,9 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeUInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeUInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedUint32_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedUint32MemoizedSerializedSize = dataSize; packedUint32MemoizedSerializedSize = dataSize;
} }
{ {
...@@ -12795,8 +12798,9 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12795,8 +12798,9 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeUInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeUInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedUint64_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedUint64MemoizedSerializedSize = dataSize; packedUint64MemoizedSerializedSize = dataSize;
} }
{ {
...@@ -12805,8 +12809,9 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12805,8 +12809,9 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeSInt32SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeSInt32SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedSint32_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedSint32MemoizedSerializedSize = dataSize; packedSint32MemoizedSerializedSize = dataSize;
} }
{ {
...@@ -12815,64 +12820,72 @@ namespace Google.ProtocolBuffers.TestProtos { ...@@ -12815,64 +12820,72 @@ namespace Google.ProtocolBuffers.TestProtos {
dataSize += pb::CodedOutputStream.ComputeSInt64SizeNoTag(element); dataSize += pb::CodedOutputStream.ComputeSInt64SizeNoTag(element);
} }
size += dataSize; size += dataSize;
size += 2; if (packedSint64_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedSint64MemoizedSerializedSize = dataSize; packedSint64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * packedFixed32_.Count; dataSize = 4 * packedFixed32_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedFixed32_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedFixed32MemoizedSerializedSize = dataSize; packedFixed32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * packedFixed64_.Count; dataSize = 8 * packedFixed64_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedFixed64_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedFixed64MemoizedSerializedSize = dataSize; packedFixed64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * packedSfixed32_.Count; dataSize = 4 * packedSfixed32_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedSfixed32_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedSfixed32MemoizedSerializedSize = dataSize; packedSfixed32MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * packedSfixed64_.Count; dataSize = 8 * packedSfixed64_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedSfixed64_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedSfixed64MemoizedSerializedSize = dataSize; packedSfixed64MemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 4 * packedFloat_.Count; dataSize = 4 * packedFloat_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedFloat_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedFloatMemoizedSerializedSize = dataSize; packedFloatMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 8 * packedDouble_.Count; dataSize = 8 * packedDouble_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedDouble_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedDoubleMemoizedSerializedSize = dataSize; packedDoubleMemoizedSerializedSize = dataSize;
} }
{ {
int dataSize = 0; int dataSize = 0;
dataSize = 1 * packedBool_.Count; dataSize = 1 * packedBool_.Count;
size += dataSize; size += dataSize;
size += 2; if (packedBool_.Count != 0) {
size += pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize); size += 2 + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);
}
packedBoolMemoizedSerializedSize = dataSize; packedBoolMemoizedSerializedSize = dataSize;
} }
{ {
......
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