diff_test_utils.cpp 1.9 KB

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