Skip to content
Permalink
Browse files

Conformance packed (#6447)

* Add conformance tests for explicit packed/unpacked fields

* Fix typo

* Update failure lists

* Update failure list

* Use enum class to make enum scoped
  • Loading branch information...
TeBoring committed Aug 2, 2019
1 parent d1eeb85 commit 0c4b607e98349bea40e6d1a22843b5a82118f312
@@ -151,18 +151,43 @@ string tag(uint32_t fieldnum, char wire_type) {

#define UNKNOWN_FIELD 666

const FieldDescriptor* GetFieldForType(FieldDescriptor::Type type,
bool repeated, bool is_proto3) {

enum class Packed {
UNSPECIFIED = 0,
TRUE = 1,
FALSE = 2,
};

const FieldDescriptor* GetFieldForType(
FieldDescriptor::Type type, bool repeated, bool is_proto3,
Packed packed = Packed::UNSPECIFIED) {
const Descriptor* d = is_proto3 ?
TestAllTypesProto3().GetDescriptor() : TestAllTypesProto2().GetDescriptor();
for (int i = 0; i < d->field_count(); i++) {
const FieldDescriptor* f = d->field(i);
if (f->type() == type && f->is_repeated() == repeated) {
if (packed == Packed::TRUE && !f->is_packed() ||
packed == Packed::FALSE && f->is_packed()) {
continue;
}
return f;
}
}
GOOGLE_LOG(FATAL) << "Couldn't find field with type " << (int)type;

string packed_string = "";
const string repeated_string = repeated ? "Repeated " : "Singular ";
const string proto_string = is_proto3 ? "Proto3" : "Proto2";
if (packed == Packed::TRUE) {
packed_string = "Packed ";
}
if (packed == Packed::FALSE) {
packed_string = "Unpacked ";
}
GOOGLE_LOG(FATAL) << "Couldn't find field with type: "
<< repeated_string.c_str()
<< packed_string.c_str()
<< FieldDescriptor::TypeName(type)
<< " for "
<< proto_string.c_str();
return nullptr;
}

@@ -633,57 +658,114 @@ void BinaryAndJsonConformanceSuite::TestValidDataForType(

// Test repeated fields.
if (FieldDescriptor::IsTypePackable(type)) {
string packed_proto;
string unpacked_proto;
const FieldDescriptor* packed_field =
GetFieldForType(type, true, is_proto3, Packed::TRUE);
const FieldDescriptor* unpacked_field =
GetFieldForType(type, true, is_proto3, Packed::FALSE);

string default_proto_packed;
string default_proto_unpacked;
string default_proto_packed_expected;
string default_proto_unpacked_expected;
string packed_proto_packed;
string packed_proto_unpacked;
string packed_proto_expected;
string unpacked_proto_packed;
string unpacked_proto_unpacked;
string unpacked_proto_expected;

for (size_t i = 0; i < values.size(); i++) {
unpacked_proto +=
default_proto_unpacked +=
cat(tag(rep_field->number(), wire_type), values[i].first);
unpacked_proto_expected +=
default_proto_unpacked_expected +=
cat(tag(rep_field->number(), wire_type), values[i].second);
packed_proto += values[i].first;
default_proto_packed += values[i].first;
default_proto_packed_expected += values[i].second;
packed_proto_unpacked +=
cat(tag(packed_field->number(), wire_type), values[i].first);
packed_proto_packed += values[i].first;
packed_proto_expected += values[i].second;
unpacked_proto_unpacked +=
cat(tag(unpacked_field->number(), wire_type), values[i].first);
unpacked_proto_packed += values[i].first;
unpacked_proto_expected +=
cat(tag(unpacked_field->number(), wire_type), values[i].second);
}
packed_proto =
default_proto_packed =
cat(tag(rep_field->number(),
WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
delim(packed_proto));
packed_proto_expected =
delim(default_proto_packed));
default_proto_packed_expected =
cat(tag(rep_field->number(),
WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
delim(default_proto_packed_expected));
packed_proto_packed =
cat(tag(packed_field->number(),
WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
delim(packed_proto_packed));
packed_proto_expected =
cat(tag(packed_field->number(),
WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
delim(packed_proto_expected));
unpacked_proto_packed =
cat(tag(unpacked_field->number(),
WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
delim(unpacked_proto_packed));


std::unique_ptr<Message> test_message = NewTestMessage(is_proto3);
test_message->MergeFromString(packed_proto_expected);
test_message->MergeFromString(default_proto_packed_expected);
string text = test_message->DebugString();

// Ensures both packed and unpacked data can be parsed.
RunValidProtobufTest(
StrCat("ValidDataRepeated", type_name, ".UnpackedInput"),
REQUIRED, unpacked_proto, text, is_proto3);
REQUIRED, default_proto_unpacked, text, is_proto3);
RunValidProtobufTest(
StrCat("ValidDataRepeated", type_name, ".PackedInput"),
REQUIRED, packed_proto, text, is_proto3);
REQUIRED, default_proto_packed, text, is_proto3);

// proto2 should encode as unpacked by default and proto3 should encode as
// packed by default.
string expected_proto =
rep_field->is_packed() ? packed_proto_expected :
unpacked_proto_expected;
rep_field->is_packed() ? default_proto_packed_expected :
default_proto_unpacked_expected;
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".UnpackedInput.DefaultOutput"),
RECOMMENDED,
unpacked_proto,
default_proto_unpacked,
expected_proto, is_proto3);
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".PackedInput.DefaultOutput"),
RECOMMENDED,
packed_proto,
default_proto_packed,
expected_proto, is_proto3);
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".UnpackedInput.PackedOutput"),
RECOMMENDED,
packed_proto_unpacked,
packed_proto_expected, is_proto3);
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".PackedInput.PackedOutput"),
RECOMMENDED,
packed_proto_packed,
packed_proto_expected, is_proto3);
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".UnpackedInput.UnpackedOutput"),
RECOMMENDED,
unpacked_proto_unpacked,
unpacked_proto_expected, is_proto3);
RunValidBinaryProtobufTest(
StrCat("ValidDataRepeated", type_name,
".PackedInput.UnpackedOutput"),
RECOMMENDED,
unpacked_proto_packed,
unpacked_proto_expected, is_proto3);
} else {
string proto;
string expected_proto;
@@ -1,31 +1,87 @@
Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.DOUBLE.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.DOUBLE.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.DOUBLE.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.ENUM.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED32.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED64.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FIXED64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FLOAT.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FLOAT.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.FLOAT.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT64.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.INT64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED32.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED64.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SFIXED64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT32.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT64.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.SINT64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT64.PackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto2.ProtobufInput.ValidDataRepeated.UINT64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.DOUBLE.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.DOUBLE.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.DOUBLE.UnpackedInput.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.FIXED32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FIXED32.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FIXED32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FIXED64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FIXED64.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FIXED64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.FLOAT.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT64.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.INT64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED32.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED64.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SFIXED64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT32.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT64.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.SINT64.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT32.UnpackedInput.PackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT64.PackedInput.UnpackedOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT64.UnpackedInput.DefaultOutput.ProtobufOutput
Recommended.Proto3.ProtobufInput.ValidDataRepeated.UINT64.UnpackedInput.PackedOutput.ProtobufOutput
Required.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.ProtobufOutput
Required.Proto2.ProtobufInput.ValidDataRepeated.DOUBLE.PackedInput.ProtobufOutput
Required.Proto2.ProtobufInput.ValidDataRepeated.ENUM.PackedInput.ProtobufOutput
@@ -1,6 +1,14 @@
# 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.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

0 comments on commit 0c4b607

Please sign in to comment.
You can’t perform that action at this time.