b323e284bcb146dd219618f930391506be51b519
[oota-llvm.git] / unittests / Support / RegexTest.cpp
1 //===- llvm/unittest/Support/RegexTest.cpp - Regex tests --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "gtest/gtest.h"
11 #include "llvm/Support/Regex.h"
12 #include <cstring>
13
14 using namespace llvm;
15 namespace {
16
17 class RegexTest : public ::testing::Test {
18 };
19
20 TEST_F(RegexTest, Basics) {
21   Regex r1("^[0-9]+$");
22   EXPECT_TRUE(r1.match("916"));
23   EXPECT_TRUE(r1.match("9"));
24   EXPECT_FALSE(r1.match("9a"));
25
26   SmallVector<StringRef, 1> Matches;
27   Regex r2("[0-9]+", Regex::Sub);
28   EXPECT_TRUE(r2.match("aa216b", &Matches));
29   EXPECT_EQ(1u, Matches.size());
30   EXPECT_EQ("216", Matches[0].str());
31
32   Regex r3("[0-9]+([a-f])?:([0-9]+)", Regex::Sub);
33   EXPECT_TRUE(r3.match("9a:513b", &Matches));
34   EXPECT_EQ(3u, Matches.size());
35   EXPECT_EQ("9a:513", Matches[0].str());
36   EXPECT_EQ("a", Matches[1].str());
37   EXPECT_EQ("513", Matches[2].str());
38
39   EXPECT_TRUE(r3.match("9:513b", &Matches));
40   EXPECT_EQ(3u, Matches.size());
41   EXPECT_EQ("9:513", Matches[0].str());
42   EXPECT_EQ("", Matches[1].str());
43   EXPECT_EQ("513", Matches[2].str());
44
45   Regex r4("a[^b]+b", Regex::Sub);
46   std::string String="axxb";
47   String[2] = '\0';
48   EXPECT_FALSE(r4.match("abb"));
49   EXPECT_TRUE(r4.match(String, &Matches));
50   EXPECT_EQ(1u, Matches.size());
51   EXPECT_EQ(String, Matches[0].str());
52
53
54   std::string NulPattern="X[0-9]+X([a-f])?:([0-9]+)";
55   String="YX99a:513b";
56   NulPattern[7] = '\0';
57   Regex r5(NulPattern, Regex::Sub);
58   EXPECT_FALSE(r5.match(String));
59   EXPECT_FALSE(r5.match("X9"));
60   String[3]='\0';
61   EXPECT_TRUE(r5.match(String));
62 }
63
64 }