/[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 1502 - (hide annotations) (download)
Mon Sep 15 13:56:18 2014 UTC (6 days, 22 hours ago) by ph10
File size: 5294 byte(s)
Files tidied for 8.36-RC1.

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 ph10 200 #ifdef HAVE_CONFIG_H
36 ph10 236 #include "config.h"
37 ph10 200 #endif
38 ph10 199
39 nigel 77 #include <stdio.h>
40 ph10 594 #include <string.h> /* for strchr */
41 nigel 93 #include <string>
42 nigel 77 #include <vector>
43 ph10 199
44 ph10 236 #include "pcrecpp.h"
45     #include "pcre_stringpiece.h"
46     #include "pcre_scanner.h"
47 nigel 77
48 nigel 93 #define FLAGS_unittest_stack_size 49152
49    
50 nigel 77 // 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 pcrecpp::StringPiece;
61     using pcrecpp::Scanner;
62    
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";
68    
69     const char *re = "(\\w+) = (\\d+);";
70    
71     Scanner s(input);
72     string var;
73     int number;
74     s.SkipCXXComments();
75     s.set_save_comments(true);
76     vector<StringPiece> comments;
77    
78     s.Consume(re, &var, &number);
79     CHECK_EQ(var, "alpha");
80     CHECK_EQ(number, 1);
81 nigel 87 CHECK_EQ(s.LineNumber(), 3);
82 nigel 77 s.GetNextComments(&comments);
83     CHECK_EQ(comments.size(), 1);
84     CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
85     comments.resize(0);
86    
87     s.Consume(re, &var, &number);
88     CHECK_EQ(var, "bravo");
89     CHECK_EQ(number, 2);
90     s.GetNextComments(&comments);
91     CHECK_EQ(comments.size(), 1);
92     CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
93     comments.resize(0);
94    
95     s.Consume(re, &var, &number);
96     CHECK_EQ(var, "gamma");
97     CHECK_EQ(number, 33);
98     s.GetNextComments(&comments);
99     CHECK_EQ(comments.size(), 1);
100     CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
101     comments.resize(0);
102    
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");
108     comments.resize(0);
109    
110 ph10 530 s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
111 nigel 77 CHECK_EQ(comments.size(), 0);
112     comments.resize(0);
113    
114 ph10 530 s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
115 nigel 77 &comments);
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");
120     comments.resize(0);
121    
122 ph10 530 s.GetComments((int)(strchr(input, '/') - input - 1),
123     (int)(strchr(input + 1, '\n') - input + 1), &comments);
124 nigel 77 CHECK_EQ(comments.size(), 1);
125     CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
126     comments.resize(0);
127     }
128    
129 nigel 93 static void TestBigComment() {
130     string input;
131     for (int i = 0; i < 1024; ++i) {
132 ph10 257 char buf[1024]; // definitely big enough
133     sprintf(buf, " # Comment %d\n", i);
134 nigel 93 input += buf;
135     }
136     input += "name = value;\n";
137    
138     Scanner s(input.c_str());
139     s.SetSkipExpression("\\s+|#.*\n");
140    
141     string name;
142     string value;
143     s.Consume("(\\w+) = (\\w+);", &name, &value);
144     CHECK_EQ(name, "name");
145     CHECK_EQ(value, "value");
146     }
147    
148     // TODO: also test scanner and big-comment in a thread with a
149     // small stack size
150    
151 nigel 77 int main(int argc, char** argv) {
152 ph10 1472 (void)argc;
153 ph10 1502 (void)argv;
154 nigel 77 TestScanner();
155 nigel 93 TestBigComment();
156 nigel 77
157     // Done
158     printf("OK\n");
159    
160     return 0;
161     }

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