1 /*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
19 #include <sys/types.h>
20 #include <regex.h>
21
TEST(regex,smoke)22 TEST(regex, smoke) {
23 // A quick test of all the regex functions.
24 regex_t re;
25 ASSERT_EQ(0, regcomp(&re, "ab*c", 0));
26 ASSERT_EQ(0, regexec(&re, "abbbc", 0, NULL, 0));
27 ASSERT_EQ(REG_NOMATCH, regexec(&re, "foo", 0, NULL, 0));
28
29 char buf[80];
30 regerror(REG_NOMATCH, &re, buf, sizeof(buf));
31 #if defined(__BIONIC__)
32 ASSERT_STREQ("regexec() failed to match", buf);
33 #else
34 ASSERT_STREQ("No match", buf);
35 #endif
36
37 regfree(&re);
38 }
39
TEST(regex,match_offsets)40 TEST(regex, match_offsets) {
41 regex_t re;
42 regmatch_t matches[1];
43 ASSERT_EQ(0, regcomp(&re, "b", 0));
44 ASSERT_EQ(0, regexec(&re, "abc", 1, matches, 0));
45 ASSERT_EQ(1, matches[0].rm_so);
46 ASSERT_EQ(2, matches[0].rm_eo);
47 regfree(&re);
48 }
49
TEST(regex,regerror_NULL_0)50 TEST(regex, regerror_NULL_0) {
51 regex_t re;
52 int error = regcomp(&re, "*", REG_EXTENDED);
53 ASSERT_NE(0, error);
54
55 // Passing a null pointer and a size of 0 is a legitimate way to ask
56 // how large a buffer we would need for the error message.
57 int error_length = regerror(error, &re, nullptr, 0);
58 ASSERT_GT(error_length, 0);
59 }
60