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 std::string;
61 using pcrecpp::StringPiece;
62 using pcrecpp::Scanner;
63 
TestScanner()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";
69 
70   const char *re = "(\\w+) = (\\d+);";
71 
72   Scanner s(input);
73   string var;
74   int number;
75   s.SkipCXXComments();
76   s.set_save_comments(true);
77   vector<StringPiece> comments;
78 
79   s.Consume(re, &var, &number);
80   CHECK_EQ(var, "alpha");
81   CHECK_EQ(number, 1);
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");
86   comments.resize(0);
87 
88   s.Consume(re, &var, &number);
89   CHECK_EQ(var, "bravo");
90   CHECK_EQ(number, 2);
91   s.GetNextComments(&comments);
92   CHECK_EQ(comments.size(), 1);
93   CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
94   comments.resize(0);
95 
96   s.Consume(re, &var, &number);
97   CHECK_EQ(var, "gamma");
98   CHECK_EQ(number, 33);
99   s.GetNextComments(&comments);
100   CHECK_EQ(comments.size(), 1);
101   CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
102   comments.resize(0);
103 
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");
109   comments.resize(0);
110 
111   s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
112   CHECK_EQ(comments.size(), 0);
113   comments.resize(0);
114 
115   s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
116                 &comments);
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");
121   comments.resize(0);
122 
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");
127   comments.resize(0);
128 }
129 
TestBigComment()130 static void TestBigComment() {
131   string input;
132   for (int i = 0; i < 1024; ++i) {
133     char buf[1024];  // definitely big enough
134     sprintf(buf, "    # Comment %d\n", i);
135     input += buf;
136   }
137   input += "name = value;\n";
138 
139   Scanner s(input.c_str());
140   s.SetSkipExpression("\\s+|#.*\n");
141 
142   string name;
143   string value;
144   s.Consume("(\\w+) = (\\w+);", &name, &value);
145   CHECK_EQ(name, "name");
146   CHECK_EQ(value, "value");
147 }
148 
149 // TODO: also test scanner and big-comment in a thread with a
150 //       small stack size
151 
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153   (void)argc;
154   (void)argv;
155   TestScanner();
156   TestBigComment();
157 
158   // Done
159   printf("OK\n");
160 
161   return 0;
162 }
163