Home | History | Annotate | Download | only in pcre
      1 // Copyright (c) 2005, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 //
     30 // Author: Greg J. Badros
     31 //
     32 // Unittest for scanner, especially GetNextComments and GetComments()
     33 // functionality.
     34 
     35 #ifdef HAVE_CONFIG_H
     36 #include "config.h"
     37 #endif
     38 
     39 #include <stdio.h>
     40 #include <string>
     41 #include <vector>
     42 
     43 #include "pcrecpp.h"
     44 #include "pcre_stringpiece.h"
     45 #include "pcre_scanner.h"
     46 
     47 #define FLAGS_unittest_stack_size   49152
     48 
     49 // Dies with a fatal error if the two values are not equal.
     50 #define CHECK_EQ(a, b)  do {                                    \
     51   if ( (a) != (b) ) {                                           \
     52     fprintf(stderr, "%s:%d: Check failed because %s != %s\n",   \
     53             __FILE__, __LINE__, #a, #b);                        \
     54     exit(1);                                                    \
     55   }                                                             \
     56 } while (0)
     57 
     58 using std::vector;
     59 using pcrecpp::StringPiece;
     60 using pcrecpp::Scanner;
     61 
     62 static void TestScanner() {
     63   const char input[] = "\n"
     64                        "alpha = 1; // this sets alpha\n"
     65                        "bravo = 2; // bravo is set here\n"
     66                        "gamma = 33; /* and here is gamma */\n";
     67 
     68   const char *re = "(\\w+) = (\\d+);";
     69 
     70   Scanner s(input);
     71   string var;
     72   int number;
     73   s.SkipCXXComments();
     74   s.set_save_comments(true);
     75   vector<StringPiece> comments;
     76 
     77   s.Consume(re, &var, &number);
     78   CHECK_EQ(var, "alpha");
     79   CHECK_EQ(number, 1);
     80   CHECK_EQ(s.LineNumber(), 3);
     81   s.GetNextComments(&comments);
     82   CHECK_EQ(comments.size(), 1);
     83   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
     84   comments.resize(0);
     85 
     86   s.Consume(re, &var, &number);
     87   CHECK_EQ(var, "bravo");
     88   CHECK_EQ(number, 2);
     89   s.GetNextComments(&comments);
     90   CHECK_EQ(comments.size(), 1);
     91   CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
     92   comments.resize(0);
     93 
     94   s.Consume(re, &var, &number);
     95   CHECK_EQ(var, "gamma");
     96   CHECK_EQ(number, 33);
     97   s.GetNextComments(&comments);
     98   CHECK_EQ(comments.size(), 1);
     99   CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
    100   comments.resize(0);
    101 
    102   s.GetComments(0, sizeof(input), &comments);
    103   CHECK_EQ(comments.size(), 3);
    104   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
    105   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
    106   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
    107   comments.resize(0);
    108 
    109   s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
    110   CHECK_EQ(comments.size(), 0);
    111   comments.resize(0);
    112 
    113   s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
    114                 &comments);
    115   CHECK_EQ(comments.size(), 3);
    116   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
    117   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
    118   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
    119   comments.resize(0);
    120 
    121   s.GetComments((int)(strchr(input, '/') - input - 1),
    122                 (int)(strchr(input + 1, '\n') - input + 1), &comments);
    123   CHECK_EQ(comments.size(), 1);
    124   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
    125   comments.resize(0);
    126 }
    127 
    128 static void TestBigComment() {
    129   string input;
    130   for (int i = 0; i < 1024; ++i) {
    131     char buf[1024];  // definitely big enough
    132     sprintf(buf, "    # Comment %d\n", i);
    133     input += buf;
    134   }
    135   input += "name = value;\n";
    136 
    137   Scanner s(input.c_str());
    138   s.SetSkipExpression("\\s+|#.*\n");
    139 
    140   string name;
    141   string value;
    142   s.Consume("(\\w+) = (\\w+);", &name, &value);
    143   CHECK_EQ(name, "name");
    144   CHECK_EQ(value, "value");
    145 }
    146 
    147 // TODO: also test scanner and big-comment in a thread with a
    148 //       small stack size
    149 
    150 int main(int argc, char** argv) {
    151   TestScanner();
    152   TestBigComment();
    153 
    154   // Done
    155   printf("OK\n");
    156 
    157   return 0;
    158 }
    159