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); \
60 using pcrecpp::StringPiece
;
61 using pcrecpp::Scanner
;
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";
69 const char *re
= "(\\w+) = (\\d+);";
75 s
.set_save_comments(true);
76 vector
<StringPiece
> comments
;
78 s
.Consume(re
, &var
, &number
);
79 CHECK_EQ(var
, "alpha");
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");
87 s
.Consume(re
, &var
, &number
);
88 CHECK_EQ(var
, "bravo");
90 s
.GetNextComments(&comments
);
91 CHECK_EQ(comments
.size(), 1);
92 CHECK_EQ(comments
[0].as_string(), " // bravo is set here\n");
95 s
.Consume(re
, &var
, &number
);
96 CHECK_EQ(var
, "gamma");
98 s
.GetNextComments(&comments
);
99 CHECK_EQ(comments
.size(), 1);
100 CHECK_EQ(comments
[0].as_string(), " /* and here is gamma */\n");
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");
110 s
.GetComments(0, (int)(strchr(input
, '/') - input
), &comments
);
111 CHECK_EQ(comments
.size(), 0);
114 s
.GetComments((int)(strchr(input
, '/') - input
- 1), sizeof(input
),
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");
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");
129 static void TestBigComment() {
131 for (int i
= 0; i
< 1024; ++i
) {
132 char buf
[1024]; // definitely big enough
133 sprintf(buf
, " # Comment %d\n", i
);
136 input
+= "name = value;\n";
138 Scanner
s(input
.c_str());
139 s
.SetSkipExpression("\\s+|#.*\n");
143 s
.Consume("(\\w+) = (\\w+);", &name
, &value
);
144 CHECK_EQ(name
, "name");
145 CHECK_EQ(value
, "value");
148 // TODO: also test scanner and big-comment in a thread with a
151 int main(int argc
, char** argv
) {