![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #include <WNS/testing/TestTool.hpp> 00029 00030 #include <iostream> 00031 #include <fstream> 00032 #include <sstream> 00033 00034 #include <sys/types.h> 00035 #include <regex.h> 00036 00037 bool 00038 wns::testing::compareFile(const std::string filename, 00039 const std::string regex) 00040 { 00041 // and the efficiency award goes tooooo... the following code! 00042 std::ifstream file(filename.c_str()); 00043 std::stringstream fileContent; 00044 std::string line; 00045 while(std::getline(file, line)) 00046 fileContent << line << std::endl; 00047 00048 return compareString(fileContent.str(), regex); 00049 } 00050 00051 bool 00052 wns::testing::compareString(const std::string content, 00053 const std::string regex, 00054 bool matchEntireContent) 00055 { 00056 regex_t preg; 00057 00058 // we compile the regular expression and then try to match 00059 // the file content against it. we make sure that the whole file 00060 // was matched, by looking at the match position in pmatch. 00061 // since regular expressions match greedy, rm_so equals 0 and rm_eo 00062 // equals the file length iff there exists a match of the 00063 // expression agains the complete file content. 00064 // REG_NEWLINE is necessary to make the '.' not match a newline. 00065 // as consequence you have to make sure to match every line of the 00066 // file explicitly. 00067 // a possibly empty sequence of comment lines may be skipped using 00068 // "(#.*\n)*". 00069 // note, that regular expression special characters have to be 00070 // escaped twice. the first escape for c string literal escaping, 00071 // the second for the regular expression compiler: to match a '(', 00072 // you have to write '\\('. 00073 int errorCode = regcomp(&preg, regex.c_str(), 00074 REG_EXTENDED | REG_NEWLINE); 00075 if(errorCode) { 00076 const int stringLength = 1024; 00077 char errorString[stringLength]; 00078 00079 regerror(errorCode, &preg, errorString, stringLength); 00080 std::cerr << errorString << std::endl; 00081 return false; 00082 } 00083 00084 regmatch_t pmatch; 00085 int result; 00086 result = regexec(&preg, content.c_str(), 1, &pmatch, 0); 00087 regfree(&preg); 00088 00089 if(REG_NOMATCH == result) 00090 return false; 00091 00092 if (matchEntireContent == true) 00093 { 00094 if(pmatch.rm_so != 0) 00095 return false; 00096 00097 if(pmatch.rm_eo != (int) /* 8) */ content.size()) 00098 return false; 00099 } 00100 00101 return true; 00102 } // compareFile 00103 00104 bool 00105 wns::testing::matchInFile(const std::string filename, const std::string regex) 00106 { 00107 std::ifstream file(filename.c_str()); 00108 std::stringstream fileContent; 00109 std::string line; 00110 while(std::getline(file, line)) 00111 fileContent << line << std::endl; 00112 00113 return matchInString(fileContent.str(), regex); 00114 } 00115 00116 bool 00117 wns::testing::matchInFile(const std::string filename, std::vector<std::string> regexps) 00118 { 00119 std::ifstream file(filename.c_str()); 00120 std::stringstream fileContent; 00121 std::string line; 00122 while(std::getline(file, line)) 00123 fileContent << line << std::endl; 00124 00125 return matchInString(fileContent.str(), regexps); 00126 } 00127 00128 bool 00129 wns::testing::matchInString(const std::string content, std::string regex) 00130 { 00131 return compareString(content, regex, false); 00132 } 00133 00134 bool 00135 wns::testing::matchInString(const std::string content, std::vector<std::string> regexps) 00136 { 00137 std::vector<std::string>::const_iterator iter = regexps.begin(); 00138 std::vector<std::string>::const_iterator end = regexps.end(); 00139 bool allMatched = true; 00140 for ( ; iter != end; ++iter) 00141 { 00142 bool thisMatches = matchInString(content, (*iter)); 00143 if (thisMatches == false) 00144 { 00145 std::cerr << "Failed to match regexp '" << (*iter) << "'" << std::endl; 00146 } 00147 allMatched = allMatched && thisMatches; 00148 } 00149 return allMatched; 00150 }
1.5.5