xref: /aosp_15_r20/external/stg/stgdiff_test.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1*9e3b08aeSAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2*9e3b08aeSAndroid Build Coastguard Worker // -*- mode: C++ -*-
3*9e3b08aeSAndroid Build Coastguard Worker //
4*9e3b08aeSAndroid Build Coastguard Worker // Copyright 2022 Google LLC
5*9e3b08aeSAndroid Build Coastguard Worker //
6*9e3b08aeSAndroid Build Coastguard Worker // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7*9e3b08aeSAndroid Build Coastguard Worker // "License"); you may not use this file except in compliance with the
8*9e3b08aeSAndroid Build Coastguard Worker // License.  You may obtain a copy of the License at
9*9e3b08aeSAndroid Build Coastguard Worker //
10*9e3b08aeSAndroid Build Coastguard Worker //     https://llvm.org/LICENSE.txt
11*9e3b08aeSAndroid Build Coastguard Worker //
12*9e3b08aeSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
13*9e3b08aeSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
14*9e3b08aeSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*9e3b08aeSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
16*9e3b08aeSAndroid Build Coastguard Worker // limitations under the License.
17*9e3b08aeSAndroid Build Coastguard Worker //
18*9e3b08aeSAndroid Build Coastguard Worker // Author: Siddharth Nayyar
19*9e3b08aeSAndroid Build Coastguard Worker 
20*9e3b08aeSAndroid Build Coastguard Worker #include <filesystem>
21*9e3b08aeSAndroid Build Coastguard Worker #include <fstream>
22*9e3b08aeSAndroid Build Coastguard Worker #include <sstream>
23*9e3b08aeSAndroid Build Coastguard Worker #include <string>
24*9e3b08aeSAndroid Build Coastguard Worker 
25*9e3b08aeSAndroid Build Coastguard Worker #include <catch2/catch.hpp>
26*9e3b08aeSAndroid Build Coastguard Worker #include "comparison.h"
27*9e3b08aeSAndroid Build Coastguard Worker #include "fidelity.h"
28*9e3b08aeSAndroid Build Coastguard Worker #include "graph.h"
29*9e3b08aeSAndroid Build Coastguard Worker #include "input.h"
30*9e3b08aeSAndroid Build Coastguard Worker #include "naming.h"
31*9e3b08aeSAndroid Build Coastguard Worker #include "reader_options.h"
32*9e3b08aeSAndroid Build Coastguard Worker #include "reporting.h"
33*9e3b08aeSAndroid Build Coastguard Worker #include "runtime.h"
34*9e3b08aeSAndroid Build Coastguard Worker 
35*9e3b08aeSAndroid Build Coastguard Worker namespace stg {
36*9e3b08aeSAndroid Build Coastguard Worker namespace {
37*9e3b08aeSAndroid Build Coastguard Worker 
38*9e3b08aeSAndroid Build Coastguard Worker struct IgnoreTestCase {
39*9e3b08aeSAndroid Build Coastguard Worker   const std::string name;
40*9e3b08aeSAndroid Build Coastguard Worker   const InputFormat format0;
41*9e3b08aeSAndroid Build Coastguard Worker   const std::string file0;
42*9e3b08aeSAndroid Build Coastguard Worker   const InputFormat format1;
43*9e3b08aeSAndroid Build Coastguard Worker   const std::string file1;
44*9e3b08aeSAndroid Build Coastguard Worker   const diff::Ignore ignore;
45*9e3b08aeSAndroid Build Coastguard Worker   const std::string expected_output;
46*9e3b08aeSAndroid Build Coastguard Worker   const bool expected_same;
47*9e3b08aeSAndroid Build Coastguard Worker };
48*9e3b08aeSAndroid Build Coastguard Worker 
filename_to_path(const std::string & f)49*9e3b08aeSAndroid Build Coastguard Worker std::string filename_to_path(const std::string& f) {
50*9e3b08aeSAndroid Build Coastguard Worker   return std::filesystem::path("testdata") / f;
51*9e3b08aeSAndroid Build Coastguard Worker }
52*9e3b08aeSAndroid Build Coastguard Worker 
Read(Runtime & runtime,Graph & graph,InputFormat format,const std::string & input)53*9e3b08aeSAndroid Build Coastguard Worker Id Read(Runtime& runtime, Graph& graph, InputFormat format,
54*9e3b08aeSAndroid Build Coastguard Worker         const std::string& input) {
55*9e3b08aeSAndroid Build Coastguard Worker   return Read(runtime, graph, format, filename_to_path(input).c_str(),
56*9e3b08aeSAndroid Build Coastguard Worker               ReadOptions(), nullptr);
57*9e3b08aeSAndroid Build Coastguard Worker }
58*9e3b08aeSAndroid Build Coastguard Worker 
59*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("ignore") {
60*9e3b08aeSAndroid Build Coastguard Worker   const auto test = GENERATE(
61*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
62*9e3b08aeSAndroid Build Coastguard Worker           {"symbol type presence change",
63*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
64*9e3b08aeSAndroid Build Coastguard Worker            "symbol_type_presence_0.xml",
65*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
66*9e3b08aeSAndroid Build Coastguard Worker            "symbol_type_presence_1.xml",
67*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
68*9e3b08aeSAndroid Build Coastguard Worker            "symbol_type_presence_small_diff",
69*9e3b08aeSAndroid Build Coastguard Worker            false}),
70*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
71*9e3b08aeSAndroid Build Coastguard Worker           {"symbol type presence change pruned",
72*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
73*9e3b08aeSAndroid Build Coastguard Worker            "symbol_type_presence_0.xml",
74*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
75*9e3b08aeSAndroid Build Coastguard Worker            "symbol_type_presence_1.xml",
76*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::SYMBOL_TYPE_PRESENCE),
77*9e3b08aeSAndroid Build Coastguard Worker            "empty",
78*9e3b08aeSAndroid Build Coastguard Worker            true}),
79*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
80*9e3b08aeSAndroid Build Coastguard Worker           {"type declaration status change",
81*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
82*9e3b08aeSAndroid Build Coastguard Worker            "type_declaration_status_0.xml",
83*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
84*9e3b08aeSAndroid Build Coastguard Worker            "type_declaration_status_1.xml",
85*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
86*9e3b08aeSAndroid Build Coastguard Worker            "type_declaration_status_small_diff",
87*9e3b08aeSAndroid Build Coastguard Worker            false}),
88*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
89*9e3b08aeSAndroid Build Coastguard Worker           {"type declaration status change pruned",
90*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
91*9e3b08aeSAndroid Build Coastguard Worker            "type_declaration_status_0.xml",
92*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::ABI,
93*9e3b08aeSAndroid Build Coastguard Worker            "type_declaration_status_1.xml",
94*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::TYPE_DECLARATION_STATUS),
95*9e3b08aeSAndroid Build Coastguard Worker            "empty",
96*9e3b08aeSAndroid Build Coastguard Worker            true}),
97*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
98*9e3b08aeSAndroid Build Coastguard Worker           {"primitive type encoding",
99*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
100*9e3b08aeSAndroid Build Coastguard Worker            "primitive_type_encoding_0.stg",
101*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
102*9e3b08aeSAndroid Build Coastguard Worker            "primitive_type_encoding_1.stg",
103*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
104*9e3b08aeSAndroid Build Coastguard Worker            "primitive_type_encoding_small_diff",
105*9e3b08aeSAndroid Build Coastguard Worker            false}),
106*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
107*9e3b08aeSAndroid Build Coastguard Worker           {"primitive type encoding ignored",
108*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
109*9e3b08aeSAndroid Build Coastguard Worker            "primitive_type_encoding_0.stg",
110*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
111*9e3b08aeSAndroid Build Coastguard Worker            "primitive_type_encoding_1.stg",
112*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::PRIMITIVE_TYPE_ENCODING),
113*9e3b08aeSAndroid Build Coastguard Worker            "empty",
114*9e3b08aeSAndroid Build Coastguard Worker            true}),
115*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
116*9e3b08aeSAndroid Build Coastguard Worker           {"member size",
117*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
118*9e3b08aeSAndroid Build Coastguard Worker            "member_size_0.stg",
119*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
120*9e3b08aeSAndroid Build Coastguard Worker            "member_size_1.stg",
121*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
122*9e3b08aeSAndroid Build Coastguard Worker            "member_size_small_diff",
123*9e3b08aeSAndroid Build Coastguard Worker            false}),
124*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
125*9e3b08aeSAndroid Build Coastguard Worker           {"member size ignored",
126*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
127*9e3b08aeSAndroid Build Coastguard Worker            "member_size_0.stg",
128*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
129*9e3b08aeSAndroid Build Coastguard Worker            "member_size_1.stg",
130*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::MEMBER_SIZE),
131*9e3b08aeSAndroid Build Coastguard Worker            "empty",
132*9e3b08aeSAndroid Build Coastguard Worker            true}),
133*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
134*9e3b08aeSAndroid Build Coastguard Worker           {"enum underlying type",
135*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
136*9e3b08aeSAndroid Build Coastguard Worker            "enum_underlying_type_0.stg",
137*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
138*9e3b08aeSAndroid Build Coastguard Worker            "enum_underlying_type_1.stg",
139*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
140*9e3b08aeSAndroid Build Coastguard Worker            "enum_underlying_type_small_diff",
141*9e3b08aeSAndroid Build Coastguard Worker            false}),
142*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
143*9e3b08aeSAndroid Build Coastguard Worker           {"enum underlying type ignored",
144*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
145*9e3b08aeSAndroid Build Coastguard Worker            "enum_underlying_type_0.stg",
146*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
147*9e3b08aeSAndroid Build Coastguard Worker            "enum_underlying_type_1.stg",
148*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::ENUM_UNDERLYING_TYPE),
149*9e3b08aeSAndroid Build Coastguard Worker            "empty",
150*9e3b08aeSAndroid Build Coastguard Worker            true}),
151*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
152*9e3b08aeSAndroid Build Coastguard Worker           {"qualifier",
153*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
154*9e3b08aeSAndroid Build Coastguard Worker            "qualifier_0.stg",
155*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
156*9e3b08aeSAndroid Build Coastguard Worker            "qualifier_1.stg",
157*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
158*9e3b08aeSAndroid Build Coastguard Worker            "qualifier_small_diff",
159*9e3b08aeSAndroid Build Coastguard Worker            false}),
160*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
161*9e3b08aeSAndroid Build Coastguard Worker           {"qualifier ignored",
162*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
163*9e3b08aeSAndroid Build Coastguard Worker            "qualifier_0.stg",
164*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
165*9e3b08aeSAndroid Build Coastguard Worker            "qualifier_1.stg",
166*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::QUALIFIER),
167*9e3b08aeSAndroid Build Coastguard Worker            "empty",
168*9e3b08aeSAndroid Build Coastguard Worker            true}),
169*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
170*9e3b08aeSAndroid Build Coastguard Worker           {"CRC change",
171*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
172*9e3b08aeSAndroid Build Coastguard Worker            "crc_change_0.stg",
173*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
174*9e3b08aeSAndroid Build Coastguard Worker            "crc_change_1.stg",
175*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
176*9e3b08aeSAndroid Build Coastguard Worker            "crc_change_small_diff",
177*9e3b08aeSAndroid Build Coastguard Worker            false}),
178*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
179*9e3b08aeSAndroid Build Coastguard Worker           {"CRC change ignored",
180*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
181*9e3b08aeSAndroid Build Coastguard Worker            "crc_change_0.stg",
182*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
183*9e3b08aeSAndroid Build Coastguard Worker            "crc_change_1.stg",
184*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::SYMBOL_CRC),
185*9e3b08aeSAndroid Build Coastguard Worker            "empty",
186*9e3b08aeSAndroid Build Coastguard Worker            true}),
187*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
188*9e3b08aeSAndroid Build Coastguard Worker           {"interface addition",
189*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
190*9e3b08aeSAndroid Build Coastguard Worker            "interface_addition_0.stg",
191*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
192*9e3b08aeSAndroid Build Coastguard Worker            "interface_addition_1.stg",
193*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
194*9e3b08aeSAndroid Build Coastguard Worker            "interface_addition_small_diff",
195*9e3b08aeSAndroid Build Coastguard Worker            false}),
196*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
197*9e3b08aeSAndroid Build Coastguard Worker           {"interface addition ignored",
198*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
199*9e3b08aeSAndroid Build Coastguard Worker            "interface_addition_0.stg",
200*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
201*9e3b08aeSAndroid Build Coastguard Worker            "interface_addition_1.stg",
202*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::INTERFACE_ADDITION),
203*9e3b08aeSAndroid Build Coastguard Worker            "empty",
204*9e3b08aeSAndroid Build Coastguard Worker            true}),
205*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
206*9e3b08aeSAndroid Build Coastguard Worker           {"type addition",
207*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
208*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_0.stg",
209*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
210*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_1.stg",
211*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
212*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_small_diff",
213*9e3b08aeSAndroid Build Coastguard Worker            false}),
214*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
215*9e3b08aeSAndroid Build Coastguard Worker           {"type addition ignored",
216*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
217*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_0.stg",
218*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
219*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_1.stg",
220*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::INTERFACE_ADDITION),
221*9e3b08aeSAndroid Build Coastguard Worker            "empty",
222*9e3b08aeSAndroid Build Coastguard Worker            true}),
223*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
224*9e3b08aeSAndroid Build Coastguard Worker           {"type definition addition",
225*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
226*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_1.stg",
227*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
228*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_2.stg",
229*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(),
230*9e3b08aeSAndroid Build Coastguard Worker            "type_definition_addition_small_diff",
231*9e3b08aeSAndroid Build Coastguard Worker            false}),
232*9e3b08aeSAndroid Build Coastguard Worker       IgnoreTestCase(
233*9e3b08aeSAndroid Build Coastguard Worker           {"type definition addition ignored",
234*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
235*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_1.stg",
236*9e3b08aeSAndroid Build Coastguard Worker            InputFormat::STG,
237*9e3b08aeSAndroid Build Coastguard Worker            "type_addition_2.stg",
238*9e3b08aeSAndroid Build Coastguard Worker            diff::Ignore(diff::Ignore::TYPE_DEFINITION_ADDITION),
239*9e3b08aeSAndroid Build Coastguard Worker            "empty",
240*9e3b08aeSAndroid Build Coastguard Worker            true})
241*9e3b08aeSAndroid Build Coastguard Worker       );
242*9e3b08aeSAndroid Build Coastguard Worker 
243*9e3b08aeSAndroid Build Coastguard Worker   SECTION(test.name) {
244*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream os;
245*9e3b08aeSAndroid Build Coastguard Worker     Runtime runtime(os, false);
246*9e3b08aeSAndroid Build Coastguard Worker 
247*9e3b08aeSAndroid Build Coastguard Worker     // Read inputs.
248*9e3b08aeSAndroid Build Coastguard Worker     Graph graph;
249*9e3b08aeSAndroid Build Coastguard Worker     const Id id0 = Read(runtime, graph, test.format0, test.file0);
250*9e3b08aeSAndroid Build Coastguard Worker     const Id id1 = Read(runtime, graph, test.format1, test.file1);
251*9e3b08aeSAndroid Build Coastguard Worker 
252*9e3b08aeSAndroid Build Coastguard Worker     // Compute differences.
253*9e3b08aeSAndroid Build Coastguard Worker     stg::diff::Outcomes outcomes;
254*9e3b08aeSAndroid Build Coastguard Worker     const auto comparison =
255*9e3b08aeSAndroid Build Coastguard Worker         diff::Compare(runtime, test.ignore, graph, id0, id1, outcomes);
256*9e3b08aeSAndroid Build Coastguard Worker     const bool same = comparison == diff::Comparison{};
257*9e3b08aeSAndroid Build Coastguard Worker 
258*9e3b08aeSAndroid Build Coastguard Worker     // Write SMALL reports.
259*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream output;
260*9e3b08aeSAndroid Build Coastguard Worker     if (!same) {
261*9e3b08aeSAndroid Build Coastguard Worker       NameCache names;
262*9e3b08aeSAndroid Build Coastguard Worker       const reporting::Options options{reporting::OutputFormat::SMALL};
263*9e3b08aeSAndroid Build Coastguard Worker       const reporting::Reporting reporting{graph, outcomes, options, names};
264*9e3b08aeSAndroid Build Coastguard Worker       Report(reporting, comparison, output);
265*9e3b08aeSAndroid Build Coastguard Worker     }
266*9e3b08aeSAndroid Build Coastguard Worker 
267*9e3b08aeSAndroid Build Coastguard Worker     // Check comparison outcome and report output.
268*9e3b08aeSAndroid Build Coastguard Worker     CHECK(same == test.expected_same);
269*9e3b08aeSAndroid Build Coastguard Worker     const std::ifstream expected_output_file(
270*9e3b08aeSAndroid Build Coastguard Worker         filename_to_path(test.expected_output));
271*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream expected_output;
272*9e3b08aeSAndroid Build Coastguard Worker     expected_output << expected_output_file.rdbuf();
273*9e3b08aeSAndroid Build Coastguard Worker     CHECK(output.str() == expected_output.str());
274*9e3b08aeSAndroid Build Coastguard Worker   }
275*9e3b08aeSAndroid Build Coastguard Worker }
276*9e3b08aeSAndroid Build Coastguard Worker 
277*9e3b08aeSAndroid Build Coastguard Worker struct ShortReportTestCase {
278*9e3b08aeSAndroid Build Coastguard Worker   const std::string name;
279*9e3b08aeSAndroid Build Coastguard Worker   InputFormat format;
280*9e3b08aeSAndroid Build Coastguard Worker   const std::string file0;
281*9e3b08aeSAndroid Build Coastguard Worker   const std::string file1;
282*9e3b08aeSAndroid Build Coastguard Worker   const std::string expected_output;
283*9e3b08aeSAndroid Build Coastguard Worker };
284*9e3b08aeSAndroid Build Coastguard Worker 
285*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("short report") {
286*9e3b08aeSAndroid Build Coastguard Worker   const auto test = GENERATE(
287*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
288*9e3b08aeSAndroid Build Coastguard Worker           {"crc changes", InputFormat::ABI, "crc_0.xml", "crc_1.xml",
289*9e3b08aeSAndroid Build Coastguard Worker            "crc_changes_short_diff"}),
290*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
291*9e3b08aeSAndroid Build Coastguard Worker           {"only crc changes", InputFormat::ABI, "crc_only_0.xml",
292*9e3b08aeSAndroid Build Coastguard Worker            "crc_only_1.xml", "crc_only_changes_short_diff"}),
293*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
294*9e3b08aeSAndroid Build Coastguard Worker           {"offset changes", InputFormat::ABI, "offset_0.xml",
295*9e3b08aeSAndroid Build Coastguard Worker            "offset_1.xml", "offset_changes_short_diff"}),
296*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
297*9e3b08aeSAndroid Build Coastguard Worker           {"symbols added and removed", InputFormat::ABI,
298*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_symbols_0.xml", "added_removed_symbols_1.xml",
299*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_symbols_short_diff"}),
300*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
301*9e3b08aeSAndroid Build Coastguard Worker           {"symbols added and removed only", InputFormat::ABI,
302*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_symbols_only_0.xml",
303*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_symbols_only_1.xml",
304*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_symbols_only_short_diff"}),
305*9e3b08aeSAndroid Build Coastguard Worker       ShortReportTestCase(
306*9e3b08aeSAndroid Build Coastguard Worker           {"enumerators added and removed", stg::InputFormat::STG,
307*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_enumerators_0.stg",
308*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_enumerators_1.stg",
309*9e3b08aeSAndroid Build Coastguard Worker            "added_removed_enumerators_short_diff"}));
310*9e3b08aeSAndroid Build Coastguard Worker 
311*9e3b08aeSAndroid Build Coastguard Worker   SECTION(test.name) {
312*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream os;
313*9e3b08aeSAndroid Build Coastguard Worker     Runtime runtime(os, false);
314*9e3b08aeSAndroid Build Coastguard Worker 
315*9e3b08aeSAndroid Build Coastguard Worker     // Read inputs.
316*9e3b08aeSAndroid Build Coastguard Worker     Graph graph;
317*9e3b08aeSAndroid Build Coastguard Worker     const Id id0 = Read(runtime, graph, test.format, test.file0);
318*9e3b08aeSAndroid Build Coastguard Worker     const Id id1 = Read(runtime, graph, test.format, test.file1);
319*9e3b08aeSAndroid Build Coastguard Worker 
320*9e3b08aeSAndroid Build Coastguard Worker     // Compute differences.
321*9e3b08aeSAndroid Build Coastguard Worker     stg::diff::Outcomes outcomes;
322*9e3b08aeSAndroid Build Coastguard Worker     const auto comparison =
323*9e3b08aeSAndroid Build Coastguard Worker         diff::Compare(runtime, {}, graph, id0, id1, outcomes);
324*9e3b08aeSAndroid Build Coastguard Worker     const bool same = comparison == diff::Comparison{};
325*9e3b08aeSAndroid Build Coastguard Worker 
326*9e3b08aeSAndroid Build Coastguard Worker     // Write SHORT reports.
327*9e3b08aeSAndroid Build Coastguard Worker     std::stringstream output;
328*9e3b08aeSAndroid Build Coastguard Worker     if (!same) {
329*9e3b08aeSAndroid Build Coastguard Worker       NameCache names;
330*9e3b08aeSAndroid Build Coastguard Worker       const reporting::Options options{reporting::OutputFormat::SHORT};
331*9e3b08aeSAndroid Build Coastguard Worker       const reporting::Reporting reporting{graph, outcomes, options, names};
332*9e3b08aeSAndroid Build Coastguard Worker       Report(reporting, comparison, output);
333*9e3b08aeSAndroid Build Coastguard Worker     }
334*9e3b08aeSAndroid Build Coastguard Worker 
335*9e3b08aeSAndroid Build Coastguard Worker     // Check comparison outcome and report output.
336*9e3b08aeSAndroid Build Coastguard Worker     CHECK(!same);
337*9e3b08aeSAndroid Build Coastguard Worker     const std::ifstream expected_output_file(
338*9e3b08aeSAndroid Build Coastguard Worker         filename_to_path(test.expected_output));
339*9e3b08aeSAndroid Build Coastguard Worker     std::ostringstream expected_output;
340*9e3b08aeSAndroid Build Coastguard Worker     expected_output << expected_output_file.rdbuf();
341*9e3b08aeSAndroid Build Coastguard Worker     CHECK(output.str() == expected_output.str());
342*9e3b08aeSAndroid Build Coastguard Worker   }
343*9e3b08aeSAndroid Build Coastguard Worker }
344*9e3b08aeSAndroid Build Coastguard Worker 
345*9e3b08aeSAndroid Build Coastguard Worker TEST_CASE("fidelity diff") {
346*9e3b08aeSAndroid Build Coastguard Worker   std::ostringstream os;
347*9e3b08aeSAndroid Build Coastguard Worker   Runtime runtime(os, false);
348*9e3b08aeSAndroid Build Coastguard Worker 
349*9e3b08aeSAndroid Build Coastguard Worker   // Read inputs.
350*9e3b08aeSAndroid Build Coastguard Worker   Graph graph;
351*9e3b08aeSAndroid Build Coastguard Worker   const Id id0 = Read(runtime, graph, InputFormat::STG, "fidelity_diff_0.stg");
352*9e3b08aeSAndroid Build Coastguard Worker   const Id id1 = Read(runtime, graph, InputFormat::STG, "fidelity_diff_1.stg");
353*9e3b08aeSAndroid Build Coastguard Worker 
354*9e3b08aeSAndroid Build Coastguard Worker   // Compute fidelity diff.
355*9e3b08aeSAndroid Build Coastguard Worker   auto fidelity_diff = GetFidelityTransitions(graph, id0, id1);
356*9e3b08aeSAndroid Build Coastguard Worker 
357*9e3b08aeSAndroid Build Coastguard Worker   // Write fidelity diff report.
358*9e3b08aeSAndroid Build Coastguard Worker   std::ostringstream report;
359*9e3b08aeSAndroid Build Coastguard Worker   reporting::FidelityDiff(fidelity_diff, report);
360*9e3b08aeSAndroid Build Coastguard Worker 
361*9e3b08aeSAndroid Build Coastguard Worker   // Check report.
362*9e3b08aeSAndroid Build Coastguard Worker   const std::ifstream expected_report_file(
363*9e3b08aeSAndroid Build Coastguard Worker       filename_to_path("fidelity_diff_report"));
364*9e3b08aeSAndroid Build Coastguard Worker   std::ostringstream expected_report;
365*9e3b08aeSAndroid Build Coastguard Worker   expected_report << expected_report_file.rdbuf();
366*9e3b08aeSAndroid Build Coastguard Worker   CHECK(report.str() == expected_report.str());
367*9e3b08aeSAndroid Build Coastguard Worker }
368*9e3b08aeSAndroid Build Coastguard Worker 
369*9e3b08aeSAndroid Build Coastguard Worker }  // namespace
370*9e3b08aeSAndroid Build Coastguard Worker }  // namespace stg
371