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