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
23 // @CddTest = 6.1/C-0-2
TEST(RegEx,smoke)24 TEST(RegEx, smoke) {
25 auto re = RegEx::Create("b+");
26 ASSERT_EQ(re->GetPattern(), "b+");
27 ASSERT_FALSE(re->Search("aaa"));
28 ASSERT_TRUE(re->Search("aba"));
29 ASSERT_FALSE(re->Match("aba"));
30 ASSERT_TRUE(re->Match("bbb"));
31
32 auto match = re->SearchAll("aaa");
33 ASSERT_FALSE(match->IsValid());
34 match = re->SearchAll("ababb");
35 ASSERT_TRUE(match->IsValid());
36 ASSERT_EQ(match->GetField(0), "b");
37 match->MoveToNextMatch();
38 ASSERT_TRUE(match->IsValid());
39 ASSERT_EQ(match->GetField(0), "bb");
40 match->MoveToNextMatch();
41 ASSERT_FALSE(match->IsValid());
42
43 ASSERT_EQ(re->Replace("ababb", "c").value(), "acac");
44 }
45
46 // @CddTest = 6.1/C-0-2
TEST(RegEx,invalid_pattern)47 TEST(RegEx, invalid_pattern) {
48 ASSERT_TRUE(RegEx::Create("?hello") == nullptr);
49 }
50