InstrProf: Use a test fixture in the coverage mapping tests
[oota-llvm.git] / unittests / ProfileData / CoverageMappingTest.cpp
1 //===- unittest/ProfileData/CoverageMappingTest.cpp -------------------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ProfileData/CoverageMapping.h"
11 #include "llvm/ProfileData/CoverageMappingReader.h"
12 #include "llvm/ProfileData/CoverageMappingWriter.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include "gtest/gtest.h"
15
16 #include <sstream>
17
18 using namespace llvm;
19 using namespace coverage;
20
21 namespace llvm {
22 namespace coverage {
23 void PrintTo(const Counter &C, ::std::ostream *os) {
24   if (C.isZero())
25     *os << "Zero";
26   else if (C.isExpression())
27     *os << "Expression " << C.getExpressionID();
28   else
29     *os << "Counter " << C.getCounterID();
30 }
31 }
32 }
33
34 namespace {
35
36 struct CoverageMappingTest : ::testing::Test {
37   StringMap<unsigned> Files;
38   unsigned NextFile;
39   std::vector<CounterMappingRegion> InputCMRs;
40
41   std::vector<StringRef> OutputFiles;
42   std::vector<CounterExpression> OutputExpressions;
43   std::vector<CounterMappingRegion> OutputCMRs;
44
45   void SetUp() override {
46     NextFile = 0;
47   }
48
49   unsigned getFile(StringRef Name) {
50     auto R = Files.find(Name);
51     if (R != Files.end())
52       return R->second;
53     Files[Name] = NextFile;
54     return NextFile++;
55   }
56
57   void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
58               unsigned CE) {
59     InputCMRs.push_back(
60         CounterMappingRegion::makeRegion(C, getFile(File), LS, CS, LE, CE));
61   }
62
63   void addExpansionCMR(StringRef File, StringRef ExpandedFile, unsigned LS,
64                        unsigned CS, unsigned LE, unsigned CE) {
65     InputCMRs.push_back(CounterMappingRegion::makeExpansion(
66         getFile(File), getFile(ExpandedFile), LS, CS, LE, CE));
67   }
68
69   std::string writeCoverageRegions() {
70     SmallVector<unsigned, 8> FileIDs;
71     for (const auto &E : Files)
72       FileIDs.push_back(E.getValue());
73     std::string Coverage;
74     llvm::raw_string_ostream OS(Coverage);
75     CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS);
76     return OS.str();
77   }
78
79   void readCoverageRegions(std::string Coverage) {
80     SmallVector<StringRef, 8> Filenames;
81     for (const auto &E : Files)
82       Filenames.push_back(E.getKey());
83     RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles,
84                                     OutputExpressions, OutputCMRs);
85     std::error_code EC = Reader.read();
86     ASSERT_EQ(instrprof_error::success, EC);
87   }
88 };
89
90 TEST_F(CoverageMappingTest, basic_write_read) {
91   addCMR(Counter::getCounter(0), "foo", 1, 1, 1, 1);
92   addCMR(Counter::getCounter(1), "foo", 2, 1, 2, 2);
93   addCMR(Counter::getZero(),     "foo", 3, 1, 3, 4);
94   addCMR(Counter::getCounter(2), "foo", 4, 1, 4, 8);
95   addCMR(Counter::getCounter(3), "bar", 1, 2, 3, 4);
96   std::string Coverage = writeCoverageRegions();
97   readCoverageRegions(Coverage);
98
99   size_t N = makeArrayRef(InputCMRs).size();
100   ASSERT_EQ(N, OutputCMRs.size());
101   for (size_t I = 0; I < N; ++I) {
102     ASSERT_EQ(InputCMRs[I].Count,      OutputCMRs[I].Count);
103     ASSERT_EQ(InputCMRs[I].FileID,     OutputCMRs[I].FileID);
104     ASSERT_EQ(InputCMRs[I].startLoc(), OutputCMRs[I].startLoc());
105     ASSERT_EQ(InputCMRs[I].endLoc(),   OutputCMRs[I].endLoc());
106     ASSERT_EQ(InputCMRs[I].Kind,       OutputCMRs[I].Kind);
107   }
108 }
109
110 TEST_F(CoverageMappingTest, expansion_gets_first_counter) {
111   addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
112   // This starts earlier in "foo", so the expansion should get its counter.
113   addCMR(Counter::getCounter(2), "foo", 1, 1, 20, 1);
114   addExpansionCMR("bar", "foo", 3, 3, 3, 3);
115   std::string Coverage = writeCoverageRegions();
116   readCoverageRegions(Coverage);
117
118   ASSERT_EQ(CounterMappingRegion::ExpansionRegion, OutputCMRs[2].Kind);
119   ASSERT_EQ(Counter::getCounter(2), OutputCMRs[2].Count);
120   ASSERT_EQ(3U, OutputCMRs[2].LineStart);
121 }
122
123
124 } // end anonymous namespace