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