[英]OpenGL How to unit test?
Is there a good way to unit test a function or a class using OpenGL commands? 有没有一种使用OpenGL命令对函数或类进行单元测试的好方法?
For c++, I know I could make the class a template and pass a class doing direct opengl calls : 对于c ++,我知道我可以将类作为模板并传递一个直接执行opengl调用的类:
namespace myNamespace
{
struct RealOpenglCall
{
static inline void glVertex2fv( const GLfloat * v)
{ ::glVertex2fv( v ); }
};
template< typename T >
class SomeRendering
{
public:
SomeRendering() : v()
{
// set v
}
void Draw()
{
T::glVertex2fv(v);
}
GLfloat v[4];
};
}
In C and c++, I could pass function pointers to functions calling opengl functions (then for unit testing passing pointers to mock functions). 在C和c ++中,我可以将函数指针传递给调用opengl函数的函数(然后进行单元测试,将指针传递给mock函数)。
I could also link with different library (instead of opengl), but that sounds like a big complication. 我也可以链接不同的库(而不是opengl),但这听起来像是一个很大的复杂功能。
So, what are other techniques to unit test code calling opengl functions? 那么,什么是单元测试代码调用opengl函数的其他技术呢?
Here is a nice trick i learned a while back. 这是我回过头来学到的一个很好的技巧。 You can use regular old
#define
s to let you mock all kinds of API functions: 您可以使用常规的旧
#define
来模拟各种API函数:
#ifdef _test_
#define glVertex3f(x,y,z) (mockGlVertex3f((x),(y),(z)))
...
#endif
With a configured preprocessor. 使用配置的预处理器。 There is no need to change your drawing-functions at all.
根本不需要更改绘图功能。 Further: you can implement
mockGlVertex3f
in such a way that it eg checks the arguments or counts the number of calls to it which can then later be checked. 进一步:你可以实现
mockGlVertex3f
,例如检查参数或计算对它的调用次数,然后再进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.