1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/tools/pcre/pcre_scanner_unittest.cc Tue Jul 28 17:51:34 2009 -0400
1.3 @@ -0,0 +1,158 @@
1.4 +// Copyright (c) 2005, Google Inc.
1.5 +// All rights reserved.
1.6 +//
1.7 +// Redistribution and use in source and binary forms, with or without
1.8 +// modification, are permitted provided that the following conditions are
1.9 +// met:
1.10 +//
1.11 +// * Redistributions of source code must retain the above copyright
1.12 +// notice, this list of conditions and the following disclaimer.
1.13 +// * Redistributions in binary form must reproduce the above
1.14 +// copyright notice, this list of conditions and the following disclaimer
1.15 +// in the documentation and/or other materials provided with the
1.16 +// distribution.
1.17 +// * Neither the name of Google Inc. nor the names of its
1.18 +// contributors may be used to endorse or promote products derived from
1.19 +// this software without specific prior written permission.
1.20 +//
1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.32 +//
1.33 +// Author: Greg J. Badros
1.34 +//
1.35 +// Unittest for scanner, especially GetNextComments and GetComments()
1.36 +// functionality.
1.37 +
1.38 +#ifdef HAVE_CONFIG_H
1.39 +#include "config.h"
1.40 +#endif
1.41 +
1.42 +#include <stdio.h>
1.43 +#include <string>
1.44 +#include <vector>
1.45 +
1.46 +#include "pcrecpp.h"
1.47 +#include "pcre_stringpiece.h"
1.48 +#include "pcre_scanner.h"
1.49 +
1.50 +#define FLAGS_unittest_stack_size 49152
1.51 +
1.52 +// Dies with a fatal error if the two values are not equal.
1.53 +#define CHECK_EQ(a, b) do { \
1.54 + if ( (a) != (b) ) { \
1.55 + fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
1.56 + __FILE__, __LINE__, #a, #b); \
1.57 + exit(1); \
1.58 + } \
1.59 +} while (0)
1.60 +
1.61 +using std::vector;
1.62 +using pcrecpp::StringPiece;
1.63 +using pcrecpp::Scanner;
1.64 +
1.65 +static void TestScanner() {
1.66 + const char input[] = "\n"
1.67 + "alpha = 1; // this sets alpha\n"
1.68 + "bravo = 2; // bravo is set here\n"
1.69 + "gamma = 33; /* and here is gamma */\n";
1.70 +
1.71 + const char *re = "(\\w+) = (\\d+);";
1.72 +
1.73 + Scanner s(input);
1.74 + string var;
1.75 + int number;
1.76 + s.SkipCXXComments();
1.77 + s.set_save_comments(true);
1.78 + vector<StringPiece> comments;
1.79 +
1.80 + s.Consume(re, &var, &number);
1.81 + CHECK_EQ(var, "alpha");
1.82 + CHECK_EQ(number, 1);
1.83 + CHECK_EQ(s.LineNumber(), 3);
1.84 + s.GetNextComments(&comments);
1.85 + CHECK_EQ(comments.size(), 1);
1.86 + CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
1.87 + comments.resize(0);
1.88 +
1.89 + s.Consume(re, &var, &number);
1.90 + CHECK_EQ(var, "bravo");
1.91 + CHECK_EQ(number, 2);
1.92 + s.GetNextComments(&comments);
1.93 + CHECK_EQ(comments.size(), 1);
1.94 + CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
1.95 + comments.resize(0);
1.96 +
1.97 + s.Consume(re, &var, &number);
1.98 + CHECK_EQ(var, "gamma");
1.99 + CHECK_EQ(number, 33);
1.100 + s.GetNextComments(&comments);
1.101 + CHECK_EQ(comments.size(), 1);
1.102 + CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
1.103 + comments.resize(0);
1.104 +
1.105 + s.GetComments(0, sizeof(input), &comments);
1.106 + CHECK_EQ(comments.size(), 3);
1.107 + CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
1.108 + CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
1.109 + CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
1.110 + comments.resize(0);
1.111 +
1.112 + s.GetComments(0, strchr(input, '/') - input, &comments);
1.113 + CHECK_EQ(comments.size(), 0);
1.114 + comments.resize(0);
1.115 +
1.116 + s.GetComments(strchr(input, '/') - input - 1, sizeof(input),
1.117 + &comments);
1.118 + CHECK_EQ(comments.size(), 3);
1.119 + CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
1.120 + CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
1.121 + CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
1.122 + comments.resize(0);
1.123 +
1.124 + s.GetComments(strchr(input, '/') - input - 1,
1.125 + strchr(input + 1, '\n') - input + 1, &comments);
1.126 + CHECK_EQ(comments.size(), 1);
1.127 + CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
1.128 + comments.resize(0);
1.129 +}
1.130 +
1.131 +static void TestBigComment() {
1.132 + string input;
1.133 + for (int i = 0; i < 1024; ++i) {
1.134 + char buf[1024]; // definitely big enough
1.135 + sprintf(buf, " # Comment %d\n", i);
1.136 + input += buf;
1.137 + }
1.138 + input += "name = value;\n";
1.139 +
1.140 + Scanner s(input.c_str());
1.141 + s.SetSkipExpression("\\s+|#.*\n");
1.142 +
1.143 + string name;
1.144 + string value;
1.145 + s.Consume("(\\w+) = (\\w+);", &name, &value);
1.146 + CHECK_EQ(name, "name");
1.147 + CHECK_EQ(value, "value");
1.148 +}
1.149 +
1.150 +// TODO: also test scanner and big-comment in a thread with a
1.151 +// small stack size
1.152 +
1.153 +int main(int argc, char** argv) {
1.154 + TestScanner();
1.155 + TestBigComment();
1.156 +
1.157 + // Done
1.158 + printf("OK\n");
1.159 +
1.160 + return 0;
1.161 +}