From: Justin Bogner Date: Tue, 17 Feb 2015 21:33:43 +0000 (+0000) Subject: Re-apply "InstrProf: Use a test fixture in the coverage mapping tests" X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ad295b5546b6633468e3e3131ed3ab09136ac934;p=oota-llvm.git Re-apply "InstrProf: Use a test fixture in the coverage mapping tests" This time we use a helper to format the assertion so we can just use ASSERT_TRUE instead of relying on ASSERT_EQ being able to deal with conversions between enum types. This reverts r229496, re-applying r229473. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229547 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp index 859c5127a67..04bc19dc4e4 100644 --- a/unittests/ProfileData/CoverageMappingTest.cpp +++ b/unittests/ProfileData/CoverageMappingTest.cpp @@ -18,6 +18,13 @@ using namespace llvm; using namespace coverage; +static ::testing::AssertionResult NoError(std::error_code EC) { + if (!EC) + return ::testing::AssertionSuccess(); + return ::testing::AssertionFailure() << "error " << EC.value() + << ": " << EC.message(); +} + namespace llvm { namespace coverage { void PrintTo(const Counter &C, ::std::ostream *os) { @@ -33,83 +40,90 @@ void PrintTo(const Counter &C, ::std::ostream *os) { namespace { -static std::string writeCoverage(MutableArrayRef Regions, - int NumFiles) { - SmallVector FileIDs; - for (int I = 0; I < NumFiles; ++I) - FileIDs.push_back(I); +struct CoverageMappingTest : ::testing::Test { + StringMap Files; + unsigned NextFile; + std::vector InputCMRs; - std::string Coverage; - llvm::raw_string_ostream OS(Coverage); - CoverageMappingWriter(FileIDs, None, Regions).write(OS); - OS.flush(); + std::vector OutputFiles; + std::vector OutputExpressions; + std::vector OutputCMRs; - return Coverage; -} + void SetUp() override { + NextFile = 0; + } -static std::vector -readCoverageRegions(std::string Coverage, int NumFiles) { - SmallVector Filenames; - SmallVector FilenameRefs; - for (int I = 0; I < NumFiles; ++I) { - std::ostringstream S; - S << "file" << I; - Filenames.push_back(S.str()); - FilenameRefs.push_back(Filenames.back()); + unsigned getFile(StringRef Name) { + auto R = Files.find(Name); + if (R != Files.end()) + return R->second; + Files[Name] = NextFile; + return NextFile++; } - std::vector FuncFiles; - std::vector Expressions; - std::vector Regions; - RawCoverageMappingReader Reader(Coverage, FilenameRefs, FuncFiles, - Expressions, Regions); - if (Reader.read()) - // ASSERT doesn't work here, we'll just return an empty vector. - Regions.clear(); - return Regions; -} + void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE, + unsigned CE) { + InputCMRs.push_back( + CounterMappingRegion::makeRegion(C, getFile(File), LS, CS, LE, CE)); + } -TEST(CoverageMappingTest, basic_write_read) { - int NumFiles = 2; - CounterMappingRegion InputRegions[] = { - CounterMappingRegion::makeRegion(Counter::getCounter(0), 0, 1, 1, 1, 1), - CounterMappingRegion::makeRegion(Counter::getCounter(1), 0, 2, 1, 2, 2), - CounterMappingRegion::makeRegion(Counter::getZero(), 0, 3, 1, 3, 4), - CounterMappingRegion::makeRegion(Counter::getCounter(2), 0, 4, 1, 4, 8), - CounterMappingRegion::makeRegion(Counter::getCounter(3), 1, 1, 2, 3, 4), - }; - std::string Coverage = writeCoverage(InputRegions, NumFiles); - std::vector OutputRegions = - readCoverageRegions(Coverage, NumFiles); - ASSERT_FALSE(OutputRegions.empty()); - - size_t N = makeArrayRef(InputRegions).size(); - ASSERT_EQ(N, OutputRegions.size()); + void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS, + unsigned CS, unsigned LE, unsigned CE) { + InputCMRs.push_back(CounterMappingRegion::makeExpansion( + getFile(File), getFile(ExpandedFile), LS, CS, LE, CE)); + } + + std::string writeCoverageRegions() { + SmallVector FileIDs; + for (const auto &E : Files) + FileIDs.push_back(E.getValue()); + std::string Coverage; + llvm::raw_string_ostream OS(Coverage); + CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS); + return OS.str(); + } + + void readCoverageRegions(std::string Coverage) { + SmallVector Filenames; + for (const auto &E : Files) + Filenames.push_back(E.getKey()); + RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles, + OutputExpressions, OutputCMRs); + ASSERT_TRUE(NoError(Reader.read())); + } +}; + +TEST_F(CoverageMappingTest, basic_write_read) { + addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1); + addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2); + addCMR(Counter::getZero(), "foo", 3, 1, 3, 4); + addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8); + addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4); + std::string Coverage = writeCoverageRegions(); + readCoverageRegions(Coverage); + + size_t N = makeArrayRef(InputCMRs).size(); + ASSERT_EQ(N, OutputCMRs.size()); for (size_t I = 0; I < N; ++I) { - ASSERT_EQ(InputRegions[I].Count, OutputRegions[I].Count); - ASSERT_EQ(InputRegions[I].FileID, OutputRegions[I].FileID); - ASSERT_EQ(InputRegions[I].startLoc(), OutputRegions[I].startLoc()); - ASSERT_EQ(InputRegions[I].endLoc(), OutputRegions[I].endLoc()); - ASSERT_EQ(InputRegions[I].Kind, OutputRegions[I].Kind); + ASSERT_EQ(InputCMRs[I].Count, OutputCMRs[I].Count); + ASSERT_EQ(InputCMRs[I].FileID, OutputCMRs[I].FileID); + ASSERT_EQ(InputCMRs[I].startLoc(), OutputCMRs[I].startLoc()); + ASSERT_EQ(InputCMRs[I].endLoc(), OutputCMRs[I].endLoc()); + ASSERT_EQ(InputCMRs[I].Kind, OutputCMRs[I].Kind); } } -TEST(CoverageMappingTest, expansion_gets_first_counter) { - int NumFiles = 2; - CounterMappingRegion InputRegions[] = { - CounterMappingRegion::makeRegion(Counter::getCounter(1), 0, 10, 1, 10, 2), - // This starts earlier in file 0, so the expansion should get its counter. - CounterMappingRegion::makeRegion(Counter::getCounter(2), 0, 1, 1, 20, 1), - CounterMappingRegion::makeExpansion(1, 0, 3, 3, 3, 3), - }; - std::string Coverage = writeCoverage(InputRegions, NumFiles); - std::vector OutputRegions = - readCoverageRegions(Coverage, NumFiles); - ASSERT_FALSE(OutputRegions.empty()); - - ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputRegions[2].Kind); - ASSERT_EQ(Counter::getCounter(2), OutputRegions[2].Count); - ASSERT_EQ(3U, OutputRegions[2].LineStart); +TEST_F(CoverageMappingTest, expansion_gets_first_counter) { + addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2); + // This starts earlier in "foo", so the expansion should get its counter. + addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1); + addExpansionCMR("bar", "foo", 3, 3, 3, 3); + std::string Coverage = writeCoverageRegions(); + readCoverageRegions(Coverage); + + ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputCMRs[2].Kind); + ASSERT_EQ(Counter::getCounter(2), OutputCMRs[2].Count); + ASSERT_EQ(3U, OutputCMRs[2].LineStart); }