1 // Copyright (c) 2022 Google LLC.
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 "diff_test_utils.h"
16
17 #include "source/opt/build_module.h"
18 #include "source/opt/ir_context.h"
19
20 #include "spirv-tools/libspirv.hpp"
21 #include "tools/util/cli_consumer.h"
22
23 #include "gtest/gtest.h"
24
25 namespace spvtools {
26 namespace diff {
27
28 static constexpr auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_6;
29
DoStringDiffTest(const std::string & src_spirv,const std::string & dst_spirv,const std::string & expected_diff,Options options)30 void DoStringDiffTest(const std::string& src_spirv,
31 const std::string& dst_spirv,
32 const std::string& expected_diff, Options options) {
33 // Load the src and dst modules
34 std::unique_ptr<spvtools::opt::IRContext> src = spvtools::BuildModule(
35 kDefaultEnvironment, spvtools::utils::CLIMessageConsumer, src_spirv,
36 spvtools::SpirvTools::kDefaultAssembleOption |
37 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
38 ASSERT_TRUE(src);
39
40 std::unique_ptr<spvtools::opt::IRContext> dst = spvtools::BuildModule(
41 kDefaultEnvironment, spvtools::utils::CLIMessageConsumer, dst_spirv,
42 spvtools::SpirvTools::kDefaultAssembleOption |
43 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
44 ASSERT_TRUE(dst);
45
46 // Take the diff
47 std::ostringstream diff_result;
48 spv_result_t result =
49 spvtools::diff::Diff(src.get(), dst.get(), diff_result, options);
50 ASSERT_EQ(result, SPV_SUCCESS);
51
52 // Expect they match
53 EXPECT_EQ(diff_result.str(), expected_diff);
54 }
55
56 } // namespace diff
57 } // namespace spvtools
58