C++读写csv文件的测试用例

测试用例的csv文件的格式

用Excel打开:

用Notepad++打开:

编号,输入,预计结果,备注
1,"
",0,空值
2,"1
",1,1个值
3,"11111
",1,1行
4,"1
1
1
1",1,1列
5,"00000
00000
00000
00000",0,零值

需求

把测试用例文件中的第二列存成vector<vector<vector<char>>>的格式;

C++读取csv文件的方法

#include<vector>
#include<iostream>
#include <fstream>  
#include <sstream>
using namespace std;

vector<vector<vector<char>>>testcases;

void Test::readCsv () {
	ifstream inFile (_testFile, ios::in); //声明一个ifstream对象,与文件名关联
	string lineStr;//声明一个字符串变量,用于接收文件流的一行字符串
	vector<vector<char>> grid;
    //getline函数的参数是:一个输入流、一个string。当读到换行符时返回,string中不存储换行符。
	while (getline (inFile, lineStr))
	{
		vector<char>row;
		for (int i = 0; i < lineStr.size (); i++) {
				row.push_back (lineStr[i]);//把该行字符存到row中
		}
		if(!row.empty())grid.push_back (row);//把该row存到grid中
		if (isLast) {//最后一行时
			testcases.push_back (grid);
			cout << "读取了第" << testcases.size () << "个用例" << endl;
			grid.clear ();
		}
	}	
}

C++写入csv文件的方法

//用测试用例来测试代码,并将结果写入文件
void Test::writeCsv () {
	ofstream outFile;//声明一个ofstream 对象
	outFile.open (_resultFile, ios::out); // 打开模式可省略  
	outFile << "序号" << ',' << "测试结果" << endl;//写csv文件的第一行的两列作为表头
	Solution solution;//声明一个测试对象
      int n_right = 0, n_wrong = 0;//用于统计正确和错误的结果
	for (int i = 0; i < testcases.size (); i++) {
		int ans=solution.numIslands (testcases[i]);//用每个测试用例对代码进行测试
		outFile << i + 1 << ',';//输出序号
		cout << "case#" << i + 1;	
		if (ans == answers[i]) {
			outFile << "正确" << endl;//输出到csv文件
			cout << ":正确" << endl;//输出到控制台
			n_right++;
		}
		else {
			outFile << "错误" << endl;//输出到csv文件
			cout << ":错误" << endl;//输出到控制台
			n_wrong++;
              //如果答案错误,在控制台显示两种答案,方便作对比
			cout << "你的答案:" << ans << "   正确答案:" << answers[i] << endl;
		}
	}
	cout <<endl<<"共"<< testcases.size () << "个用例,正确"<<n_right<<"个,错误"
               <<n_wrong<<"个。" << endl<<endl;
	outFile.close ();
}

 用记事本打开:用Excel打开:

包含测试对象和测试过程的完整代码

#include<vector>
#include<iostream>
#include <fstream>  
#include <sstream>
using namespace std;

class Solution {
private:
	void dfs (vector<vector<char>>& grid, int r, int c);
public:
	int numIslands (vector<vector<char>>& grid);
};
void Solution::dfs (vector<vector<char>>& grid, int r, int c) {
	int nr = grid.size ();
	int nc = grid[0].size ();
	grid[r][c] = 'x';
	if (r - 1 >= 0 && grid[r - 1][c] == '1') dfs (grid, r - 1, c);
	if (r + 1 < nr && grid[r + 1][c] == '1') dfs (grid, r + 1, c);
	if (c - 1 >= 0 && grid[r][c - 1] == '1') dfs (grid, r, c - 1);
	if (c + 1 < nc && grid[r][c + 1] == '1') dfs (grid, r, c + 1);
}
int Solution::numIslands (vector<vector<char>>& grid) {
	int nr = grid.size ();
	if (!nr) return 0;
	int nc = grid[0].size ();
	int num_islands = 0;
	for (int r = 0; r < nr; ++r) {
		for (int c = 0; c < nc; ++c) {
			if (grid[r][c] == '1') {
				++num_islands;
				dfs (grid, r, c);
			}
		}
	}
	return num_islands;
}

