Is there a good way to unit test a function or a class using OpenGL commands?
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];
};
}
In C and c++, I could pass function pointers to functions calling opengl functions (then for unit testing passing pointers to mock functions).
I could also link with different library (instead of opengl), but that sounds like a big complication.
So, what are other techniques to unit test code calling opengl functions?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With