binary_to_text.literal_test.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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 <string>
  15. #include <utility>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "test/test_fixture.h"
  19. #include "test/unit_spirv.h"
  20. namespace spvtools {
  21. namespace {
  22. using ::testing::Eq;
  23. using RoundTripLiteralsTest =
  24. spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
  25. TEST_P(RoundTripLiteralsTest, Sample) {
  26. EXPECT_THAT(EncodeAndDecodeSuccessfully(GetParam()), Eq(GetParam()));
  27. }
  28. // clang-format off
  29. INSTANTIATE_TEST_SUITE_P(
  30. StringLiterals, RoundTripLiteralsTest,
  31. ::testing::ValuesIn(std::vector<std::string>{
  32. "OpName %1 \"\"\n", // empty
  33. "OpName %1 \"foo\"\n", // normal
  34. "OpName %1 \"foo bar\"\n", // string with spaces
  35. "OpName %1 \"foo\tbar\"\n", // string with tab
  36. "OpName %1 \"\tfoo\"\n", // starts with tab
  37. "OpName %1 \" foo\"\n", // starts with space
  38. "OpName %1 \"foo \"\n", // ends with space
  39. "OpName %1 \"foo\t\"\n", // ends with tab
  40. "OpName %1 \"foo\nbar\"\n", // contains newline
  41. "OpName %1 \"\nfoo\nbar\"\n", // starts with newline
  42. "OpName %1 \"\n\n\nfoo\nbar\"\n", // multiple newlines
  43. "OpName %1 \"\\\"foo\nbar\\\"\"\n", // escaped quote
  44. "OpName %1 \"\\\\foo\nbar\\\\\"\n", // escaped backslash
  45. "OpName %1 \"\xE4\xBA\xB2\"\n", // UTF-8
  46. }));
  47. // clang-format on
  48. using RoundTripSpecialCaseLiteralsTest = spvtest::TextToBinaryTestBase<
  49. ::testing::TestWithParam<std::pair<std::string, std::string>>>;
  50. // Test case where the generated disassembly is not the same as the
  51. // assembly passed in.
  52. TEST_P(RoundTripSpecialCaseLiteralsTest, Sample) {
  53. EXPECT_THAT(EncodeAndDecodeSuccessfully(std::get<0>(GetParam())),
  54. Eq(std::get<1>(GetParam())));
  55. }
  56. // clang-format off
  57. INSTANTIATE_TEST_SUITE_P(
  58. StringLiterals, RoundTripSpecialCaseLiteralsTest,
  59. ::testing::ValuesIn(std::vector<std::pair<std::string, std::string>>{
  60. {"OpName %1 \"\\foo\"\n", "OpName %1 \"foo\"\n"}, // Escape f
  61. {"OpName %1 \"\\\nfoo\"\n", "OpName %1 \"\nfoo\"\n"}, // Escape newline
  62. {"OpName %1 \"\\\xE4\xBA\xB2\"\n", "OpName %1 \"\xE4\xBA\xB2\"\n"}, // Escape utf-8
  63. }));
  64. // clang-format on
  65. } // namespace
  66. } // namespace spvtools