0

我在 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;
}
4

1 回答 1

0

不,开箱即用的 C++ 没有任何单元测试。不过,现在许多 IDE 都这样做了。是在 Visual Studio 中进行单元测试的方法,还有一些框架,其中很少值得一提的是CppUnitUnitTest++Google C++ 测试框架。如果您正在寻找更具体的内容,可以在此处查看有关该主题的旧线程。

于 2013-10-19T22:28:11.440 回答