/[pcre]/code/trunk/pcre_scanner_unittest.cc
ViewVC logotype

Diff of /code/trunk/pcre_scanner_unittest.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 77 by nigel, Sat Feb 24 21:40:45 2007 UTC revision 594 by ph10, Sun May 1 08:22:12 2011 UTC
# Line 32  Line 32 
32  // Unittest for scanner, especially GetNextComments and GetComments()  // Unittest for scanner, especially GetNextComments and GetComments()
33  // functionality.  // functionality.
34    
35    #ifdef HAVE_CONFIG_H
36    #include "config.h"
37    #endif
38    
39  #include <stdio.h>  #include <stdio.h>
40    #include <string.h>      /* for strchr */
41    #include <string>
42  #include <vector>  #include <vector>
43  #include <pcre_stringpiece.h>  
44  #include <pcre_scanner.h>  #include "pcrecpp.h"
45    #include "pcre_stringpiece.h"
46    #include "pcre_scanner.h"
47    
48    #define FLAGS_unittest_stack_size   49152
49    
50  // Dies with a fatal error if the two values are not equal.  // Dies with a fatal error if the two values are not equal.
51  #define CHECK_EQ(a, b)  do {                                    \  #define CHECK_EQ(a, b)  do {                                    \
# Line 68  static void TestScanner() { Line 78  static void TestScanner() {
78    s.Consume(re, &var, &number);    s.Consume(re, &var, &number);
79    CHECK_EQ(var, "alpha");    CHECK_EQ(var, "alpha");
80    CHECK_EQ(number, 1);    CHECK_EQ(number, 1);
81      CHECK_EQ(s.LineNumber(), 3);
82    s.GetNextComments(&comments);    s.GetNextComments(&comments);
83    CHECK_EQ(comments.size(), 1);    CHECK_EQ(comments.size(), 1);
84    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
# Line 96  static void TestScanner() { Line 107  static void TestScanner() {
107    CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");    CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
108    comments.resize(0);    comments.resize(0);
109    
110    s.GetComments(0, strchr(input, '/') - input, &comments);    s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
111    CHECK_EQ(comments.size(), 0);    CHECK_EQ(comments.size(), 0);
112    comments.resize(0);    comments.resize(0);
113    
114    s.GetComments(strchr(input, '/') - input - 1, sizeof(input),    s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
115                  &comments);                  &comments);
116    CHECK_EQ(comments.size(), 3);    CHECK_EQ(comments.size(), 3);
117    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
# Line 108  static void TestScanner() { Line 119  static void TestScanner() {
119    CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");    CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
120    comments.resize(0);    comments.resize(0);
121    
122    s.GetComments(strchr(input, '/') - input - 1,    s.GetComments((int)(strchr(input, '/') - input - 1),
123                  strchr(input + 1, '\n') - input + 1, &comments);                  (int)(strchr(input + 1, '\n') - input + 1), &comments);
124    CHECK_EQ(comments.size(), 1);    CHECK_EQ(comments.size(), 1);
125    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");    CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
126    comments.resize(0);    comments.resize(0);
127  }  }
128    
129    static void TestBigComment() {
130      string input;
131      for (int i = 0; i < 1024; ++i) {
132        char buf[1024];  // definitely big enough
133        sprintf(buf, "    # Comment %d\n", i);
134        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  int main(int argc, char** argv) {  int main(int argc, char** argv) {
152    TestScanner();    TestScanner();
153      TestBigComment();
154    
155    // Done    // Done
156    printf("OK\n");    printf("OK\n");

Legend:
Removed from v.77  
changed lines
  Added in v.594

  ViewVC Help
Powered by ViewVC 1.1.5