named_id_test.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "test/test_fixture.h"
  17. #include "test/unit_spirv.h"
  18. namespace spvtools {
  19. namespace {
  20. using NamedIdTest = spvtest::TextToBinaryTest;
  21. TEST_F(NamedIdTest, Default) {
  22. const std::string input = R"(
  23. OpCapability Shader
  24. OpMemoryModel Logical Simple
  25. OpEntryPoint Vertex %main "foo"
  26. %void = OpTypeVoid
  27. %fnMain = OpTypeFunction %void
  28. %main = OpFunction %void None %fnMain
  29. %lbMain = OpLabel
  30. OpReturn
  31. OpFunctionEnd)";
  32. const std::string output =
  33. "OpCapability Shader\n"
  34. "OpMemoryModel Logical Simple\n"
  35. "OpEntryPoint Vertex %1 \"foo\"\n"
  36. "%2 = OpTypeVoid\n"
  37. "%3 = OpTypeFunction %2\n"
  38. "%1 = OpFunction %2 None %3\n"
  39. "%4 = OpLabel\n"
  40. "OpReturn\n"
  41. "OpFunctionEnd\n";
  42. EXPECT_EQ(output, EncodeAndDecodeSuccessfully(input));
  43. }
  44. struct IdCheckCase {
  45. std::string id;
  46. bool valid;
  47. };
  48. using IdValidityTest =
  49. spvtest::TextToBinaryTestBase<::testing::TestWithParam<IdCheckCase>>;
  50. TEST_P(IdValidityTest, IdTypes) {
  51. const std::string input = GetParam().id + " = OpTypeVoid";
  52. SetText(input);
  53. if (GetParam().valid) {
  54. CompileSuccessfully(input);
  55. } else {
  56. CompileFailure(input);
  57. }
  58. }
  59. INSTANTIATE_TEST_SUITE_P(
  60. ValidAndInvalidIds, IdValidityTest,
  61. ::testing::ValuesIn(std::vector<IdCheckCase>(
  62. {{"%1", true}, {"%2abc", true}, {"%3Def", true},
  63. {"%4GHI", true}, {"%5_j_k", true}, {"%6J_M", true},
  64. {"%n", true}, {"%O", true}, {"%p7", true},
  65. {"%Q8", true}, {"%R_S", true}, {"%T_10_U", true},
  66. {"%V_11", true}, {"%W_X_13", true}, {"%_A", true},
  67. {"%_", true}, {"%__", true}, {"%A_", true},
  68. {"%_A_", true},
  69. {"%@", false}, {"%!", false}, {"%ABC!", false},
  70. {"%__A__@", false}, {"%%", false}, {"%-", false},
  71. {"%foo_@_bar", false}, {"%", false},
  72. {"5", false}, {"32", false}, {"foo", false},
  73. {"a%bar", false}})));
  74. } // namespace
  75. } // namespace spvtools