assembly_context_test.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 "source/util/string_utils.h"
  19. #include "test/unit_spirv.h"
  20. namespace spvtools {
  21. namespace {
  22. using spvtest::AutoText;
  23. using spvtest::Concatenate;
  24. using ::testing::Eq;
  25. struct EncodeStringCase {
  26. std::string str;
  27. std::vector<uint32_t> initial_contents;
  28. };
  29. using EncodeStringTest = ::testing::TestWithParam<EncodeStringCase>;
  30. TEST_P(EncodeStringTest, Sample) {
  31. AssemblyContext context(AutoText(""), nullptr);
  32. spv_instruction_t inst;
  33. inst.words = GetParam().initial_contents;
  34. ASSERT_EQ(SPV_SUCCESS,
  35. context.binaryEncodeString(GetParam().str.c_str(), &inst));
  36. // We already trust MakeVector
  37. EXPECT_THAT(inst.words, Eq(Concatenate({GetParam().initial_contents,
  38. utils::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