llvm.org GIT mirror llvm / 77eb420
[Coverage] Update testing methods to support more than two files Differential Revision: http://reviews.llvm.org/D18757 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266289 91177308-0d34-0410-b5e6-96231b3b80d8 Igor Kudrin 4 years ago
1 changed file(s) with 58 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
7777
7878 struct CoverageMappingTest : ::testing::Test {
7979 StringMap Files;
80 unsigned NextFile;
8180 std::vector InputCMRs;
8281
8382 std::vector OutputFiles;
9089 std::unique_ptr LoadedCoverage;
9190
9291 void SetUp() override {
93 NextFile = 0;
9492 ProfileWriter.setOutputSparse(false);
9593 }
9694
9896 auto R = Files.find(Name);
9997 if (R != Files.end())
10098 return R->second;
101 Files[Name] = NextFile;
102 return NextFile++;
99 unsigned Index = Files.size();
100 Files.emplace_second(Name, Index);
101 return Index;
103102 }
104103
105104 void addCMR(Counter C, StringRef File, unsigned LS, unsigned CS, unsigned LE,
115114 }
116115
117116 std::string writeCoverageRegions() {
118 SmallVector FileIDs;
119 for (const auto &E : Files)
120 FileIDs.push_back(E.getValue());
117 SmallVector FileIDs(Files.size());
118 for (unsigned I = 0; I < FileIDs.size(); ++I)
119 FileIDs[I] = I;
121120 std::string Coverage;
122121 llvm::raw_string_ostream OS(Coverage);
123122 CoverageMappingWriter(FileIDs, None, InputCMRs).write(OS);
125124 }
126125
127126 void readCoverageRegions(std::string Coverage) {
128 SmallVector Filenames;
127 SmallVector Filenames(Files.size());
129128 for (const auto &E : Files)
130 Filenames.push_back(E.getKey());
129 Filenames[E.getValue()] = E.getKey();
131130 RawCoverageMappingReader Reader(Coverage, Filenames, OutputFiles,
132131 OutputExpressions, OutputCMRs);
133132 ASSERT_TRUE(NoError(Reader.read()));
146145 readCoverageRegions(Regions);
147146
148147 SmallVector Filenames;
149 if (EmitFilenames)
148 if (EmitFilenames) {
149 Filenames.resize(Files.size());
150150 for (const auto &E : Files)
151 Filenames.push_back(E.getKey());
151 Filenames[E.getValue()] = E.getKey();
152 }
152153 OneFunctionCoverageReader CovReader(FuncName, Hash, Filenames, OutputCMRs);
153154 auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
154155 ASSERT_TRUE(NoError(CoverageOrErr.getError()));
185186 }
186187 }
187188
189 TEST_P(MaybeSparseCoverageMappingTest,
190 correct_deserialize_for_more_than_two_files) {
191 const char *FileNames[] = {"bar", "baz", "foo"};
192 static const unsigned N = array_lengthof(FileNames);
193
194 for (unsigned I = 0; I < N; ++I)
195 // Use LineStart to hold the index of the file name
196 // in order to preserve that information during possible sorting of CMRs.
197 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
198
199 std::string Coverage = writeCoverageRegions();
200 readCoverageRegions(Coverage);
201
202 ASSERT_EQ(N, OutputCMRs.size());
203 ASSERT_EQ(N, OutputFiles.size());
204
205 for (unsigned I = 0; I < N; ++I) {
206 ASSERT_GT(N, OutputCMRs[I].FileID);
207 ASSERT_GT(N, OutputCMRs[I].LineStart);
208 EXPECT_EQ(FileNames[OutputCMRs[I].LineStart],
209 OutputFiles[OutputCMRs[I].FileID]);
210 }
211 }
212
213 TEST_P(MaybeSparseCoverageMappingTest, load_coverage_for_more_than_two_files) {
214 InstrProfRecord Record("func", 0x1234, {0});
215 ProfileWriter.addRecord(std::move(Record));
216 readProfCounts();
217
218 const char *FileNames[] = {"bar", "baz", "foo"};
219 static const unsigned N = array_lengthof(FileNames);
220
221 for (unsigned I = 0; I < N; ++I)
222 // Use LineStart to hold the index of the file name
223 // in order to preserve that information during possible sorting of CMRs.
224 addCMR(Counter::getCounter(0), FileNames[I], I, 1, I, 1);
225
226 loadCoverageMapping("func", 0x1234);
227
228 for (unsigned I = 0; I < N; ++I) {
229 CoverageData Data = LoadedCoverage->getCoverageForFile(FileNames[I]);
230 ASSERT_TRUE(!Data.empty());
231 EXPECT_EQ(I, Data.begin()->Line);
232 }
233 }
234
188235 TEST_P(MaybeSparseCoverageMappingTest, expansion_gets_first_counter) {
189236 addCMR(Counter::getCounter(1), "foo", 10, 1, 10, 2);
190237 // This starts earlier in "foo", so the expansion should get its counter.