binary_header_get_test.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "source/spirv_constant.h"
  15. #include "test/unit_spirv.h"
  16. namespace spvtools {
  17. namespace {
  18. class BinaryHeaderGet : public ::testing::Test {
  19. public:
  20. BinaryHeaderGet() { memset(code, 0, sizeof(code)); }
  21. virtual void SetUp() {
  22. code[0] = static_cast<uint32_t>(spv::MagicNumber);
  23. code[1] = static_cast<uint32_t>(spv::Version);
  24. code[2] = SPV_GENERATOR_CODEPLAY;
  25. code[3] = 1; // NOTE: Bound
  26. code[4] = 0; // NOTE: Schema; reserved
  27. code[5] = 0; // NOTE: Instructions
  28. binary.code = code;
  29. binary.wordCount = 6;
  30. }
  31. spv_const_binary_t get_const_binary() {
  32. return spv_const_binary_t{binary.code, binary.wordCount};
  33. }
  34. virtual void TearDown() {}
  35. uint32_t code[6];
  36. spv_binary_t binary;
  37. };
  38. TEST_F(BinaryHeaderGet, Default) {
  39. spv_endianness_t endian;
  40. spv_const_binary_t const_bin = get_const_binary();
  41. ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
  42. spv_header_t header;
  43. ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
  44. ASSERT_EQ(static_cast<uint32_t>(spv::MagicNumber), header.magic);
  45. // Expect SPIRV-Headers updated to SPIR-V 1.6.
  46. ASSERT_EQ(0x00010600u, header.version);
  47. ASSERT_EQ(static_cast<uint32_t>(SPV_GENERATOR_CODEPLAY), header.generator);
  48. ASSERT_EQ(1u, header.bound);
  49. ASSERT_EQ(0u, header.schema);
  50. ASSERT_EQ(&code[5], header.instructions);
  51. }
  52. TEST_F(BinaryHeaderGet, InvalidCode) {
  53. spv_const_binary_t my_binary = {nullptr, 0};
  54. spv_header_t header;
  55. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  56. spvBinaryHeaderGet(&my_binary, SPV_ENDIANNESS_LITTLE, &header));
  57. }
  58. TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
  59. spv_const_binary_t const_bin = get_const_binary();
  60. ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
  61. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
  62. }
  63. TEST_F(BinaryHeaderGet, TruncatedHeader) {
  64. for (uint8_t i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
  65. binary.wordCount = i;
  66. spv_const_binary_t const_bin = get_const_binary();
  67. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  68. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
  69. }
  70. }
  71. TEST_F(BinaryHeaderGet, VersionNonZeroHighByte) {
  72. spv_header_t header;
  73. code[1] = 0xFF010300;
  74. spv_const_binary_t const_bin = get_const_binary();
  75. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  76. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
  77. }
  78. TEST_F(BinaryHeaderGet, VersionNonZeroLowByte) {
  79. spv_header_t header;
  80. code[1] = 0x000103F0;
  81. spv_const_binary_t const_bin = get_const_binary();
  82. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  83. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
  84. }
  85. TEST_F(BinaryHeaderGet, VersionTooLow) {
  86. spv_header_t header;
  87. code[1] = 0x00000300;
  88. spv_const_binary_t const_bin = get_const_binary();
  89. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  90. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
  91. }
  92. TEST_F(BinaryHeaderGet, VersionTooHigh) {
  93. spv_header_t header;
  94. code[1] = 0x000F0300;
  95. spv_const_binary_t const_bin = get_const_binary();
  96. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  97. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
  98. }
  99. } // namespace
  100. } // namespace spvtools