text_to_binary.literal_test.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Assembler tests for literal numbers and literal strings.
  15. #include <string>
  16. #include "test/test_fixture.h"
  17. namespace spvtools {
  18. namespace {
  19. using spvtest::TextToBinaryTest;
  20. TEST_F(TextToBinaryTest, LiteralStringInPlaceOfLiteralNumber) {
  21. EXPECT_EQ(
  22. R"(Invalid unsigned integer literal: "I shouldn't be a string")",
  23. CompileFailure(R"(OpSource GLSL "I shouldn't be a string")"));
  24. }
  25. TEST_F(TextToBinaryTest, GarbageInPlaceOfLiteralString) {
  26. EXPECT_EQ("Invalid literal string 'nice-source-code'.",
  27. CompileFailure("OpSourceExtension nice-source-code"));
  28. }
  29. TEST_F(TextToBinaryTest, LiteralNumberInPlaceOfLiteralString) {
  30. EXPECT_EQ("Expected literal string, found literal number '1000'.",
  31. CompileFailure("OpSourceExtension 1000"));
  32. }
  33. TEST_F(TextToBinaryTest, LiteralFloatInPlaceOfLiteralInteger) {
  34. EXPECT_EQ("Invalid unsigned integer literal: 10.5",
  35. CompileFailure("OpSource GLSL 10.5"));
  36. EXPECT_EQ("Invalid unsigned integer literal: 0.2",
  37. CompileFailure(R"(OpMemberName %type 0.2 "member0.2")"));
  38. EXPECT_EQ("Invalid unsigned integer literal: 32.42",
  39. CompileFailure("%int = OpTypeInt 32.42 0"));
  40. EXPECT_EQ("Invalid unsigned integer literal: 4.5",
  41. CompileFailure("%mat = OpTypeMatrix %vec 4.5"));
  42. EXPECT_EQ("Invalid unsigned integer literal: 1.5",
  43. CompileFailure("OpExecutionMode %main LocalSize 1.5 1.6 1.7"));
  44. EXPECT_EQ("Invalid unsigned integer literal: 0.123",
  45. CompileFailure("%i32 = OpTypeInt 32 1\n"
  46. "%c = OpConstant %i32 0.123"));
  47. }
  48. TEST_F(TextToBinaryTest, LiteralInt64) {
  49. const std::string code =
  50. "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n";
  51. EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
  52. }
  53. TEST_F(TextToBinaryTest, LiteralDouble) {
  54. const std::string code =
  55. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.14159265358979\n";
  56. EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
  57. }
  58. TEST_F(TextToBinaryTest, LiteralStringASCIILong) {
  59. // SPIR-V allows strings up to 65535 characters.
  60. // Test the simple case of UTF-8 code points corresponding
  61. // to ASCII characters.
  62. EXPECT_EQ(65535, SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX);
  63. const std::string code =
  64. "OpSourceExtension \"" +
  65. std::string(SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX, 'o') + "\"\n";
  66. EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
  67. }
  68. TEST_F(TextToBinaryTest, LiteralStringUTF8LongEncodings) {
  69. // SPIR-V allows strings up to 65535 characters.
  70. // Test the case of many Unicode characters, each of which has
  71. // a 4-byte UTF-8 encoding.
  72. // An instruction is at most 65535 words long. The first one
  73. // contains the wordcount and opcode. So the worst case number of
  74. // 4-byte UTF-8 characters is 65533, since we also need to
  75. // store a terminating null character.
  76. // This string fits exactly into 65534 words.
  77. const std::string good_string =
  78. spvtest::MakeLongUTF8String(65533)
  79. // The following single character has a 3 byte encoding,
  80. // which fits snugly against the terminating null.
  81. + "\xe8\x80\x80";
  82. // These strings will overflow any instruction with 0 or 1 other
  83. // arguments, respectively.
  84. const std::string bad_0_arg_string = spvtest::MakeLongUTF8String(65534);
  85. const std::string bad_1_arg_string = spvtest::MakeLongUTF8String(65533);
  86. const std::string good_code = "OpSourceExtension \"" + good_string + "\"\n";
  87. EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
  88. // Prove that it works on more than one instruction.
  89. const std::string good_code_2 = "OpSourceContinued \"" + good_string + "\"\n";
  90. EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
  91. // Failure cases.
  92. EXPECT_EQ("Instruction too long: more than 65535 words.",
  93. CompileFailure("OpSourceExtension \"" + bad_0_arg_string + "\"\n"));
  94. EXPECT_EQ("Instruction too long: more than 65535 words.",
  95. CompileFailure("OpSourceContinued \"" + bad_0_arg_string + "\"\n"));
  96. EXPECT_EQ("Instruction too long: more than 65535 words.",
  97. CompileFailure("OpName %target \"" + bad_1_arg_string + "\"\n"));
  98. }
  99. } // namespace
  100. } // namespace spvtools