1fd4e5da5Sopenharmony_ci// Copyright (c) 2015-2016 The Khronos Group Inc.
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include <string>
16fd4e5da5Sopenharmony_ci#include <vector>
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include "gmock/gmock.h"
19fd4e5da5Sopenharmony_ci#include "source/instruction.h"
20fd4e5da5Sopenharmony_ci#include "source/util/string_utils.h"
21fd4e5da5Sopenharmony_ci#include "test/unit_spirv.h"
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_cinamespace spvtools {
24fd4e5da5Sopenharmony_cinamespace {
25fd4e5da5Sopenharmony_ci
26fd4e5da5Sopenharmony_ciusing spvtest::AutoText;
27fd4e5da5Sopenharmony_ciusing spvtest::Concatenate;
28fd4e5da5Sopenharmony_ciusing ::testing::Eq;
29fd4e5da5Sopenharmony_ci
30fd4e5da5Sopenharmony_cistruct EncodeStringCase {
31fd4e5da5Sopenharmony_ci  std::string str;
32fd4e5da5Sopenharmony_ci  std::vector<uint32_t> initial_contents;
33fd4e5da5Sopenharmony_ci};
34fd4e5da5Sopenharmony_ci
35fd4e5da5Sopenharmony_ciusing EncodeStringTest = ::testing::TestWithParam<EncodeStringCase>;
36fd4e5da5Sopenharmony_ci
37fd4e5da5Sopenharmony_ciTEST_P(EncodeStringTest, Sample) {
38fd4e5da5Sopenharmony_ci  AssemblyContext context(AutoText(""), nullptr);
39fd4e5da5Sopenharmony_ci  spv_instruction_t inst;
40fd4e5da5Sopenharmony_ci  inst.words = GetParam().initial_contents;
41fd4e5da5Sopenharmony_ci  ASSERT_EQ(SPV_SUCCESS,
42fd4e5da5Sopenharmony_ci            context.binaryEncodeString(GetParam().str.c_str(), &inst));
43fd4e5da5Sopenharmony_ci  // We already trust MakeVector
44fd4e5da5Sopenharmony_ci  EXPECT_THAT(inst.words, Eq(Concatenate({GetParam().initial_contents,
45fd4e5da5Sopenharmony_ci                                          utils::MakeVector(GetParam().str)})));
46fd4e5da5Sopenharmony_ci}
47fd4e5da5Sopenharmony_ci
48fd4e5da5Sopenharmony_ci// clang-format off
49fd4e5da5Sopenharmony_ciINSTANTIATE_TEST_SUITE_P(
50fd4e5da5Sopenharmony_ci    BinaryEncodeString, EncodeStringTest,
51fd4e5da5Sopenharmony_ci    ::testing::ValuesIn(std::vector<EncodeStringCase>{
52fd4e5da5Sopenharmony_ci      // Use cases that exercise at least one to two words,
53fd4e5da5Sopenharmony_ci      // and both empty and non-empty initial contents.
54fd4e5da5Sopenharmony_ci      {"", {}},
55fd4e5da5Sopenharmony_ci      {"", {1,2,3}},
56fd4e5da5Sopenharmony_ci      {"a", {}},
57fd4e5da5Sopenharmony_ci      {"a", {4}},
58fd4e5da5Sopenharmony_ci      {"ab", {4}},
59fd4e5da5Sopenharmony_ci      {"abc", {}},
60fd4e5da5Sopenharmony_ci      {"abc", {18}},
61fd4e5da5Sopenharmony_ci      {"abcd", {}},
62fd4e5da5Sopenharmony_ci      {"abcd", {22}},
63fd4e5da5Sopenharmony_ci      {"abcde", {4}},
64fd4e5da5Sopenharmony_ci      {"abcdef", {}},
65fd4e5da5Sopenharmony_ci      {"abcdef", {99,42}},
66fd4e5da5Sopenharmony_ci      {"abcdefg", {}},
67fd4e5da5Sopenharmony_ci      {"abcdefg", {101}},
68fd4e5da5Sopenharmony_ci      {"abcdefgh", {}},
69fd4e5da5Sopenharmony_ci      {"abcdefgh", {102, 103, 104}},
70fd4e5da5Sopenharmony_ci      // A very long string, encoded after an initial word.
71fd4e5da5Sopenharmony_ci      // SPIR-V limits strings to 65535 characters.
72fd4e5da5Sopenharmony_ci      {std::string(65535, 'a'), {1}},
73fd4e5da5Sopenharmony_ci    }));
74fd4e5da5Sopenharmony_ci// clang-format on
75fd4e5da5Sopenharmony_ci
76fd4e5da5Sopenharmony_ci}  // namespace
77fd4e5da5Sopenharmony_ci}  // namespace spvtools
78