blob: fc598227665cce580e3dd20894167560c1e9ea49 [file]
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Basic round-trip tests for POSIX regex functions.
///
//===----------------------------------------------------------------------===//
#include "src/regex/regcomp.h"
#include "src/regex/regexec.h"
#include "src/regex/regfree.h"
#include "test/UnitTest/Test.h"
#include "hdr/regex_macros.h"
#include "hdr/types/regex_t.h"
TEST(LlvmLibcRegexTest, BasicLiteralRoundTrip) {
regex_t preg;
ASSERT_EQ(0,
LIBC_NAMESPACE::regcomp(&preg, "hello", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(0,
LIBC_NAMESPACE::regexec(&preg, "say hello world", 0, nullptr, 0));
ASSERT_EQ(REG_NOMATCH,
LIBC_NAMESPACE::regexec(&preg, "goodbye", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
}
TEST(LlvmLibcRegexTest, MismatchCases) {
regex_t preg;
// Partial match
ASSERT_EQ(0,
LIBC_NAMESPACE::regcomp(&preg, "hello", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(REG_NOMATCH, LIBC_NAMESPACE::regexec(&preg, "hell", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
// Case sensitivity
ASSERT_EQ(0,
LIBC_NAMESPACE::regcomp(&preg, "Hello", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(REG_NOMATCH,
LIBC_NAMESPACE::regexec(&preg, "hello", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
// Empty string vs non-empty pattern
ASSERT_EQ(0, LIBC_NAMESPACE::regcomp(&preg, "a", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(REG_NOMATCH, LIBC_NAMESPACE::regexec(&preg, "", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
}
TEST(LlvmLibcRegexTest, EmptyString) {
regex_t preg;
ASSERT_EQ(0, LIBC_NAMESPACE::regcomp(&preg, "", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(0, LIBC_NAMESPACE::regexec(&preg, "anything", 0, nullptr, 0));
ASSERT_EQ(0, LIBC_NAMESPACE::regexec(&preg, "", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
}
TEST(LlvmLibcRegexTest, ExactMatch) {
regex_t preg;
ASSERT_EQ(0,
LIBC_NAMESPACE::regcomp(&preg, "test", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(0, LIBC_NAMESPACE::regexec(&preg, "test", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
}
TEST(LlvmLibcRegexTest, NullByteStopsParsing) {
regex_t preg;
ASSERT_EQ(0,
LIBC_NAMESPACE::regcomp(&preg, "match", REG_EXTENDED | REG_NOSUB));
ASSERT_EQ(REG_NOMATCH,
LIBC_NAMESPACE::regexec(&preg, "doesn't \0 match", 0, nullptr, 0));
LIBC_NAMESPACE::regfree(&preg);
}