/[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 93 - (hide annotations) (download)
Sat Feb 24 21:41:42 2007 UTC (7 years, 5 months ago) by nigel
File size: 5113 byte(s)
Load pcre-7.0 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12