Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

TestRegExp.C

Go to the documentation of this file.
00001 // Copyright (C) 2003, Kenneth C. Schalk
00002 
00003 // This file is part of Vesta.
00004 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Thu May 27 00:58:09 EDT 2004 by ken@xorian.net        
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       // We want substring matches, so we pass non-default cflags
00058       Basics::RegExp bStarc("b*c", REG_EXTENDED | REG_ICASE);
00059 
00060       // Match and get information about the match
00061       Text orig("----aabbbbc----");
00062       regmatch_t match;
00063       if(bStarc.match(orig, 1, &match))
00064         {
00065           // Extract the matched text
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           // Match the matched text, which better work.
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 }

Generated on Mon May 8 00:48:29 2006 for Vesta by  doxygen 1.4.2