<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RasterGrid Blog &#187; mocks</title>
	<atom:link href="http://rastergrid.com/blog/tag/mocks/feed/" rel="self" type="application/rss+xml" />
	<link>http://rastergrid.com/blog</link>
	<description>A technical blog from Daniel Rákos (aka aqnuep)</description>
	<lastBuildDate>Fri, 24 Feb 2012 03:23:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Unit testing OpenGL applications</title>
		<link>http://rastergrid.com/blog/2010/02/unit-testing-opengl-applications/</link>
		<comments>http://rastergrid.com/blog/2010/02/unit-testing-opengl-applications/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 19:54:15 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[GLEW]]></category>
		<category><![CDATA[GoogleMock]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=182</guid>
		<description><![CDATA[Nowadays comprehensive testing is a must for any software product. However, it isn&#8217;t such a general rule when it comes to graphics applications. Many developers face difficulties when they have to test their rendering codes. Manual tests and visual feedback is sometimes satisfactory but if one would like to have automated regression tests usual approaches]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F02%252Funit-testing-opengl-applications%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F9vHcy8%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Unit%20testing%20OpenGL%20applications%22%20%7D);"></div>
<p>Nowadays comprehensive testing is a must for any software product. However, it isn&#8217;t such a general rule when it comes to graphics applications. Many developers face difficulties when they have to test their rendering codes. Manual tests and visual feedback is sometimes satisfactory but if one would like to have automated regression tests usual approaches seem to fail. Even if at first sight unit testing of rendering code doesn&#8217;t look really straightforward, in fact it is. OpenGL is not an exception from this rule as well. In this article I would like to briefly present a few methods how to unit test OpenGL rendering code and also present my choice and the reasons behind the decision.</p>
<p><span id="more-182"></span>There are several ways how to create automated test cases for rendering code. To present the different approaches we first have to select a small portion of our rendering code to demonstrate the differences of each technique, mentioning the strengths and weaknesses of them.</p>
<p>Before going any further, we have to lay down our requirements against a good OpenGL unit testing environment:</p>
<ul>
<li><strong>Verifies results</strong> &#8211; This is the most basic requirement for any testing framework. We have to have the ability to check whether the rendering code executed by the module is valid and works as expected.</li>
<li><strong>Productive</strong> &#8211; The usage and maintenance of the framework shall require minimal effort. Many times unit testing is attacked because it requires additional code writing. While this is generally true, a nice unit testing environment can be kept very simple yet flexible. An OpenGL testing environment shouldn&#8217;t be different.</li>
<li><strong>Fast</strong> &#8211; This is a general requirement for any unit testing environment especially when combined with a continuous integration framework. We want our test results as fast as possible as long feedback cycles severely slow down the development process.</li>
<li><strong>Standalone</strong> &#8211; Does not require complex setup or environmental support in order to be executed. This is a general requirement when we deal with unit testing as if the code is tightly coupled by any of the surroundings then both development and maintenance costs increase.</li>
<li><strong>Compatible</strong> &#8211; Does not require any special hardware so it can be tested on a machine that wouldn&#8217;t necessarily be suitable for manually testing the actual product. This is especially important when the target hardware is some type of embedded platform. It is also important to ensure that it will work on hardware provided by different vendors. In one word, it should comply to the standard, not to driver implementations.</li>
<li><strong>Cross-platform</strong> &#8211; Does not rely on the services of a particular operating system or platform, instead it can be executed on any machine as usually all unit tests. Of course, this restriction can be relaxed depending on actual use case scenarios.</li>
</ul>
<p>Now that we know what we would like to achieve, we can continue with a sample use case. Lets say we would like to create an OpenGL 3.2 based rendering engine. One of the first things that we would write is a class (or set of classes) that will help us handling OpenGL buffer objects as it seems to be one of the main building blocks of such a system. As a very basic example, our first version of the buffer handling class will act simply as a wrapper for buffer objects having the following interface:</p>
<pre class="brush: cpp">class Buffer {
public:
    Buffer();
    virtual ~Buffer();
};</pre>
<p>As it can be seen for now we just require that our class to handle the creation and deletion of a buffer object. Obviously, our test has to check that the constructor successfully creates a buffer by calling <em>glGenBuffers</em> and the destructor deletes that by calling <em>glDeleteBuffers</em> with proper arguments. Now lets see what possibilities we have to test OpenGL rendering code and whether it conforms to our requirements and is able to test our simple module.</p>
<h3>Checking rendered image</h3>
<p>The most naive solution for creating automated tests for rendering code is to actually execute the OpenGL commands and check whether the rendering happened as expected. This can be done by comparing reference rendering results to the actual ones. This approach has the benefit that we actually verify the concrete behavior but lets see how it looks like when we check against our previously laid down requirements:</p>
<ul>
<li><strong>Verifies results</strong> &#8211; Partially fulfilled. We check against the correct behavior, however, the ability to reproduce the actual same image is often difficult if not impossible due to different relaxations regarding to precision in both the standard and driver implementations. In order to have reproducible results the testing environment shall also provide some mechanisms to allow slight differences.</li>
<li><strong>Productive</strong> &#8211; Not met. It can be quite expensive to create an assertion system. Also, the production of reference data can be quite time consuming.</li>
<li><strong>Fast</strong> &#8211; Not met. Even if the checkers are highly optimized components of the framework, it wouldn&#8217;t fit into the time-frame of unit test cycles to execute possibly thousands of test cases that require complete verification of the produced image.</li>
<li><strong>Standalone</strong> &#8211; Not met. We have to setup a complete rendering environment in order to test even the simplest rendering code. Also, it relies on the assumption that the rendering code actually produces some image. As we can see in our buffer handling example, this is not always the case.</li>
<li><strong>Compatible</strong> &#8211; Not met. We need a testing machine that has the hardware capabilities to execute the rendering code and produce the required image.</li>
<li><strong>Cross-platform</strong> &#8211; Partially fulfilled. If our rendering code is cross-platform then it is possible to test it on any of the supported platforms. However, this makes the assertion system even more complicated as it also has to support the target platforms. Also, driver implementations may vary even further when dealing with different operating systems.</li>
</ul>
<p>As we can see, even if this version is quite natural way of thinking for anybody it&#8217;s simply impractical and not feasible for actual use. To be able to find a good solution we must look deeper into what unit testing exactly is as the presented solution has nothing to do with it. In order to be able to do real unit testing we have to eliminate the dependency on OpenGL driver implementations and strictly concentrating on the module under test.</p>
<h3>Fake OpenGL driver</h3>
<p>The second presented solution is to create a layer between the code under testing and the actual OpenGL driver implementation. This can be easily achieved by creating a fake driver, as an example a dynamic library called <em>opengl32.dll</em> in case of Windows. This additional layer would do nothing else than just recording and checking whether the required API calls happened as expected. Providing an interface towards the testing environment that can be used to request the informations needed to make a verdict about the successfulness of the test case.</p>
<p>Beside that this version accommodates much more to the idea behind unit testing it also has the benefit that it is acting as a totally independent layer and does not directly disturb the development of the actual code. Still, if we go back to our checklist we have some issues that raise some concerns regarding to the applicability of this approach:</p>
<ul>
<li><strong>Verifies results</strong> &#8211; Partially fulfilled. It is up to the implementation of the new layer whether it provides the required facilities to properly check the behavior of our tested code. Nevertheless, it also highly depends on the implementation on how we define correct behavior and the responsibility of the library.</li>
<li><strong>Productive</strong> &#8211; Partially fulfilled. Now we have a separate module that helps us in the testing. This may introduce some additional maintenance work but, of course, this depends on how intelligently is the library actually implemented.</li>
<li><strong>Fast</strong> &#8211; Mostly resolved. We do not have expensive assertions, however, as we have a quite restricted interface between our testing environment and the new layer we most probably met situations when we have to make trade-offs between speed and flexibility.</li>
<li><strong>Standalone</strong> &#8211; Resolved. We have a totally independent module that is responsible to simulate the surrounding environment of the code under testing as it should be when doing unit test. However, the question arises whether we would like this layer to be that separated from the testing code.</li>
<li><strong>Compatible</strong> &#8211; Resolved. There is no dependency on dedicated graphics hardware or any other piece of metal. In case of a robust driver simulation layer we can test our code on whatever platform we prefer.</li>
<li><strong>Cross-platform</strong> &#8211; Resolved. As previously mentioned, if the additional layer is well designed, there should be no problems regarding to this issue.</li>
</ul>
<p>Now we have a resolution that can be seriously taken into consideration as a good way to test rendering code. It can also be simply applied to test our buffer handling code as well. Also, as it is a totally standalone software element it is also very portable so it is easy to reuse between projects written in different programming languages and for different platforms.</p>
<p>Still, there is one thing that may need further investigation. Most probably for the other portions of our production code we already use some kind of mocking mechanisms for our unit testing. Having an additional interface type to handle the OpenGL related mocking (as the presented fake driver approach is nothing more than a mock library for OpenGL) may reduce the productivity of our developers. Also, it can make the testing code less uniform so introducing a slight maintenance penalty. At least for comparison, we should try to integrate the OpenGL mocking into our existing mocking facilities.</p>
<h3>API mocks</h3>
<p>All the people who seriously do unit testing use some mocking techniques to eliminate dependency on any external software element like databases, network or another code element. Why should the OpenGL API be different?</p>
<p>As I already written about that I use <a title="GoogleMock" href="http://code.google.com/p/googlemock/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/googlemock/?referer=');">GoogleMock</a> to test my C++ code. Lets see how this mocking framework is capable for removing OpenGL related dependencies. By default, GoogleMock does support only class mocks, however it is fairly straightforward to mock out OpenGL API functions as well. As an example, our buffer handling class needs at least a mock for the <em>glGenBuffers</em> and <em>glDeleteBuffers</em> API functions. These mocks can be very easily created using GoogleMock as part of a class in the following way:</p>
<pre class="brush: cpp">class CGLMock {
public:
    MOCK_METHOD2( GenBuffers, void(GLsizei n, GLuint* buffers) );
    MOCK_METHOD2( DeleteBuffers, void(GLsizei n, GLuint* buffers) );
};
CGLMock GLMock;</pre>
<p>This, however is not enough to replace the already existing real API function pointers with the fake ones. I did this with a nasty little trick by taking advantage of the C preprocessor:</p>
<pre class="brush: cpp">#undef glGenBuffers
#define glGenBuffers                  GLMock.GenBuffers
#undef glDeleteBuffers
#define glDeleteBuffers               GLMock.DeleteBuffers</pre>
<p>The <em>#undef</em> is needed because I use <a title="GLEW" href="http://glew.sourceforge.net/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/glew.sourceforge.net/?referer=');">GLEW</a> for accessing OpenGL API functions and it uses macros for the API function names as well.</p>
<p>All these are put into a file that can be called like <em>glmock.h</em>. In order to force the production code to use these definitions when trying to access the API inside a test case we have to create a wrapper header called something like <em>opengl.h</em> that will include original headers in case of normal build and include the mock library in case of unit test build. This is kind of a workaround but it works quite well in practice.</p>
<p>In theory, this trick can be applied in case of any mocking framework. As a result, from now we can write a very simple test case to check the creation and deletion of our buffer object as easily as the following few lines of code:</p>
<pre class="brush: cpp">TEST(BufferTest, CreationAndDestruction) {
    EXPECT_CALL(GLMock, GenBuffers(1,_))
        .WillOnce(SetArgumentPointee&lt;1&gt;(13));
    Buffer* buffer = new Buffer;
    EXPECT_CALL(GLMock, DeleteBuffers(1,Pointee(13)));
    delete buffer;
}</pre>
<p>I would not like to go into the details related to the interface of GoogleMock. In one word, the test case above checks whether the constructor calls <em>glGenBuffers</em> with a number of 1 for the requested number of buffer objects and returns a buffer ID in the pointer argument, and at the end it checks if <em>glDeleteBuffers</em> was called with the buffer ID value got at creation.</p>
<p>It is maybe a matter of taste whether the second or this third solution is more attractive for you. My choice was this last solution because I didn&#8217;t want to develop an separate library and also was afraid of messing up my test code with different syntactical representations of mocks. Finally, lets sum up the achievements of this last version:</p>
<ul>
<li><strong>Verifies results</strong> &#8211; Fulfilled. An existing mocking framework is used for emulating the OpenGL API thus we have all the facilities required for the proper checking of the API calls.</li>
<li><strong>Productive</strong> &#8211; Fulfilled. Again, we don&#8217;t have to deal with writing an own mocking mechanisms as we have everything out of the box. We can also incrementally extend our mock library on-the-fly while editing the test cases and the production code.</li>
<li><strong>Fast</strong> &#8211; Resolved. Our rendering related unit test cases should be as fast as any other test codes as they are indifferent, just the purposes are dissimilar.</li>
<li><strong>Standalone</strong> &#8211; Mostly resolved. The mocking library is independent, however, as we&#8217;ve seen, the introduction may require some nasty tricks in order to inject foreign code into the production code.</li>
<li><strong>Compatible</strong> &#8211; Resolved. From this point of view, this approach behaves the same as the previous version.</li>
<li><strong>Cross-platform</strong> &#8211; Resolved. Again, the same like in the previous case, maybe even a bit easier to make it portable.</li>
</ul>
<h3>Conclusion</h3>
<p>We&#8217;ve seen a few ways how we can extend our testing environment in order to support the verification of rendering code. We&#8217;ve also seen that the range varies from techniques that provide high level methods suitable especially for functional testing, until very low level methods that tightly integrate in the mocking methodology of unit testing. These, of course, do not replace traditional testing methods rather they extend it in order to find problems in the early phases of software development.</p>
<p>I also tried to present a very basic example of production code that needs such a facility in order to be tested, as well as a sample test case written using GoogleMocks applying the last presented technique.</p>
<p>While writing this article I got the idea that it would be nice to have a complete and general framework for OpenGL testing. If there is interest for it, maybe I&#8217;ll allocate some time to write one. I&#8217;m also interested which approach is the most attractive for you, especially if you have some concrete experience with any of these or with some other technique.</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/02/unit-testing-opengl-applications/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Unit testing in C++</title>
		<link>http://rastergrid.com/blog/2010/01/unit-testing-in-c/</link>
		<comments>http://rastergrid.com/blog/2010/01/unit-testing-in-c/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 16:41:39 +0000</pubDate>
		<dc:creator>Daniel Rákos</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CPPUTest]]></category>
		<category><![CDATA[CxxTest]]></category>
		<category><![CDATA[GoogleMock]]></category>
		<category><![CDATA[GoogleTest]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[unit test]]></category>

		<guid isPermaLink="false">http://rastergrid.com/blog/?p=9</guid>
		<description><![CDATA[Many people are looking for information about which particular C++ unit testing framework they should use for their project and there are also many articles discuss the topic but few articles talk about mock frameworks which are even more important factor when applying unit testing in practice and they have much greater effect on the]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_light-green" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Frastergrid.com%252Fblog%252F2010%252F01%252Funit-testing-in-c%252F%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2FadyyfS%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Unit%20testing%20in%20C%2B%2B%22%20%7D);"></div>
<p>Many people are looking for information about which particular C++ unit testing framework they should use for their project and there are also many articles discuss the topic but few articles talk about mock frameworks which are even more important factor when applying unit testing in practice and they have much greater effect on the productivity when doing test-driven development.<br />
<span id="more-9"></span></p>
<h2>Test-driven development in a nutshell</h2>
<p>My first experience with unit testing happened to be during my university studies. As we learned Java development, we also worked a little with JUnit as it is the most popular unit testing framework for Java and also the grandfather of almost all current frameworks. I felt about it like pain in the a** because it seemed ridiculous to use it for our very dummy programs and because I&#8217;m really not a big fan of Java and it&#8217;s monolithic development environment called NetBeans, but I will talk about it in another article.</p>
<p>Next I&#8217;ve met unit testing at my current job where we used TNSDLUnit which is a proprietary framework developed by the employees as an internal open-source project for our TNSDL language. Basically this framework is just a TNSDL specific version of the well known CPPUTest framework. This time I&#8217;ve seen real life examples and I soon discovered the potentials in unit testing and in particular its application in test-driven development.</p>
<p>Test-driven development, or TDD in short, is a software development process that became very popular in the recent past and not accidentally as it is a very powerful tool in good hands. It introduces a very simple development cycle that results in simple, clean and test covered code. The process consists of five straightforward steps:</p>
<ol>
<li><strong>Add a test</strong> &#8211; this is made based on a particular requirement</li>
<li><strong>Run all tests and see if the new one fails</strong> &#8211; this ensures that the test really needs modification to the code</li>
<li><strong>Write some code</strong> &#8211; modify code to make the new test pass</li>
<li><strong>Run all tests and see them succeed</strong> &#8211; if the implementation is good then the new test now must pass</li>
<li><strong>Refactor code</strong> &#8211; tidy up the code as you can be sure now that if you do something wrong then tests will fail</li>
</ol>
<p>Not just this development method provides 100% test coverage for your code but it forces you to make the implementation based on requirements not vice versa.</p>
<p>TDD in a more strict manner also states that one must not write more code than it is necessary to make the test pass. This results sometimes in very awkward implementation stubs at the beginning but it prevents the developer from creating untested code (note that 100% coverage usually does not enough for proper testing).</p>
<h2>Why use unit testing?</h2>
<p>Many people have concerns about the necessity of testing and ask why test and why use unit testing. Well, as it maybe seems ridiculous to unit test a code which consists of a few hundreds or thousands of source line and is perspicuous even for the less competent people, the importance of testing comes into force when developing huge sized codes which are continuously modified by a group of people. One can not just test whether a newly implemented functionality is working as expected but can execute automated regression sets in order to ensure that no old functionality is crashed due to a modification.</p>
<p>Others have concerns about that unit testing doubles the code as so increases maintenance because the testing code is as big as the implementation itself. By the way, in a real life example the testing code can even be double the size of the production code. However, the added benefit of a well tested product eliminates most of the cost of bug fixes as they are discovered in the very beginning of the development process.</p>
<h2>C++ unit test frameworks</h2>
<p>When I&#8217;ve started to work with C++ one of my first thing was to look around for a good unit test framework. There are plenty of them so I will mention only some of the most popular ones: CPPUnit, CPPUTest, CxxTest, UnitTest++, Boost Test Library, GoogleTest, etc.</p>
<p>Most people spend too much time on deciding which unit test framework to use while they all have very similar syntax structure and all support the most needed assertions and facilities that are needed to get started with TDD. I think this is just a matter of taste.</p>
<h2>Resolving dependencies</h2>
<p>As one starts to actively use unit testing will face difficulties that rise from the dependencies between different elements of the software system, whether it be an external database, a foreign module or just an own class referenced by the code unit under test. These problems force people to write stubs or mocks to remove external dependencies.</p>
<p>Stubs are just not flexible enough and writing mocks sometimes seems to be too expensive. However, both have a great penalty on development time and maintenance cost. So, at a point, dependencies become the main problem and as such the relevance of which unit test framework to use gets hidden and people start to look for a mock framework. These libraries not just enable easy creation of mock objects but also integrates them tightly into the unit test framework and sometimes they even provide automated mock generation. Fortunately there are also plenty of mock frameworks for C++. Here are some of them: mockpp, Mock Objects, mockcpp, GoogleMock, etc.</p>
<p>Here the differences are much more visible from both from usability, portability and maturity point of view. Some of them automatically generate mocks, others need code for that as well. Some of them depend on specific application binary interface (ABI) formats, others not. Some of them need exotic language features while others work on all the significant compilers. Also, due to the complexity of the C++ language it is very hard to correctly parse code that is heavily obfuscated by macros, so in many cases the automatic generation of mocks simply doesn&#8217;t work.</p>
<p>I&#8217;ve put my vote on GoogleMock and as such I started to use GoogleTest as a unit test framework because of the following reasons:</p>
<ul>
<li>It does not depend on any special language feature or ABI format so it&#8217;s portable</li>
<li>It is natively integrates with GoogleTest so no need to worry about compatibility issues</li>
<li>Mocks have to be hand written, however it needs just a few lines of code (it also has a mock generator, however I don&#8217;t use it)</li>
</ul>
<h2>Summary</h2>
<p>As a final conclusion, if you take my advice then you don&#8217;t take anybody&#8217;s advice. Try it out yourself and see it whether a particular tool set fits your taste or not. It strongly depends on what you plan to unit test. Just keep it simple and have fun with TDD. Yes, it&#8217;s really fun!</p>

]]></content:encoded>
			<wfw:commentRss>http://rastergrid.com/blog/2010/01/unit-testing-in-c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

