binary_header_get_test.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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] = SpvMagicNumber;
  23. code[1] = SpvVersion;
  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>(SpvMagicNumber), header.magic);
  45. ASSERT_EQ(0x00010300u, header.version);
  46. ASSERT_EQ(static_cast<uint32_t>(SPV_GENERATOR_CODEPLAY), header.generator);
  47. ASSERT_EQ(1u, header.bound);
  48. ASSERT_EQ(0u, header.schema);
  49. ASSERT_EQ(&code[5], header.instructions);
  50. }
  51. TEST_F(BinaryHeaderGet, InvalidCode) {
  52. spv_const_binary_t my_binary = {nullptr, 0};
  53. spv_header_t header;
  54. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  55. spvBinaryHeaderGet(&my_binary, SPV_ENDIANNESS_LITTLE, &header));
  56. }
  57. TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
  58. spv_const_binary_t const_bin = get_const_binary();
  59. ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
  60. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
  61. }
  62. TEST_F(BinaryHeaderGet, TruncatedHeader) {
  63. for (uint8_t i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
  64. binary.wordCount = i;
  65. spv_const_binary_t const_bin = get_const_binary();
  66. ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
  67. spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
  68. }
  69. }
  70. } // namespace
  71. } // namespace spvtools