如何测试简单的 Cpp 文件
How to test Simple Cpp file?
我用 Cpp 编写了一个简单的程序,但我不知道测试它的最佳方法是什么?是否有像Java那样的单元测试格式?
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
static vector<int> getAllFlipLocations(
vector<int> & pancakesRepresentedByDiameter);
/**
* Problem statement can be viewed at:
* http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110402&format=html
*
* The following is a solution for the above problem.
*
* @author Quinn Liu (quinnliu@vt.edu)
*/
int main(void) {
string stackOfPancakes;
while (getline(cin, stackOfPancakes)) {
istringstream is(stackOfPancakes);
vector<int> pancakesRepresentedByDiameter;
int diameterOfPancake;
while (is >> diameterOfPancake) {
pancakesRepresentedByDiameter.push_back(diameterOfPancake);
}
reverse(pancakesRepresentedByDiameter.begin(),
pancakesRepresentedByDiameter.end());
vector<int> orderOfFlipLocations = getAllFlipLocations(
pancakesRepresentedByDiameter);
// first print original stack of pancakes
cout << stackOfPancakes << endl;
// now print location within stack to flip pancakes to get a stack
// of pancakes where the pancake diameters decrease as they move
// from the bottom to the top
for (int i = 0; i < orderOfFlipLocations.size(); i++) {
if (i != 0) {
cout << ' ';
}
cout << orderOfFlipLocations[i];
}
cout << endl;
}
}
/**
* Return the order of the locations to flip pancakes in the pancake stack.
*/
vector<int> getAllFlipLocations(vector<int> &pancakesRepresentedByDiameter) {
vector<int> orderOfFlipLocations;
vector<int>::iterator beginIndex = pancakesRepresentedByDiameter.begin();
vector<int>::iterator endIndex = pancakesRepresentedByDiameter.end();
for (int i = 0; i < pancakesRepresentedByDiameter.size(); i++) {
vector<int>::iterator currentIndex = beginIndex + i;
vector<int>::iterator maximumIndex = max_element(currentIndex,
endIndex);
// iterate through the stack of pancakes
if (currentIndex != maximumIndex) {
if (maximumIndex + 1 != endIndex) {
// adds value of (maximumIndex - beginIndex + 1) to the end of the vector
orderOfFlipLocations.push_back(maximumIndex - beginIndex + 1);
reverse(maximumIndex, endIndex);
}
orderOfFlipLocations.push_back(i + 1);
reverse(currentIndex, endIndex);
}
}
orderOfFlipLocations.push_back(0);
return orderOfFlipLocations;
}
不,开箱即用C++没有任何单元测试。不过,如今许多 IDE 都在这样做。这是在Visual Studio中进行单元测试的方法,还有一些框架值得一提的是CppUnit,UnitTest++或Google C++测试框架。如果您正在寻找更具体的内容,可以在此处查看有关该主题的旧线程。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 在c++中用vector填充一个简单的动态数组
- 内置函数可查看CPP中的成员变量
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 处理简单 cpp 类构造函数中的错误
- 将一系列数字映射到 CPP 中的值的简单方法
- 在 cpp 中最简单的图形实现
- 简单的 CPP 地图存储和访问无法按预期工作
- 如果将功能的非常简单的定义移动到.cpp,则编译时间的减少是多少
- 如何运行需要QT的简单CPP文件
- yaml-cpp迭代具有未定义值的映射的最简单方法
- CPP 上的简单求和程序
- 如何测试简单的 Cpp 文件
- 实现.cpp文件的更简单方法
- 为什么我的简单cpp-netlib程序需要这么长时间才能编译