Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL How to unit test?

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?

like image 749
BЈовић Avatar asked Mar 11 '11 09:03

BЈовић


1 Answers

Here is a nice trick i learned a while back. You can use regular old #defines 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.

like image 150
Bernd Elkemann Avatar answered Sep 21 '22 15:09

Bernd Elkemann