aboutsummaryrefslogtreecommitdiff
path: root/stgdiff_test.cc
blob: 306ddc399cfbe8643b219a985ee6911e82ad2a39 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- mode: C++ -*-
//
// Copyright 2022 Google LLC
//
// Licensed under the Apache License v2.0 with LLVM Exceptions (the
// "License"); you may not use this file except in compliance with the
// License.  You may obtain a copy of the License at
//
//     https://llvm.org/LICENSE.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Siddharth Nayyar

#include <filesystem>
#include <fstream>
#include <sstream>
#include <string>

#include <catch2/catch.hpp>
#include "comparison.h"
#include "graph.h"
#include "input.h"
#include "reader_options.h"
#include "reporting.h"
#include "runtime.h"

namespace {

struct IgnoreTestCase {
  const std::string name;
  const stg::InputFormat format0;
  const std::string file0;
  const stg::InputFormat format1;
  const std::string file1;
  const stg::Ignore ignore;
  const std::string expected_output;
  const bool expected_equals;
};

std::string filename_to_path(const std::string& f) {
  return std::filesystem::path("testdata") / f;
}

stg::Id Read(stg::Runtime& runtime, stg::Graph& graph, stg::InputFormat format,
             const std::string& input) {
  return stg::Read(runtime, graph, format, filename_to_path(input).c_str(),
                   stg::ReadOptions(), nullptr);
}

TEST_CASE("ignore") {
  const auto test = GENERATE(
      IgnoreTestCase(
          {"symbol type presence change",
           stg::InputFormat::ABI,
           "symbol_type_presence_0.xml",
           stg::InputFormat::ABI,
           "symbol_type_presence_1.xml",
           stg::Ignore(),
           "symbol_type_presence_small_diff",
           false}),
      IgnoreTestCase(
          {"symbol type presence change pruned",
           stg::InputFormat::ABI,
           "symbol_type_presence_0.xml",
           stg::InputFormat::ABI,
           "symbol_type_presence_1.xml",
           stg::Ignore(stg::Ignore::SYMBOL_TYPE_PRESENCE),
           "empty",
           true}),
      IgnoreTestCase(
          {"type declaration status change",
           stg::InputFormat::ABI,
           "type_declaration_status_0.xml",
           stg::InputFormat::ABI,
           "type_declaration_status_1.xml",
           stg::Ignore(),
           "type_declaration_status_small_diff",
           false}),
      IgnoreTestCase(
          {"type declaration status change pruned",
           stg::InputFormat::ABI,
           "type_declaration_status_0.xml",
           stg::InputFormat::ABI,
           "type_declaration_status_1.xml",
           stg::Ignore(stg::Ignore::TYPE_DECLARATION_STATUS),
           "empty",
           true}),
      IgnoreTestCase(
          {"primitive type encoding",
           stg::InputFormat::STG,
           "primitive_type_encoding_0.stg",
           stg::InputFormat::STG,
           "primitive_type_encoding_1.stg",
           stg::Ignore(),
           "primitive_type_encoding_small_diff",
           false}),
      IgnoreTestCase(
          {"primitive type encoding ignored",
           stg::InputFormat::STG,
           "primitive_type_encoding_0.stg",
           stg::InputFormat::STG,
           "primitive_type_encoding_1.stg",
           stg::Ignore(stg::Ignore::PRIMITIVE_TYPE_ENCODING),
           "empty",
           true}),
      IgnoreTestCase(
          {"member size",
           stg::InputFormat::STG,
           "member_size_0.stg",
           stg::InputFormat::STG,
           "member_size_1.stg",
           stg::Ignore(),
           "member_size_small_diff",
           false}),
      IgnoreTestCase(
          {"member size ignored",
           stg::InputFormat::STG,
           "member_size_0.stg",
           stg::InputFormat::STG,
           "member_size_1.stg",
           stg::Ignore(stg::Ignore::MEMBER_SIZE),
           "empty",
           true}),
      IgnoreTestCase(
          {"enum underlying type",
           stg::InputFormat::STG,
           "enum_underlying_type_0.stg",
           stg::InputFormat::STG,
           "enum_underlying_type_1.stg",
           stg::Ignore(),
           "enum_underlying_type_small_diff",
           false}),
      IgnoreTestCase(
          {"enum underlying type ignored",
           stg::InputFormat::STG,
           "enum_underlying_type_0.stg",
           stg::InputFormat::STG,
           "enum_underlying_type_1.stg",
           stg::Ignore(stg::Ignore::ENUM_UNDERLYING_TYPE),
           "empty",
           true}),
      IgnoreTestCase(
          {"qualifier",
           stg::InputFormat::STG,
           "qualifier_0.stg",
           stg::InputFormat::STG,
           "qualifier_1.stg",
           stg::Ignore(),
           "qualifier_small_diff",
           false}),
      IgnoreTestCase(
          {"qualifier ignored",
           stg::InputFormat::STG,
           "qualifier_0.stg",
           stg::InputFormat::STG,
           "qualifier_1.stg",
           stg::Ignore(stg::Ignore::QUALIFIER),
           "empty",
           true}),
      IgnoreTestCase(
          {"CRC change",
           stg::InputFormat::STG,
           "crc_change_0.stg",
           stg::InputFormat::STG,
           "crc_change_1.stg",
           stg::Ignore(),
           "crc_change_small_diff",
           false}),
      IgnoreTestCase(
          {"CRC change ignored",
           stg::InputFormat::STG,
           "crc_change_0.stg",
           stg::InputFormat::STG,
           "crc_change_1.stg",
           stg::Ignore(stg::Ignore::SYMBOL_CRC),
           "empty",
           true}),
      IgnoreTestCase(
          {"interface addition",
           stg::InputFormat::STG,
           "interface_addition_0.stg",
           stg::InputFormat::STG,
           "interface_addition_1.stg",
           stg::Ignore(),
           "interface_addition_small_diff",
           false}),
      IgnoreTestCase(
          {"interface addition ignored",
           stg::InputFormat::STG,
           "interface_addition_0.stg",
           stg::InputFormat::STG,
           "interface_addition_1.stg",
           stg::Ignore(stg::Ignore::INTERFACE_ADDITION),
           "empty",
           true}),
      IgnoreTestCase(
          {"type addition",
           stg::InputFormat::STG,
           "type_addition_0.stg",
           stg::InputFormat::STG,
           "type_addition_1.stg",
           stg::Ignore(),
           "type_addition_small_diff",
           false}),
      IgnoreTestCase(
          {"type addition ignored",
           stg::InputFormat::STG,
           "type_addition_0.stg",
           stg::InputFormat::STG,
           "type_addition_1.stg",
           stg::Ignore(stg::Ignore::INTERFACE_ADDITION),
           "empty",
           true}),
      IgnoreTestCase(
          {"type definition addition",
           stg::InputFormat::STG,
           "type_addition_1.stg",
           stg::InputFormat::STG,
           "type_addition_2.stg",
           stg::Ignore(),
           "type_definition_addition_small_diff",
           false}),
      IgnoreTestCase(
          {"type definition addition ignored",
           stg::InputFormat::STG,
           "type_addition_1.stg",
           stg::InputFormat::STG,
           "type_addition_2.stg",
           stg::Ignore(stg::Ignore::TYPE_DEFINITION_ADDITION),
           "empty",
           true})
      );

  SECTION(test.name) {
    std::ostringstream os;
    stg::Runtime runtime(os, false);

    // Read inputs.
    stg::Graph graph;
    const auto id0 = Read(runtime, graph, test.format0, test.file0);
    const auto id1 = Read(runtime, graph, test.format1, test.file1);

    // Compute differences.
    stg::Compare compare{runtime, graph, test.ignore};
    const auto& [equals, comparison] = compare(id0, id1);

    // Write SMALL reports.
    std::ostringstream output;
    if (comparison) {
      stg::NameCache names;
      stg::reporting::Options options{stg::reporting::OutputFormat::SMALL, 0};
      stg::reporting::Reporting reporting{graph, compare.outcomes, options,
                                          names};
      Report(reporting, *comparison, output);
    }

    // Check comparison outcome and report output.
    CHECK(equals == test.expected_equals);
    std::ifstream expected_output_file(filename_to_path(test.expected_output));
    std::ostringstream expected_output;
    expected_output << expected_output_file.rdbuf();
    CHECK(output.str() == expected_output.str());
  }
}

struct ShortReportTestCase {
  const std::string name;
  const std::string xml0;
  const std::string xml1;
  const std::string expected_output;
};

TEST_CASE("short report") {
  const auto test = GENERATE(
      ShortReportTestCase(
          {"crc changes", "crc_0.xml", "crc_1.xml", "crc_changes_short_diff"}),
      ShortReportTestCase({"only crc changes", "crc_only_0.xml",
                           "crc_only_1.xml", "crc_only_changes_short_diff"}),
      ShortReportTestCase({"offset changes", "offset_0.xml", "offset_1.xml",
                           "offset_changes_short_diff"}),
      ShortReportTestCase(
          {"symbols added and removed", "added_removed_symbols_0.xml",
           "added_removed_symbols_1.xml", "added_removed_symbols_short_diff"}),
      ShortReportTestCase({"symbols added and removed only",
                           "added_removed_symbols_only_0.xml",
                           "added_removed_symbols_only_1.xml",
                           "added_removed_symbols_only_short_diff"}));

  SECTION(test.name) {
    std::ostringstream os;
    stg::Runtime runtime(os, false);

    // Read inputs.
    stg::Graph graph;
    const auto id0 = Read(runtime, graph, stg::InputFormat::ABI, test.xml0);
    const auto id1 = Read(runtime, graph, stg::InputFormat::ABI, test.xml1);

    // Compute differences.
    stg::Compare compare{runtime, graph, {}};
    const auto& [equals, comparison] = compare(id0, id1);

    // Write SHORT reports.
    std::stringstream output;
    if (comparison) {
      stg::NameCache names;
      stg::reporting::Options options{stg::reporting::OutputFormat::SHORT, 2};
      stg::reporting::Reporting reporting{graph, compare.outcomes, options,
                                          names};
      Report(reporting, *comparison, output);
    }

    // Check comparison outcome and report output.
    CHECK(equals == false);
    std::ifstream expected_output_file(filename_to_path(test.expected_output));
    std::ostringstream expected_output;
    expected_output << expected_output_file.rdbuf();
    CHECK(output.str() == expected_output.str());
  }
}

TEST_CASE("fidelity diff") {
  std::ostringstream os;
  stg::Runtime runtime(os, false);

  // Read inputs.
  stg::Graph graph;
  const auto id0 =
      Read(runtime, graph, stg::InputFormat::STG, "fidelity_diff_0.stg");
  const auto id1 =
      Read(runtime, graph, stg::InputFormat::STG, "fidelity_diff_1.stg");

  // Compute fidelity diff.
  auto fidelity_diff = stg::GetFidelityTransitions(graph, id0, id1);

  // Write fidelity diff report.
  std::ostringstream report;
  stg::reporting::FidelityDiff(fidelity_diff, report);

  // Check report.
  std::ifstream expected_report_file(filename_to_path("fidelity_diff_report"));
  std::ostringstream expected_report;
  expected_report << expected_report_file.rdbuf();
  CHECK(report.str() == expected_report.str());
}

}  // namespace