pcre_scanner_unittest.cc

Go to the documentation of this file.
00001 // Copyright (c) 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: Greg J. Badros
00031 //
00032 // Unittest for scanner, especially GetNextComments and GetComments()
00033 // functionality.
00034 
00035 #ifdef HAVE_CONFIG_H
00036 #include "config.h"
00037 #endif
00038 
00039 #include <stdio.h>
00040 #include <string>
00041 #include <vector>
00042 
00043 #include "pcrecpp.h"
00044 #include "pcre_stringpiece.h"
00045 #include "pcre_scanner.h"
00046 
00047 #define FLAGS_unittest_stack_size   49152
00048 
00049 // Dies with a fatal error if the two values are not equal.
00050 #define CHECK_EQ(a, b)  do {                                    \
00051   if ( (a) != (b) ) {                                           \
00052     fprintf(stderr, "%s:%d: Check failed because %s != %s\n",   \
00053             __FILE__, __LINE__, #a, #b);                        \
00054     exit(1);                                                    \
00055   }                                                             \
00056 } while (0)
00057 
00058 using std::vector;
00059 using pcrecpp::StringPiece;
00060 using pcrecpp::Scanner;
00061 
00062 static void TestScanner() {
00063   const char input[] = "\n"
00064                        "alpha = 1; // this sets alpha\n"
00065                        "bravo = 2; // bravo is set here\n"
00066                        "gamma = 33; /* and here is gamma */\n";
00067 
00068   const char *re = "(\\w+) = (\\d+);";
00069 
00070   Scanner s(input);
00071   string var;
00072   int number;
00073   s.SkipCXXComments();
00074   s.set_save_comments(true);
00075   vector<StringPiece> comments;
00076 
00077   s.Consume(re, &var, &number);
00078   CHECK_EQ(var, "alpha");
00079   CHECK_EQ(number, 1);
00080   CHECK_EQ(s.LineNumber(), 3);
00081   s.GetNextComments(&comments);
00082   CHECK_EQ(comments.size(), 1);
00083   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
00084   comments.resize(0);
00085 
00086   s.Consume(re, &var, &number);
00087   CHECK_EQ(var, "bravo");
00088   CHECK_EQ(number, 2);
00089   s.GetNextComments(&comments);
00090   CHECK_EQ(comments.size(), 1);
00091   CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
00092   comments.resize(0);
00093 
00094   s.Consume(re, &var, &number);
00095   CHECK_EQ(var, "gamma");
00096   CHECK_EQ(number, 33);
00097   s.GetNextComments(&comments);
00098   CHECK_EQ(comments.size(), 1);
00099   CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
00100   comments.resize(0);
00101 
00102   s.GetComments(0, sizeof(input), &comments);
00103   CHECK_EQ(comments.size(), 3);
00104   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
00105   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
00106   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
00107   comments.resize(0);
00108 
00109   s.GetComments(0, strchr(input, '/') - input, &comments);
00110   CHECK_EQ(comments.size(), 0);
00111   comments.resize(0);
00112 
00113   s.GetComments(strchr(input, '/') - input - 1, sizeof(input),
00114                 &comments);
00115   CHECK_EQ(comments.size(), 3);
00116   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
00117   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
00118   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
00119   comments.resize(0);
00120 
00121   s.GetComments(strchr(input, '/') - input - 1,
00122                 strchr(input + 1, '\n') - input + 1, &comments);
00123   CHECK_EQ(comments.size(), 1);
00124   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
00125   comments.resize(0);
00126 }
00127 
00128 static void TestBigComment() {
00129   string input;
00130   for (int i = 0; i < 1024; ++i) {
00131     char buf[1024];  // definitely big enough
00132     sprintf(buf, "    # Comment %d\n", i);
00133     input += buf;
00134   }
00135   input += "name = value;\n";
00136 
00137   Scanner s(input.c_str());
00138   s.SetSkipExpression("\\s+|#.*\n");
00139 
00140   string name;
00141   string value;
00142   s.Consume("(\\w+) = (\\w+);", &name, &value);
00143   CHECK_EQ(name, "name");
00144   CHECK_EQ(value, "value");
00145 }
00146 
00147 // TODO: also test scanner and big-comment in a thread with a
00148 //       small stack size
00149 
00150 int main(int argc, char** argv) {
00151   TestScanner();
00152   TestBigComment();
00153 
00154   // Done
00155   printf("OK\n");
00156 
00157   return 0;
00158 }

Generated on Tue Jul 5 14:11:57 2011 for ROOT_528-00b_version by  doxygen 1.5.1