| 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> |
| 41 |
#include <vector> |
#include <vector> |
| 42 |
|
|
| 43 |
|
#include <pcrecpp.h> |
| 44 |
#include <pcre_stringpiece.h> |
#include <pcre_stringpiece.h> |
| 45 |
#include <pcre_scanner.h> |
#include <pcre_scanner.h> |
| 46 |
|
|
| 47 |
|
#ifdef _WIN32 |
| 48 |
|
# define snprintf _snprintf |
| 49 |
|
#endif |
| 50 |
|
|
| 51 |
|
#define FLAGS_unittest_stack_size 49152 |
| 52 |
|
|
| 53 |
// Dies with a fatal error if the two values are not equal. |
// Dies with a fatal error if the two values are not equal. |
| 54 |
#define CHECK_EQ(a, b) do { \ |
#define CHECK_EQ(a, b) do { \ |
| 55 |
if ( (a) != (b) ) { \ |
if ( (a) != (b) ) { \ |
| 81 |
s.Consume(re, &var, &number); |
s.Consume(re, &var, &number); |
| 82 |
CHECK_EQ(var, "alpha"); |
CHECK_EQ(var, "alpha"); |
| 83 |
CHECK_EQ(number, 1); |
CHECK_EQ(number, 1); |
| 84 |
|
CHECK_EQ(s.LineNumber(), 3); |
| 85 |
s.GetNextComments(&comments); |
s.GetNextComments(&comments); |
| 86 |
CHECK_EQ(comments.size(), 1); |
CHECK_EQ(comments.size(), 1); |
| 87 |
CHECK_EQ(comments[0].as_string(), " // this sets alpha\n"); |
CHECK_EQ(comments[0].as_string(), " // this sets alpha\n"); |
| 129 |
comments.resize(0); |
comments.resize(0); |
| 130 |
} |
} |
| 131 |
|
|
| 132 |
|
static void TestBigComment() { |
| 133 |
|
string input; |
| 134 |
|
for (int i = 0; i < 1024; ++i) { |
| 135 |
|
char buf[1024]; |
| 136 |
|
snprintf(buf, sizeof(buf), " # Comment %d\n", i); |
| 137 |
|
input += buf; |
| 138 |
|
} |
| 139 |
|
input += "name = value;\n"; |
| 140 |
|
|
| 141 |
|
Scanner s(input.c_str()); |
| 142 |
|
s.SetSkipExpression("\\s+|#.*\n"); |
| 143 |
|
|
| 144 |
|
string name; |
| 145 |
|
string value; |
| 146 |
|
s.Consume("(\\w+) = (\\w+);", &name, &value); |
| 147 |
|
CHECK_EQ(name, "name"); |
| 148 |
|
CHECK_EQ(value, "value"); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
// TODO: also test scanner and big-comment in a thread with a |
| 152 |
|
// small stack size |
| 153 |
|
|
| 154 |
int main(int argc, char** argv) { |
int main(int argc, char** argv) { |
| 155 |
TestScanner(); |
TestScanner(); |
| 156 |
|
TestBigComment(); |
| 157 |
|
|
| 158 |
// Done |
// Done |
| 159 |
printf("OK\n"); |
printf("OK\n"); |