DirectXShaderCompiler/unittests/Support/RegexTest.cpp

157 строки
4.5 KiB
C++
Исходник Постоянная ссылка Обычный вид История

2016-12-28 22:52:27 +03:00
//===- llvm/unittest/Support/RegexTest.cpp - Regex tests --===//
Revert license text in banner comments to original llvm verbage (#33) Fix #30: Revert license text in banner comments to original llvm verbage This commit removes the Microsoft-specific copyright in llvm files and reverts the copyright wording to the original llvm wording. We used the following method to find the files to change: 1. Find all files in DirectXShaderCompiler that are also in llvm 3.7 2. For those files that have the Microsoft-specific copyright, revert it to the original llvm copyright as present in llvm 3.7 3. Revert the copyright in a few files that are not in llvm, but are mostly copies of files in llvm: lib\Transforms\Scalar\ScalarReplAggregatesHLSL.cpp lib\Transforms\Scalar\Reg2MemHLSL.cpp Leave the Microsoft-specific copyright header in files not present in stock llvm: include\dxc\* lib\HLSL\* lib\DxcSupport\* tools\clang\test\HLSL\* tools\clang\test\CodeGenHLSL\* tools\clang\unittests\HLSL\* tools\clang\unittests\HLSLHost\* tools\clang\tools\dxcompiler\* tools\clang\tools\dxa\* tools\clang\tools\dxc\* tools\clang\tools\dxopt\* tools\clang\tools\dxr\* tools\clang\tools\dxv\* tools\clang\tools\dotnetc\* utils\hct\* CONTRIBUTING.md COPYRIGHT LICENSE-MIT README.md cmake\modules\FindD3D12.cmake cmake\modules\FindDiaSDK.cmake cmake\modules\FindTAEF.cmake docs\DXIL.rst docs\HLSLChanges.rst docs\_themes\dxc-theme\layout.html docs\_themes\dxc-theme\theme.conf docs\_themes\dxc-theme\static\dxc-theme.css include\llvm\llvm_assert\assert.h include\llvm\llvm_assert\cassert include\llvm\Support\MSFileSystem.h include\llvm\Support\OacrIgnoreCond.h lib\MSSupport\CMakeLists.txt lib\MSSupport\MSFileSystemImpl.cpp lib\Support\assert.cpp lib\Support\MSFileSystemBasic.cpp lib\Support\Windows\MSFileSystem.inc.cpp lib\Transforms\Scalar\Reg2MemHLSL.cpp lib\Transforms\Scalar\ScalarReplAggregatesHLSL.cpp tools\clang\docs\UsingDxc.rst tools\clang\include\clang\AST\HlslTypes.h tools\clang\include\clang\Basic\BuiltinsDXIL.def tools\clang\include\clang\Basic\LangOptions.fixed.def tools\clang\include\clang\Parse\ParseHLSL.h tools\clang\include\clang\Sema\SemaHLSL.h tools\clang\lib\AST\ASTContextHLSL.cpp tools\clang\lib\AST\HlslTypes.cpp tools\clang\lib\CodeGen\CGHLSLMS.cpp tools\clang\lib\CodeGen\CGHLSLRuntime.cpp tools\clang\lib\CodeGen\CGHLSLRuntime.h tools\clang\lib\Frontend\Rewrite\FrontendActions_rewrite.cpp tools\clang\lib\Parse\HLSLRootSignature.cpp tools\clang\lib\Parse\HLSLRootSignature.h tools\clang\lib\Parse\ParseHLSL.cpp tools\clang\lib\Sema\gen_intrin_main_tables_15.h tools\clang\lib\Sema\SemaHLSL.cpp tools\clang\tools\d3dcomp\CMakeLists.txt tools\clang\tools\d3dcomp\d3dcomp.cpp tools\clang\tools\d3dcomp\d3dcomp.def tools\clang\tools\libclang\dxcisenseimpl.cpp tools\clang\tools\libclang\dxcisenseimpl.h tools\clang\tools\libclang\dxcrewriteunused.cpp tools\clang\tools\libclang\libclang.rc tools\dxexp\CMakeLists.txt tools\dxexp\dxexp.cpp tools\dxexp\LLVMBuild.txt
2017-01-25 04:54:00 +03:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
2016-12-28 22:52:27 +03:00
#include "llvm/Support/Regex.h"
#include "llvm/ADT/SmallVector.h"
#include "gtest/gtest.h"
#include <cstring>
using namespace llvm;
namespace {
class RegexTest : public ::testing::Test {
};
TEST_F(RegexTest, Basics) {
Regex r1("^[0-9]+$");
EXPECT_TRUE(r1.match("916"));
EXPECT_TRUE(r1.match("9"));
EXPECT_FALSE(r1.match("9a"));
SmallVector<StringRef, 1> Matches;
Regex r2("[0-9]+");
EXPECT_TRUE(r2.match("aa216b", &Matches));
EXPECT_EQ(1u, Matches.size());
EXPECT_EQ("216", Matches[0].str());
Regex r3("[0-9]+([a-f])?:([0-9]+)");
EXPECT_TRUE(r3.match("9a:513b", &Matches));
EXPECT_EQ(3u, Matches.size());
EXPECT_EQ("9a:513", Matches[0].str());
EXPECT_EQ("a", Matches[1].str());
EXPECT_EQ("513", Matches[2].str());
EXPECT_TRUE(r3.match("9:513b", &Matches));
EXPECT_EQ(3u, Matches.size());
EXPECT_EQ("9:513", Matches[0].str());
EXPECT_EQ("", Matches[1].str());
EXPECT_EQ("513", Matches[2].str());
Regex r4("a[^b]+b");
std::string String="axxb";
String[2] = '\0';
EXPECT_FALSE(r4.match("abb"));
EXPECT_TRUE(r4.match(String, &Matches));
EXPECT_EQ(1u, Matches.size());
EXPECT_EQ(String, Matches[0].str());
std::string NulPattern="X[0-9]+X([a-f])?:([0-9]+)";
String="YX99a:513b";
NulPattern[7] = '\0';
Regex r5(NulPattern);
EXPECT_FALSE(r5.match(String));
EXPECT_FALSE(r5.match("X9"));
String[3]='\0';
EXPECT_TRUE(r5.match(String));
}
TEST_F(RegexTest, Backreferences) {
Regex r1("([a-z]+)_\\1");
SmallVector<StringRef, 4> Matches;
EXPECT_TRUE(r1.match("abc_abc", &Matches));
EXPECT_EQ(2u, Matches.size());
EXPECT_FALSE(r1.match("abc_ab", &Matches));
Regex r2("a([0-9])b\\1c\\1");
EXPECT_TRUE(r2.match("a4b4c4", &Matches));
EXPECT_EQ(2u, Matches.size());
EXPECT_EQ("4", Matches[1].str());
EXPECT_FALSE(r2.match("a2b2c3"));
Regex r3("a([0-9])([a-z])b\\1\\2");
EXPECT_TRUE(r3.match("a6zb6z", &Matches));
EXPECT_EQ(3u, Matches.size());
EXPECT_EQ("6", Matches[1].str());
EXPECT_EQ("z", Matches[2].str());
EXPECT_FALSE(r3.match("a6zb6y"));
EXPECT_FALSE(r3.match("a6zb7z"));
}
TEST_F(RegexTest, Substitution) {
std::string Error;
EXPECT_EQ("aNUMber", Regex("[0-9]+").sub("NUM", "a1234ber"));
// Standard Escapes
EXPECT_EQ("a\\ber", Regex("[0-9]+").sub("\\\\", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("a\nber", Regex("[0-9]+").sub("\\n", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("a\tber", Regex("[0-9]+").sub("\\t", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("ajber", Regex("[0-9]+").sub("\\j", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("aber", Regex("[0-9]+").sub("\\", "a1234ber", &Error));
EXPECT_EQ(Error, "replacement string contained trailing backslash");
// Backreferences
EXPECT_EQ("aa1234bber", Regex("a[0-9]+b").sub("a\\0b", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("a1234ber", Regex("a([0-9]+)b").sub("a\\1b", "a1234ber", &Error));
EXPECT_EQ("", Error);
EXPECT_EQ("aber", Regex("a[0-9]+b").sub("a\\100b", "a1234ber", &Error));
EXPECT_EQ(Error, "invalid backreference string '100'");
}
TEST_F(RegexTest, IsLiteralERE) {
EXPECT_TRUE(Regex::isLiteralERE("abc"));
EXPECT_FALSE(Regex::isLiteralERE("a(bc)"));
EXPECT_FALSE(Regex::isLiteralERE("^abc"));
EXPECT_FALSE(Regex::isLiteralERE("abc$"));
EXPECT_FALSE(Regex::isLiteralERE("a|bc"));
EXPECT_FALSE(Regex::isLiteralERE("abc*"));
EXPECT_FALSE(Regex::isLiteralERE("abc+"));
EXPECT_FALSE(Regex::isLiteralERE("abc?"));
EXPECT_FALSE(Regex::isLiteralERE("abc."));
EXPECT_FALSE(Regex::isLiteralERE("a[bc]"));
EXPECT_FALSE(Regex::isLiteralERE("abc\\1"));
EXPECT_FALSE(Regex::isLiteralERE("abc{1,2}"));
}
TEST_F(RegexTest, Escape) {
EXPECT_EQ("a\\[bc\\]", Regex::escape("a[bc]"));
EXPECT_EQ("abc\\{1\\\\,2\\}", Regex::escape("abc{1\\,2}"));
}
TEST_F(RegexTest, IsValid) {
std::string Error;
EXPECT_FALSE(Regex("(foo").isValid(Error));
EXPECT_EQ("parentheses not balanced", Error);
EXPECT_FALSE(Regex("a[b-").isValid(Error));
EXPECT_EQ("invalid character range", Error);
}
TEST_F(RegexTest, MoveConstruct) {
Regex r1("^[0-9]+$");
Regex r2(std::move(r1));
EXPECT_TRUE(r2.match("916"));
}
TEST_F(RegexTest, MoveAssign) {
Regex r1("^[0-9]+$");
Regex r2("abc");
r2 = std::move(r1);
EXPECT_TRUE(r2.match("916"));
}
}