• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

test/TestRegex.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/text/Regex.h>
00018 
00019 #ifdef DEBUG
00020 
00021 #include <spl/Log.h>
00022 
00023 static void _TestRegexIsMatch()
00024 {
00025         {
00026                 Regex rex("^-?\\d+(\\.\\d{2})?$");
00027 
00028                 DEBUG_CLEAR_MEM_CHECK_POINTS();
00029                 rex.CheckMem();
00030                 DEBUG_DUMP_MEM_LEAKS();
00031                 UNIT_ASSERT_MEM_NOTED("Regex 1.0");
00032 
00033                 UNIT_ASSERT("-42", rex.IsMatch("-42"));
00034                 UNIT_ASSERT("19.99", rex.IsMatch("19.99"));
00035                 UNIT_ASSERT("0.001", !rex.IsMatch("0.001"));
00036                 UNIT_ASSERT("100 USD", !rex.IsMatch("100 USD"));
00037                 UNIT_ASSERT(".34", !rex.IsMatch(".34"));
00038                 UNIT_ASSERT("0.34", rex.IsMatch("0.34"));
00039                 UNIT_ASSERT("1,052.21", !rex.IsMatch("1,052.21"));
00040 
00041                 DEBUG_CLEAR_MEM_CHECK_POINTS();
00042                 rex.CheckMem();
00043                 DEBUG_DUMP_MEM_LEAKS();
00044                 UNIT_ASSERT_MEM_NOTED("Regex 1.0");
00045         }
00046 
00047         DEBUG_CLEAR_MEM_CHECK_POINTS();
00048         DEBUG_DUMP_MEM_LEAKS();
00049         UNIT_ASSERT_MEM_NOTED("Regex 1.2");
00050         Log::SWriteOkFail( "Regex test IsMatch" );
00051 }
00052 
00053 static void _TestRegexMatch()
00054 {
00055         {
00056                 Regex rex("^-?\\d+(\\.\\d{2})?$");
00057                 
00058                 StringPtr match = rex.Match("100 USD");
00059                 UNIT_ASSERT("100 USD", match->Length() == 0);
00060 
00061                 match = rex.Match("100 USD-42");
00062                 UNIT_ASSERT("100 USD", match->Length() == 0);
00063 
00064                 match = rex.Match("-42");
00065                 UNIT_ASSERT("100 USD-42", match->Length() == 3);
00066                 UNIT_ASSERT("100 USD-42", match->Equals("-42"));
00067         }
00068 
00069         DEBUG_CLEAR_MEM_CHECK_POINTS();
00070         DEBUG_DUMP_MEM_LEAKS();
00071         UNIT_ASSERT_MEM_NOTED("Regex 1.1");
00072 
00073         {
00074                 Regex rex("-?\\d+(\\.\\d{2})?$");
00075                 
00076                 StringPtr match = rex.Match("100 USD");
00077                 UNIT_ASSERT("100 USD", match->Length() == 0);
00078 
00079                 match = rex.Match("100 USD-42");
00080                 UNIT_ASSERT("100 USD-42", match->Length() == 3);
00081                 UNIT_ASSERT("100 USD-42", match->Equals("-42"));
00082         }
00083 
00084         DEBUG_CLEAR_MEM_CHECK_POINTS();
00085         DEBUG_DUMP_MEM_LEAKS();
00086         UNIT_ASSERT_MEM_NOTED("Regex 1.2");
00087         Log::SWriteOkFail( "Regex test Match" );
00088 }
00089 
00090 static void _TestRegexMatches()
00091 {
00092         {
00093                 Regex rex("-?\\d+(\\.\\d{2})?[ ]");
00094                 
00095                 RefCountPtr<Array<StringPtr> > matches = rex.Matches("-42 19.99 0.001 100USD .34 0.34 1,052.21");
00096                 UNIT_ASSERT("Mateches count", matches->Count() == 5);
00097 
00098                 UNIT_ASSERT("Match 1", matches->ElementAt(0)->Equals("-42 "));
00099                 UNIT_ASSERT("Match 2", matches->ElementAt(1)->Equals("19.99 "));
00100                 UNIT_ASSERT("Match 3", matches->ElementAt(2)->Equals("001 "));
00101                 UNIT_ASSERT("Match 3", matches->ElementAt(3)->Equals("34 "));
00102                 UNIT_ASSERT("Match 3", matches->ElementAt(4)->Equals("0.34 "));
00103         }
00104 
00105         DEBUG_CLEAR_MEM_CHECK_POINTS();
00106         DEBUG_DUMP_MEM_LEAKS();
00107         UNIT_ASSERT_MEM_NOTED("Regex 1.2");
00108         Log::SWriteOkFail( "Regex test Matches" );
00109 }
00110 
00111 void _TestRegex()
00112 {
00113         _TestRegexIsMatch();
00114         DEBUG_CLEAR_MEM_CHECK_POINTS();
00115         DEBUG_DUMP_MEM_LEAKS();
00116 
00117         _TestRegexMatch();
00118         DEBUG_CLEAR_MEM_CHECK_POINTS();
00119         DEBUG_DUMP_MEM_LEAKS();
00120 
00121         _TestRegexMatches();
00122         DEBUG_CLEAR_MEM_CHECK_POINTS();
00123         DEBUG_DUMP_MEM_LEAKS();
00124 }
00125 
00126 
00127 #endif