Przeglądaj źródła

Add conformance tests for explicit packed/unpacked fields

Bo Yang 6 lat temu
rodzic
commit
9e46c3a5aa

+ 101 - 19
conformance/binary_json_conformance_suite.cc

@@ -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 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;

+ 32 - 0
src/google/protobuf/test_messages_proto2.proto

@@ -119,6 +119,38 @@ message TestAllTypesProto2 {
   repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
   repeated string repeated_cord = 55 [ctype=CORD];
 
+  // Packed
+  repeated    int32 packed_int32    = 75 [packed=true];
+  repeated    int64 packed_int64    = 76 [packed=true];
+  repeated   uint32 packed_uint32   = 77 [packed=true];
+  repeated   uint64 packed_uint64   = 78 [packed=true];
+  repeated   sint32 packed_sint32   = 79 [packed=true];
+  repeated   sint64 packed_sint64   = 80 [packed=true];
+  repeated  fixed32 packed_fixed32  = 81 [packed=true];
+  repeated  fixed64 packed_fixed64  = 82 [packed=true];
+  repeated sfixed32 packed_sfixed32 = 83 [packed=true];
+  repeated sfixed64 packed_sfixed64 = 84 [packed=true];
+  repeated    float packed_float    = 85 [packed=true];
+  repeated   double packed_double   = 86 [packed=true];
+  repeated     bool packed_bool     = 87 [packed=true];
+  repeated NestedEnum packed_nested_enum = 88 [packed=true];
+
+  // Unpacked
+  repeated    int32 unpacked_int32    = 89  [packed=false];
+  repeated    int64 unpacked_int64    = 90  [packed=false];
+  repeated   uint32 unpacked_uint32   = 91  [packed=false];
+  repeated   uint64 unpacked_uint64   = 92  [packed=false];
+  repeated   sint32 unpacked_sint32   = 93  [packed=false];
+  repeated   sint64 unpacked_sint64   = 94  [packed=false];
+  repeated  fixed32 unpacked_fixed32  = 95  [packed=false];
+  repeated  fixed64 unpacked_fixed64  = 96  [packed=false];
+  repeated sfixed32 unpacked_sfixed32 = 97  [packed=false];
+  repeated sfixed64 unpacked_sfixed64 = 98  [packed=false];
+  repeated    float unpacked_float    = 99  [packed=false];
+  repeated   double unpacked_double   = 100 [packed=false];
+  repeated     bool unpacked_bool     = 101 [packed=false];
+  repeated NestedEnum unpacked_nested_enum = 102 [packed=false];
+
   // Map
   map <   int32, int32>    map_int32_int32 = 56;
   map <   int64, int64>    map_int64_int64 = 57;

+ 32 - 0
src/google/protobuf/test_messages_proto3.proto

@@ -139,6 +139,38 @@ message TestAllTypesProto3 {
   repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
   repeated string repeated_cord = 55 [ctype=CORD];
 
+  // Packed
+  repeated    int32 packed_int32    = 75 [packed=true];
+  repeated    int64 packed_int64    = 76 [packed=true];
+  repeated   uint32 packed_uint32   = 77 [packed=true];
+  repeated   uint64 packed_uint64   = 78 [packed=true];
+  repeated   sint32 packed_sint32   = 79 [packed=true];
+  repeated   sint64 packed_sint64   = 80 [packed=true];
+  repeated  fixed32 packed_fixed32  = 81 [packed=true];
+  repeated  fixed64 packed_fixed64  = 82 [packed=true];
+  repeated sfixed32 packed_sfixed32 = 83 [packed=true];
+  repeated sfixed64 packed_sfixed64 = 84 [packed=true];
+  repeated    float packed_float    = 85 [packed=true];
+  repeated   double packed_double   = 86 [packed=true];
+  repeated     bool packed_bool     = 87 [packed=true];
+  repeated NestedEnum packed_nested_enum = 88 [packed=true];
+
+  // Unpacked
+  repeated    int32 unpacked_int32    = 89  [packed=false];
+  repeated    int64 unpacked_int64    = 90  [packed=false];
+  repeated   uint32 unpacked_uint32   = 91  [packed=false];
+  repeated   uint64 unpacked_uint64   = 92  [packed=false];
+  repeated   sint32 unpacked_sint32   = 93  [packed=false];
+  repeated   sint64 unpacked_sint64   = 94  [packed=false];
+  repeated  fixed32 unpacked_fixed32  = 95  [packed=false];
+  repeated  fixed64 unpacked_fixed64  = 96  [packed=false];
+  repeated sfixed32 unpacked_sfixed32 = 97  [packed=false];
+  repeated sfixed64 unpacked_sfixed64 = 98  [packed=false];
+  repeated    float unpacked_float    = 99  [packed=false];
+  repeated   double unpacked_double   = 100 [packed=false];
+  repeated     bool unpacked_bool     = 101 [packed=false];
+  repeated NestedEnum unpacked_nested_enum = 102 [packed=false];
+
   // Map
   map <   int32, int32>    map_int32_int32 = 56;
   map <   int64, int64>    map_int64_int64 = 57;