File: testGccDepfileReader.cxx

package info (click to toggle)
cmake 4.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152,456 kB
  • sloc: ansic: 403,896; cpp: 303,920; sh: 4,105; python: 3,583; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 111; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (141 lines) | stat: -rw-r--r-- 3,989 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstddef> // IWYU pragma: keep
#include <iostream>
#include <string>
#include <utility>
#include <vector>

#include <cm/optional>

#include "cmsys/FStream.hxx"

#include "cmGccDepfileReader.h"
#include "cmGccDepfileReaderTypes.h" // for cmGccDepfileContent, cmGccStyle...
#include "cmSystemTools.h"

namespace {

cmGccDepfileContent readPlainDepfile(char const* filePath)
{
  cmGccDepfileContent result;
  cmsys::ifstream is(filePath);
  if (!is.is_open())
    return result;
  std::string line;

  cmGccStyleDependency dep;
  bool readingRules = true;
  while (cmSystemTools::GetLineFromStream(is, line)) {
    if (line == "--RULES--") {
      if (!dep.rules.empty()) {
        result.push_back(std::move(dep));
        dep = cmGccStyleDependency();
      }
      readingRules = true;
    } else if (line == "--DEPENDENCIES--") {
      readingRules = false;
    } else {
      std::vector<std::string>& dst = readingRules ? dep.rules : dep.paths;
      dst.push_back(std::move(line));
      line = std::string();
    }
  }

  if (!dep.rules.empty()) {
    result.push_back(std::move(dep));
  }

  return result;
}

bool compare(std::vector<std::string> const& actual,
             std::vector<std::string> const& expected, char const* msg)
{
  if (actual.size() != expected.size()) {
    std::cerr << msg << "expected " << expected.size() << " entries."
              << std::endl
              << "Actual number of entries: " << actual.size() << std::endl;
    return false;
  }
  for (std::size_t i = 0; i < actual.size(); ++i) {
    if (actual[i] != expected[i]) {
      std::cerr << msg << std::endl
                << "expected: " << expected[i] << std::endl
                << "actual: " << actual[i] << std::endl;
      return false;
    }
  }
  return true;
}

bool compare(cmGccDepfileContent const& actual,
             cmGccDepfileContent const& expected)
{
  if (actual.size() != expected.size()) {
    std::cerr << "Expected " << expected.size() << " entries." << std::endl
              << "Actual number of entries: " << actual.size() << std::endl;
    return false;
  }
  for (std::size_t i = 0; i < actual.size(); ++i) {
    if (!compare(actual[i].rules, expected[i].rules, "Rules differ: ") ||
        !compare(actual[i].paths, expected[i].paths, "Paths differ: ")) {
      return false;
    }
  }
  return true;
}

void dump(char const* label, cmGccDepfileContent const& dfc)
{
  std::cerr << label << std::endl;
  for (auto const& entry : dfc) {
    auto rit = entry.rules.cbegin();
    if (rit != entry.rules.cend()) {
      std::cerr << *rit;
      for (++rit; rit != entry.rules.cend(); ++rit) {
        std::cerr << "  " << *rit;
      }
      std::cerr << ": " << std::endl;
    }
    for (auto const& path : entry.paths) {
      std::cerr << "    " << path << std::endl;
    }
  }
}

} // anonymous namespace

int testGccDepfileReader(int argc, char* argv[])
{
  if (argc < 2) {
    std::cout << "Invalid arguments.\n";
    return -1;
  }

  std::string dataDirPath = argv[1];
  dataDirPath += "/testGccDepfileReader_data";
  int const numberOfTestFiles = 7; // 6th file doesn't exist
  for (int i = 1; i <= numberOfTestFiles; ++i) {
    std::string const base = dataDirPath + "/deps" + std::to_string(i);
    std::string const depfile = base + ".d";
    std::string const plainDepfile = base + ".txt";
    std::cout << "Comparing " << base << " with " << plainDepfile << std::endl;
    auto const actual = cmReadGccDepfile(depfile.c_str());
    if (cmSystemTools::FileExists(plainDepfile)) {
      if (!actual) {
        std::cerr << "Reading " << depfile << " should have succeeded\n";
        return 1;
      }
      auto const expected = readPlainDepfile(plainDepfile.c_str());
      if (!compare(*actual, expected)) {
        dump("actual", *actual);
        dump("expected", expected);
        return 1;
      }
    } else if (actual) {
      std::cerr << "Reading " << depfile << " should have failed\n";
      return 1;
    }
  }

  return 0;
}