1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License. You may obtain a copy of the License at
9 //
10 // https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Siddharth Nayyar
19
20 #include "reporting.h"
21
22 #include <filesystem>
23 #include <fstream>
24 #include <sstream>
25 #include <string>
26
27 #include <catch2/catch.hpp>
28 #include "fidelity.h"
29
30 namespace stg {
31 namespace {
32
filename_to_path(const std::string & f)33 std::string filename_to_path(const std::string& f) {
34 return std::filesystem::path("testdata") / f;
35 }
36
37 TEST_CASE("fidelity diff") {
38 const stg::FidelityDiff diff = {
39 .symbol_transitions =
40 {
41 {{SymbolFidelity::TYPED, SymbolFidelity::UNTYPED},
42 {"symbol1", "symbol2"}},
43 {{SymbolFidelity::UNTYPED, SymbolFidelity::TYPED}, {"symbol3"}},
44 {{SymbolFidelity::ABSENT, SymbolFidelity::UNTYPED},
45 {"symbol4", "symbol5"}},
46 {{SymbolFidelity::ABSENT, SymbolFidelity::TYPED}, {"symbol6"}},
47 {{SymbolFidelity::TYPED, SymbolFidelity::ABSENT},
48 {"symbol7", "symbol8"}},
49 {{SymbolFidelity::UNTYPED, SymbolFidelity::ABSENT}, {"symbol9"}},
50 },
51 .type_transitions =
52 {{{TypeFidelity::FULLY_DEFINED, TypeFidelity::ABSENT},
53 {"struct s1", "union u1"}},
54 {{TypeFidelity::DECLARATION_ONLY, TypeFidelity::ABSENT},
55 {"struct s2"}},
56 {{TypeFidelity::FULLY_DEFINED, TypeFidelity::DECLARATION_ONLY},
57 {"enum e1"}},
58 {{TypeFidelity::ABSENT, TypeFidelity::DECLARATION_ONLY},
59 {"union u2"}},
60 {{TypeFidelity::DECLARATION_ONLY, TypeFidelity::FULLY_DEFINED},
61 {"enum e2", "union u3"}},
62 {{TypeFidelity::ABSENT, TypeFidelity::FULLY_DEFINED},
63 {"struct s3"}}},
64 };
65
66 std::ostringstream report;
67 CHECK(reporting::FidelityDiff(diff, report));
68
69 const std::ifstream expected_report_file(
70 filename_to_path("fidelity_diff_report"));
71 std::ostringstream expected_report;
72 expected_report << expected_report_file.rdbuf();
73 CHECK(report.str() == expected_report.str());
74 }
75
76 } // namespace
77 } // namespace stg
78