• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 Google 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 <gtest/gtest.h>
16 
17 #include "pass_utils.h"
18 
19 namespace {
20 
21 using namespace spvtools;
22 
TEST(JoinAllInsts,Cases)23 TEST(JoinAllInsts, Cases) {
24   EXPECT_EQ("", JoinAllInsts({}));
25   EXPECT_EQ("a\n", JoinAllInsts({"a"}));
26   EXPECT_EQ("a\nb\n", JoinAllInsts({"a", "b"}));
27   EXPECT_EQ("a\nb\nc\n", JoinAllInsts({"a", "b", "c"}));
28   EXPECT_EQ("hello,\nworld!\n\n\n", JoinAllInsts({"hello,", "world!", "\n"}));
29 }
30 
TEST(JoinNonDebugInsts,Cases)31 TEST(JoinNonDebugInsts, Cases) {
32   EXPECT_EQ("", JoinNonDebugInsts({}));
33   EXPECT_EQ("a\n", JoinNonDebugInsts({"a"}));
34   EXPECT_EQ("", JoinNonDebugInsts({"OpName"}));
35   EXPECT_EQ("a\nb\n", JoinNonDebugInsts({"a", "b"}));
36   EXPECT_EQ("", JoinNonDebugInsts({"OpName", "%1 = OpString \"42\""}));
37   EXPECT_EQ("Opstring\n", JoinNonDebugInsts({"OpName", "Opstring"}));
38   EXPECT_EQ("the only remaining string\n",
39             JoinNonDebugInsts(
40                 {"OpSourceContinued", "OpSource", "OpSourceExtension",
41                  "lgtm OpName", "hello OpMemberName", "this is a OpString",
42                  "lonely OpLine", "happy OpNoLine", "OpModuleProcessed",
43                  "the only remaining string"}));
44 }
45 
46 namespace {
47 struct SubstringReplacementTestCase {
48   const char* orig_str;
49   const char* find_substr;
50   const char* replace_substr;
51   const char* expected_str;
52   bool replace_should_succeed;
53 };
54 }
55 using FindAndReplaceTest =
56     ::testing::TestWithParam<SubstringReplacementTestCase>;
57 
TEST_P(FindAndReplaceTest,SubstringReplacement)58 TEST_P(FindAndReplaceTest, SubstringReplacement) {
59   auto process = std::string(GetParam().orig_str);
60   EXPECT_EQ(GetParam().replace_should_succeed,
61             FindAndReplace(&process, GetParam().find_substr,
62                            GetParam().replace_substr))
63       << "Original string: " << GetParam().orig_str
64       << " replace: " << GetParam().find_substr
65       << " to: " << GetParam().replace_substr
66       << " should returns: " << GetParam().replace_should_succeed;
67   EXPECT_STREQ(GetParam().expected_str, process.c_str())
68       << "Original string: " << GetParam().orig_str
69       << " replace: " << GetParam().find_substr
70       << " to: " << GetParam().replace_substr
71       << " expected string: " << GetParam().expected_str;
72 }
73 
74 INSTANTIATE_TEST_CASE_P(
75     SubstringReplacement, FindAndReplaceTest,
76     ::testing::ValuesIn(std::vector<SubstringReplacementTestCase>({
77         // orig string, find substring, replace substring, expected string,
78         // replacement happened
79         {"", "", "", "", false},
80         {"", "b", "", "", false},
81         {"", "", "c", "", false},
82         {"", "a", "b", "", false},
83 
84         {"a", "", "c", "a", false},
85         {"a", "b", "c", "a", false},
86         {"a", "b", "", "a", false},
87         {"a", "a", "", "", true},
88         {"a", "a", "b", "b", true},
89 
90         {"ab", "a", "b", "bb", true},
91         {"ab", "a", "", "b", true},
92         {"ab", "b", "", "a", true},
93         {"ab", "ab", "", "", true},
94         {"ab", "ab", "cd", "cd", true},
95         {"bc", "abc", "efg", "bc", false},
96 
97         {"abc", "ab", "bc", "bcc", true},
98         {"abc", "ab", "", "c", true},
99         {"abc", "bc", "", "a", true},
100         {"abc", "bc", "d", "ad", true},
101         {"abc", "a", "123", "123bc", true},
102         {"abc", "ab", "a", "ac", true},
103         {"abc", "a", "aab", "aabbc", true},
104         {"abc", "abcd", "efg", "abc", false},
105     })));
106 }  // anonymous namespace
107