2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include <sys/types.h>
23 // A quick test of all the regex functions.
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));
30 regerror(REG_NOMATCH, &re, buf, sizeof(buf));
31 #if defined(__BIONIC__)
32 ASSERT_STREQ("regexec() failed to match", buf);
34 ASSERT_STREQ("No match", buf);
40 TEST(regex, match_offsets) {
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);
50 TEST(regex, regerror_NULL_0) {
52 int error = regcomp(&re, "*", REG_EXTENDED);
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);