Fix enum writing.

Enums use varint, so if the value is negative it should end up being longer.

This was caught my new conformance test cases.
This commit is contained in:
Thomas Van Lenten 2019-08-26 16:29:24 -04:00
parent 1be79eefd5
commit b273cba192
3 changed files with 12 additions and 18 deletions

View File

@ -1,16 +1,2 @@
# JSON input or output tests are skipped (in conformance_objc.m) as mobile
# platforms don't support JSON wire format to avoid code bloat.
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataScalarBinary.ENUM[3].ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataScalarBinary.ENUM[3].ProtobufOutput

View File

@ -374,12 +374,12 @@ static void GPBWriteRawLittleEndian64(GPBOutputBufferState *state,
}
- (void)writeEnumNoTag:(int32_t)value {
GPBWriteRawVarint32(&state_, value);
GPBWriteInt32NoTag(&state_, value);
}
- (void)writeEnum:(int32_t)fieldNumber value:(int32_t)value {
GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint);
GPBWriteRawVarint32(&state_, value);
GPBWriteInt32NoTag(&state_, value);
}
- (void)writeSFixed32NoTag:(int32_t)value {
@ -1053,7 +1053,7 @@ size_t GPBComputeUInt32SizeNoTag(int32_t value) {
}
size_t GPBComputeEnumSizeNoTag(int32_t value) {
return GPBComputeRawVarint32Size(value);
return GPBComputeInt32SizeNoTag(value);
}
size_t GPBComputeSFixed32SizeNoTag(int32_t value) {

View File

@ -237,7 +237,15 @@
}
- (void)testWriteVarint5 {
// 2961488830
// The sign/nosign distinction is done here because normally varints are
// around 64bit values, but for some cases a 32bit value is forced with
// with the sign bit (tags, uint32, etc.)
// 1887747006 (no sign bit)
[self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x07)
value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
(0x04 << 21) | (0x07LL << 28)];
// 2961488830 (sign bit)
[self assertWriteVarint:bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b)
value:(0x3e << 0) | (0x77 << 7) | (0x12 << 14) |
(0x04 << 21) | (0x0bLL << 28)];