diff_test_utils.cpp 2.0 KB

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