【发布时间】:2016-10-27 04:55:53
【问题描述】:
我最近为我正在从事的项目实施了一些矩形箱包装算法。我一直在通过生成大量矩形并查看结果来稍微天真地对其进行测试,但我想更有条理地进行测试。
公开的界面如下所示:
class RectPacker
{
public:
RectPacker(int width, int height);
virtual ~RectPacker();
//////
/// \brief fit a rectangle into the available space
///
/// \param[in] width, height size of the rectangle to be fitted
/// \param[out] x, y position where the rectangle was fitted (disregard if function returns false)
/// \param[out] rotated whether or not the rectangle was rotated during the fitting
/// \return true if position for the rectangle could be found, false if rectangle could not be fitted.
///
virtual bool findBestPosition(int width, int height, int& x, int& y, bool& rotated) = 0;
//////
/// \brief clear the rectangle packer
///
/// Resets the packer to an empty state. This is usefull when you want to free up space.
/// Since just freeing space will the space fragmented and degrade packing performance
/// it is usually better to just clear the entire packer and repack all the remaining
/// rectangles.
///
virtual void clear() = 0;
};
基本上你用一个 bin 大小来初始化打包器。对 findBestPosition 的每次调用都会将该 bin 的一部分分配给一个矩形,直到它被填满并且不能再容纳(在这种情况下,该方法将返回 false)。
那么,鉴于系统接口如此之小,算法相当复杂,我无法检查内部状态,我该如何为此编写单元测试?
我的直觉告诉我,我可以用大量随机生成的矩形来敲击它。我会保留一份退回配件的清单,并检查它们是否都在垃圾箱的范围内并且相互不相交。但是,这并不能测试某些退化的情况(例如将所有矩形堆叠在一侧并留出 >80% 的空间)。当然,我可以尝试定义最大允许的浪费百分比,并在算法开始拒绝配件时检查它。
这有几个不满意的方面:
- 如何设置允许的废物百分比?对我来说,这似乎非常武断和一厢情愿。将其设置得太低会导致太多误报,设置得太高会错过算法中的问题。
- 我希望我的单元测试具有确定性和可重复性。随机数据感觉不对。但是,如果我每次冒着丢失东西的风险都使用同一组矩形。
- 通过跟踪退回的配件并对其进行分析,测试本身变得如此复杂以至于容易出错。单元测试应该非常简单。
- 某些不当行为只有通过用一双人眼查看数据才能显现出来。不确定如何检查算法是否“表现良好”。例如,它可能会留下很大的间隙,或者它可能无法旋转矩形以获得最大效率,或者它可能会以错误的方式旋转它们(符号错误)并以这种方式浪费空间......
我知道如何手动测试它,但是如何为此编写测试套件?我是不是让这件事变得更难了?
【问题讨论】:
-
虽然没有使用Golden ratio通过使用螺旋的属性R1 = (a x Gr) x a, R2 = a x (a / Gr), ...跨度>
标签: c++ unit-testing