/[pcre]/code/tags/pcre-8.10/pcre_scanner_unittest.cc
ViewVC logotype

Contents of /code/tags/pcre-8.10/pcre_scanner_unittest.cc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (show annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 8 months ago) by nigel
Original Path: code/trunk/pcre_scanner_unittest.cc
File size: 4529 byte(s)
Load pcre-6.5 into code/trunk.

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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12