pcre_scanner_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include <stdio.h>
  38. #include <string.h> /* for strchr */
  39. #include <string>
  40. #include <vector>
  41. #include "pcrecpp.h"
  42. #include "pcre_stringpiece.h"
  43. #include "pcre_scanner.h"
  44. #define FLAGS_unittest_stack_size 49152
  45. // Dies with a fatal error if the two values are not equal.
  46. #define CHECK_EQ(a, b) do { \
  47. if ( (a) != (b) ) { \
  48. fprintf(stderr, "%s:%d: Check failed because %s != %s\n", \
  49. __FILE__, __LINE__, #a, #b); \
  50. exit(1); \
  51. } \
  52. } while (0)
  53. using std::vector;
  54. using std::string;
  55. using pcrecpp::StringPiece;
  56. using pcrecpp::Scanner;
  57. static void TestScanner() {
  58. const char input[] = "\n"
  59. "alpha = 1; // this sets alpha\n"
  60. "bravo = 2; // bravo is set here\n"
  61. "gamma = 33; /* and here is gamma */\n";
  62. const char *re = "(\\w+) = (\\d+);";
  63. Scanner s(input);
  64. string var;
  65. int number;
  66. s.SkipCXXComments();
  67. s.set_save_comments(true);
  68. vector<StringPiece> comments;
  69. s.Consume(re, &var, &number);
  70. CHECK_EQ(var, "alpha");
  71. CHECK_EQ(number, 1);
  72. CHECK_EQ(s.LineNumber(), 3);
  73. s.GetNextComments(&comments);
  74. CHECK_EQ(comments.size(), 1);
  75. CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
  76. comments.resize(0);
  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. 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. s.GetComments(0, sizeof(input), &comments);
  92. CHECK_EQ(comments.size(), 3);
  93. CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
  94. CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
  95. CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
  96. comments.resize(0);
  97. s.GetComments(0, (int)(strchr(input, '/') - input), &comments);
  98. CHECK_EQ(comments.size(), 0);
  99. comments.resize(0);
  100. s.GetComments((int)(strchr(input, '/') - input - 1), sizeof(input),
  101. &comments);
  102. CHECK_EQ(comments.size(), 3);
  103. CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
  104. CHECK_EQ(comments[1].as_string(), " // bravo is set here\n");
  105. CHECK_EQ(comments[2].as_string(), " /* and here is gamma */\n");
  106. comments.resize(0);
  107. s.GetComments((int)(strchr(input, '/') - input - 1),
  108. (int)(strchr(input + 1, '\n') - input + 1), &comments);
  109. CHECK_EQ(comments.size(), 1);
  110. CHECK_EQ(comments[0].as_string(), " // this sets alpha\n");
  111. comments.resize(0);
  112. }
  113. static void TestBigComment() {
  114. string input;
  115. for (int i = 0; i < 1024; ++i) {
  116. char buf[1024]; // definitely big enough
  117. sprintf(buf, " # Comment %d\n", i);
  118. input += buf;
  119. }
  120. input += "name = value;\n";
  121. Scanner s(input.c_str());
  122. s.SetSkipExpression("\\s+|#.*\n");
  123. string name;
  124. string value;
  125. s.Consume("(\\w+) = (\\w+);", &name, &value);
  126. CHECK_EQ(name, "name");
  127. CHECK_EQ(value, "value");
  128. }
  129. // TODO: also test scanner and big-comment in a thread with a
  130. // small stack size
  131. int main(int argc, char** argv) {
  132. (void)argc;
  133. (void)argv;
  134. TestScanner();
  135. TestBigComment();
  136. // Done
  137. printf("OK\n");
  138. return 0;
  139. }