utils_test.cpp 3.9 KB

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