diff options
author | Xin Li <delphij@google.com> | 2020-08-31 21:21:38 -0700 |
---|---|---|
committer | Xin Li <delphij@google.com> | 2020-08-31 21:21:38 -0700 |
commit | 628590d7ec80e10a3fc24b1c18a1afb55cca10a8 (patch) | |
tree | 4b1c3f52d86d7fb53afbe9e9438468588fa489f8 /tools/stats_log_api_gen/test_collation.cpp | |
parent | b11b8ec3aec8bb42f2c07e1c5ac7942da293baa8 (diff) | |
parent | d2d3a20624d968199353ccf6ddbae6f3ac39c9af (diff) |
Merge Android R (rvc-dev-plus-aosp-without-vendor@6692709)
Bug: 166295507
Merged-In: I3d92a6de21a938f6b352ec26dc23420c0fe02b27
Change-Id: Ifdb80563ef042738778ebb8a7581a97c4e3d96e2
Diffstat (limited to 'tools/stats_log_api_gen/test_collation.cpp')
-rw-r--r-- | tools/stats_log_api_gen/test_collation.cpp | 365 |
1 files changed, 222 insertions, 143 deletions
diff --git a/tools/stats_log_api_gen/test_collation.cpp b/tools/stats_log_api_gen/test_collation.cpp index bcf18ae8bf19..dbae58889333 100644 --- a/tools/stats_log_api_gen/test_collation.cpp +++ b/tools/stats_log_api_gen/test_collation.cpp @@ -15,11 +15,10 @@ */ #include <gtest/gtest.h> +#include <stdio.h> -#include "frameworks/base/tools/stats_log_api_gen/test.pb.h" #include "Collation.h" - -#include <stdio.h> +#include "frameworks/base/tools/stats_log_api_gen/test.pb.h" namespace android { namespace stats_log_api_gen { @@ -29,16 +28,14 @@ using std::set; using std::vector; /** - * Return whether the set contains a vector of the elements provided. + * Return whether the map contains a vector of the elements provided. */ -static bool -set_contains_vector(const map<vector<java_type_t>, set<string>>& s, int count, ...) -{ +static bool map_contains_vector(const SignatureInfoMap& s, int count, ...) { va_list args; vector<java_type_t> v; va_start(args, count); - for (int i=0; i<count; i++) { + for (int i = 0; i < count; i++) { v.push_back((java_type_t)va_arg(args, int)); } va_end(args); @@ -47,103 +44,102 @@ set_contains_vector(const map<vector<java_type_t>, set<string>>& s, int count, . } /** - * Expect that the provided set contains the elements provided. + * Expect that the provided map contains the elements provided. */ -#define EXPECT_SET_CONTAINS_SIGNATURE(s, ...) \ - do { \ - int count = sizeof((int[]){__VA_ARGS__})/sizeof(int); \ - EXPECT_TRUE(set_contains_vector(s, count, __VA_ARGS__)); \ - } while(0) +#define EXPECT_MAP_CONTAINS_SIGNATURE(s, ...) \ + do { \ + int count = sizeof((int[]){__VA_ARGS__}) / sizeof(int); \ + EXPECT_TRUE(map_contains_vector(s, count, __VA_ARGS__)); \ + } while (0) /** Expects that the provided atom has no enum values for any field. */ -#define EXPECT_NO_ENUM_FIELD(atom) \ - do { \ +#define EXPECT_NO_ENUM_FIELD(atom) \ + do { \ for (vector<AtomField>::const_iterator field = atom->fields.begin(); \ - field != atom->fields.end(); field++) { \ - EXPECT_TRUE(field->enumValues.empty()); \ - } \ - } while(0) + field != atom->fields.end(); field++) { \ + EXPECT_TRUE(field->enumValues.empty()); \ + } \ + } while (0) /** Expects that exactly one specific field has expected enum values. */ -#define EXPECT_HAS_ENUM_FIELD(atom, field_name, values) \ - do { \ +#define EXPECT_HAS_ENUM_FIELD(atom, field_name, values) \ + do { \ for (vector<AtomField>::const_iterator field = atom->fields.begin(); \ - field != atom->fields.end(); field++) { \ - if (field->name == field_name) { \ - EXPECT_EQ(field->enumValues, values); \ - } else { \ - EXPECT_TRUE(field->enumValues.empty()); \ - } \ - } \ - } while(0) - + field != atom->fields.end(); field++) { \ + if (field->name == field_name) { \ + EXPECT_EQ(field->enumValues, values); \ + } else { \ + EXPECT_TRUE(field->enumValues.empty()); \ + } \ + } \ + } while (0) /** * Test a correct collation, with all the types. */ TEST(CollationTest, CollateStats) { Atoms atoms; - int errorCount = collate_atoms(Event::descriptor(), &atoms); + int errorCount = collate_atoms(Event::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(0, errorCount); - EXPECT_EQ(3ul, atoms.signatures_to_modules.size()); + EXPECT_EQ(3ul, atoms.signatureInfoMap.size()); // IntAtom, AnotherIntAtom - EXPECT_SET_CONTAINS_SIGNATURE(atoms.signatures_to_modules, JAVA_TYPE_INT); + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, JAVA_TYPE_INT); // OutOfOrderAtom - EXPECT_SET_CONTAINS_SIGNATURE(atoms.signatures_to_modules, JAVA_TYPE_INT, JAVA_TYPE_INT); + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, JAVA_TYPE_INT, JAVA_TYPE_INT); // AllTypesAtom - EXPECT_SET_CONTAINS_SIGNATURE( - atoms.signatures_to_modules, - JAVA_TYPE_ATTRIBUTION_CHAIN, // AttributionChain - JAVA_TYPE_DOUBLE, // double - JAVA_TYPE_FLOAT, // float - JAVA_TYPE_LONG, // int64 - JAVA_TYPE_LONG, // uint64 - JAVA_TYPE_INT, // int32 - JAVA_TYPE_LONG, // fixed64 - JAVA_TYPE_INT, // fixed32 - JAVA_TYPE_BOOLEAN, // bool - JAVA_TYPE_STRING, // string - JAVA_TYPE_INT, // uint32 - JAVA_TYPE_INT, // AnEnum - JAVA_TYPE_INT, // sfixed32 - JAVA_TYPE_LONG, // sfixed64 - JAVA_TYPE_INT, // sint32 - JAVA_TYPE_LONG // sint64 + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, + JAVA_TYPE_ATTRIBUTION_CHAIN, // AttributionChain + JAVA_TYPE_FLOAT, // float + JAVA_TYPE_LONG, // int64 + JAVA_TYPE_LONG, // uint64 + JAVA_TYPE_INT, // int32 + JAVA_TYPE_LONG, // fixed64 + JAVA_TYPE_INT, // fixed32 + JAVA_TYPE_BOOLEAN, // bool + JAVA_TYPE_STRING, // string + JAVA_TYPE_INT, // uint32 + JAVA_TYPE_INT, // AnEnum + JAVA_TYPE_INT, // sfixed32 + JAVA_TYPE_LONG, // sfixed64 + JAVA_TYPE_INT, // sint32 + JAVA_TYPE_LONG // sint64 ); - set<AtomDecl>::const_iterator atom = atoms.decls.begin(); - EXPECT_EQ(1, atom->code); - EXPECT_EQ("int_atom", atom->name); - EXPECT_EQ("IntAtom", atom->message); - EXPECT_NO_ENUM_FIELD(atom); - atom++; - - EXPECT_EQ(2, atom->code); - EXPECT_EQ("out_of_order_atom", atom->name); - EXPECT_EQ("OutOfOrderAtom", atom->message); - EXPECT_NO_ENUM_FIELD(atom); - atom++; - - EXPECT_EQ(3, atom->code); - EXPECT_EQ("another_int_atom", atom->name); - EXPECT_EQ("AnotherIntAtom", atom->message); - EXPECT_NO_ENUM_FIELD(atom); - atom++; - - EXPECT_EQ(4, atom->code); - EXPECT_EQ("all_types_atom", atom->name); - EXPECT_EQ("AllTypesAtom", atom->message); + EXPECT_EQ(4ul, atoms.decls.size()); + + AtomDeclSet::const_iterator atomIt = atoms.decls.begin(); + EXPECT_EQ(1, (*atomIt)->code); + EXPECT_EQ("int_atom", (*atomIt)->name); + EXPECT_EQ("IntAtom", (*atomIt)->message); + EXPECT_NO_ENUM_FIELD((*atomIt)); + atomIt++; + + EXPECT_EQ(2, (*atomIt)->code); + EXPECT_EQ("out_of_order_atom", (*atomIt)->name); + EXPECT_EQ("OutOfOrderAtom", (*atomIt)->message); + EXPECT_NO_ENUM_FIELD((*atomIt)); + atomIt++; + + EXPECT_EQ(3, (*atomIt)->code); + EXPECT_EQ("another_int_atom", (*atomIt)->name); + EXPECT_EQ("AnotherIntAtom", (*atomIt)->message); + EXPECT_NO_ENUM_FIELD((*atomIt)); + atomIt++; + + EXPECT_EQ(4, (*atomIt)->code); + EXPECT_EQ("all_types_atom", (*atomIt)->name); + EXPECT_EQ("AllTypesAtom", (*atomIt)->message); map<int, string> enumValues; enumValues[0] = "VALUE0"; enumValues[1] = "VALUE1"; - EXPECT_HAS_ENUM_FIELD(atom, "enum_field", enumValues); - atom++; + EXPECT_HAS_ENUM_FIELD((*atomIt), "enum_field", enumValues); + atomIt++; - EXPECT_TRUE(atom == atoms.decls.end()); + EXPECT_EQ(atoms.decls.end(), atomIt); } /** @@ -151,19 +147,20 @@ TEST(CollationTest, CollateStats) { */ TEST(CollationTest, NonMessageTypeFails) { Atoms atoms; - int errorCount = collate_atoms(IntAtom::descriptor(), &atoms); + int errorCount = collate_atoms(IntAtom::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(1, errorCount); } /** - * Test that atoms that have non-primitive types are rejected. + * Test that atoms that have non-primitive types or repeated fields are + * rejected. */ TEST(CollationTest, FailOnBadTypes) { Atoms atoms; - int errorCount = collate_atoms(BadTypesEvent::descriptor(), &atoms); + int errorCount = collate_atoms(BadTypesEvent::descriptor(), DEFAULT_MODULE_NAME, &atoms); - EXPECT_EQ(2, errorCount); + EXPECT_EQ(4, errorCount); } /** @@ -171,18 +168,20 @@ TEST(CollationTest, FailOnBadTypes) { */ TEST(CollationTest, FailOnSkippedFieldsSingle) { Atoms atoms; - int errorCount = collate_atoms(BadSkippedFieldSingle::descriptor(), &atoms); + int errorCount = + collate_atoms(BadSkippedFieldSingle::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(1, errorCount); } /** - * Test that atoms that skip field numbers (not in the first position, and multiple - * times) are rejected. + * Test that atoms that skip field numbers (not in the first position, and + * multiple times) are rejected. */ TEST(CollationTest, FailOnSkippedFieldsMultiple) { Atoms atoms; - int errorCount = collate_atoms(BadSkippedFieldMultiple::descriptor(), &atoms); + int errorCount = + collate_atoms(BadSkippedFieldMultiple::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(2, errorCount); } @@ -192,99 +191,179 @@ TEST(CollationTest, FailOnSkippedFieldsMultiple) { * rejected. */ TEST(CollationTest, FailBadAttributionNodePosition) { - Atoms atoms; - int errorCount = - collate_atoms(BadAttributionNodePosition::descriptor(), &atoms); + Atoms atoms; + int errorCount = + collate_atoms(BadAttributionNodePosition::descriptor(), DEFAULT_MODULE_NAME, &atoms); - EXPECT_EQ(1, errorCount); + EXPECT_EQ(1, errorCount); } TEST(CollationTest, FailOnBadStateAtomOptions) { Atoms atoms; - int errorCount = collate_atoms(BadStateAtoms::descriptor(), &atoms); + int errorCount = collate_atoms(BadStateAtoms::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(3, errorCount); } TEST(CollationTest, PassOnGoodStateAtomOptions) { Atoms atoms; - int errorCount = collate_atoms(GoodStateAtoms::descriptor(), &atoms); + int errorCount = collate_atoms(GoodStateAtoms::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(0, errorCount); } TEST(CollationTest, PassOnGoodBinaryFieldAtom) { Atoms atoms; int errorCount = - collate_atoms(GoodEventWithBinaryFieldAtom::descriptor(), &atoms); + collate_atoms(GoodEventWithBinaryFieldAtom::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(0, errorCount); } TEST(CollationTest, FailOnBadBinaryFieldAtom) { Atoms atoms; int errorCount = - collate_atoms(BadEventWithBinaryFieldAtom::descriptor(), &atoms); + collate_atoms(BadEventWithBinaryFieldAtom::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_TRUE(errorCount > 0); } -TEST(CollationTest, PassOnWhitelistedAtom) { +TEST(CollationTest, PassOnLogFromModuleAtom) { Atoms atoms; - int errorCount = collate_atoms(ListedAtoms::descriptor(), &atoms); + int errorCount = collate_atoms(ModuleAtoms::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(errorCount, 0); - EXPECT_EQ(atoms.decls.size(), 2ul); + EXPECT_EQ(atoms.decls.size(), 4ul); } -TEST(CollationTest, RecogniseWhitelistedAtom) { - Atoms atoms; - collate_atoms(ListedAtoms::descriptor(), &atoms); - for (const auto& atomDecl : atoms.decls) { - if (atomDecl.code == 1) { - EXPECT_TRUE(atomDecl.whitelisted); - } else { - EXPECT_FALSE(atomDecl.whitelisted); - } - } -} - -TEST(CollationTest, PassOnLogFromModuleAtom) { +TEST(CollationTest, RecognizeModuleAtom) { Atoms atoms; - int errorCount = collate_atoms(ModuleAtoms::descriptor(), &atoms); + int errorCount = collate_atoms(ModuleAtoms::descriptor(), DEFAULT_MODULE_NAME, &atoms); EXPECT_EQ(errorCount, 0); - EXPECT_EQ(atoms.decls.size(), 3ul); + EXPECT_EQ(atoms.decls.size(), 4ul); + EXPECT_EQ(atoms.signatureInfoMap.size(), 2u); + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, JAVA_TYPE_INT); + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, JAVA_TYPE_STRING); + + SignatureInfoMap::const_iterator signatureInfoMapIt; + const vector<java_type_t>* signature; + const FieldNumberToAtomDeclSet* fieldNumberToAtomDeclSet; + FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt; + const AtomDeclSet* atomDeclSet; + AtomDeclSet::const_iterator atomDeclSetIt; + AtomDecl* atomDecl; + FieldNumberToAnnotations* fieldNumberToAnnotations; + FieldNumberToAnnotations::const_iterator fieldNumberToAnnotationsIt; + const AnnotationSet* annotationSet; + AnnotationSet::const_iterator annotationSetIt; + Annotation* annotation; + + signatureInfoMapIt = atoms.signatureInfoMap.begin(); + signature = &(signatureInfoMapIt->first); + fieldNumberToAtomDeclSet = &signatureInfoMapIt->second; + EXPECT_EQ(1ul, signature->size()); + EXPECT_EQ(JAVA_TYPE_INT, signature->at(0)); + EXPECT_EQ(1ul, fieldNumberToAtomDeclSet->size()); + fieldNumberToAtomDeclSetIt = fieldNumberToAtomDeclSet->begin(); + EXPECT_EQ(1, fieldNumberToAtomDeclSetIt->first); + atomDeclSet = &fieldNumberToAtomDeclSetIt->second; + EXPECT_EQ(2ul, atomDeclSet->size()); + atomDeclSetIt = atomDeclSet->begin(); + atomDecl = atomDeclSetIt->get(); + EXPECT_EQ(1, atomDecl->code); + fieldNumberToAnnotations = &atomDecl->fieldNumberToAnnotations; + fieldNumberToAnnotationsIt = fieldNumberToAnnotations->find(1); + EXPECT_NE(fieldNumberToAnnotations->end(), fieldNumberToAnnotationsIt); + annotationSet = &fieldNumberToAnnotationsIt->second; + EXPECT_EQ(1ul, annotationSet->size()); + annotationSetIt = annotationSet->begin(); + annotation = annotationSetIt->get(); + EXPECT_EQ(ANNOTATION_ID_IS_UID, annotation->annotationId); + EXPECT_EQ(1, annotation->atomId); + EXPECT_EQ(ANNOTATION_TYPE_BOOL, annotation->type); + EXPECT_TRUE(annotation->value.boolValue); + + atomDeclSetIt++; + atomDecl = atomDeclSetIt->get(); + EXPECT_EQ(3, atomDecl->code); + fieldNumberToAnnotations = &atomDecl->fieldNumberToAnnotations; + fieldNumberToAnnotationsIt = fieldNumberToAnnotations->find(1); + EXPECT_NE(fieldNumberToAnnotations->end(), fieldNumberToAnnotationsIt); + annotationSet = &fieldNumberToAnnotationsIt->second; + EXPECT_EQ(1ul, annotationSet->size()); + annotationSetIt = annotationSet->begin(); + annotation = annotationSetIt->get(); + EXPECT_EQ(ANNOTATION_ID_EXCLUSIVE_STATE, annotation->annotationId); + EXPECT_EQ(3, annotation->atomId); + EXPECT_EQ(ANNOTATION_TYPE_BOOL, annotation->type); + EXPECT_TRUE(annotation->value.boolValue); + + signatureInfoMapIt++; + signature = &signatureInfoMapIt->first; + fieldNumberToAtomDeclSet = &signatureInfoMapIt->second; + EXPECT_EQ(1ul, signature->size()); + EXPECT_EQ(JAVA_TYPE_STRING, signature->at(0)); + EXPECT_EQ(0ul, fieldNumberToAtomDeclSet->size()); } -TEST(CollationTest, RecognizeModuleAtom) { +TEST(CollationTest, RecognizeModule1Atom) { Atoms atoms; - int errorCount = collate_atoms(ModuleAtoms::descriptor(), &atoms); + const string moduleName = "module1"; + int errorCount = collate_atoms(ModuleAtoms::descriptor(), moduleName, &atoms); EXPECT_EQ(errorCount, 0); - EXPECT_EQ(atoms.decls.size(), 3ul); - for (const auto& atomDecl: atoms.decls) { - if (atomDecl.code == 1) { - EXPECT_TRUE(atomDecl.hasModule); - EXPECT_EQ(atomDecl.moduleName, "module1"); - } else if (atomDecl.code == 2) { - EXPECT_TRUE(atomDecl.hasModule); - EXPECT_EQ(atomDecl.moduleName, "module2"); - } else { - EXPECT_FALSE(atomDecl.hasModule); - } - } - - EXPECT_EQ(atoms.signatures_to_modules.size(), 2u); - EXPECT_SET_CONTAINS_SIGNATURE(atoms.signatures_to_modules, JAVA_TYPE_INT); - EXPECT_SET_CONTAINS_SIGNATURE(atoms.signatures_to_modules, JAVA_TYPE_STRING); - for (auto signature_to_modules_it : atoms.signatures_to_modules) { - vector<java_type_t> signature = signature_to_modules_it.first; - if (signature[0] == JAVA_TYPE_STRING) { - EXPECT_EQ(signature_to_modules_it.second.size(), 0u); - } else if (signature[0] == JAVA_TYPE_INT) { - set<string> modules = signature_to_modules_it.second; - EXPECT_EQ(modules.size(), 2u); - // Assert that the set contains "module1" and "module2". - EXPECT_NE(modules.find("module1"), modules.end()); - EXPECT_NE(modules.find("module2"), modules.end()); - } - } + EXPECT_EQ(atoms.decls.size(), 2ul); + EXPECT_EQ(atoms.signatureInfoMap.size(), 1u); + EXPECT_MAP_CONTAINS_SIGNATURE(atoms.signatureInfoMap, JAVA_TYPE_INT); + + SignatureInfoMap::const_iterator signatureInfoMapIt; + const vector<java_type_t>* signature; + const FieldNumberToAtomDeclSet* fieldNumberToAtomDeclSet; + FieldNumberToAtomDeclSet::const_iterator fieldNumberToAtomDeclSetIt; + const AtomDeclSet* atomDeclSet; + AtomDeclSet::const_iterator atomDeclSetIt; + AtomDecl* atomDecl; + FieldNumberToAnnotations* fieldNumberToAnnotations; + FieldNumberToAnnotations::const_iterator fieldNumberToAnnotationsIt; + const AnnotationSet* annotationSet; + AnnotationSet::const_iterator annotationSetIt; + Annotation* annotation; + + signatureInfoMapIt = atoms.signatureInfoMap.begin(); + signature = &(signatureInfoMapIt->first); + fieldNumberToAtomDeclSet = &signatureInfoMapIt->second; + EXPECT_EQ(1ul, signature->size()); + EXPECT_EQ(JAVA_TYPE_INT, signature->at(0)); + EXPECT_EQ(1ul, fieldNumberToAtomDeclSet->size()); + fieldNumberToAtomDeclSetIt = fieldNumberToAtomDeclSet->begin(); + EXPECT_EQ(1, fieldNumberToAtomDeclSetIt->first); + atomDeclSet = &fieldNumberToAtomDeclSetIt->second; + EXPECT_EQ(2ul, atomDeclSet->size()); + atomDeclSetIt = atomDeclSet->begin(); + atomDecl = atomDeclSetIt->get(); + EXPECT_EQ(1, atomDecl->code); + fieldNumberToAnnotations = &atomDecl->fieldNumberToAnnotations; + fieldNumberToAnnotationsIt = fieldNumberToAnnotations->find(1); + EXPECT_NE(fieldNumberToAnnotations->end(), fieldNumberToAnnotationsIt); + annotationSet = &fieldNumberToAnnotationsIt->second; + EXPECT_EQ(1ul, annotationSet->size()); + annotationSetIt = annotationSet->begin(); + annotation = annotationSetIt->get(); + EXPECT_EQ(ANNOTATION_ID_IS_UID, annotation->annotationId); + EXPECT_EQ(1, annotation->atomId); + EXPECT_EQ(ANNOTATION_TYPE_BOOL, annotation->type); + EXPECT_TRUE(annotation->value.boolValue); + + atomDeclSetIt++; + atomDecl = atomDeclSetIt->get(); + EXPECT_EQ(3, atomDecl->code); + fieldNumberToAnnotations = &atomDecl->fieldNumberToAnnotations; + fieldNumberToAnnotationsIt = fieldNumberToAnnotations->find(1); + EXPECT_NE(fieldNumberToAnnotations->end(), fieldNumberToAnnotationsIt); + annotationSet = &fieldNumberToAnnotationsIt->second; + EXPECT_EQ(1ul, annotationSet->size()); + annotationSetIt = annotationSet->begin(); + annotation = annotationSetIt->get(); + EXPECT_EQ(ANNOTATION_ID_EXCLUSIVE_STATE, annotation->annotationId); + EXPECT_EQ(3, annotation->atomId); + EXPECT_EQ(ANNOTATION_TYPE_BOOL, annotation->type); + EXPECT_TRUE(annotation->value.boolValue); } } // namespace stats_log_api_gen -} // namespace android
\ No newline at end of file +} // namespace android |