• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RegEx.h"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace simpleperf;
22 
TEST(RegEx,smoke)23 TEST(RegEx, smoke) {
24   auto re = RegEx::Create("b+");
25   ASSERT_EQ(re->GetPattern(), "b+");
26   ASSERT_FALSE(re->Search("aaa"));
27   ASSERT_TRUE(re->Search("aba"));
28   ASSERT_FALSE(re->Match("aba"));
29   ASSERT_TRUE(re->Match("bbb"));
30 
31   auto match = re->SearchAll("aaa");
32   ASSERT_FALSE(match->IsValid());
33   match = re->SearchAll("ababb");
34   ASSERT_TRUE(match->IsValid());
35   ASSERT_EQ(match->GetField(0), "b");
36   match->MoveToNextMatch();
37   ASSERT_TRUE(match->IsValid());
38   ASSERT_EQ(match->GetField(0), "bb");
39   match->MoveToNextMatch();
40   ASSERT_FALSE(match->IsValid());
41 
42   ASSERT_EQ(re->Replace("ababb", "c").value(), "acac");
43 }
44 
TEST(RegEx,invalid_pattern)45 TEST(RegEx, invalid_pattern) {
46   ASSERT_TRUE(RegEx::Create("?hello") == nullptr);
47 }
48