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 <utility>
17 #include <vector>
18
19 #include "gmock/gmock.h"
20 #include "test/test_fixture.h"
21 #include "test/unit_spirv.h"
22
23 namespace spvtools {
24 namespace {
25
26 using ::testing::Eq;
27 using RoundTripLiteralsTest =
28 spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
29
30 static const bool kSwapEndians[] = {false, true};
31
TEST_P(RoundTripLiteralsTest,Sample)32 TEST_P(RoundTripLiteralsTest, Sample) {
33 for (bool endian_swap : kSwapEndians) {
34 EXPECT_THAT(
35 EncodeAndDecodeSuccessfully(GetParam(), SPV_BINARY_TO_TEXT_OPTION_NONE,
36 SPV_TEXT_TO_BINARY_OPTION_NONE,
37 SPV_ENV_UNIVERSAL_1_0, endian_swap),
38 Eq(GetParam()));
39 }
40 }
41
42 // clang-format off
43 INSTANTIATE_TEST_SUITE_P(
44 StringLiterals, RoundTripLiteralsTest,
45 ::testing::ValuesIn(std::vector<std::string>{
46 "OpName %1 \"\"\n", // empty
47 "OpName %1 \"foo\"\n", // normal
48 "OpName %1 \"foo bar\"\n", // string with spaces
49 "OpName %1 \"foo\tbar\"\n", // string with tab
50 "OpName %1 \"\tfoo\"\n", // starts with tab
51 "OpName %1 \" foo\"\n", // starts with space
52 "OpName %1 \"foo \"\n", // ends with space
53 "OpName %1 \"foo\t\"\n", // ends with tab
54 "OpName %1 \"foo\nbar\"\n", // contains newline
55 "OpName %1 \"\nfoo\nbar\"\n", // starts with newline
56 "OpName %1 \"\n\n\nfoo\nbar\"\n", // multiple newlines
57 "OpName %1 \"\\\"foo\nbar\\\"\"\n", // escaped quote
58 "OpName %1 \"\\\\foo\nbar\\\\\"\n", // escaped backslash
59 "OpName %1 \"\xE4\xBA\xB2\"\n", // UTF-8
60 }));
61 // clang-format on
62
63 using RoundTripSpecialCaseLiteralsTest = spvtest::TextToBinaryTestBase<
64 ::testing::TestWithParam<std::pair<std::string, std::string>>>;
65
66 // Test case where the generated disassembly is not the same as the
67 // assembly passed in.
TEST_P(RoundTripSpecialCaseLiteralsTest,Sample)68 TEST_P(RoundTripSpecialCaseLiteralsTest, Sample) {
69 for (bool endian_swap : kSwapEndians) {
70 EXPECT_THAT(EncodeAndDecodeSuccessfully(std::get<0>(GetParam()),
71 SPV_BINARY_TO_TEXT_OPTION_NONE,
72 SPV_TEXT_TO_BINARY_OPTION_NONE,
73 SPV_ENV_UNIVERSAL_1_0, endian_swap),
74 Eq(std::get<1>(GetParam())));
75 }
76 }
77
78 // clang-format off
79 INSTANTIATE_TEST_SUITE_P(
80 StringLiterals, RoundTripSpecialCaseLiteralsTest,
81 ::testing::ValuesIn(std::vector<std::pair<std::string, std::string>>{
82 {"OpName %1 \"\\foo\"\n", "OpName %1 \"foo\"\n"}, // Escape f
83 {"OpName %1 \"\\\nfoo\"\n", "OpName %1 \"\nfoo\"\n"}, // Escape newline
84 {"OpName %1 \"\\\xE4\xBA\xB2\"\n", "OpName %1 \"\xE4\xBA\xB2\"\n"}, // Escape utf-8
85 }));
86 // clang-format on
87
88 } // namespace
89 } // namespace spvtools
90