📄 pcrecpp_unittest.cc
字号:
// Copyright (c) 2005, Google Inc.// All rights reserved.//// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are// met://// * Redistributions of source code must retain the above copyright// notice, this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above// copyright notice, this list of conditions and the following disclaimer// in the documentation and/or other materials provided with the// distribution.// * Neither the name of Google Inc. nor the names of its// contributors may be used to endorse or promote products derived from// this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//// Author: Sanjay Ghemawat//// TODO: Test extractions for PartialMatch/Consume#include <stdio.h>#include <vector>#include "config.h"#include "pcrecpp.h"using pcrecpp::StringPiece;using pcrecpp::RE;using pcrecpp::RE_Options;using pcrecpp::Hex;using pcrecpp::Octal;using pcrecpp::CRadix;static bool VERBOSE_TEST = false;// CHECK dies with a fatal error if condition is not true. It is *not*// controlled by NDEBUG, so the check will be executed regardless of// compilation mode. Therefore, it is safe to do things like:// CHECK_EQ(fp->Write(x), 4)#define CHECK(condition) do { \ if (!(condition)) { \ fprintf(stderr, "%s:%d: Check failed: %s\n", \ __FILE__, __LINE__, #condition); \ exit(1); \ } \} while (0)#define CHECK_EQ(a, b) CHECK(a == b)static void Timing1(int num_iters) { // Same pattern lots of times RE pattern("ruby:\\d+"); StringPiece p("ruby:1234"); for (int j = num_iters; j > 0; j--) { CHECK(pattern.FullMatch(p)); }}static void Timing2(int num_iters) { // Same pattern lots of times RE pattern("ruby:(\\d+)"); int i; for (int j = num_iters; j > 0; j--) { CHECK(pattern.FullMatch("ruby:1234", &i)); CHECK_EQ(i, 1234); }}static void Timing3(int num_iters) { string text_string; for (int j = num_iters; j > 0; j--) { text_string += "this is another line\n"; } RE line_matcher(".*\n"); string line; StringPiece text(text_string); int counter = 0; while (line_matcher.Consume(&text)) { counter++; } printf("Matched %d lines\n", counter);}#if 0 // uncomment this if you have a way of defining VirtualProcessSize()static void LeakTest() { // Check for memory leaks unsigned long long initial_size = 0; for (int i = 0; i < 100000; i++) { if (i == 50000) { initial_size = VirtualProcessSize(); printf("Size after 50000: %llu\n", initial_size); } char buf[100]; snprintf(buf, sizeof(buf), "pat%09d", i); RE newre(buf); } uint64 final_size = VirtualProcessSize(); printf("Size after 100000: %llu\n", final_size); const double growth = double(final_size - initial_size) / final_size; printf("Growth: %0.2f%%", growth * 100); CHECK(growth < 0.02); // Allow < 2% growth}#endifstatic void RadixTests() { printf("Testing hex\n");#define CHECK_HEX(type, value) \ do { \ type v; \ CHECK(RE("([0-9a-fA-F]+)[uUlL]*").FullMatch(#value, Hex(&v))); \ CHECK_EQ(v, 0x ## value); \ CHECK(RE("([0-9a-fA-FxX]+)[uUlL]*").FullMatch("0x" #value, CRadix(&v))); \ CHECK_EQ(v, 0x ## value); \ } while(0) CHECK_HEX(short, 2bad); CHECK_HEX(unsigned short, 2badU); CHECK_HEX(int, dead); CHECK_HEX(unsigned int, deadU); CHECK_HEX(long, 7eadbeefL); CHECK_HEX(unsigned long, deadbeefUL);#ifdef HAVE_LONG_LONG CHECK_HEX(long long, 12345678deadbeefLL);#endif#ifdef HAVE_UNSIGNED_LONG_LONG CHECK_HEX(unsigned long long, cafebabedeadbeefULL);#endif#undef CHECK_HEX printf("Testing octal\n");#define CHECK_OCTAL(type, value) \ do { \ type v; \ CHECK(RE("([0-7]+)[uUlL]*").FullMatch(#value, Octal(&v))); \ CHECK_EQ(v, 0 ## value); \ CHECK(RE("([0-9a-fA-FxX]+)[uUlL]*").FullMatch("0" #value, CRadix(&v))); \ CHECK_EQ(v, 0 ## value); \ } while(0) CHECK_OCTAL(short, 77777); CHECK_OCTAL(unsigned short, 177777U); CHECK_OCTAL(int, 17777777777); CHECK_OCTAL(unsigned int, 37777777777U); CHECK_OCTAL(long, 17777777777L); CHECK_OCTAL(unsigned long, 37777777777UL);#ifdef HAVE_LONG_LONG CHECK_OCTAL(long long, 777777777777777777777LL);#endif#ifdef HAVE_UNSIGNED_LONG_LONG CHECK_OCTAL(unsigned long long, 1777777777777777777777ULL);#endif#undef CHECK_OCTAL printf("Testing decimal\n");#define CHECK_DECIMAL(type, value) \ do { \ type v; \ CHECK(RE("(-?[0-9]+)[uUlL]*").FullMatch(#value, &v)); \ CHECK_EQ(v, value); \ CHECK(RE("(-?[0-9a-fA-FxX]+)[uUlL]*").FullMatch(#value, CRadix(&v))); \ CHECK_EQ(v, value); \ } while(0) CHECK_DECIMAL(short, -1); CHECK_DECIMAL(unsigned short, 9999); CHECK_DECIMAL(int, -1000); CHECK_DECIMAL(unsigned int, 12345U); CHECK_DECIMAL(long, -10000000L); CHECK_DECIMAL(unsigned long, 3083324652U);#ifdef HAVE_LONG_LONG CHECK_DECIMAL(long long, -100000000000000LL);#endif#ifdef HAVE_UNSIGNED_LONG_LONG CHECK_DECIMAL(unsigned long long, 1234567890987654321ULL);#endif#undef CHECK_DECIMAL}static void TestReplace() { printf("Testing Replace\n"); struct ReplaceTest { const char *regexp; const char *rewrite; const char *original; const char *single; const char *global; }; static const ReplaceTest tests[] = { { "(qu|[b-df-hj-np-tv-z]*)([a-z]+)", "\\2\\1ay", "the quick brown fox jumps over the lazy dogs.", "ethay quick brown fox jumps over the lazy dogs.", "ethay ickquay ownbray oxfay umpsjay overay ethay azylay ogsday." }, { "\\w+", "\\0-NOSPAM", "paul.haahr@google.com", "paul-NOSPAM.haahr@google.com", "paul-NOSPAM.haahr-NOSPAM@google-NOSPAM.com-NOSPAM" }, { "^", "(START)", "foo", "(START)foo", "(START)foo" }, { "^", "(START)", "", "(START)", "(START)" }, { "$", "(END)", "", "(END)", "(END)" }, { "b", "bb", "ababababab", "abbabababab", "abbabbabbabbabb" }, { "b", "bb", "bbbbbb", "bbbbbbb", "bbbbbbbbbbbb" }, { "b+", "bb", "bbbbbb", "bb", "bb" }, { "b*", "bb", "bbbbbb", "bb", "bb" }, { "b*", "bb", "aaaaa", "bbaaaaa", "bbabbabbabbabbabb" }, { "", NULL, NULL, NULL, NULL } }; for (const ReplaceTest *t = tests; t->original != NULL; ++t) { string one(t->original); CHECK(RE(t->regexp).Replace(t->rewrite, &one)); CHECK_EQ(one, t->single); string all(t->original); CHECK(RE(t->regexp).GlobalReplace(t->rewrite, &all) > 0); CHECK_EQ(all, t->global); }}static void TestExtract() { printf("Testing Extract\n"); string s; CHECK(RE("(.*)@([^.]*)").Extract("\\2!\\1", "boris@kremvax.ru", &s)); CHECK_EQ(s, "kremvax!boris"); // check the RE interface as well CHECK(RE(".*").Extract("'\\0'", "foo", &s)); CHECK_EQ(s, "'foo'"); CHECK(!RE("bar").Extract("'\\0'", "baz", &s)); CHECK_EQ(s, "'foo'");}static void TestConsume() { printf("Testing Consume\n"); string word; string s(" aaa b!@#$@#$cccc"); StringPiece input(s); RE r("\\s*(\\w+)"); // matches a word, possibly proceeded by whitespace CHECK(r.Consume(&input, &word)); CHECK_EQ(word, "aaa"); CHECK(r.Consume(&input, &word)); CHECK_EQ(word, "b"); CHECK(! r.Consume(&input, &word));}static void TestFindAndConsume() { printf("Testing FindAndConsume\n"); string word; string s(" aaa b!@#$@#$cccc"); StringPiece input(s); RE r("(\\w+)"); // matches a word CHECK(r.FindAndConsume(&input, &word)); CHECK_EQ(word, "aaa"); CHECK(r.FindAndConsume(&input, &word)); CHECK_EQ(word, "b"); CHECK(r.FindAndConsume(&input, &word)); CHECK_EQ(word, "cccc"); CHECK(! r.FindAndConsume(&input, &word));}static void TestMatchNumberPeculiarity() { printf("Testing match-number peculiaraity\n"); string word1; string word2; string word3; RE r("(foo)|(bar)|(baz)"); CHECK(r.PartialMatch("foo", &word1, &word2, &word3)); CHECK_EQ(word1, "foo"); CHECK_EQ(word2, ""); CHECK_EQ(word3, ""); CHECK(r.PartialMatch("bar", &word1, &word2, &word3)); CHECK_EQ(word1, ""); CHECK_EQ(word2, "bar"); CHECK_EQ(word3, ""); CHECK(r.PartialMatch("baz", &word1, &word2, &word3)); CHECK_EQ(word1, ""); CHECK_EQ(word2, ""); CHECK_EQ(word3, "baz"); CHECK(!r.PartialMatch("f", &word1, &word2, &word3)); string a; CHECK(RE("(foo)|hello").FullMatch("hello", &a)); CHECK_EQ(a, "");}static void TestRecursion() { printf("Testing recursion\n"); // Get one string that passes (sometimes), one that never does.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -