| 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 |
|
| 15 |
#include <string> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include "gmock/gmock.h" |
| 19 |
#include "source/instruction.h" |
| 20 |
#include "source/util/string_utils.h" |
| 21 |
#include "test/unit_spirv.h" |
| 22 |
|
| 23 |
namespace spvtools { |
| 24 |
namespace { |
| 25 |
|
| 26 |
using spvtest::AutoText; |
| 27 |
using spvtest::Concatenate; |
| 28 |
using ::testing::Eq; |
| 29 |
|
| 30 |
struct EncodeStringCase { |
| 31 |
std::string str; |
| 32 |
std::vector<uint32_t> initial_contents; |
| 33 |
}; |
| 34 |
|
| 35 |
using EncodeStringTest = ::testing::TestWithParam<EncodeStringCase>; |
| 36 |
|
| 37 |
TEST_P(EncodeStringTest, Sample) { |
| 38 |
AssemblyContext context(AutoText(""), nullptr); |
| 39 |
spv_instruction_t inst; |
| 40 |
inst.words = GetParam().initial_contents; |
| 41 |
ASSERT_EQ(SPV_SUCCESS, |
| 42 |
context.binaryEncodeString(GetParam().str.c_str(), &inst)); |
| 43 |
// We already trust MakeVector |
| 44 |
EXPECT_THAT(inst.words, Eq(Concatenate({GetParam().initial_contents, |
| 45 |
utils::MakeVector(GetParam().str)}))); |
| 46 |
} |
| 47 |
|
| 48 |
// clang-format off |
| 49 |
INSTANTIATE_TEST_SUITE_P( |
| 50 |
BinaryEncodeString, EncodeStringTest, |
| 51 |
::testing::ValuesIn(std::vector<EncodeStringCase>{ |
| 52 |
// Use cases that exercise at least one to two words, |
| 53 |
// and both empty and non-empty initial contents. |
| 54 |
{"", {}}, |
| 55 |
{"", {1,2,3}}, |
| 56 |
{"a", {}}, |
| 57 |
{"a", {4}}, |
| 58 |
{"ab", {4}}, |
| 59 |
{"abc", {}}, |
| 60 |
{"abc", {18}}, |
| 61 |
{"abcd", {}}, |
| 62 |
{"abcd", {22}}, |
| 63 |
{"abcde", {4}}, |
| 64 |
{"abcdef", {}}, |
| 65 |
{"abcdef", {99,42}}, |
| 66 |
{"abcdefg", {}}, |
| 67 |
{"abcdefg", {101}}, |
| 68 |
{"abcdefgh", {}}, |
| 69 |
{"abcdefgh", {102, 103, 104}}, |
| 70 |
// A very long string, encoded after an initial word. |
| 71 |
// SPIR-V limits strings to 65535 characters. |
| 72 |
{std::string(65535, 'a'), {1}}, |
| 73 |
})); |
| 74 |
// clang-format on |
| 75 |
|
| 76 |
} // namespace |
| 77 |
} // namespace spvtools |