assembly_context_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <vector>
  16. #include "gmock/gmock.h"
  17. #include "source/instruction.h"
  18. #include "test/unit_spirv.h"
  19. namespace spvtools {
  20. namespace {
  21. using spvtest::AutoText;
  22. using spvtest::Concatenate;
  23. using ::testing::Eq;
  24. struct EncodeStringCase {
  25. std::string str;
  26. std::vector<uint32_t> initial_contents;
  27. };
  28. using EncodeStringTest = ::testing::TestWithParam<EncodeStringCase>;
  29. TEST_P(EncodeStringTest, Sample) {
  30. AssemblyContext context(AutoText(""), nullptr);
  31. spv_instruction_t inst;
  32. inst.words = GetParam().initial_contents;
  33. ASSERT_EQ(SPV_SUCCESS,
  34. context.binaryEncodeString(GetParam().str.c_str(), &inst));
  35. // We already trust MakeVector
  36. EXPECT_THAT(inst.words,
  37. Eq(Concatenate({GetParam().initial_contents,
  38. spvtest::MakeVector(GetParam().str)})));
  39. }
  40. // clang-format off
  41. INSTANTIATE_TEST_SUITE_P(
  42. BinaryEncodeString, EncodeStringTest,
  43. ::testing::ValuesIn(std::vector<EncodeStringCase>{
  44. // Use cases that exercise at least one to two words,
  45. // and both empty and non-empty initial contents.
  46. {"", {}},
  47. {"", {1,2,3}},
  48. {"a", {}},
  49. {"a", {4}},
  50. {"ab", {4}},
  51. {"abc", {}},
  52. {"abc", {18}},
  53. {"abcd", {}},
  54. {"abcd", {22}},
  55. {"abcde", {4}},
  56. {"abcdef", {}},
  57. {"abcdef", {99,42}},
  58. {"abcdefg", {}},
  59. {"abcdefg", {101}},
  60. {"abcdefgh", {}},
  61. {"abcdefgh", {102, 103, 104}},
  62. // A very long string, encoded after an initial word.
  63. // SPIR-V limits strings to 65535 characters.
  64. {std::string(65535, 'a'), {1}},
  65. }));
  66. // clang-format on
  67. } // namespace
  68. } // namespace spvtools