mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2024-12-26 17:51:02 +00:00
106 lines
4.3 KiB
C++
106 lines
4.3 KiB
C++
// Copyright (c) 2015 The Khronos Group Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
// copy of this software and/or associated documentation files (the
|
|
// "Materials"), to deal in the Materials without restriction, including
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
// distribute, sublicense, and/or sell copies of the Materials, and to
|
|
// permit persons to whom the Materials are furnished to do so, subject to
|
|
// the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included
|
|
// in all copies or substantial portions of the Materials.
|
|
//
|
|
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
|
|
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
|
|
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
|
|
// https://www.khronos.org/registry/
|
|
//
|
|
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
|
|
|
|
// Assembler tests for literal numbers and literal strings.
|
|
|
|
#include "TestFixture.h"
|
|
|
|
namespace {
|
|
|
|
using spvtest::TextToBinaryTest;
|
|
|
|
TEST_F(TextToBinaryTest, LiteralStringInPlaceOfLiteralNumber) {
|
|
EXPECT_EQ(
|
|
R"(Expected literal number, found literal string '"I shouldn't be a string"'.)",
|
|
CompileFailure(R"(OpSource GLSL "I shouldn't be a string")"));
|
|
}
|
|
|
|
TEST_F(TextToBinaryTest, GarbageInPlaceOfLiteralString) {
|
|
EXPECT_EQ(
|
|
R"(Invalid literal string 'nice-source-code'.)",
|
|
CompileFailure(R"(OpSourceExtension nice-source-code)"));
|
|
}
|
|
|
|
TEST_F(TextToBinaryTest, LiteralNumberInPlaceOfLiteralString) {
|
|
EXPECT_EQ(
|
|
R"(Expected literal string, found literal number '1000'.)",
|
|
CompileFailure(R"(OpSourceExtension 1000)"));
|
|
}
|
|
|
|
TEST_F(TextToBinaryTest, LiteralStringASCIILong) {
|
|
// SPIR-V allows strings up to 65535 characters.
|
|
// Test the simple case of UTF-8 code points corresponding
|
|
// to ASCII characters.
|
|
EXPECT_EQ(65535, SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX);
|
|
const std::string code =
|
|
"OpSourceExtension \"" +
|
|
std::string(SPV_LIMIT_LITERAL_STRING_UTF8_CHARS_MAX, 'o') + "\"\n";
|
|
EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
|
|
}
|
|
|
|
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.
|
|
+ "\u8000";
|
|
|
|
// 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.
|
|
EXPECT_EQ(
|
|
R"(Instruction too long: more than 65535 words.)",
|
|
CompileFailure("OpSourceExtension \"" + bad_0_arg_string + "\"\n"));
|
|
EXPECT_EQ(
|
|
R"(Instruction too long: more than 65535 words.)",
|
|
CompileFailure("OpSourceContinued \"" + bad_0_arg_string + "\"\n"));
|
|
EXPECT_EQ(
|
|
R"(Instruction too long: more than 65535 words.)",
|
|
CompileFailure("OpName %target \"" + bad_1_arg_string + "\"\n"));
|
|
}
|
|
|
|
} // anonymous namespace
|