Annotation of embedaddon/pcre/pcre_scanner_unittest.cc, revision 1.1.1.1

1.1       misho       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.h>      /* for strchr */
                     41: #include <string>
                     42: #include <vector>
                     43: 
                     44: #include "pcrecpp.h"
                     45: #include "pcre_stringpiece.h"
                     46: #include "pcre_scanner.h"
                     47: 
                     48: #define FLAGS_unittest_stack_size   49152
                     49: 
                     50: // Dies with a fatal error if the two values are not equal.
                     51: #define CHECK_EQ(a, b)  do {                                    \
                     52:   if ( (a) != (b) ) {                                           \
                     53:     fprintf(stderr, "%s:%d: Check failed because %s != %s\n",   \
                     54:             __FILE__, __LINE__, #a, #b);                        \
                     55:     exit(1);                                                    \
                     56:   }                                                             \
                     57: } while (0)
                     58: 
                     59: using std::vector;
                     60: using pcrecpp::StringPiece;
                     61: using pcrecpp::Scanner;
                     62: 
                     63: static void TestScanner() {
                     64:   const char input[] = "\n"
                     65:                        "alpha = 1; // this sets alpha\n"
                     66:                        "bravo = 2; // bravo is set here\n"
                     67:                        "gamma = 33; /* and here is gamma */\n";
                     68: 
                     69:   const char *re = "(\\w+) = (\\d+);";
                     70: 
                     71:   Scanner s(input);
                     72:   string var;
                     73:   int number;
                     74:   s.SkipCXXComments();
                     75:   s.set_save_comments(true);
                     76:   vector<StringPiece> comments;
                     77: 
                     78:   s.Consume(re, &var, &number);
                     79:   CHECK_EQ(var, "alpha");
                     80:   CHECK_EQ(number, 1);
                     81:   CHECK_EQ(s.LineNumber(), 3);
                     82:   s.GetNextComments(&comments);
                     83:   CHECK_EQ(comments.size(), 1);
                     84:   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
                     85:   comments.resize(0);
                     86: 
                     87:   s.Consume(re, &var, &number);
                     88:   CHECK_EQ(var, "bravo");
                     89:   CHECK_EQ(number, 2);
                     90:   s.GetNextComments(&comments);
                     91:   CHECK_EQ(comments.size(), 1);
                     92:   CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
                     93:   comments.resize(0);
                     94: 
                     95:   s.Consume(re, &var, &number);
                     96:   CHECK_EQ(var, "gamma");
                     97:   CHECK_EQ(number, 33);
                     98:   s.GetNextComments(&comments);
                     99:   CHECK_EQ(comments.size(), 1);
                    100:   CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
                    101:   comments.resize(0);
                    102: 
                    103:   s.GetComments(0, sizeof(input), &comments);
                    104:   CHECK_EQ(comments.size(), 3);
                    105:   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
                    106:   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
                    107:   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
                    108:   comments.resize(0);
                    109: 
                    110:   s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
                    111:   CHECK_EQ(comments.size(), 0);
                    112:   comments.resize(0);
                    113: 
                    114:   s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
                    115:                 &comments);
                    116:   CHECK_EQ(comments.size(), 3);
                    117:   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
                    118:   CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
                    119:   CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
                    120:   comments.resize(0);
                    121: 
                    122:   s.GetComments((int)(strchr(input, '/') - input - 1),
                    123:                 (int)(strchr(input + 1, '\n') - input + 1), &comments);
                    124:   CHECK_EQ(comments.size(), 1);
                    125:   CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
                    126:   comments.resize(0);
                    127: }
                    128: 
                    129: static void TestBigComment() {
                    130:   string input;
                    131:   for (int i = 0; i < 1024; ++i) {
                    132:     char buf[1024];  // definitely big enough
                    133:     sprintf(buf, "    # Comment %d\n", i);
                    134:     input += buf;
                    135:   }
                    136:   input += "name = value;\n";
                    137: 
                    138:   Scanner s(input.c_str());
                    139:   s.SetSkipExpression("\\s+|#.*\n");
                    140: 
                    141:   string name;
                    142:   string value;
                    143:   s.Consume("(\\w+) = (\\w+);", &name, &value);
                    144:   CHECK_EQ(name, "name");
                    145:   CHECK_EQ(value, "value");
                    146: }
                    147: 
                    148: // TODO: also test scanner and big-comment in a thread with a
                    149: //       small stack size
                    150: 
                    151: int main(int argc, char** argv) {
                    152:   TestScanner();
                    153:   TestBigComment();
                    154: 
                    155:   // Done
                    156:   printf("OK\n");
                    157: 
                    158:   return 0;
                    159: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>