2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-09-15 17:36:21 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-09-15 17:36:21 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-09-15 17:36:21 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-09-15 17:36:21 +00:00
|
|
|
|
|
|
|
// Assembler tests for literal numbers and literal strings.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "test/test_fixture.h"
|
2015-09-15 17:36:21 +00:00
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
namespace spvtools {
|
2015-09-15 17:36:21 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
using spvtest::TextToBinaryTest;
|
2015-09-15 17:36:21 +00:00
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, LiteralStringInPlaceOfLiteralNumber) {
|
|
|
|
EXPECT_EQ(
|
2015-10-05 17:28:46 +00:00
|
|
|
R"(Invalid unsigned integer literal: "I shouldn't be a string")",
|
2015-09-15 17:36:21 +00:00
|
|
|
CompileFailure(R"(OpSource GLSL "I shouldn't be a string")"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, GarbageInPlaceOfLiteralString) {
|
2015-10-16 14:05:46 +00:00
|
|
|
EXPECT_EQ("Invalid literal string 'nice-source-code'.",
|
|
|
|
CompileFailure("OpSourceExtension nice-source-code"));
|
2015-09-15 17:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, LiteralNumberInPlaceOfLiteralString) {
|
2015-10-16 14:05:46 +00:00
|
|
|
EXPECT_EQ("Expected literal string, found literal number '1000'.",
|
|
|
|
CompileFailure("OpSourceExtension 1000"));
|
2015-09-15 17:36:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 21:02:39 +00:00
|
|
|
TEST_F(TextToBinaryTest, LiteralFloatInPlaceOfLiteralInteger) {
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 10.5",
|
|
|
|
CompileFailure("OpSource GLSL 10.5"));
|
|
|
|
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 0.2",
|
|
|
|
CompileFailure(R"(OpMemberName %type 0.2 "member0.2")"));
|
|
|
|
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 32.42",
|
|
|
|
CompileFailure("%int = OpTypeInt 32.42 0"));
|
|
|
|
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 4.5",
|
|
|
|
CompileFailure("%mat = OpTypeMatrix %vec 4.5"));
|
|
|
|
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 1.5",
|
|
|
|
CompileFailure("OpExecutionMode %main LocalSize 1.5 1.6 1.7"));
|
|
|
|
|
|
|
|
EXPECT_EQ("Invalid unsigned integer literal: 0.123",
|
|
|
|
CompileFailure("%i32 = OpTypeInt 32 1\n"
|
|
|
|
"%c = OpConstant %i32 0.123"));
|
|
|
|
}
|
|
|
|
|
2015-11-12 15:45:36 +00:00
|
|
|
TEST_F(TextToBinaryTest, LiteralInt64) {
|
|
|
|
const std::string code =
|
|
|
|
"%1 = OpTypeInt 64 0\n%2 = OpConstant %1 123456789021\n";
|
|
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(TextToBinaryTest, LiteralDouble) {
|
|
|
|
const std::string code =
|
|
|
|
"%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 3.14159265358979\n";
|
|
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
|
|
|
}
|
|
|
|
|
2015-10-08 19:20:25 +00:00
|
|
|
TEST_F(TextToBinaryTest, LiteralStringASCIILong) {
|
2015-10-06 20:22:00 +00:00
|
|
|
// SPIR-V allows strings up to 65535 characters.
|
2015-10-08 19:20:25 +00:00
|
|
|
// Test the simple case of UTF-8 code points corresponding
|
|
|
|
// to ASCII characters.
|
|
|
|
EXPECT_EQ(65535, SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX);
|
2015-09-15 17:36:21 +00:00
|
|
|
const std::string code =
|
2015-10-08 19:20:25 +00:00
|
|
|
"OpSourceExtension \"" +
|
|
|
|
std::string(SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX, 'o') + "\"\n";
|
2015-09-17 22:24:53 +00:00
|
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
2015-09-15 17:36:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 19:20:25 +00:00
|
|
|
TEST_F(TextToBinaryTest, LiteralStringUTF8LongEncodings) {
|
|
|
|
// SPIR-V allows strings up to 65535 characters.
|
|
|
|
// Test the case of many Unicode characters, each of which has
|
|
|
|
// a 4-byte UTF-8 encoding.
|
|
|
|
|
|
|
|
// An instruction is at most 65535 words long. The first one
|
|
|
|
// contains the wordcount and opcode. So the worst case number of
|
|
|
|
// 4-byte UTF-8 characters is 65533, since we also need to
|
|
|
|
// store a terminating null character.
|
|
|
|
|
|
|
|
// This string fits exactly into 65534 words.
|
|
|
|
const std::string good_string =
|
|
|
|
spvtest::MakeLongUTF8String(65533)
|
|
|
|
// The following single character has a 3 byte encoding,
|
|
|
|
// which fits snugly against the terminating null.
|
2015-10-20 13:12:32 +00:00
|
|
|
+ "\xe8\x80\x80";
|
2015-10-08 19:20:25 +00:00
|
|
|
|
|
|
|
// These strings will overflow any instruction with 0 or 1 other
|
|
|
|
// arguments, respectively.
|
|
|
|
const std::string bad_0_arg_string = spvtest::MakeLongUTF8String(65534);
|
|
|
|
const std::string bad_1_arg_string = spvtest::MakeLongUTF8String(65533);
|
|
|
|
|
|
|
|
const std::string good_code = "OpSourceExtension \"" + good_string + "\"\n";
|
|
|
|
EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
|
|
|
|
|
|
|
|
// Prove that it works on more than one instruction.
|
|
|
|
const std::string good_code_2 = "OpSourceContinued \"" + good_string + "\"\n";
|
|
|
|
EXPECT_EQ(good_code, EncodeAndDecodeSuccessfully(good_code));
|
|
|
|
|
|
|
|
// Failure cases.
|
2015-10-16 14:05:46 +00:00
|
|
|
EXPECT_EQ("Instruction too long: more than 65535 words.",
|
|
|
|
CompileFailure("OpSourceExtension \"" + bad_0_arg_string + "\"\n"));
|
|
|
|
EXPECT_EQ("Instruction too long: more than 65535 words.",
|
|
|
|
CompileFailure("OpSourceContinued \"" + bad_0_arg_string + "\"\n"));
|
|
|
|
EXPECT_EQ("Instruction too long: more than 65535 words.",
|
|
|
|
CompileFailure("OpName %target \"" + bad_1_arg_string + "\"\n"));
|
2015-10-08 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|