/[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 144 - (hide annotations) (download)
Mon Apr 2 13:32:07 2007 UTC (7 years, 3 months ago) by ph10
File size: 5164 byte(s)
Daniel's patches to add to the CMake support.

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

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