00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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