binary_to_text.literal_test.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. static const bool kSwapEndians[] = {false, true};
  26. TEST_P(RoundTripLiteralsTest, Sample) {
  27. for (bool endian_swap : kSwapEndians) {
  28. EXPECT_THAT(
  29. EncodeAndDecodeSuccessfully(GetParam(), SPV_BINARY_TO_TEXT_OPTION_NONE,
  30. SPV_TEXT_TO_BINARY_OPTION_NONE,
  31. SPV_ENV_UNIVERSAL_1_0, endian_swap),
  32. Eq(GetParam()));
  33. }
  34. }
  35. // clang-format off
  36. INSTANTIATE_TEST_SUITE_P(
  37. StringLiterals, RoundTripLiteralsTest,
  38. ::testing::ValuesIn(std::vector<std::string>{
  39. "OpName %1 \"\"\n", // empty
  40. "OpName %1 \"foo\"\n", // normal
  41. "OpName %1 \"foo bar\"\n", // string with spaces
  42. "OpName %1 \"foo\tbar\"\n", // string with tab
  43. "OpName %1 \"\tfoo\"\n", // starts with tab
  44. "OpName %1 \" foo\"\n", // starts with space
  45. "OpName %1 \"foo \"\n", // ends with space
  46. "OpName %1 \"foo\t\"\n", // ends with tab
  47. "OpName %1 \"foo\nbar\"\n", // contains newline
  48. "OpName %1 \"\nfoo\nbar\"\n", // starts with newline
  49. "OpName %1 \"\n\n\nfoo\nbar\"\n", // multiple newlines
  50. "OpName %1 \"\\\"foo\nbar\\\"\"\n", // escaped quote
  51. "OpName %1 \"\\\\foo\nbar\\\\\"\n", // escaped backslash
  52. "OpName %1 \"\xE4\xBA\xB2\"\n", // UTF-8
  53. }));
  54. // clang-format on
  55. using RoundTripSpecialCaseLiteralsTest = spvtest::TextToBinaryTestBase<
  56. ::testing::TestWithParam<std::pair<std::string, std::string>>>;
  57. // Test case where the generated disassembly is not the same as the
  58. // assembly passed in.
  59. TEST_P(RoundTripSpecialCaseLiteralsTest, Sample) {
  60. for (bool endian_swap : kSwapEndians) {
  61. EXPECT_THAT(EncodeAndDecodeSuccessfully(std::get<0>(GetParam()),
  62. SPV_BINARY_TO_TEXT_OPTION_NONE,
  63. SPV_TEXT_TO_BINARY_OPTION_NONE,
  64. SPV_ENV_UNIVERSAL_1_0, endian_swap),
  65. Eq(std::get<1>(GetParam())));
  66. }
  67. }
  68. // clang-format off
  69. INSTANTIATE_TEST_SUITE_P(
  70. StringLiterals, RoundTripSpecialCaseLiteralsTest,
  71. ::testing::ValuesIn(std::vector<std::pair<std::string, std::string>>{
  72. {"OpName %1 \"\\foo\"\n", "OpName %1 \"foo\"\n"}, // Escape f
  73. {"OpName %1 \"\\\nfoo\"\n", "OpName %1 \"\nfoo\"\n"}, // Escape newline
  74. {"OpName %1 \"\\\xE4\xBA\xB2\"\n", "OpName %1 \"\xE4\xBA\xB2\"\n"}, // Escape utf-8
  75. }));
  76. // clang-format on
  77. } // namespace
  78. } // namespace spvtools