| Elliott Hughes | cc213f8 | 2012-08-14 15:32:42 -0700 | [diff] [blame] | 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 |  | 
|  | 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)); | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 26 | ASSERT_EQ(0, regexec(&re, "abbbc", 0, nullptr, 0)); | 
|  | 27 | ASSERT_EQ(REG_NOMATCH, regexec(&re, "foo", 0, nullptr, 0)); | 
| Elliott Hughes | cc213f8 | 2012-08-14 15:32:42 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | char buf[80]; | 
|  | 30 | regerror(REG_NOMATCH, &re, buf, sizeof(buf)); | 
| Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 31 | #if defined(__BIONIC__) | 
| Elliott Hughes | cc213f8 | 2012-08-14 15:32:42 -0700 | [diff] [blame] | 32 | ASSERT_STREQ("regexec() failed to match", buf); | 
|  | 33 | #else | 
|  | 34 | ASSERT_STREQ("No match", buf); | 
|  | 35 | #endif | 
|  | 36 |  | 
|  | 37 | regfree(&re); | 
|  | 38 | } | 
| Elliott Hughes | adf5c72 | 2015-08-27 14:48:32 -0700 | [diff] [blame] | 39 |  | 
|  | 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 | } | 
| Elliott Hughes | cac2908 | 2016-02-12 16:00:53 -0800 | [diff] [blame] | 49 |  | 
|  | 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 | } |