Coverage for /Syzygy/grinder/line_info_unittest.cc

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%83830.C++test

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc. All Rights Reserved.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  
  15    :  #include "syzygy/grinder/line_info.h"
  16    :  
  17    :  #include "base/win/scoped_com_initializer.h"
  18    :  #include "gmock/gmock.h"
  19    :  #include "gtest/gtest.h"
  20    :  #include "syzygy/core/unittest_util.h"
  21    :  #include "syzygy/pe/unittest_util.h"
  22    :  
  23    :  namespace grinder {
  24    :  
  25    :  namespace {
  26    :  
  27    :  class TestLineInfo : public LineInfo {
  28    :   public:
  29    :    using LineInfo::source_files_;
  30    :    using LineInfo::source_lines_;
  31    :  
  32  E :    void ResetVisitedLines() {
  33  E :      for (size_t i = 0; i < source_lines_.size(); ++i) {
  34  E :        source_lines_[i].visit_count = 0;
  35  E :      }
  36  E :    }
  37    :  
  38  E :    void GetVisitedLines(std::vector<size_t>* visited_lines) const {
  39  E :      DCHECK(visited_lines != NULL);
  40  E :      visited_lines->clear();
  41  E :      for (size_t i = 0; i < source_lines_.size(); ++i) {
  42  E :        if (source_lines_[i].visit_count > 0)
  43  E :          visited_lines->push_back(source_lines_[i].line_number);
  44  E :      }
  45  E :    }
  46    :  };
  47    :  
  48    :  class LineInfoTest : public testing::Test {
  49    :   public:
  50  E :    virtual void SetUp() override {
  51  E :      testing::Test::SetUp();
  52    :  
  53    :      pdb_path_ = testing::GetExeTestDataRelativePath(
  54  E :          testing::kCoverageInstrumentedTestDllPdbName);
  55    :  
  56    :  
  57    :      std::wstring static_pdb_path(
  58  E :          L"syzygy/grinder/test_data/coverage_instrumented_test_dll.pdb");
  59  E :      static_pdb_path_ = testing::GetSrcRelativePath(static_pdb_path.c_str());
  60  E :    }
  61    :  
  62    :    // Ensures that COM is initialized for tests in this fixture.
  63    :    base::win::ScopedCOMInitializer com_initializer_;
  64    :  
  65    :    base::FilePath pdb_path_;
  66    :    base::FilePath static_pdb_path_;
  67    :  };
  68    :  
  69    :  void PushBackSourceLine(
  70    :      TestLineInfo* line_info,
  71    :      const std::string* source_file_name,
  72    :      size_t line_number,
  73    :      uint32 address,
  74  E :      size_t size) {
  75  E :    DCHECK(line_info != NULL);
  76    :    line_info->source_lines_.push_back(LineInfo::SourceLine(
  77    :        source_file_name,
  78    :        line_number,
  79    :        core::RelativeAddress(address),
  80  E :        size));
  81  E :  }
  82    :  
  83    :  #define EXPECT_LINES_VISITED(line_info, ...) \
  84    :      { \
  85    :        const size_t kLineNumbers[] = { __VA_ARGS__ }; \
  86    :        std::vector<size_t> visited, expected; \
  87    :        expected.assign(kLineNumbers, kLineNumbers + arraysize(kLineNumbers)); \
  88    :        line_info.GetVisitedLines(&visited); \
  89    :        std::sort(expected.begin(), expected.end()); \
  90    :        std::sort(visited.begin(), visited.end()); \
  91    :        EXPECT_THAT(expected, ::testing::ContainerEq(visited)); \
  92    :      }
  93    :  
  94    :  #define EXPECT_NO_LINES_VISITED(line_info) \
  95    :      { \
  96    :        std::vector<size_t> visited; \
  97    :        line_info.GetVisitedLines(&visited); \
  98    :        EXPECT_EQ(0u, visited.size()); \
  99    :      }
 100    :  
 101    :  }  // namespace
 102    :  
 103  E :  TEST_F(LineInfoTest, InitDynamicPdb) {
 104  E :    TestLineInfo line_info;
 105  E :    EXPECT_TRUE(line_info.Init(pdb_path_));
 106  E :  }
 107    :  
 108  E :  TEST_F(LineInfoTest, InitStaticPdb) {
 109  E :    TestLineInfo line_info;
 110  E :    EXPECT_TRUE(line_info.Init(static_pdb_path_));
 111    :  
 112    :    // The expected values were taken by running "pdb_dumper --dump-modules
 113    :    // syzygy/grinder/test_data/coverage_instrumented_test_dll.pdb" and running
 114    :    // through the following filters:
 115    :    // grep "line at" | sed 's/(.*$//' | uniq | sort | uniq | wc -l
 116  E :    EXPECT_EQ(138u, line_info.source_files().size());
 117    :    // grep "line at" | wc -l
 118  E :    EXPECT_EQ(8379u, line_info.source_lines().size());
 119  E :  }
 120    :  
 121  E :  TEST_F(LineInfoTest, Visit) {
 122  E :    TestLineInfo line_info;
 123    :  
 124    :    // Create a single dummy source file.
 125  E :    std::string source_file("foo.cc");
 126    :  
 127    :    // The first two entries have identical ranges, and map multiple lines to
 128    :    // those ranges.
 129  E :    PushBackSourceLine(&line_info, &source_file, 1, 4096, 2);
 130  E :    PushBackSourceLine(&line_info, &source_file, 2, 4096, 2);
 131  E :    PushBackSourceLine(&line_info, &source_file, 3, 4098, 2);
 132  E :    PushBackSourceLine(&line_info, &source_file, 5, 4100, 2);
 133    :    // Leave a gap between these two entries.
 134  E :    PushBackSourceLine(&line_info, &source_file, 6, 4104, 6);
 135  E :    PushBackSourceLine(&line_info, &source_file, 7, 4110, 2);
 136    :  
 137    :    // So, our line info looks like this:
 138    :    //  1,2   3    5         6    7        <-- line numbers
 139    :    // +----+----+----+----+----+----+
 140    :    // |0,1 | 2  | 3  |gap | 4  | 5  |     <-- source_lines_ indices
 141    :    // +----+----+----+----+----+----+
 142    :    // 4096 4098 4100 4102 4104 4110 4112  <-- address ranges
 143    :  
 144    :    // Visit a repeated BB (multiple lines).
 145  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 1));
 146  E :    EXPECT_LINES_VISITED(line_info, 1, 2);
 147    :  
 148    :    // Visit a range spanning multiple BBs (we don't reset the previously
 149    :    // visited lines to ensure that stats are kept correctly across multiple
 150    :    // calls to LineInfo::Visit).
 151  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4098), 4, 1));
 152  E :    EXPECT_LINES_VISITED(line_info, 1, 2, 3, 5);
 153    :  
 154    :    // Visit a gap and no blocks.
 155  E :    line_info.ResetVisitedLines();
 156  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4102), 2, 1));
 157  E :    EXPECT_NO_LINES_VISITED(line_info);
 158    :  
 159    :    // Visit a range spanning a gap (at the left) and a BB.
 160  E :    line_info.ResetVisitedLines();
 161  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4102), 8, 1));
 162  E :    EXPECT_LINES_VISITED(line_info, 6);
 163    :  
 164    :    // Visit a range spanning a gap (at the right) and a BB.
 165  E :    line_info.ResetVisitedLines();
 166  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 4, 1));
 167  E :    EXPECT_LINES_VISITED(line_info, 5);
 168    :  
 169    :    // Visit a range spanning 2 BBs with a gap in the middle.
 170  E :    line_info.ResetVisitedLines();
 171  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 10, 1));
 172  E :    EXPECT_LINES_VISITED(line_info, 5, 6);
 173    :  
 174    :    // Visit a range only partially spanning a single BB.
 175  E :    line_info.ResetVisitedLines();
 176  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 1, 1));
 177  E :    EXPECT_LINES_VISITED(line_info, 5);
 178    :  
 179    :    // Visit a range partially spanning a BB on the left.
 180  E :    line_info.ResetVisitedLines();
 181  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4108), 4, 1));
 182  E :    EXPECT_LINES_VISITED(line_info, 6, 7);
 183    :  
 184    :    // Visit a range partially spanning a BB on the right.
 185  E :    line_info.ResetVisitedLines();
 186  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4104), 7, 1));
 187  E :    EXPECT_LINES_VISITED(line_info, 6, 7);
 188  E :  }
 189    :  
 190  E :  TEST_F(LineInfoTest, VisitCounterWorks) {
 191  E :    TestLineInfo line_info;
 192    :  
 193    :    // Create a single dummy source file.
 194  E :    std::string source_file("foo.cc");
 195    :  
 196    :    // Add a source line.
 197  E :    PushBackSourceLine(&line_info, &source_file, 1, 4096, 2);
 198    :    LineInfo::SourceLines::const_iterator line_it =
 199  E :        line_info.source_lines().begin();
 200  E :    EXPECT_EQ(0u, line_it->visit_count);
 201    :  
 202  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 1));
 203  E :    EXPECT_EQ(1u, line_it->visit_count);
 204    :  
 205  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 2));
 206  E :    EXPECT_EQ(3u, line_it->visit_count);
 207    :  
 208    :    // Ensure our saturation addition works by trying to overflow.
 209  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 0xffffffff));
 210  E :    EXPECT_EQ(0xffffffff, line_it->visit_count);
 211  E :    EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 10));
 212  E :    EXPECT_EQ(0xffffffff, line_it->visit_count);
 213  E :  }
 214    :  
 215    :  }  // namespace grinder

Coverage information generated Thu Jan 14 17:40:38 2016.