00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "RegExp.H"
00022
00023 using std::cout;
00024 using std::cerr;
00025 using std::endl;
00026
00027 int main(void)
00028 {
00029 try
00030 {
00031 Basics::RegExp aStarb("a*b");
00032
00033 if(!aStarb.match("b"))
00034 {
00035 cerr << "Regular expression \"a*b\" didn't match \"b\"" << endl;
00036 return 1;
00037 }
00038
00039 if(!aStarb.match("ab"))
00040 {
00041 cerr << "Regular expression \"a*b\" didn't match \"ab\"" << endl;
00042 return 1;
00043 }
00044
00045 if(!aStarb.match("aaaaab"))
00046 {
00047 cerr << "Regular expression \"a*b\" didn't match \"aaaaab\"" << endl;
00048 return 1;
00049 }
00050
00051 if(aStarb.match("aa"))
00052 {
00053 cerr << "Regular expression \"a*b\" did match \"aa\"" << endl;
00054 return 1;
00055 }
00056
00057
00058 Basics::RegExp bStarc("b*c", REG_EXTENDED | REG_ICASE);
00059
00060
00061 Text orig("----aabbbbc----");
00062 regmatch_t match;
00063 if(bStarc.match(orig, 1, &match))
00064 {
00065
00066 Text match_text = orig.Sub(match.rm_so, (match.rm_eo - match.rm_so));
00067
00068 if(match_text != "bbbbc")
00069 {
00070 cerr << "Regular expression \"b*c\" matched chars "
00071 << match.rm_so << "-" << match.rm_eo
00072 << " in \"" << orig
00073 << "\" (\"" << match_text
00074 << "\"), which isn't what we expected (\"bbbbc\")"
00075 << endl;
00076 return 1;
00077
00078 }
00079
00080
00081 if(!bStarc.match(match_text))
00082 {
00083 cerr << "Regular expression \"b*c\" matched chars "
00084 << match.rm_so << "-" << match.rm_eo
00085 << " in \"" << orig
00086 << "\", but then didn't match the matched text (\""
00087 << match_text << "\")" << endl;
00088 return 1;
00089 }
00090 }
00091 else
00092 {
00093 cerr << "Regular expression \"b*c\" didn't match \"" << orig << "\""
00094 << endl;
00095 return 1;
00096 }
00097 }
00098 catch(const Basics::RegExp::ParseError &err)
00099 {
00100 cerr << "Error parsing redular expression \"" << err.re << "\": "
00101 << err.msg << " (error code = " << err.code << ")" << endl;
00102 return 1;
00103 }
00104
00105 cout << "All tests passed!" << endl;
00106 return 0;
00107 }