OpenGL的如何单元测试? [英] OpenGL How to unit test?
问题描述
有没有单元测试的好方法函数或使用OpenGL命令一类?
Is there a good way to unit test a function or a class using OpenGL commands?
对于C ++,我知道我可以做类的模板,并通过一类做直接的OpenGL调用:
For c++, I know I could make the class a template and pass a class doing direct opengl calls :
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];
};
}
在C和C ++中,我可以通过函数指针调用函数OpenGL函数(当时为单位检测合格的指针模拟功能)。
In C and c++, I could pass function pointers to functions calling opengl functions (then for unit testing passing pointers to mock functions).
我也可以用不同的库(而不是OpenGL的)联系,但是这听起来像一个大的并发症。
I could also link with different library (instead of opengl), but that sounds like a big complication.
那么,什么是其他技术进行单元测试code调用OpenGL函数?
So, what are other techniques to unit test code calling opengl functions?
推荐答案
下面是一个不错的把戏我学到了一会儿回来。您可以使用普通的旧的#define
s到让你模拟各种API函数:
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:
#ifdef _test_
#define glVertex3f(x,y,z) (mockGlVertex3f((x),(y),(z)))
...
#endif
通过配置的preprocessor。有没有必要改变你的绘图功能都没有。另外:你可以实现 mockGlVertex3f
以这样一种方式,它如检查参数或计数调用它然后可以稍后进行检查的数目
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 e.g. checks the arguments or counts the number of calls to it which can then later be checked.
这篇关于OpenGL的如何单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!