class Test {
private:
	vector<vector<vector<char>>>testcases;
	vector<int>answers;
	string _testFile;
	string _resultFile;
public:
	Test (string testFile, string resultFile);
	void writeCsv ();
	void readCsv ();
	bool isFirstRow (string str,string& trueStr);
	bool isLastRow (string str, string& trueStr);
};
//testFile:测试用例   resultFile:测试结果
Test::Test (string testFile,string resultFile){
	_testFile = testFile;
	_resultFile = resultFile;
}
bool Test::isFirstRow (string str, string& trueStr) {
	if (str.size() < 2)return false;
	for (int i = 0; i < str.size ()-1; i++) {
		if (str[i] == ','&&str[i+1] == '"') {
			if (i + 1 < str.size () - 1) {
				trueStr = str.substr (i + 2, str.size () - i - 2);
			}			
			return true;
		}
	}
	return false;
}
bool Test::isLastRow (string str, string& trueStr) {
	if (str.size () < 3)return false;
	for (int i = 0; i < str.size () - 2; i++) {
		if (str[i] == '"' &&str[i+1] == ',') {
			answers.push_back (str[i + 2]-'0');
			if (i > 0) {
				trueStr = str.substr (0, i);//从0开始的i+1个字符
			}
			return true;
		}
	}
	return false;
}
//用测试用例来测试代码,并将结果写入文件
void Test::writeCsv () {
	ofstream outFile;
	outFile.open (_resultFile, ios::out); // 打开模式可省略  
	outFile << "序号" << ',' << "测试结果" << endl;
	Solution solution; int n_right = 0, n_wrong = 0;
	for (int i = 0; i < testcases.size (); i++) {//用每个测试用例对代码进行测试
		int ans=solution.numIslands (testcases[i]);
		outFile << i + 1 << ',';
		cout << "case#" << i + 1;	
		if (ans == answers[i]) {
			outFile << "正确" << endl;
			cout << ":正确" << endl;
			n_right++;
		}
		else {
			outFile << "错误" << endl;
			cout << ":错误" << endl;
			n_wrong++;
			cout << "你的答案:" << ans << "   正确答案:" << answers[i] << endl;
		}
	}
	cout <<endl<<"共"<< testcases.size () << "个用例,正确"<<n_right<<"个,错误"<<n_wrong<<"个。" << endl<<endl;
	outFile.close ();
}
//读取测试用例
void Test::readCsv () {
	ifstream inFile (_testFile, ios::in); //声明一个ifstream对象,与文件名关联
	string lineStr;
	vector<vector<char>> grid;
	getline (inFile, lineStr);//第一行
	answers.clear ();
	while (getline (inFile, lineStr))//getline函数的参数是:一个输入流、一个string。当读到换行符时返回,string中不存储换行符。
	{
		string trueStr;
		bool isFirst = isFirstRow (lineStr, trueStr);
		bool isLast = isLastRow (lineStr, trueStr);
		if (!isFirst && !isLast) {
			trueStr = lineStr;
		}
		vector<char>row;
		for (int i = 0; i < trueStr.size (); i++) {
				row.push_back (trueStr[i]);//把该行字符存到row中
		}
		if(!row.empty())grid.push_back (row);//把该row存到grid中
		if (isLast) {//最后一行时
			testcases.push_back (grid);
			cout << "读取了第" << testcases.size () << "个用例" << endl;
			grid.clear ();
		}
	}	
}

int main ()
{
	Test test("testData/test_lyl.csv","testResult/result_lyl.csv");
	test.readCsv ();
	test.writeCsv ();
	return 0;
}
发布了132 篇原创文章 · 获赞 40 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36622009/article/details/103997375