1
// Copyright (c) 2005, Google Inc.
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
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
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.
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.
30
// Author: Greg J. Badros
32
// Unittest for scanner, especially GetNextComments and GetComments()
44
#include "pcre_stringpiece.h"
45
#include "pcre_scanner.h"
47
#define FLAGS_unittest_stack_size 49152
49
// Dies with a fatal error if the two values are not equal.
50
#define CHECK_EQ(a, b) do { \
52
fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
53
__FILE__, __LINE__, #a, #b); \
59
using pcrecpp::StringPiece;
60
using pcrecpp::Scanner;
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";
68
const char *re = "(\\w+) = (\\d+);";
74
s.set_save_comments(true);
75
vector<StringPiece> comments;
77
s.Consume(re, &var, &number);
78
CHECK_EQ(var, "alpha");
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");
86
s.Consume(re, &var, &number);
87
CHECK_EQ(var, "bravo");
89
s.GetNextComments(&comments);
90
CHECK_EQ(comments.size(), 1);
91
CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
94
s.Consume(re, &var, &number);
95
CHECK_EQ(var, "gamma");
97
s.GetNextComments(&comments);
98
CHECK_EQ(comments.size(), 1);
99
CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
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");
109
s.GetComments(0, strchr(input, '/') - input, &comments);
110
CHECK_EQ(comments.size(), 0);
113
s.GetComments(strchr(input, '/') - input - 1, sizeof(input),
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");
121
s.GetComments(strchr(input, '/') - input - 1,
122
strchr(input + 1, '\n') - input + 1, &comments);
123
CHECK_EQ(comments.size(), 1);
124
CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
128
static void TestBigComment() {
130
for (int i = 0; i < 1024; ++i) {
131
char buf[1024]; // definitely big enough
132
sprintf(buf, " # Comment %d\n", i);
135
input += "name = value;\n";
137
Scanner s(input.c_str());
138
s.SetSkipExpression("\\s+|#.*\n");
142
s.Consume("(\\w+) = (\\w+);", &name, &value);
143
CHECK_EQ(name, "name");
144
CHECK_EQ(value, "value");
147
// TODO: also test scanner and big-comment in a thread with a
150
int main(int argc, char** argv) {