global_values_amount_test.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright (c) 2017 Pierre Moreau
  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 "gmock/gmock.h"
  16. #include "test/link/linker_fixture.h"
  17. namespace spvtools {
  18. namespace {
  19. using ::testing::HasSubstr;
  20. class EntryPointsAmountTest : public spvtest::LinkerTest {
  21. public:
  22. EntryPointsAmountTest() { binaries.reserve(0xFFFF); }
  23. void SetUp() override {
  24. binaries.push_back({SpvMagicNumber,
  25. SpvVersion,
  26. SPV_GENERATOR_CODEPLAY,
  27. 10u, // NOTE: Bound
  28. 0u, // NOTE: Schema; reserved
  29. 3u << SpvWordCountShift | SpvOpTypeFloat,
  30. 1u, // NOTE: Result ID
  31. 32u, // NOTE: Width
  32. 4u << SpvWordCountShift | SpvOpTypePointer,
  33. 2u, // NOTE: Result ID
  34. SpvStorageClassInput,
  35. 1u, // NOTE: Type ID
  36. 2u << SpvWordCountShift | SpvOpTypeVoid,
  37. 3u, // NOTE: Result ID
  38. 3u << SpvWordCountShift | SpvOpTypeFunction,
  39. 4u, // NOTE: Result ID
  40. 3u, // NOTE: Return type
  41. 5u << SpvWordCountShift | SpvOpFunction,
  42. 3u, // NOTE: Result type
  43. 5u, // NOTE: Result ID
  44. SpvFunctionControlMaskNone,
  45. 4u, // NOTE: Function type
  46. 2u << SpvWordCountShift | SpvOpLabel,
  47. 6u, // NOTE: Result ID
  48. 4u << SpvWordCountShift | SpvOpVariable,
  49. 2u, // NOTE: Type ID
  50. 7u, // NOTE: Result ID
  51. SpvStorageClassFunction,
  52. 4u << SpvWordCountShift | SpvOpVariable,
  53. 2u, // NOTE: Type ID
  54. 8u, // NOTE: Result ID
  55. SpvStorageClassFunction,
  56. 4u << SpvWordCountShift | SpvOpVariable,
  57. 2u, // NOTE: Type ID
  58. 9u, // NOTE: Result ID
  59. SpvStorageClassFunction,
  60. 1u << SpvWordCountShift | SpvOpReturn,
  61. 1u << SpvWordCountShift | SpvOpFunctionEnd});
  62. for (size_t i = 0u; i < 2u; ++i) {
  63. spvtest::Binary binary = {
  64. SpvMagicNumber,
  65. SpvVersion,
  66. SPV_GENERATOR_CODEPLAY,
  67. 103u, // NOTE: Bound
  68. 0u, // NOTE: Schema; reserved
  69. 3u << SpvWordCountShift | SpvOpTypeFloat,
  70. 1u, // NOTE: Result ID
  71. 32u, // NOTE: Width
  72. 4u << SpvWordCountShift | SpvOpTypePointer,
  73. 2u, // NOTE: Result ID
  74. SpvStorageClassInput,
  75. 1u // NOTE: Type ID
  76. };
  77. for (uint32_t j = 0u; j < 0xFFFFu / 2u; ++j) {
  78. binary.push_back(4u << SpvWordCountShift | SpvOpVariable);
  79. binary.push_back(2u); // NOTE: Type ID
  80. binary.push_back(j + 3u); // NOTE: Result ID
  81. binary.push_back(SpvStorageClassInput);
  82. }
  83. binaries.push_back(binary);
  84. }
  85. }
  86. void TearDown() override { binaries.clear(); }
  87. spvtest::Binaries binaries;
  88. };
  89. TEST_F(EntryPointsAmountTest, UnderLimit) {
  90. spvtest::Binary linked_binary;
  91. EXPECT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary));
  92. EXPECT_THAT(GetErrorMessage(), std::string());
  93. }
  94. TEST_F(EntryPointsAmountTest, OverLimit) {
  95. binaries.push_back({SpvMagicNumber,
  96. SpvVersion,
  97. SPV_GENERATOR_CODEPLAY,
  98. 5u, // NOTE: Bound
  99. 0u, // NOTE: Schema; reserved
  100. 3u << SpvWordCountShift | SpvOpTypeFloat,
  101. 1u, // NOTE: Result ID
  102. 32u, // NOTE: Width
  103. 4u << SpvWordCountShift | SpvOpTypePointer,
  104. 2u, // NOTE: Result ID
  105. SpvStorageClassInput,
  106. 1u, // NOTE: Type ID
  107. 4u << SpvWordCountShift | SpvOpVariable,
  108. 2u, // NOTE: Type ID
  109. 3u, // NOTE: Result ID
  110. SpvStorageClassInput,
  111. 4u << SpvWordCountShift | SpvOpVariable,
  112. 2u, // NOTE: Type ID
  113. 4u, // NOTE: Result ID
  114. SpvStorageClassInput});
  115. spvtest::Binary linked_binary;
  116. EXPECT_EQ(SPV_ERROR_INTERNAL, Link(binaries, &linked_binary));
  117. EXPECT_THAT(GetErrorMessage(),
  118. HasSubstr("The limit of global values, 65535, was exceeded; "
  119. "65536 global values were found."));
  120. }
  121. } // namespace
  122. } // namespace spvtools