#include
#include "readTxt.h"
#include
#include // for strcpy(), strcat()
#include <io.h>
#include <WINDOWS.H>
#include
#include
string g_CaseFilePath("CaseFilePath:");
int numCaseFilePath = 13;
string path_BugTest("\GGPTest\Source\BugTest");
string path_WhiteboxTest("\GGPTest\Source\WhiteboxTest");
string path_AlgorithmTest("\UnitTest\AlgorithmTest");
string testCase("\GGPTest\TestCase");
// Ignore these use cases
string CurveSurface_CEllipsurf("CurveSurface_CEllipsurf");
// Find if a line in the file is the same as str
bool IsStringInTxt(ifstream & file, pair<string, string> pair_case_file)
{
string s;
while(getline(file,s))
{
if(s == "" || s.find(g_CaseFilePath) == 0)
continue;
//streampos pos = infile.tellg(); // Record the current file pointer location //Removal of DISAB LED and comparison string re_s = removeDisabled(s); string re_fir = removeDisabled(pair_case_file.first); //Use cases correspond to file names string caseName = getName(re_s); string fileName = getName(getTestCaseFile(file)); //Use case full name correspondence is also correspondence if((re_s == re_fir) || (caseName == getName(re_fir) && fileName == pair_case_file.second)) return true; //infile.seekg(pos); // Returns the original file pointer location } return false;
}
bool IsStringInTxt(ifstream & file, const string & path_file)
{
string s, new_trunk;
//Find the relative path to testcase size_t pos = path_file.find(testCase); if(pos == string::npos) return false; new_trunk = path_file.substr(pos, path_file.size() - pos); if(new_trunk.empty()) return false; while(getline(file,s)) { pos = s.find(testCase); if(pos == string::npos) continue; string new_prodn = s.substr(pos, s.size() - pos); /*if(new_prodn == "\\GGPTest\\TestCase\\BugCase\\BuildGeometry\\BUG_GDQ_10278.txt") return true;*/ if(new_prodn == new_trunk) return true; } return false;
}
// View rows in aFile that are not in bFile
void AfileNotInBfile(const string & aFile, const string & bFile, const string & cFile, bool flag_cpp)
{
ifstream AInfile, BInfile;
AInfile.open(aFile.data());
assert(AInfile.is_open());
BInfile.open(bFile.data()); assert(BInfile.is_open()); ofstream justPutout(cFile, ios::app); string s; while(getline(AInfile, s)) { bool isCaseName = (s != "") && s.find(g_CaseFilePath) != 0 && s.find(CurveSurface_CEllipsurf) != 0; if(isCaseName) { //streampos pos = AInfile.tellg(); // Record the current file pointer location bool isInBfile = true; //Return to the beginning of the file BInfile.clear(); BInfile.seekg(0, ios::beg); string TestCaseFile; if(flag_cpp) { TestCaseFile = getTestCaseFile(AInfile); isInBfile = IsStringInTxt(BInfile, make_pair(s, getName(TestCaseFile))); } else isInBfile = IsStringInTxt(BInfile, s); //cout<<new_s<<" "<<isInBfile<<endl; if(!isInBfile) { justPutout<<s<<endl; if(flag_cpp) { justPutout<<TestCaseFile<<endl; justPutout<<getPathInProdN(bFile, TestCaseFile)<<endl<<endl; } } } } AInfile.close(); BInfile.close(); justPutout.close();
}
// Find all. cpp under a folder Or. txt file, written to file
void listFiles(const char * dir, const char * file, const char * flag)
{
ofstream justPutout(file, ios::app);
char dirNew[200]; strcpy(dirNew, dir); strcat(dirNew, "\\*.*"); // Add "\*. *" after the directory for the first search intptr_t handle; _finddata_t findData; handle = _findfirst(dirNew, &findData); if (handle == -1) // Check for success return; do { if (findData.attrib & _A_SUBDIR) { if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0) continue; // Add "\" and the name of the directory after the directory for the next search strcpy(dirNew, dir); strcat(dirNew, "\\"); strcat(dirNew, findData.name); listFiles(dirNew, file, flag); } else //cout <<dir<<"\\"<< findData.name << "\n"; { if(string(findData.name) == "BUG_TJGJ_27688.txt") int ok = 5; string str(findData.name); size_t i = str.find_last_of(string(".")); if(i != string::npos && str.substr(i, str.size() - i) == flag) { string path = string(dir) + "\\" + findData.name; justPutout<<path<<endl; } } } while (_findnext(handle, &findData) == 0); _findclose(handle); // Close the search handle justPutout.close();
}
// Traverse a document under a folder, find TEST in the document, and enter the test case name into the document file
void FindTestName(const char * dir, const char * temp_file_path, const char * file)
{
Cout < < "Start looking for all test cases under the path..." Endl endl uuuuuuuuuuuu
emptyFile(file);
emptyFile(temp_file_path);
ofstream justPutout(file, ios::app);
// Find in the following three paths
string dir_BugTest = dir + path_BugTest;
string dir_WhiteboxTest = dir + path_WhiteboxTest;
string dir_AlgorithmTest = dir + path_AlgorithmTest;
//Find the file under the path, temp_file_path temporarily saves the file path name listFiles(dir_BugTest.data(), temp_file_path, ".cpp"); listFiles(dir_WhiteboxTest.data(), temp_file_path, ".cpp"); listFiles(dir_AlgorithmTest.data(), temp_file_path, ".cpp"); //Read each file ifstream Infile; Infile.open(temp_file_path); assert(Infile.is_open()); //Read Test from each file to file string s_file, s_name; while(getline(Infile, s_file)) { ifstream perFile; perFile.open(s_file.data()); assert(perFile.is_open()); while(getline(perFile, s_name)) { size_t pos = s_name.find("TEST"); if(pos == 0) { bool flag = true; //Find the location of'(' size_t pos_quot = s_name.find("("); if(pos_quot == string::npos) continue; if(pos + 4 > pos_quot) continue; //Should TEST_F be the case? for(size_t i=0; i < pos; i++) if(s_name[i] != ' ') { flag = false; break; } /*for(size_t i = pos + 4; i < pos_quot; i++) if(s_name[i] != ' ') { flag = false; break; }*/ if( !flag) continue; //Change "," to ". string s; //Use Case Name size_t pos_dot = s_name.find(','); if(pos_dot == string::npos) continue; else s_name[pos_dot] = '.'; //Find the use case name for(size_t i = pos_quot + 1; i < s_name.size(); i++) { if(s_name[i] != ' ') { if(s_name[i] != ')') s += s_name[i]; else break; } } justPutout<<s<<endl; //Output file path name justPutout<<string(g_CaseFilePath)<<s_file<<endl<<endl; } } perFile.close(); } Infile.close(); justPutout.close();
}
// Traverse a document under a folder, find the TEST in the file, and write the complete TEST into the text file
string FindTestCaseCode(const string & caseName, const string & temp_file_path)
{
//emptyFile(temp_file_path);
//// The final test case code
string caseStr;
ifstream Infile; Infile.open(temp_file_path); assert(Infile.is_open()); //Read Test from each file to file string s_file_or_name, caseCode; while(getline(Infile, s_file_or_name)) { //Continue without case Name if(s_file_or_name.find(caseName) == string::npos ) continue; //Continue without accurately reading the file where the use case is located if(!getline(Infile, s_file_or_name)) continue; //Find the file where the use case is located s_file_or_name = s_file_or_name.substr(numCaseFilePath, s_file_or_name.size() - numCaseFilePath ); string new_file; for(size_t i = 0; i <s_file_or_name.size(); i++) { if(s_file_or_name[i] == '\\') new_file += '\\\\'; else new_file += s_file_or_name[i]; } //Open the file where the use case is located ifstream perFile; perFile.open(s_file_or_name.data()); assert(perFile.is_open()); //Read the complete use case function body while(getline(perFile, caseCode)) { string s_name_new; for(size_t i=0; i<caseCode.size(); i++) { //Change it into. if(caseCode[i] == ',') s_name_new += '.'; //Remove blank space else if(caseCode[i] != ' ') s_name_new += caseCode[i]; } //Determine whether the test case is complete? size_t casePos1 = s_name_new.find("TEST(" + caseName + ")"); size_t casePos2 = s_name_new.find("TEST_F(" + caseName + ")"); if(casePos1 != 0 && casePos2 != 0) continue; //Find the function body of the test case caseStr += caseCode + "\n"; while(getline(perFile, caseCode)) { if(caseCode.find('}') != 0) //The "}" at the top represents the end function body caseStr += caseCode + "\n"; else break; } //Successfully Obtaining Function Body if(!caseStr.empty()) break; } if(!caseStr.empty()) { caseStr += caseCode + "\n\n"; return caseStr; } perFile.close(); } Infile.close(); return caseStr;
}
// Look at the use case names in aFile, but not in bFile, which are written to cFile, and the code is written to dFile files.
void CaseAfileNotInBfile(const string & dir, const string & aFile, const string & bFile, const string & cFile, const string & dFile)
{
Cout < < "Start looking for different use case names for two files..." Endl endl uuuuuuuuuuuu
emptyFile(cFile);
emptyFile(dFile);
AfileNotInBfile(aFile, bFile, cFile, true); ifstream File; File.open(cFile); assert(File.is_open()); cout<<"Start looking for the body of functions for different use cases in two files......"<<endl<<endl; string s; while(getline(File, s)) { /*size_t pos = s.find('.'); if(pos != string::npos) s[pos] = ',';*/ //If not the case name, skip if(s.find(g_CaseFilePath) == 0) continue; if(s == "") continue; //Find the corresponding file path in prodn string file_prodn; getline(File, file_prodn); getline(File, file_prodn); //Use cases found are entered into dFile string code_case = FindTestCaseCode(s, aFile); ofstream justPutout(dFile, ios::app); justPutout<<code_case; justPutout.close(); //Write the test case found into the file in prodn if(file_prodn == "" && file_prodn.find(g_CaseFilePath) != 0) continue; file_prodn = file_prodn.substr(g_CaseFilePath.size(), file_prodn.size() - g_CaseFilePath.size()); ofstream prodFile(file_prodn, ios::app); prodFile<<endl; prodFile<<code_case; prodFile.close(); cout<<s<<" Writing in prodn"<<endl; }
}
// Clear a document
void emptyFile(const string & file)
{
ofstream justPutout(file);
justPutout.close();
}
// Find the path name of the file where the test case is located
string getTestCaseFile(ifstream & Infile)
{
string fileName;
while(getline(Infile, fileName))
{
if(fileName.find("CaseFilePath:") == 0)
return fileName;
}
return fileName;
}
// Find the name of the test case or file
string getName(const string & str)
{
string name;
size_t beginPos = 0;
//If it is the path name of the file string flag = "\\"; if(str.find(g_CaseFilePath) == 0) { beginPos = str.find_last_of(flag); } else //If it is the name of the test case beginPos = str.find_last_of('.'); if(beginPos != string::npos && beginPos < str.size()) return str.substr(beginPos + 1, str.size() - beginPos - 1); return name;
}
string removeDisabled(const string & str)
{
string name, str_1, str_2;
vector vec;
vec.push_back("/DISABLED_/");
vec.push_back("DISABLED_");
for(size_t i = 0; i < vec.size(); i++)
{
if(getName(str).find(vec[i]) == 0)
{
size_t pos = str.find('.');
str_1 = str.substr(0, pos + 1);
str_2 = str.substr(pos + 1 + vec[i].size(), str.size() - pos - 1 - vec[i].size());
name += str_1 + str_2;
return name;
}
}
return str;
}
// Get the path cpp file of prodn from the trunk path
string getPathInProdN(const string & ProdPathNFile, const string & trunkFile)
{
String re_trunk_file; //trunk's relative path
size_t pos = 0;
String str_path; //prodn
vector<string> vec; vec.push_back(path_BugTest); vec.push_back(path_WhiteboxTest); vec.push_back(path_AlgorithmTest); for(size_t i = 0; i < vec.size(); i ++) { pos = trunkFile.find(vec[i]); if(pos != string::npos) { re_trunk_file = trunkFile.substr(pos, trunkFile.size() - pos); break; } } if(pos == 0 || pos == string::npos) return str_path; //Find ProdPathNFile to get the file path in prodn ifstream File; File.open(ProdPathNFile); while(getline(File, str_path)) { size_t pos_prodn = str_path.find(re_trunk_file); if(pos_prodn != string::npos && str_path.size() - pos_prodn == re_trunk_file.size()) break; } return str_path;
}
// Determine whether a file exists in a path
bool IsFileExit(const string & dir, const string &fileName)
{
string new_dir = dir + "\" + fileName;
ifstream file;
file.open(new_dir);
if(file)
{
file.close();
return true;
}
else
return false;
}
// Get the path txt file of prodn from the trunk path
string getPathInProdNTxt(const string & dir_prodn, const string & trunkFile)
{
String re_trunk_file; //trunk's relative path
size_t pos = 0;
String str_path; //prodn
pos = trunkFile.find(testCase); if(pos != string::npos) re_trunk_file = trunkFile.substr(pos + testCase.size(), trunkFile.size() - pos - testCase.size()); if(pos == 0 || pos == string::npos) return str_path; //re_trunk_file = re_trunk_file.substr(0, re_trunk_file.find_last_of("\\")); return dir_prodn + re_trunk_file;
}
// Transfer the test case. txt file from trunk to prodn
void transTxtFromTrunkToProdn(const string & dir_trunk, const string & dir_prodn, const string & testcase_trunk_file, const string & testcase_prodn_file, const string & diff_file)
{
emptyFile(testcase_trunk_file);
emptyFile(testcase_prodn_file);
emptyFile(diff_file);
// Find test cases in trunk and prodn
listFiles(dir_trunk.data(), testcase_trunk_file.data(), ".txt");
listFiles(dir_prodn.data(), testcase_prodn_file.data(), ".txt");
//Find different test cases in trunk and prodn AfileNotInBfile(testcase_trunk_file, testcase_prodn_file, diff_file, false); //Find many test cases in trunk ifstream file; file.open(diff_file); string test_case; while(getline(file, test_case)) { if(test_case == "") exit(0); //Path under prodn string path_prodn = getPathInProdNTxt(dir_prodn, test_case); //Find if the file exists under prodn bool IsFileInProdn= IsFileExit(path_prodn, test_case); //If not, copy past to prodn if(!IsFileInProdn) cout<<"Have you copied it?"<<CopyFile(test_case.data(), path_prodn.data(), false)<<endl;//false represents coverage, true does not }
}