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

Diff of /code/trunk/pcrecpp_unittest.cc

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

revision 91 by nigel, Sat Feb 24 21:41:34 2007 UTC revision 200 by ph10, Wed Aug 1 09:10:40 2007 UTC
# Line 1  Line 1 
1  // Copyright (c) 2005, Google Inc.  // -*- coding: utf-8 -*-
2    //
3    // Copyright (c) 2005 - 2006, Google Inc.
4  // All rights reserved.  // All rights reserved.
5  //  //
6  // Redistribution and use in source and binary forms, with or without  // Redistribution and use in source and binary forms, with or without
# Line 31  Line 33 
33  //  //
34  // TODO: Test extractions for PartialMatch/Consume  // TODO: Test extractions for PartialMatch/Consume
35    
36    #ifdef HAVE_CONFIG_H
37    #include <config.h>
38    #endif
39    
40  #include <stdio.h>  #include <stdio.h>
41  #include <cassert>  #include <cassert>
42  #include <vector>  #include <vector>
 #include "config.h"  
43  #include "pcrecpp.h"  #include "pcrecpp.h"
44    
45  using pcrecpp::StringPiece;  using pcrecpp::StringPiece;
# Line 445  static void TestRecursion() { Line 450  static void TestRecursion() {
450    CHECK(re4.FullMatch(text_bad) == false);    CHECK(re4.FullMatch(text_bad) == false);
451  }  }
452    
453    // A meta-quoted string, interpreted as a pattern, should always match
454    // the original unquoted string.
455    static void TestQuoteMeta(string unquoted, RE_Options options = RE_Options()) {
456      string quoted = RE::QuoteMeta(unquoted);
457      RE re(quoted, options);
458      CHECK(re.FullMatch(unquoted));
459    }
460    
461    // A string containing meaningful regexp characters, which is then meta-
462    // quoted, should not generally match a string the unquoted string does.
463    static void NegativeTestQuoteMeta(string unquoted, string should_not_match,
464                                      RE_Options options = RE_Options()) {
465      string quoted = RE::QuoteMeta(unquoted);
466      RE re(quoted, options);
467      CHECK(!re.FullMatch(should_not_match));
468    }
469    
470    // Tests that quoted meta characters match their original strings,
471    // and that a few things that shouldn't match indeed do not.
472    static void TestQuotaMetaSimple() {
473      TestQuoteMeta("foo");
474      TestQuoteMeta("foo.bar");
475      TestQuoteMeta("foo\\.bar");
476      TestQuoteMeta("[1-9]");
477      TestQuoteMeta("1.5-2.0?");
478      TestQuoteMeta("\\d");
479      TestQuoteMeta("Who doesn't like ice cream?");
480      TestQuoteMeta("((a|b)c?d*e+[f-h]i)");
481      TestQuoteMeta("((?!)xxx).*yyy");
482      TestQuoteMeta("([");
483    }
484    
485    static void TestQuoteMetaSimpleNegative() {
486      NegativeTestQuoteMeta("foo", "bar");
487      NegativeTestQuoteMeta("...", "bar");
488      NegativeTestQuoteMeta("\\.", ".");
489      NegativeTestQuoteMeta("\\.", "..");
490      NegativeTestQuoteMeta("(a)", "a");
491      NegativeTestQuoteMeta("(a|b)", "a");
492      NegativeTestQuoteMeta("(a|b)", "(a)");
493      NegativeTestQuoteMeta("(a|b)", "a|b");
494      NegativeTestQuoteMeta("[0-9]", "0");
495      NegativeTestQuoteMeta("[0-9]", "0-9");
496      NegativeTestQuoteMeta("[0-9]", "[9]");
497      NegativeTestQuoteMeta("((?!)xxx)", "xxx");
498    }
499    
500    static void TestQuoteMetaLatin1() {
501      TestQuoteMeta("3\xb2 = 9");
502    }
503    
504    static void TestQuoteMetaUtf8() {
505    #ifdef SUPPORT_UTF8
506      TestQuoteMeta("Pl\xc3\xa1\x63ido Domingo", pcrecpp::UTF8());
507      TestQuoteMeta("xyz", pcrecpp::UTF8());            // No fancy utf8
508      TestQuoteMeta("\xc2\xb0", pcrecpp::UTF8());       // 2-byte utf8 (degree symbol)
509      TestQuoteMeta("27\xc2\xb0 degrees", pcrecpp::UTF8());  // As a middle character
510      TestQuoteMeta("\xe2\x80\xb3", pcrecpp::UTF8());   // 3-byte utf8 (double prime)
511      TestQuoteMeta("\xf0\x9d\x85\x9f", pcrecpp::UTF8()); // 4-byte utf8 (music note)
512      TestQuoteMeta("27\xc2\xb0"); // Interpreted as Latin-1, but should still work
513      NegativeTestQuoteMeta("27\xc2\xb0",               // 2-byte utf (degree symbol)
514                            "27\\\xc2\\\xb0",
515                            pcrecpp::UTF8());
516    #endif
517    }
518    
519    static void TestQuoteMetaAll() {
520      printf("Testing QuoteMeta\n");
521      TestQuotaMetaSimple();
522      TestQuoteMetaSimpleNegative();
523      TestQuoteMetaLatin1();
524      TestQuoteMetaUtf8();
525    }
526    
527  //  //
528  // Options tests contributed by  // Options tests contributed by
529  // Giuseppe Maxia, CTO, Stardata s.r.l.  // Giuseppe Maxia, CTO, Stardata s.r.l.
# Line 667  static void TestOptions() { Line 746  static void TestOptions() {
746    Test_all_options();    Test_all_options();
747  }  }
748    
749    static void TestConstructors() {
750      printf("Testing constructors\n");
751    
752      RE_Options options;
753      options.set_dotall(true);
754      const char *str = "HELLO\n" "cruel\n" "world";
755    
756      RE orig("HELLO.*world", options);
757      CHECK(orig.FullMatch(str));
758    
759      RE copy1(orig);
760      CHECK(copy1.FullMatch(str));
761    
762      RE copy2("not a match");
763      CHECK(!copy2.FullMatch(str));
764      copy2 = copy1;
765      CHECK(copy2.FullMatch(str));
766      copy2 = orig;
767      CHECK(copy2.FullMatch(str));
768    
769      // Make sure when we assign to ourselves, nothing bad happens
770      orig = orig;
771      copy1 = copy1;
772      copy2 = copy2;
773      CHECK(orig.FullMatch(str));
774      CHECK(copy1.FullMatch(str));
775      CHECK(copy2.FullMatch(str));
776    }
777    
778  int main(int argc, char** argv) {  int main(int argc, char** argv) {
779    // Treat any flag as --help    // Treat any flag as --help
780    if (argc > 1 && argv[1][0] == '-') {    if (argc > 1 && argv[1][0] == '-') {
# Line 701  int main(int argc, char** argv) { Line 809  int main(int argc, char** argv) {
809    /***** FullMatch with no args *****/    /***** FullMatch with no args *****/
810    
811    CHECK(RE("h.*o").FullMatch("hello"));    CHECK(RE("h.*o").FullMatch("hello"));
812    CHECK(!RE("h.*o").FullMatch("othello"));    CHECK(!RE("h.*o").FullMatch("othello"));     // Must be anchored at front
813    CHECK(!RE("h.*o").FullMatch("hello!"));    CHECK(!RE("h.*o").FullMatch("hello!"));      // Must be anchored at end
814      CHECK(RE("a*").FullMatch("aaaa"));           // Fullmatch with normal op
815      CHECK(RE("a*?").FullMatch("aaaa"));          // Fullmatch with nongreedy op
816      CHECK(RE("a*?\\z").FullMatch("aaaa"));       // Two unusual ops
817    
818    /***** FullMatch with args *****/    /***** FullMatch with args *****/
819    
# Line 797  int main(int argc, char** argv) { Line 908  int main(int argc, char** argv) {
908      CHECK(!RE("(\\d+)").FullMatch("4294967296", &v));      CHECK(!RE("(\\d+)").FullMatch("4294967296", &v));
909    }    }
910  #ifdef HAVE_LONG_LONG  #ifdef HAVE_LONG_LONG
911    # if defined(__MINGW__) || defined(__MINGW32__)
912    #   define LLD "%I64d"
913    # else
914    #   define LLD "%lld"
915    # endif
916    {    {
917      long long v;      long long v;
918      static const long long max_value = 0x7fffffffffffffffLL;      static const long long max_value = 0x7fffffffffffffffLL;
# Line 806  int main(int argc, char** argv) { Line 922  int main(int argc, char** argv) {
922      CHECK(RE("(-?\\d+)").FullMatch("100", &v)); CHECK_EQ(v, 100);      CHECK(RE("(-?\\d+)").FullMatch("100", &v)); CHECK_EQ(v, 100);
923      CHECK(RE("(-?\\d+)").FullMatch("-100",&v)); CHECK_EQ(v, -100);      CHECK(RE("(-?\\d+)").FullMatch("-100",&v)); CHECK_EQ(v, -100);
924    
925      snprintf(buf, sizeof(buf), "%lld", max_value);      snprintf(buf, sizeof(buf), LLD, max_value);
926      CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value);      CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, max_value);
927    
928      snprintf(buf, sizeof(buf), "%lld", min_value);      snprintf(buf, sizeof(buf), LLD, min_value);
929      CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, min_value);      CHECK(RE("(-?\\d+)").FullMatch(buf,&v)); CHECK_EQ(v, min_value);
930    
931      snprintf(buf, sizeof(buf), "%lld", max_value);      snprintf(buf, sizeof(buf), LLD, max_value);
932      assert(buf[strlen(buf)-1] != '9');      assert(buf[strlen(buf)-1] != '9');
933      buf[strlen(buf)-1]++;      buf[strlen(buf)-1]++;
934      CHECK(!RE("(-?\\d+)").FullMatch(buf, &v));      CHECK(!RE("(-?\\d+)").FullMatch(buf, &v));
935    
936      snprintf(buf, sizeof(buf), "%lld", min_value);      snprintf(buf, sizeof(buf), LLD, min_value);
937      assert(buf[strlen(buf)-1] != '9');      assert(buf[strlen(buf)-1] != '9');
938      buf[strlen(buf)-1]++;      buf[strlen(buf)-1]++;
939      CHECK(!RE("(-?\\d+)").FullMatch(buf, &v));      CHECK(!RE("(-?\\d+)").FullMatch(buf, &v));
# Line 985  int main(int argc, char** argv) { Line 1101  int main(int argc, char** argv) {
1101    CHECK(RE("h.*o").PartialMatch("hello!"));    CHECK(RE("h.*o").PartialMatch("hello!"));
1102    CHECK(RE("((((((((((((((((((((x))))))))))))))))))))").PartialMatch("x"));    CHECK(RE("((((((((((((((((((((x))))))))))))))))))))").PartialMatch("x"));
1103    
1104      /***** other tests *****/
1105    
1106    RadixTests();    RadixTests();
1107    TestReplace();    TestReplace();
1108    TestExtract();    TestExtract();
1109    TestConsume();    TestConsume();
1110    TestFindAndConsume();    TestFindAndConsume();
1111      TestQuoteMetaAll();
1112    TestMatchNumberPeculiarity();    TestMatchNumberPeculiarity();
1113    
1114    // Check the pattern() accessor    // Check the pattern() accessor
# Line 1109  int main(int argc, char** argv) { Line 1228  int main(int argc, char** argv) {
1228      VERBOSE_TEST  = true;      VERBOSE_TEST  = true;
1229    TestOptions();    TestOptions();
1230    
1231      // Test the constructors
1232      TestConstructors();
1233    
1234    // Done    // Done
1235    printf("OK\n");    printf("OK\n");
1236    

Legend:
Removed from v.91  
changed lines
  Added in v.200

  ViewVC Help
Powered by ViewVC 1.1.5