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()
40 #include <string.h> /* for strchr */
45 #include "pcre_stringpiece.h"
46 #include "pcre_scanner.h"
48 #define FLAGS_unittest_stack_size 49152
50 // Dies with a fatal error if the two values are not equal.
51 #define CHECK_EQ(a, b) do { \
53 fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
54 __FILE__, __LINE__, #a, #b); \
61 using pcrecpp::StringPiece
;
62 using pcrecpp::Scanner
;
64 static void TestScanner() {
65 const char input
[] = "\n"
66 "alpha = 1; // this sets alpha\n"
67 "bravo = 2; // bravo is set here\n"
68 "gamma = 33; /* and here is gamma */\n";
70 const char *re
= "(\\w+) = (\\d+);";
76 s
.set_save_comments(true);
77 vector
<StringPiece
> comments
;
79 s
.Consume(re
, &var
, &number
);
80 CHECK_EQ(var
, "alpha");
82 CHECK_EQ(s
.LineNumber(), 3);
83 s
.GetNextComments(&comments
);
84 CHECK_EQ(comments
.size(), 1);
85 CHECK_EQ(comments
[0].as_string(), " // this sets alpha\n");
88 s
.Consume(re
, &var
, &number
);
89 CHECK_EQ(var
, "bravo");
91 s
.GetNextComments(&comments
);
92 CHECK_EQ(comments
.size(), 1);
93 CHECK_EQ(comments
[0].as_string(), " // bravo is set here\n");
96 s
.Consume(re
, &var
, &number
);
97 CHECK_EQ(var
, "gamma");
99 s
.GetNextComments(&comments
);
100 CHECK_EQ(comments
.size(), 1);
101 CHECK_EQ(comments
[0].as_string(), " /* and here is gamma */\n");
104 s
.GetComments(0, sizeof(input
), &comments
);
105 CHECK_EQ(comments
.size(), 3);
106 CHECK_EQ(comments
[0].as_string(), " // this sets alpha\n");
107 CHECK_EQ(comments
[1].as_string(), " // bravo is set here\n");
108 CHECK_EQ(comments
[2].as_string(), " /* and here is gamma */\n");
111 s
.GetComments(0, (int)(strchr(input
, '/') - input
), &comments
);
112 CHECK_EQ(comments
.size(), 0);
115 s
.GetComments((int)(strchr(input
, '/') - input
- 1), sizeof(input
),
117 CHECK_EQ(comments
.size(), 3);
118 CHECK_EQ(comments
[0].as_string(), " // this sets alpha\n");
119 CHECK_EQ(comments
[1].as_string(), " // bravo is set here\n");
120 CHECK_EQ(comments
[2].as_string(), " /* and here is gamma */\n");
123 s
.GetComments((int)(strchr(input
, '/') - input
- 1),
124 (int)(strchr(input
+ 1, '\n') - input
+ 1), &comments
);
125 CHECK_EQ(comments
.size(), 1);
126 CHECK_EQ(comments
[0].as_string(), " // this sets alpha\n");
130 static void TestBigComment() {
132 for (int i
= 0; i
< 1024; ++i
) {
133 char buf
[1024]; // definitely big enough
134 sprintf(buf
, " # Comment %d\n", i
);
137 input
+= "name = value;\n";
139 Scanner
s(input
.c_str());
140 s
.SetSkipExpression("\\s+|#.*\n");
144 s
.Consume("(\\w+) = (\\w+);", &name
, &value
);
145 CHECK_EQ(name
, "name");
146 CHECK_EQ(value
, "value");
149 // TODO: also test scanner and big-comment in a thread with a
152 int main(int argc
, char** argv
) {