Home | History | Annotate | Download | only in Support
      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 "llvm/ADT/SmallVector.h"
     13 #include <cstring>
     14 
     15 using namespace llvm;
     16 namespace {
     17 
     18 class RegexTest : public ::testing::Test {
     19 };
     20 
     21 TEST_F(RegexTest, Basics) {
     22   Regex r1("^[0-9]+$");
     23   EXPECT_TRUE(r1.match("916"));
     24   EXPECT_TRUE(r1.match("9"));
     25   EXPECT_FALSE(r1.match("9a"));
     26 
     27   SmallVector<StringRef, 1> Matches;
     28   Regex r2("[0-9]+");
     29   EXPECT_TRUE(r2.match("aa216b", &Matches));
     30   EXPECT_EQ(1u, Matches.size());
     31   EXPECT_EQ("216", Matches[0].str());
     32 
     33   Regex r3("[0-9]+([a-f])?:([0-9]+)");
     34   EXPECT_TRUE(r3.match("9a:513b", &Matches));
     35   EXPECT_EQ(3u, Matches.size());
     36   EXPECT_EQ("9a:513", Matches[0].str());
     37   EXPECT_EQ("a", Matches[1].str());
     38   EXPECT_EQ("513", Matches[2].str());
     39 
     40   EXPECT_TRUE(r3.match("9:513b", &Matches));
     41   EXPECT_EQ(3u, Matches.size());
     42   EXPECT_EQ("9:513", Matches[0].str());
     43   EXPECT_EQ("", Matches[1].str());
     44   EXPECT_EQ("513", Matches[2].str());
     45 
     46   Regex r4("a[^b]+b");
     47   std::string String="axxb";
     48   String[2] = '\0';
     49   EXPECT_FALSE(r4.match("abb"));
     50   EXPECT_TRUE(r4.match(String, &Matches));
     51   EXPECT_EQ(1u, Matches.size());
     52   EXPECT_EQ(String, Matches[0].str());
     53 
     54 
     55   std::string NulPattern="X[0-9]+X([a-f])?:([0-9]+)";
     56   String="YX99a:513b";
     57   NulPattern[7] = '\0';
     58   Regex r5(NulPattern);
     59   EXPECT_FALSE(r5.match(String));
     60   EXPECT_FALSE(r5.match("X9"));
     61   String[3]='\0';
     62   EXPECT_TRUE(r5.match(String));
     63 }
     64 
     65 TEST_F(RegexTest, Substitution) {
     66   std::string Error;
     67 
     68   EXPECT_EQ("aNUMber", Regex("[0-9]+").sub("NUM", "a1234ber"));
     69 
     70   // Standard Escapes
     71   EXPECT_EQ("a\\ber", Regex("[0-9]+").sub("\\\\", "a1234ber", &Error));
     72   EXPECT_EQ(Error, "");
     73   EXPECT_EQ("a\nber", Regex("[0-9]+").sub("\\n", "a1234ber", &Error));
     74   EXPECT_EQ(Error, "");
     75   EXPECT_EQ("a\tber", Regex("[0-9]+").sub("\\t", "a1234ber", &Error));
     76   EXPECT_EQ(Error, "");
     77   EXPECT_EQ("ajber", Regex("[0-9]+").sub("\\j", "a1234ber", &Error));
     78   EXPECT_EQ(Error, "");
     79 
     80   EXPECT_EQ("aber", Regex("[0-9]+").sub("\\", "a1234ber", &Error));
     81   EXPECT_EQ(Error, "replacement string contained trailing backslash");
     82 
     83   // Backreferences
     84   EXPECT_EQ("aa1234bber", Regex("a[0-9]+b").sub("a\\0b", "a1234ber", &Error));
     85   EXPECT_EQ(Error, "");
     86 
     87   EXPECT_EQ("a1234ber", Regex("a([0-9]+)b").sub("a\\1b", "a1234ber", &Error));
     88   EXPECT_EQ(Error, "");
     89 
     90   EXPECT_EQ("aber", Regex("a[0-9]+b").sub("a\\100b", "a1234ber", &Error));
     91   EXPECT_EQ(Error, "invalid backreference string '100'");
     92 }
     93 
     94 }
     95