/[pcre]/code/trunk/pcre_scanner_unittest.cc
ViewVC logotype

Contents of /code/trunk/pcre_scanner_unittest.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 200 - (show annotations) (download)
Wed Aug 1 09:10:40 2007 UTC (7 years, 1 month ago) by ph10
File size: 5235 byte(s)
Correct errors in previous patch; tidy for test release.

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>
41 #include <vector>
42
43 #include <pcrecpp.h>
44 #include <pcre_stringpiece.h>
45 #include <pcre_scanner.h>
46
47 #ifdef _WIN32
48 # define snprintf _snprintf
49 #endif
50
51 #define FLAGS_unittest_stack_size 49152
52
53 // Dies with a fatal error if the two values are not equal.
54 #define CHECK_EQ(a, b) do { \
55 if ( (a) != (b) ) { \
56 fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
57 __FILE__, __LINE__, #a, #b); \
58 exit(1); \
59 } \
60 } while (0)
61
62 using std::vector;
63 using pcrecpp::StringPiece;
64 using pcrecpp::Scanner;
65
66 static void TestScanner() {
67 const char input[] = "\n"
68 "alpha = 1; // this sets alpha\n"
69 "bravo = 2; // bravo is set here\n"
70 "gamma = 33; /* and here is gamma */\n";
71
72 const char *re = "(\\w+) = (\\d+);";
73
74 Scanner s(input);
75 string var;
76 int number;
77 s.SkipCXXComments();
78 s.set_save_comments(true);
79 vector<StringPiece> comments;
80
81 s.Consume(re, &var, &number);
82 CHECK_EQ(var, "alpha");
83 CHECK_EQ(number, 1);
84 CHECK_EQ(s.LineNumber(), 3);
85 s.GetNextComments(&comments);
86 CHECK_EQ(comments.size(), 1);
87 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
88 comments.resize(0);
89
90 s.Consume(re, &var, &number);
91 CHECK_EQ(var, "bravo");
92 CHECK_EQ(number, 2);
93 s.GetNextComments(&comments);
94 CHECK_EQ(comments.size(), 1);
95 CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
96 comments.resize(0);
97
98 s.Consume(re, &var, &number);
99 CHECK_EQ(var, "gamma");
100 CHECK_EQ(number, 33);
101 s.GetNextComments(&comments);
102 CHECK_EQ(comments.size(), 1);
103 CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
104 comments.resize(0);
105
106 s.GetComments(0, sizeof(input), &comments);
107 CHECK_EQ(comments.size(), 3);
108 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
109 CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
110 CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
111 comments.resize(0);
112
113 s.GetComments(0, strchr(input, '/') - input, &comments);
114 CHECK_EQ(comments.size(), 0);
115 comments.resize(0);
116
117 s.GetComments(strchr(input, '/') - input - 1, sizeof(input),
118 &comments);
119 CHECK_EQ(comments.size(), 3);
120 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
121 CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
122 CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
123 comments.resize(0);
124
125 s.GetComments(strchr(input, '/') - input - 1,
126 strchr(input + 1, '\n') - input + 1, &comments);
127 CHECK_EQ(comments.size(), 1);
128 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
129 comments.resize(0);
130 }
131
132 static void TestBigComment() {
133 string input;
134 for (int i = 0; i < 1024; ++i) {
135 char buf[1024];
136 snprintf(buf, sizeof(buf), " # Comment %d\n", i);
137 input += buf;
138 }
139 input += "name = value;\n";
140
141 Scanner s(input.c_str());
142 s.SetSkipExpression("\\s+|#.*\n");
143
144 string name;
145 string value;
146 s.Consume("(\\w+) = (\\w+);", &name, &value);
147 CHECK_EQ(name, "name");
148 CHECK_EQ(value, "value");
149 }
150
151 // TODO: also test scanner and big-comment in a thread with a
152 // small stack size
153
154 int main(int argc, char** argv) {
155 TestScanner();
156 TestBigComment();
157
158 // Done
159 printf("OK\n");
160
161 return 0;
162 }

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12