blob: 4a4409ef374003f14041c8f4245d2c254560be1d [file] [log] [blame]
Elliott Hughescc213f82012-08-14 15:32:42 -07001/*
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
22TEST(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));
Elliott Hughes063525c2014-05-13 11:19:57 -070031#if defined(__BIONIC__)
Elliott Hughescc213f82012-08-14 15:32:42 -070032 ASSERT_STREQ("regexec() failed to match", buf);
33#else
34 ASSERT_STREQ("No match", buf);
35#endif
36
37 regfree(&re);
38}
Elliott Hughesadf5c722015-08-27 14:48:32 -070039
40TEST(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}