1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
// Assembler tests for instructions in the "Debug" section of the
// SPIR-V spec.
#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "source/util/string_utils.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
namespace spvtools {
namespace {
using spvtest::MakeInstruction;
using utils::MakeVector;
using spvtest::TextToBinaryTest;
using ::testing::Eq;
// Test OpSource
// A single test case for OpSource
struct LanguageCase {
uint32_t get_language_value() const {
return static_cast<uint32_t>(language_value);
}
const char* language_name;
spv::SourceLanguage language_value;
uint32_t version;
};
// clang-format off
// The list of OpSource cases to use.
const LanguageCase kLanguageCases[] = {
#define CASE(NAME, VERSION) \
{ #NAME, spv::SourceLanguage::NAME, VERSION }
CASE(Unknown, 0),
CASE(Unknown, 999),
CASE(ESSL, 310),
CASE(GLSL, 450),
CASE(OpenCL_C, 120),
CASE(OpenCL_C, 200),
CASE(OpenCL_C, 210),
CASE(OpenCL_CPP, 210),
CASE(HLSL, 5),
CASE(HLSL, 6),
#undef CASE
};
// clang-format on
using OpSourceTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<LanguageCase>>;
TEST_P(OpSourceTest, AnyLanguage) {
const std::string input = std::string("OpSource ") +
GetParam().language_name + " " +
std::to_string(GetParam().version);
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource, {GetParam().get_language_value(),
GetParam().version})));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceTest,
::testing::ValuesIn(kLanguageCases));
TEST_F(OpSourceTest, WrongLanguage) {
EXPECT_THAT(CompileFailure("OpSource xxyyzz 12345"),
Eq("Invalid source language 'xxyyzz'."));
}
TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalFileId) {
// In the grammar, the file id is an OperandOptionalId.
const std::string input = "OpSource GLSL 450 %file_id";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource,
{uint32_t(spv::SourceLanguage::GLSL), 450, 1})));
}
TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalSourceText) {
std::string fake_source = "To be or not to be";
const std::string input =
"OpSource GLSL 450 %file_id \"" + fake_source + "\"";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource,
{uint32_t(spv::SourceLanguage::GLSL), 450, 1},
MakeVector(fake_source))));
}
// Test OpSourceContinued
using OpSourceContinuedTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpSourceContinuedTest, AnyExtension) {
// TODO(dneto): utf-8, quoting, escaping
const std::string input =
std::string("OpSourceContinued \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSourceContinued, MakeVector(GetParam()))));
}
// TODO(dneto): utf-8, quoting, escaping
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceContinuedTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
// Test OpSourceExtension
using OpSourceExtensionTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpSourceExtensionTest, AnyExtension) {
// TODO(dneto): utf-8, quoting, escaping
const std::string input =
std::string("OpSourceExtension \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSourceExtension, MakeVector(GetParam()))));
}
// TODO(dneto): utf-8, quoting, escaping
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceExtensionTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
TEST_F(TextToBinaryTest, OpLine) {
EXPECT_THAT(CompiledInstructions("OpLine %srcfile 42 99"),
Eq(MakeInstruction(spv::Op::OpLine, {1, 42, 99})));
}
TEST_F(TextToBinaryTest, OpNoLine) {
EXPECT_THAT(CompiledInstructions("OpNoLine"),
Eq(MakeInstruction(spv::Op::OpNoLine, {})));
}
using OpStringTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpStringTest, AnyString) {
// TODO(dneto): utf-8, quoting, escaping
const std::string input =
std::string("%result = OpString \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpString, {1}, MakeVector(GetParam()))));
}
// TODO(dneto): utf-8, quoting, escaping
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpStringTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
using OpNameTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpNameTest, AnyString) {
const std::string input =
std::string("OpName %target \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpName, {1}, MakeVector(GetParam()))));
}
// UTF-8, quoting, escaping, etc. are covered in the StringLiterals tests in
// BinaryToText.Literal.cpp.
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpNameTest,
::testing::Values("", "foo bar this and that"));
using OpMemberNameTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpMemberNameTest, AnyString) {
// TODO(dneto): utf-8, quoting, escaping
const std::string input =
std::string("OpMemberName %type 42 \"") + GetParam() + "\"";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpMemberName, {1, 42},
MakeVector(GetParam()))));
}
// TODO(dneto): utf-8, quoting, escaping
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpMemberNameTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
// TODO(dneto): Parse failures?
using OpModuleProcessedTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpModuleProcessedTest, AnyString) {
const std::string input =
std::string("OpModuleProcessed \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input, SPV_ENV_UNIVERSAL_1_1),
Eq(MakeInstruction(spv::Op::OpModuleProcessed, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpModuleProcessedTest,
::testing::Values("", "foo bar this and that"));
} // namespace
} // namespace spvtools
|