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 "test/unit_spirv.h"
21
22 namespace spvtools {
23 namespace {
24
25 using spvtest::AutoText;
26 using spvtest::Concatenate;
27 using ::testing::Eq;
28
29 struct EncodeStringCase {
30 std::string str;
31 std::vector<uint32_t> initial_contents;
32 };
33
34 using EncodeStringTest = ::testing::TestWithParam<EncodeStringCase>;
35
TEST_P(EncodeStringTest,Sample)36 TEST_P(EncodeStringTest, Sample) {
37 AssemblyContext context(AutoText(""), nullptr);
38 spv_instruction_t inst;
39 inst.words = GetParam().initial_contents;
40 ASSERT_EQ(SPV_SUCCESS,
41 context.binaryEncodeString(GetParam().str.c_str(), &inst));
42 // We already trust MakeVector
43 EXPECT_THAT(inst.words,
44 Eq(Concatenate({GetParam().initial_contents,
45 spvtest::MakeVector(GetParam().str)})));
46 }
47
48 // clang-format off
49 INSTANTIATE_TEST_CASE_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
78