/[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 77 - (show annotations) (download)
Sat Feb 24 21:40:45 2007 UTC (7 years, 9 months ago) by nigel
File size: 4498 byte(s)
Load pcre-6.0 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 s.GetNextComments(&comments);
72 CHECK_EQ(comments.size(), 1);
73 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
74 comments.resize(0);
75
76 s.Consume(re, &var, &number);
77 CHECK_EQ(var, "bravo");
78 CHECK_EQ(number, 2);
79 s.GetNextComments(&comments);
80 CHECK_EQ(comments.size(), 1);
81 CHECK_EQ(comments[0].as_string(), " // bravo is set here\n");
82 comments.resize(0);
83
84 s.Consume(re, &var, &number);
85 CHECK_EQ(var, "gamma");
86 CHECK_EQ(number, 33);
87 s.GetNextComments(&comments);
88 CHECK_EQ(comments.size(), 1);
89 CHECK_EQ(comments[0].as_string(), " /* and here is gamma */\n");
90 comments.resize(0);
91
92 s.GetComments(0, sizeof(input), &comments);
93 CHECK_EQ(comments.size(), 3);
94 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
95 CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
96 CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
97 comments.resize(0);
98
99 s.GetComments(0, strchr(input, '/') - input, &comments);
100 CHECK_EQ(comments.size(), 0);
101 comments.resize(0);
102
103 s.GetComments(strchr(input, '/') - input - 1, sizeof(input),
104 &comments);
105 CHECK_EQ(comments.size(), 3);
106 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
107 CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
108 CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
109 comments.resize(0);
110
111 s.GetComments(strchr(input, '/') - input - 1,
112 strchr(input + 1, '\n') - input + 1, &comments);
113 CHECK_EQ(comments.size(), 1);
114 CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
115 comments.resize(0);
116 }
117
118 int main(int argc, char** argv) {
119 TestScanner();
120
121 // Done
122 printf("OK\n");
123
124 return 0;
125 }

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12