<?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/"
	>

<channel>
	<title>On Programming</title>
	<atom:link href="http://op.closedformodification.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://op.closedformodification.com</link>
	<description>A practical view on programming, software development in general, and anything that is related in sometimes surprising ways</description>
	<pubDate>Thu, 17 May 2007 16:57:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>The Law of Code Attraction</title>
		<link>http://op.closedformodification.com/2007/05/17/the-law-of-code-attraction/</link>
		<comments>http://op.closedformodification.com/2007/05/17/the-law-of-code-attraction/#comments</comments>
		<pubDate>Thu, 17 May 2007 09:39:57 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Law of Attraction]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2007/05/17/the-law-of-code-attraction/</guid>
		<description><![CDATA[I have made an observation the other day about a curious correlation between Test Driven Development (TDD) and what is nowadays called the &#8220;Law of Attraction&#8221;. This law roughly states that you will attract into your life whatever things or circumstances you repeatedly think about and desire. While I don&#8217;t agree with how this concept [...]]]></description>
			<content:encoded><![CDATA[<p>I have made an observation the other day about a curious correlation between <a href="http://en.wikipedia.org/wiki/Test-driven_development">Test Driven Development</a> (TDD) and what is nowadays called the <a href="http://en.wikipedia.org/wiki/Law_of_Attraction">&#8220;Law of Attraction&#8221;</a>. This law roughly states that you will attract into your life whatever things or circumstances you repeatedly think about and desire. While I don&#8217;t agree with how this concept is very aggressively marketed today, I tend to believe that it works.</p>

<p>A key element in the law of attraction is faith. If you repeatedly think about something that you would like to have (or have happen), and you have absolute faith in getting it, you will get it, the law says. This technique can be put to use most efficiently by repeatedly thinking about the desired thing as if it would already exist (thereby exercising absolute faith). 
For example (modern examples for this law seem to be notoriously monetary), instead of saying, &#8220;I have faith that I will have a million dollars&#8221;, one would rather say, &#8220;I have a million dollars&#8221;, ideally repeating this several times a day.</p>

<p>By stating the object of our desire as if it were already a reality, we create a strong tension between the current reality (not having whatever we desire) and the desired reality. Our persistent and absolute faith then compels us to take action that will resolve this tension, by moving us towards the reality that we desire. Depending on your believe system you might also say that this action is guided by god, the universe, your subconscious mind, the divine, your muse, inspiration, etc.</p>

<p>If you are a programmer, and you are still reading, congratulations! Because now, let&#8217;s think about Test Driven Development, where usually one goes about writing a test first, that is, before the code that would make it pass even exists. So by writing this test, we pretend that the code that will make it pass (or even compile) is already there. Our test talks about the desired object, the code, as if it were already written. In other words, we express absolute faith that the code will eventually be written.  In fact, our failing test starts to attract the code that will make it pass.<br />
Now, after we clearly stated what we want, we run our compiler and/or test framework and immediately get in touch with the current reality. The test will fail. Since we have a desire to make it pass, and absolute faith that we can make it pass, the tension between the current reality and our desired reality will compel us to write the code necessary to make the test pass. Every test becomes a mini-fulfillment of a clearly stated desire. That might just be the reason why writing failing tests and making them pass is so satisfying.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2007/05/17/the-law-of-code-attraction/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Game Developer Rant</title>
		<link>http://op.closedformodification.com/2006/09/21/game-developer-rant/</link>
		<comments>http://op.closedformodification.com/2006/09/21/game-developer-rant/#comments</comments>
		<pubDate>Thu, 21 Sep 2006 09:46:07 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Gaming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[gamedev]]></category>

		<category><![CDATA[games industry]]></category>

		<category><![CDATA[rant]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2006/09/21/game-developer-rant/</guid>
		<description><![CDATA[Yesterday I listened to  &#8216;Burning Down the House: Game Developers Rant&#8217;, available here. It is the recording of a  panel that was held last year at the Game Developers Conference and it became very popular, because big game industry names were cursing our industry, and they also used &#8220;fuck&#8221; a lot, apparently previously [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I listened to  &#8216;Burning Down the House: Game Developers Rant&#8217;, available <a href="http://cmpmedia.vo.llnwd.net/o1/gdcradio-net/GDCR/gdcr_010.mp3">here</a>. It is the recording of a  panel that was held last year at the Game Developers Conference and it became very popular, because big game industry names were cursing our industry, and they also used &#8220;fuck&#8221; a lot, apparently previously being encouraged to do so. So, I decided, to write a rant myself. Keeping with tradition, it will contain the occasional swearword, so your discretion is advised.</p>

<p><span id="more-35"></span></p>

<p><strong>Rant Start</strong></p>

<p>So we hear this year our AAA projects will cost 10 million dollars to make and next year they will cost 15 and in 5 years it will be a gazillion. Why? Well hmm, let&#8217;s see, the hardware is getting more complex, content creation becomes so much harder, photo realism, content pipelines, episodic content, oh my!</p>

<p>The real reason for exploding costs in game development is incompetent software developers. I am sorry to say. It&#8217;s not the big, all encompassing, unrelenting and unchangeable advancement of technology and refinement of consumer taste and expectations, neither is it those &#8220;risk averse&#8221; publishers who wont finance your creative idea. No, it is simply incompetence. Our skill in devising reliable and flexible software architectures is, to put it mildly, severely lacking. The advanced hardware and those pesky creative designers have created an environment that requires excellence, an environment we can&#8217;t possibly keep up with using our current skillz (sic).
<br />Programmers are the least trusted individuals in the industry. Why? Because we are poor and misunderstood individuals? Because we work so hard and are tired all the time and therefore make mistakes every now and then? No. It is because we can&#8217;t code for shit. We have no clue about the simplest and oldest principles of software development. We consistently violate well established principles and practices of object oriented programming, while cranking out one class after the other in our &#8220;oh we started out with some classes but then it got out of hand uhuhuhu&#8221;-piece-of-shit-hacked-together system, which sometimes, but not always, through brute force, individual genius, or sheer luck finds it&#8217;s way onto the shelves. And then we go on complaining about those long hours &#8220;they&#8221; put us through, when in reality, it is our crap code that has us sit there, debugging deep into our utterly untested systems, looking at the same functionality in 10 places and treading to add another line to that one forbidden include file, that will send the time of our next build cycle through the roof. But hey, more time for coffee and bitching about management in the kitchen!</p>

<p>The designer walks up to us and asks us to do a simple thing that would enhance his game, and we tell him that it will take, oh, hmm, days or maybe a week, and we will not be sure how it will affect all the other systems and therefore we can&#8217;t say if the game will behave the same as before the change, look out for those side effects, testers! The designer looks at us funny, and we roll our eyes, hidden or otherwise, because how can she possibly understand the intricate goings on in our wonderful and complex software and oh how hard it is to do this cool (and sexy) programming thing but don&#8217;t worry, we got it! After all, we are your heroes. Right? Right?? And they keep throwing these change requests at us .. can&#8217;t they make up their mind! Do it right the first time, for fucks sake!
<br />We add a feature to your game, but we just can&#8217;t tell you if we broke it or not! I mean What. The. Fuck! Dear designers, dear artists, dear producers, we have fooled you. We have stringed you along. We somehow convinced you that this is the way it goes, that software development just IS THAT WAY. No, sorry, we are full of shit. We lied to you. We lied to you because we wanted to hide our own lack of software development competency. And some of us are really good at hiding that behind shiny graphics and oh those mind blowing physics. And we usually do this by crunching our asses off and burning out in our twenties.</p>

<p>So, we need software architecture you say? You mean the thing where you write those small classes with only 3 members or something, uh, and you, like, test them, and, like, your classes have as little responsibility as possible, so you can test them in isolation, and then in the end you plug your well tested small classes together, yielding a well tested and reliable layer of functionality, ready to carry your next layer, which you can build with confidence and the courage to display excellence? 
<br />Are you, like, gay or something? Oh and  ..  you can&#8217;t use virtual functions because on the 360 this will cause cache misses and our performance uh uh uh uh. 
Well fuck cache misses. Tell your programmers to learn how  to write tested and reusable software first, and then have them go and profile those 20% of the code that is exercised 80% of the execution time. They might find out how to get past the same 20% of problems they run into over and over again, using 80% of your budget.</p>

<p>So here we are, blaming Walmart and EA and those evil money wielding publishers in general, and oh the risk it is nowadays with all that money involved.
<br />Or, rather, with all those incompetent developers involved. They build you software which will be hard to maintain and hard to change. They build you software of which they will not be able to tell you if it works the same tomorrow or 10 minutes later. They build you software that will drag them back to their desks for 80 hours a week, spending most of their time in their favorite tool, the debugger. They build you software full of non-commitments and guilty conscience. Producers, have a look at those C++ files in the elusive &#8220;Code&#8221; folder of your multimillion project,  and look at the lines beginning with // or /*. You might get a feeling for how your developers feel about their work. Search for &#8220;fuck&#8221; and &#8220;shit&#8221; and &#8220;crap&#8221; or &#8220;god&#8221;, &#8220;OMG&#8221; and &#8220;sucks&#8221;. You will see comments throughout the code saying &#8220;oh .. this sucks but I put it here&#8221; or &#8220;shit! This is sooo bad but for now it is good enough. NEED TO CHANGE LATER&#8221;.  Or, my favorite one, which I read personally in the source code of a Triple-A title: &#8220;I have no shame left&#8221;. 
<br />That is right. We, your developers, have no shame left. We walk around in your office, our penises tangling, scratching our balls, and providing you with a heap of shit that you are to use for your creative ideas and dreams, and that you are to market to a hopeful crowd of gamers, which is growing more and more frustrated. Please show us the way to the closet where we left our pants and send us back to programming school.</p>

<p><strong>Rant End</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/09/21/game-developer-rant/feed/</wfw:commentRss>
<enclosure url="http://cmpmedia.vo.llnwd.net/o1/gdcradio-net/GDCR/gdcr_010.mp3" length="14215168" type="audio/mpeg" />
		</item>
		<item>
		<title>From manual to automatic testing - A case study</title>
		<link>http://op.closedformodification.com/2006/08/14/from-manual-to-automatic-testing-a-case-study/</link>
		<comments>http://op.closedformodification.com/2006/08/14/from-manual-to-automatic-testing-a-case-study/#comments</comments>
		<pubDate>Mon, 14 Aug 2006 18:21:05 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Doom3]]></category>

		<category><![CDATA[Gaming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[idsoftware]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2006/08/14/from-manual-to-automatic-testing-a-case-study/</guid>
		<description><![CDATA[I recently implemented "picking" in the publicly available parts of the Doom 3 engine.
Picking in 3D games (or applications) means that the mouse cursor position is mapped to the 3D world in such a way that the mouse can be used to "pick" up objects in 3D space. In this post I would like to [...]]]></description>
			<content:encoded><![CDATA[<p>I recently implemented "picking" in the publicly available parts of the Doom 3 engine.
Picking in 3D games (or applications) means that the mouse cursor position is mapped to the 3D world in such a way that the mouse can be used to "pick" up objects in 3D space. In this post I would like to describe how I used testing in implementing this technique,
by first implementing a "spike" solution with visual testing, and then using data from this solution to implement
a piece of code that is fully tested without manual intervention. This is an exercise in arriving from an unreliable testing method, that is,
looking at the screen, to a reliable one, using data that a machine can process automatically. I briefly touched on the same topic <a href="http://op.closedformodification.com/2006/03/07/testing-practice-use-your-eyes-when-necessary/">here</a>.</p>

<p><span id="more-29"></span></p>

<h2>The spike</h2>

<p>In <a href="http://www.extremeprogramming.org/">"Extreme Programming"</a> the term <a href="http://c2.com/cgi/wiki?SpikeSolution">"Spike Solution"</a> basically means venturing out into the untested waters and "hacking"
together a piece of code which roughly does what we want, in order to learn about the problem as well as the solution.
So I did exactly that. I found a convenient place to implement my picking code in the update loop of the game. The code
would shoot a ray from the mouse curser position into the world, and intersect it with the geometry, providing me with
the intersection point (if any). As a visual indication, I would render a sphere of some size at the intersection point. 
If the mouse cursor appears to be always in the center of the sphere, I assume my code is correct.</p>

<p>After a few iterations over the code, and some grappling with high school trigonometry, I arrive at a satisfactory solution.
Visually, everything looks correct, the data in the (occasionally invoked) debugger also looks good. Here is an actual screenshot, with a wireframe sphere indicating the point of 
intersection on the ceiling, close to the viewer:</p>

<p><a class="imagelink" href="http://op.closedformodification.com/wp-content/uploads/2006/08/shot00007.jpg" title="Manual testing using a debug sphere"><img id="image28" src="http://op.closedformodification.com/wp-content/uploads/2006/08/shot00007.thumbnail.jpg" alt="Manual testing using a debug sphere" /></a></p>

<p>Now that I have some code which does roughly what I want, it is time to put it under test. After all, I will get tired of staring at my sphere to test
my code for correctness whenever I change it.</p>

<h2>The failing test</h2>

<p>To put my code under test, I first write a failing test. The code I have written so far essentially calculates the end-point for a ray which will be tested 
for intersections with the game geometry, the start-point being the location of the viewer. In the game, I am interested in that intersection point, to find 
out what is in the game world at this point, in order to operate on it in a certain manner. In the test, I am really only interested in the end-point of the ray 
- if this point is correct, I assume the rest of the operation will be fine. My goal here is to use data that a computer can verify 
(in my case, a vector consisting of three floating point numbers), as opposed to the visual cues I have gotten so far, which need the human eye to judge (approximate) correctness.</p>

<p>So now, having rid myself of any dependency on the game data, which is expensive and time consuming to load, my test simply has some inputs, and an expected output,
and will run completely independent of the game.</p>

<p>My inputs are (for completeness sake):</p>

<ul>
<li>The screen coordinates of the mouse cursor</li>
<li>The horizontal and the vertical field of view</li>
<li>The view matrix</li>
<li>The eye point (position of the viewer)</li>
</ul>

<p>My output is another 3D point, the end-point of the ray which is shot in the view-direction, distorted accordingly using the field of view variables.
So, I put the code I have "hacked" into the game into a class, and put this class under test. This test lives in a seperate project from the game project, 
and is run independently (and cheaply). The first version of my test looks like this:</p>

<div class="igBar"><span id="lcode-6"><a href="#" onclick="javascript:showPlainTxt('code-6'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">CODE:</span><br /><div id="code-6">
<div class="code"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#define FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>x, y, tol<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#40;</span>idMath::<span style="">Fabs</span><span style="color:#006600; font-weight:bold;">&#40;</span>x-y<span style="color:#006600; font-weight:bold;">&#41;</span> &lt;= tol<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TEST<span style="color:#006600; font-weight:bold;">&#40;</span>pickRayEval<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Pickray ray<span style="color:#006600; font-weight:bold;">&#40;</span>SCREEN_WIDTH, SCREEN_HEIGHT<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mouseX = <span style="color:#800000;color:#800000;">1</span>.0f;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mouseY = <span style="color:#800000;color:#800000;">2</span>.0f;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float fovX = <span style="color:#800000;color:#800000;">3</span>.0f;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float fovY = <span style="color:#800000;color:#800000;">4</span>.0f;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idMat3 viewMatrix<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">1</span>, <span style="color:#800000;color:#800000;">2</span>, <span style="color:#800000;color:#800000;">3</span>, <span style="color:#800000;color:#800000;">4</span>, <span style="color:#800000;color:#800000;">5</span>, <span style="color:#800000;color:#800000;">6</span>, <span style="color:#800000;color:#800000;">7</span>, <span style="color:#800000;color:#800000;">8</span>, <span style="color:#800000;color:#800000;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 start<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">10</span>, <span style="color:#800000;color:#800000;">11</span>, <span style="color:#800000;color:#800000;">12</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 expectedEnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">13</span>, <span style="color:#800000;color:#800000;">14</span>, <span style="color:#800000;color:#800000;">15</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ray.<span style="">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>mouseX, mouseY, fovX, fovY, viewMatrix, start<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>As you can see, I set up some fantasy data, put it through a function in my new "Pickray" class, which is a copy of my spike code,
and check if the result is equivalent to another fantasy 3D point (taking a small epsilon into account).
Of course the test fails. This, in the words of Kent Beck, is progress. Because now I know exactly what to do
next: I need to make this test pass. How do I make it pass? By feeding it the correct pairs of input data and expected output data. Where do I get this data?
Why, by exercising the code that I have put into the game, the code which was visually verified to be correct.</p>

<h2>Sampling data</h2>

<p>So, back in the game code, I add some lines that print the inputs and the expected outputs I am interested in. Since I am really excited at this point, I don't
bother much with formatting. The obvious idea of formatting the output so it becomes correct C++ code ready to be pasted directly into my test eludes me at this point.
That's ok though, I remembered it early enough later on. At any rate, an early version of the output code looks like this:</p>

<div class="igBar"><span id="lcode-7"><a href="#" onclick="javascript:showPlainTxt('code-7'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">CODE:</span><br /><div id="code-7">
<div class="code"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// picking code has produced a vector 'end', using mouse, </span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">// fov, view matrix and eye point as input..</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gameLocal.<span style="">Printf</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"Picking: Mx = %.20f My = %.20f "</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#CC0000;">"FovX = %.20f FovY = %.20f<span style="color:#000099; font-weight:bold;">\n</span>"</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mx, my, fovx, fovy<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gameLocal.<span style="">Printf</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"Picking: View Matrix = (%.20f %.20f %.20f)"</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#CC0000;">"(%f.20 %.20f %.20f)(%.20f %.20f %.20f)<span style="color:#000099; font-weight:bold;">\n</span>"</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>,viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>,viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>,viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">gameLocal.<span style="">Printf</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">"Picking: Ray Start = (%.20f %.20f %.20f) "</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#CC0000;">"Ray End = (%.20f %.20f %.20f)<span style="color:#000099; font-weight:bold;">\n</span>"</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;start.<span style="">x</span>, start.<span style="">y</span>, start.<span style="">z</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end.<span style="">x</span>, end.<span style="">y</span>, end.<span style="">z</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div></li></ol></div>
</div></div>

<p><br /></p>

<p>This code dumps the current data  whenever I move the mouse (and whenever a certain console variable is set to 1). The high precision in the output is
necessary to avoid imprecise results in the test - so the more digits after the comma I can grab, the better.
I fire up the game, move the cursor around a bit, and I get a lot of "real world" data to choose from. A few copy and paste operations later, and some reformatting, 
and the fantasy data in my test is now replaced with one set of this real world data. One version of my first passing test looks like this:</p>

<div class="igBar"><span id="lcode-8"><a href="#" onclick="javascript:showPlainTxt('code-8'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">CODE:</span><br /><div id="code-8">
<div class="code"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TEST<span style="color:#006600; font-weight:bold;">&#40;</span>pickRayEval<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Pickray ray<span style="color:#006600; font-weight:bold;">&#40;</span>SCREEN_WIDTH, SCREEN_HEIGHT<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mouseX = <span style="color:#800000;color:#800000;">307</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>.0f;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mouseY = <span style="color:#800000;color:#800000;">255</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>.0f;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float fovX = <span style="color:#800000;color:#800000;">90</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>.0f;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float fovY = <span style="color:#800000;color:#800000;">73</span>.<span style="color:#800000;color:#800000;">73979187011718800000</span>.0f;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idMat3 viewMatrix<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">57064485549926758000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">82073915004730225000</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">02741646952927112600</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">82104778289794922000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">57085943222045898000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">01565095037221908600</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">02251023240387439700</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">99962407350540161000</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 start<span style="color:#006600; font-weight:bold;">&#40;</span>-<span style="color:#800000;color:#800000;">832</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">64</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">68</span>.<span style="color:#800000;color:#800000;">24997711181640600000</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 expectedEnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">5200</span>.<span style="color:#800000;color:#800000;">66308593750000000000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">7900</span>.<span style="color:#800000;color:#800000;">92822265625000000000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">674</span>.<span style="color:#800000;color:#800000;">48846435546875000000</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; ray.<span style="">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>mouseX, mouseY, fovX, fovY, viewMatrix, start<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, expectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div></li></ol></div>
</div></div>

<p><br /></p>

<p>This looks pretty good already: I can get the game to output data which I can use in my test, testing the exact code that the game gives me
output samples from. By now I am confident enough to replace the "loose" code in the game with the class I have since put under test. I have
moved my tested code already to "production". Always a great feeling.</p>

<p>At this point I am ready to refactor my test for using multiple data samples. I add a little infrastructure in the test, enabling me to add
an arbitrary amount of samples for testing. My test now looks something like this:</p>

<div class="igBar"><span id="lcode-9"><a href="#" onclick="javascript:showPlainTxt('code-9'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">CODE:</span><br /><div id="code-9">
<div class="code"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">struct PickraySample</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; PickraySample<span style="color:#006600; font-weight:bold;">&#40;</span>float mX, float mY, float fovX, float fovY,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const idMat3&amp; viewMatrix, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const idVec3&amp; start,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const idVec3&amp; expectedEnd<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; : mMouseX<span style="color:#006600; font-weight:bold;">&#40;</span>mX<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mMouseY<span style="color:#006600; font-weight:bold;">&#40;</span>mY<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mFovX<span style="color:#006600; font-weight:bold;">&#40;</span>fovX<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mFovY<span style="color:#006600; font-weight:bold;">&#40;</span>fovY<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mViewMatrix<span style="color:#006600; font-weight:bold;">&#40;</span>viewMatrix<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mStart<span style="color:#006600; font-weight:bold;">&#40;</span>start<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; , mExpectedEnd<span style="color:#006600; font-weight:bold;">&#40;</span>expectedEnd<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; void runTest<span style="color:#006600; font-weight:bold;">&#40;</span>PickRay&amp; ray<span style="color:#006600; font-weight:bold;">&#41;</span> const</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ray.<span style="">eval</span><span style="color:#006600; font-weight:bold;">&#40;</span>mMouseX, mMouseY, mFovX, mFovY, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mViewMatrix, mStart<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>, mExpectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>, mExpectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; assertTrue<span style="color:#006600; font-weight:bold;">&#40;</span>FLT_EQUAL<span style="color:#006600; font-weight:bold;">&#40;</span>ray.<span style="">getEnd</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, mExpectedEnd<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;idMath::<span style="">FLT_EPSILON</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mMouseX;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mMouseY;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mFovX;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; float mFovY;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idMat3 mViewMatrix;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 mStart;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; idVec3 mExpectedEnd;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">class PickrayTester</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">public:</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; void addSample<span style="color:#006600; font-weight:bold;">&#40;</span>float mX, float mY, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;float fovX, float fovY, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const idMat3&amp; viewMatrix, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const idVec3&amp; start, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;const idVec3&amp; expectedEnd<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; mSamples.<span style="">push_back</span><span style="color:#006600; font-weight:bold;">&#40;</span>PickraySample<span style="color:#006600; font-weight:bold;">&#40;</span>mX, mY, fovX, fovY, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;viewMatrix, start, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;expectedEnd<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; void runTests<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; PickRay ray<span style="color:#006600; font-weight:bold;">&#40;</span>SCREEN_WIDTH, SCREEN_HEIGHT<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; for<span style="color:#006600; font-weight:bold;">&#40;</span>Samples::<span style="">const_iterator</span> it = mSamples.<span style="">begin</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it!=mSamples.<span style="">end</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; ++it<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#40;</span>*it<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="">runTest</span><span style="color:#006600; font-weight:bold;">&#40;</span>ray<span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">private:</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; typedef std::<span style="">vector</span>&lt;PickraySample&gt; Samples;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Samples mSamples;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TEST<span style="color:#006600; font-weight:bold;">&#40;</span>pickRayEval<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; PickrayTester tester;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; tester.<span style="">addSample</span><span style="color:#006600; font-weight:bold;">&#40;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#800000;color:#800000;">362</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, <span style="color:#800000;color:#800000;">192</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#800000;color:#800000;">90</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, <span style="color:#800000;color:#800000;">73</span>.<span style="color:#800000;color:#800000;">73979187011718800000</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; idMat3<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">06865774095058441200</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">98074030876159668000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">18285137414932251000</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">99755853414535522000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">06983511894941330000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">01276944763958454100</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">18240495026111603000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">0</span>.<span style="color:#800000;color:#800000;">98314058780670166000</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; idVec3<span style="color:#006600; font-weight:bold;">&#40;</span>-<span style="color:#800000;color:#800000;">832</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">64</span>.<span style="color:#800000;color:#800000;">00000000000000000000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#800000;color:#800000;">68</span>.<span style="color:#800000;color:#800000;">24997711181640600000</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; idVec3<span style="color:#006600; font-weight:bold;">&#40;</span>-<span style="color:#800000;color:#800000;">1435</span>.<span style="color:#800000;color:#800000;">56396484375000000000</span>, </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">10108</span>.<span style="color:#800000;color:#800000;">66894531250000000000</span>, </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-<span style="color:#800000;color:#800000;">285</span>.<span style="color:#800000;color:#800000;">55285644531250000000</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// and many more lines like this...</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; tester.<span style="">runTests</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>The class PickrayTester takes care of collecting samples and running the tests. Meanwhile, the code in the game that outputs my samples has been
changed to produce correct C++ code, nameley a block of code starting with <code>tester.addSample...</code> which I can directly paste into my test, for every
data sample I want, and for as much samples I want.</p>

<p>The final thing I want to do is to remove some of the remaining manual tedium. Also, my test starts looking a bit fat, depending on how many data
samples I use. 
So, approaching the inevitable happy end, I change my sample output code in the game to directly write into a cpp file, which is in the same directory as my test. 
I call it PickraySamples.cpp. This file now solely contains a list of <code>tester.addSample..</code> blocks.
In my test, I replace the inlined sample data with an <code>#include</code> statement that includes the generated file:</p>

<div class="igBar"><span id="lcode-10"><a href="#" onclick="javascript:showPlainTxt('code-10'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">CODE:</span><br /><div id="code-10">
<div class="code"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//-----------------------------------------------------------------------------</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TEST<span style="color:#006600; font-weight:bold;">&#40;</span>pickRayEndpointSamples<span style="color:#006600; font-weight:bold;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#FF9933; font-style:italic;">//-----------------------------------------------------------------------------</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; PickrayTester tester;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">// These samples are generated by the game</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#include <span style="color:#CC0000;">"PickRaySamples.cpp"</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; tester.<span style="">runTests</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>At this point I declare my mission to be accomplished - I can now change my picking code with the safety net of an automated test. Obviously, there is more to picking
up 3D objects than just shooting a ray into the world. But the automatically asserted correctness of this integral part is already a nice and solid foundation to build on.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/08/14/from-manual-to-automatic-testing-a-case-study/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Unit testing within Doom 3</title>
		<link>http://op.closedformodification.com/2006/07/03/unit-testing-within-doom-3/</link>
		<comments>http://op.closedformodification.com/2006/07/03/unit-testing-within-doom-3/#comments</comments>
		<pubDate>Mon, 03 Jul 2006 11:38:05 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Doom3]]></category>

		<category><![CDATA[Gaming]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[idsoftware]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2006/07/03/unit-testing-within-doom-3/</guid>
		<description><![CDATA[Allow me to gleefully report that I have integrated my unit testing framework into the Doom 3 engine.

I have previously used this framework to run unit tests via a simple text interface. Tests are easily defined using a simple set of macros and automatically enumerated.  Now, whenever my modified version of Doom 3 starts [...]]]></description>
			<content:encoded><![CDATA[<p>Allow me to gleefully report that I have integrated my unit testing framework into the <a href="http://en.wikipedia.org/wiki/Doom_3">Doom 3</a> engine.</p>

<p>I have previously used this framework to run unit tests via a simple text interface. Tests are easily defined using a simple set of macros and automatically enumerated.  Now, whenever my modified version of Doom 3 starts up, it will do the same, it will enumerate tests wherever they have been implemented in the code, and I can operate those tests via the in-game console. Have a look:</p>

<p><a class="imagelink" href="http://op.closedformodification.com/wp-content/uploads/2006/07/tdd_in_d31_cropped.gif" title="TDD in the Doom3 Engine, Exhibit 2"><img id="image22" src="http://op.closedformodification.com/wp-content/uploads/2006/07/tdd_in_d31_cropped.thumbnail.gif" alt="TDD in the Doom3 Engine, Exhibit 2" /></a></p>

<p><a class="imagelink" href="http://op.closedformodification.com/wp-content/uploads/2006/07/tdd_in_d32_cropped.gif" title="TDD in the Doom3 Engine, Exhibit 1"><img id="image24" src="http://op.closedformodification.com/wp-content/uploads/2006/07/tdd_in_d32_cropped.thumbnail.gif" alt="TDD in the Doom3 Engine, Exhibit 1" /></a></p>

<p>Now, why did I do this? I will of course also have a seperate program with a lot of unit tests, as I develop my software test by test. However, I will want to write tests that will need the game running in some stage, and part of the beauty of all <a href="http://en.wikipedia.org/wiki/Id_software">id Software</a> engines is that they make it extremely easy to hook in any kind of custom code that is readily executable in the game at any time, via custom <a href="http://en.wikipedia.org/wiki/PC_game_console">console</a> commands, thus opening up an extensible command line interface to the running game. And so I not only have the environment in which to run my tests, it also hosts them and provides an interface to them. That, I think, is quite beautiful.</p>

<p>I am not working for Id Software, in case you were wondering. I am using the publicly available tools and source code for modifying the original game.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/07/03/unit-testing-within-doom-3/feed/</wfw:commentRss>
		</item>
		<item>
		<title>X-Macros</title>
		<link>http://op.closedformodification.com/2006/06/18/x-macros/</link>
		<comments>http://op.closedformodification.com/2006/06/18/x-macros/#comments</comments>
		<pubDate>Sun, 18 Jun 2006 11:30:14 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Preprocessor]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[X-Macros]]></category>

		<category><![CDATA[c++]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2006/06/18/x-macros/</guid>
		<description><![CDATA[When looking for X-Macros on Google, the only reference I came across was this one (ddj.com), which is also the place where I happened across X-Macros by accident (in the printed version of the magazine). I have since put them to great use in production code.
What are X-Macros? They are basically a clever way to [...]]]></description>
			<content:encoded><![CDATA[<p>When looking for X-Macros on Google, the only reference I came across was <a href="http://www.ddj.com/dept/cpp/184401387">this one (ddj.com)</a>, which is also the place where I happened across X-Macros by accident (in the printed version of the magazine). I have since put them to great use in production code.
What are X-Macros? They are basically a clever way to avoid code duplication, using the C/C++ preprocessor. They come in very handy whenever you would normally need to hunt down and hand-edit multiple places in your code after expanding or modifying some lower level layer which this code depends upon. Let's look at some examples to find out what X-Macros might be able to do for you.</p>

<p><span id="more-19"></span></p>

<h2>The "Enum Example"</h2>

<p>Let's say you have an enum, and you would like to associate a string with each element, to provide a readable name. A common way to do this is as follows:</p>

<div class="igBar"><span id="lcpp-16"><a href="#" onclick="javascript:showPlainTxt('cpp-16'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">C++:</span><br /><div id="cpp-16">
<div class="cpp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#include &lt;iostream&gt;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color: #0000ff;">enum</span> IDs</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;ID0 = <span style="color: #0000dd;color:#800000;">0</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;ID1,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;ID2,&nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;NUM_IDS</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* idStrings<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> = </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #666666;">"ID 0"</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #666666;">"ID 1"</span>,</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #666666;">"ID 2"</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">NULL</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID0 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID0 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID1 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID1 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID2 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID2 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000dd;color:#800000;">0</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>The output of this program is:</p>

<pre><code>ID0 -&gt; ID 0
ID1 -&gt; ID 1
ID2 -&gt; ID 2
</code></pre>

<p>Now, whenever you add an element to <code>IDs</code> you will have to remember to add an element to <code>idStrings</code>. The compiler wont tell you about your omission, and things could get nasty only very late at runtime. Even more so, if you have code that operates on all members of the enum, you have to add to this code as well, as you would have to in the above example, where we output all the string names on standard output.</p>

<p>X-Macros relieve you of all that. Let's rewrite our code like this:</p>

<div class="igBar"><span id="lcpp-17"><a href="#" onclick="javascript:showPlainTxt('cpp-17'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">C++:</span><br /><div id="cpp-17">
<div class="cpp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#include &lt;iostream&gt;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (1) Define code generating macro</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define GENERATE_IDS&nbsp; &nbsp; \</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; X<span style="color: #000000;">&#40;</span>ID0, <span style="color: #666666;">"ID 0"</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; \</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; X<span style="color: #000000;">&#40;</span>ID1, <span style="color: #666666;">"ID 1"</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; \</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; X<span style="color: #000000;">&#40;</span>ID2, <span style="color: #666666;">"ID 2"</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (2) Define X-Macro for generating enum members</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define X(id, idString) id,</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">enum</span> IDs</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span>&nbsp; &nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">// To be on the safe side, </span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">// force ID's to start at 0</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ID_LOWERBOUND = -<span style="color: #0000dd;color:#800000;">1</span>,</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; GENERATE_IDS</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; NUM_IDS</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#undef X</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (3) Define X-Macro for generating string names</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define X(id, idString) idString,</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span>* iDStrings<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> = </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span>&nbsp; &nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; GENERATE_IDS</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">NULL</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#undef X</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID0 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID0 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID1 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID1 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"ID2 -&gt; "</span> &lt;&lt;idStrings<span style="color: #000000;">&#91;</span>ID2 <span style="color: #000000;">&#93;</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000dd;color:#800000;">0</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>Let's examine what is happening here. At (1) we define a macro which expands to a set of "X-Macros". In our case, the X-Macro has 2 Parameters. At this point in the code, they are not expanded. Note that this uses the fact that the preprocessor will not expand macros embedded in other macros, before the "parent" macro is expanded. Therefore it does not matter that, at this point, X is not defined to anything.
At (2) the whole magic happens. The X-Macro is defined. It will expand to its first parameter (ID0, ID1, ID2) and a colon.<br />
Now, inside the enum definition, we expand <code>GENERATE_IDS</code>. This will now also expand the current definition of our X-Macro, and, voila, an enum is generated. At (3) we do the same thing, only this time, we expand the X-Macro to something else, namely, it's second parameter and a colon, resulting in a list of strings generated by <code>GENERATE_IDS</code>, fitting nicely into our array definition. Note that the definition of <code>X</code> had to be undefined before we could define it again.<br />
Now, whenever we want to add an enum, all we have to do is add a line to the definition of <code>GENERATE_IDS</code>, with the enum value, and the enum string name. There is no need to edit multiple places in the code any longer. The output of the new program will be exactly the same as the first one.</p>

<h2>Beyond Enums</h2>

<p>While the above example demonstrates a useful technique to avoid code duplication in a particular implementation, X-Macros can be put to use in many different scenarios. I would like to give one more example in this post. Let's say you want to have a class which has many different data members, and the frequency of adding, removing, or changing those members is currently very high. Here is how I would implement such a class using X-Macros:</p>

<div class="igBar"><span id="lcpp-18"><a href="#" onclick="javascript:showPlainTxt('cpp-18'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">C++:</span><br /><div id="cpp-18">
<div class="cpp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#include &lt;string&gt;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#include &lt;iostream&gt;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define assertEquals(exp1, exp2) \</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; _assertEquals<span style="color: #000000;">&#40;</span>__LINE__, exp1, exp2<span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">namespace</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// Implement a minimal testing framework</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">int</span> gErrorCount = <span style="color: #0000dd;color:#800000;">0</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">template&lt;class T, <span style="color: #0000ff;">class</span> U&gt;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">void</span> _assertEquals<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span> line, T exp1, U exp2<span style="color: #000000;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>exp1!=exp2<span style="color: #000000;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"Error: Line "</span> </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;line &lt;&lt;<span style="color: #666666;">"; "</span> </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;exp1 &lt;&lt;<span style="color: #666666;">"!="</span>&nbsp; &lt;&lt;exp2 </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; gErrorCount++;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (1) The code generation macro. Each X-Macro therein</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// receives the type and the name of the new member</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define GENERATE_PROPS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span>, IntValue1<span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span>std::<span style="color: #00eeff;">string</span>, StringValue1<span style="color: #000000;">&#41;</span>&nbsp; &nbsp; \</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">float</span>, FloatValue1<span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span>, IntValue2<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (2) This is the class that will hold all our members</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">class</span> Properties</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span>&nbsp; &nbsp; </div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">public</span>: </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (3) The constructor: Set all members to their default</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define X(tp, name) m_##name = tp();</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Properties<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; GENERATE_PROPS</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#undef X</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff0000;">// (4) The getters, setters and members themselves </span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define X(tp, name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">public</span>:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">const</span> tp&amp; get<span style="color: #339900;">##name() const {return m_##name;}&nbsp; \</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">void</span> set<span style="color: #339900;">##name(const tp&amp; in##name)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \ </span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span>m_<span style="color: #339900;">##name = in##name;}&nbsp; &nbsp; &nbsp; \</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">private</span>: tp m_<span style="color: #339900;">##name;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; GENERATE_PROPS</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#undef X</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> <span style="color: #ff0000;">// unnamed namespace</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Properties p;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff0000;">// (5) Test some members</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">getIntValue1</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0000dd;color:#800000;">0</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; p.<span style="color: #00eeff;">setIntValue1</span><span style="color: #000000;">&#40;</span><span style="color: #0000dd;color:#800000;">10</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">getIntValue1</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #0000dd;color:#800000;">10</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">getStringValue1</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">""</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; p.<span style="color: #00eeff;">setStringValue1</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">"test1"</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">getStringValue1</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">"test1"</span><span style="color: #000000;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!gErrorCount<span style="color: #000000;">&#41;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"All tests passed"</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">return</span> gErrorCount;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>In this more elaborate example, X-Macros are used to generate a considerable amount of code. All members of a class <code>Properties</code> are defined, as well as getters and setters for them. The code that initializes all members to their type-default is also generated within the constructor. Since I got tired of manually checking the correctness of the output, I also introduced a minimal testing "framework" in this example - <code>assertEquals</code> will report an error if it's two arguments are not equal. Before the program terminates, it will report any of these discrepancies.<br />
Now adding a new member to <code>Properties</code> in the above code is simply a matter of adding a line to the generation macro at (1), thus completely eliminating the need to hunt around in the code for places that need to be adapted or expanded (or even reduced!).</p>

<p>By the way, X-Macros can of course also be used in tests. Testing "some" of the members (see (5)) is not satisfactory, neither is adding a new line in the test whenever a new member is defined (precisely what we want to avoid for the production code!). One way of automatically testing the correctness of the generated code is adding a test value as the third parameter to the X-Macro. The generation macro would then look like so:</p>

<div class="igBar"><span id="lcpp-19"><a href="#" onclick="javascript:showPlainTxt('cpp-19'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">C++:</span><br /><div id="cpp-19">
<div class="cpp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define GENERATE_PROPS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span>, IntValue1, <span style="color: #0000dd;color:#800000;">100</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span>std::<span style="color: #00eeff;">string</span>, StringValue1, <span style="color: #666666;">"testValue1"</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; \</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">float</span>, FloatValue1, <span style="color: #0000dd;color:#800000;">200</span>.5f<span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">X<span style="color: #000000;">&#40;</span><span style="color: #0000ff;">int</span>, IntValue2, <span style="color: #0000dd;color:#800000;">200</span><span style="color: #000000;">&#41;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>Now of course all the places where X is expanded have to be adapted, to include the new parameter. In the code we have seen so far, this parameter is ignored. However, in the test, we can do something like this:</p>

<div class="igBar"><span id="lcpp-20"><a href="#" onclick="javascript:showPlainTxt('cpp-20'); return false;">PLAIN TEXT</a></span></div>

<div class="syntax_hilite"><span class="langName">C++:</span><br /><div id="cpp-20">
<div class="cpp"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; Properties p;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #ff0000;">// (5) Test all properties</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#define X(tp, name, tv) \</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">get</span><span style="color: #339900;">##name(), tp());&nbsp; \</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; p.<span style="color: #00eeff;">set</span><span style="color: #339900;">##name(tv);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; assertEquals<span style="color: #000000;">&#40;</span>p.<span style="color: #00eeff;">get</span><span style="color: #339900;">##name(), tv) </span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; GENERATE_PROPS</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #339900;">#undef X</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span>!gErrorCount<span style="color: #000000;">&#41;</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; std::<span style="color: #0000dd;">cout</span> &lt;&lt;<span style="color: #666666;">"All tests passed"</span> &lt;&lt;std::<span style="color: #00eeff;">endl</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0000ff;">return</span> gErrorCount;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div></li></ol></div>
</div></div>

<p><br /></p>

<p>Now, (5) expands to code that first tests the default value of a member, then sets it's value to the test value given in the third parameter (<code>tv</code>) of our X-Macro, and then tests that the getter returns the same value. No new code has to be added here when a new member is defined.</p>

<h2>A few words of warning</h2>

<p>As always, there is a flipside of the coin. X-Macros degrade readability of cour code.
When introducing an X-Macro, it is well worth spending some time figuring out how to keep the code that is eventually in an X-Macro as short as possible. Sometimes one jumps the gun and wraps too much code into it, while some static elements could still be extracted. Long macros make it hard to debug the code within them. On the other hand, do you see a reason to ever need to debug the Property class above, assuming that it's feature set will not change?</p>

<p>I have put X-Macros to great use in great code, and I encourage you to do the same, and to discover many more uses for this surprisingly simple yet effective technique.</p>

<p>You can download the code used in the examples here:<br />
<a href="http://closedformodification.com/files/XMacros_Enum.cpp">XMacros_Enum.cpp</a><br />
<a href="http://closedformodification.com/files/XMacros_Properties.cpp">XMacros_Properties.cpp</a></p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/06/18/x-macros/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Your insecurity does not matter</title>
		<link>http://op.closedformodification.com/2006/06/08/your-insecurity-does-not-matter/</link>
		<comments>http://op.closedformodification.com/2006/06/08/your-insecurity-does-not-matter/#comments</comments>
		<pubDate>Thu, 08 Jun 2006 20:02:25 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Project Management]]></category>

		<category><![CDATA[Unmanagement]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/2006/06/08/your-insecurity-does-not-matter/</guid>
		<description><![CDATA[In a recent chat with two of my now ex-colleagues (our employer having been brutally, swiftly and completely obliterated about 3 weeks ago), we talked about schedules, project management and milestones, and why they don't work. I said it is because people don't like to feel insecure. And they have the false believe that schedules, [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent chat with two of my now ex-colleagues (our employer having been brutally, swiftly and completely obliterated about 3 weeks ago), we talked about schedules, project management and milestones, and why they don't work. I said it is because people don't like to feel insecure. And they have the false believe that schedules, project management and milestones will redeem them from this feeling.</p>

<p>This insecurity, however, is natural. You are insecure because you don't know and you think you are supposed to know. But still, my friend, you DO NOT KNOW. And that is just how it is.</p>

<p>Insecurity also does not matter, in that it does not hinder or otherwise impede creativity and productivity. What does hinder and severely impede creativity is your superflous thoughts and interpretations of a small, natural bodily sensation.</p>

<p>So here is a suggestion, the Cyclic 4-Step Model of Creating:</p>

<p>(1) Explore the unknown, feel and accept your insecurity as part of the process</p>

<p>(2) Find a safe harbour</p>

<p>(3) Make sure you can come back to that safe harbour at any point in time easily</p>

<p>(4) Go to (1)</p>

<p>Can you see how tests can help you there? Can you see how agile development facilitates exactly this? Do you think I have forgotten the word "Goal" or do you think it is irrelevant for this process?</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/06/08/your-insecurity-does-not-matter/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Doodle Up Front</title>
		<link>http://op.closedformodification.com/2006/04/13/doodle-up-front/</link>
		<comments>http://op.closedformodification.com/2006/04/13/doodle-up-front/#comments</comments>
		<pubDate>Thu, 13 Apr 2006 18:29:42 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Procrastination]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/?p=17</guid>
		<description><![CDATA[I find it very helpful to jot down a few squares and arrows when thinking about a new system or a new layer of a system that I am building. These squares and arrows will look very much like UML, but probably not be 100% compliant. The point is, they are really somewhat of a [...]]]></description>
			<content:encoded><![CDATA[<p>I find it very helpful to jot down a few squares and arrows when thinking about a new system or a new layer of a system that I am building. These squares and arrows will look very much like UML, but probably not be 100% compliant. The point is, they are really somewhat of a doodle. While thinking of classes and responsibilities and associations, I draw them. Eventually, I will find this one little class that I will start writing tests for.<br /><br />This kind of UML-doodling is helpful for starting to brainstorm about a system, or communicating ideas to a fellow programmer. On the other hand, I like to have a neat diagram of all my classes handy, in case I get lost and I forgot what I was doing two weeks ago. And for this, I use a tool. I currently use <a href="http://www.gnome.org/projects/dia/">Dia</a>, which is good enough for my current needs. The important point is, that I use it AFTER I have implemented the system I am drawing a diagram of. If I would use the tool too early, I would just get lost in trying to make the diagram neat, and in playing around with the options of the tool. In short, I would find perfect excuses for procrastinating on the implementation, which means on writing the first test. So I doodle up front, get my hands dirty,  and I make myself some neat documentation after the fact, if I (or my peers) need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/04/13/doodle-up-front/feed/</wfw:commentRss>
		</item>
		<item>
		<title>test by test - Some Instructions on Programming and Life</title>
		<link>http://op.closedformodification.com/2006/03/26/test-by-test-some-instructions-on-programming-and-life/</link>
		<comments>http://op.closedformodification.com/2006/03/26/test-by-test-some-instructions-on-programming-and-life/#comments</comments>
		<pubDate>Sun, 26 Mar 2006 20:44:44 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Anne Lamotte]]></category>

		<category><![CDATA[David Allen]]></category>

		<category><![CDATA[Procrastination]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<category><![CDATA[bird by bird]]></category>

		<category><![CDATA[gtd]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/?p=10</guid>
		<description><![CDATA[I can not begin to tell how much I love a little book called "bird by bird - Some Instructions on Writing and Life" by Anne Lamotte. It has influenced my life and my work in more ways than I can list here, and this is also not the subject of this post. The idea [...]]]></description>
			<content:encoded><![CDATA[<p>I can not begin to tell how much I love a little book called <a href="http://www.amazon.com/gp/product/0385480016/sr=8-1/qid=1143403118/ref=pd_bbs_1/104-3961450-8471104?%5Fencoding=UTF8">"bird by bird - Some Instructions on Writing and Life"</a> by Anne Lamotte. It has influenced my life and my work in more ways than I can list here, and this is also not the subject of this post. The idea of this post is to relate a chapter in this book called "Short Assignments" to my work.</p>

<p>What are short assignments? Ms Lamotte skillfully describes what happens to her when she sits down to write something. We hear about mental illnesses pulling up chairs around her, trying to be quiet but their "weird coppery breath" will remind her of their presence, always. We hear about the thoughts that begin to flood her mind, that tell about how her well of creativity has run dry, how she comes to the conclusion that she can not do anything else and thus will starve, and how her breath gets shallow. Finally, she finds the one-inch picture frame on her desk, that she put there to remind her of the idea of short assignments. All she has to do for now is write down as much as she can see through a one-inch picture frame.</p>

<p>When I sit down to program, sometimes my breath starts getting shallow. I, too, have my selected audience of mental illnesses, only they are not even trying to be quiet, they talk to me about how I should not be here in this high profile company in this high profile business, and that I should go back where I came from. They also eloquently read a list of programmers that have achieved so much more, while still being younger than me. Sometimes they throw in pictures of said programmers with nice cars. During that period of mental terror, I robotically switch through source files, or my email, or some webpages, until, after about 2 minutes or so, I notice this little doodle I made yesterday before I left work, an UML-ish diagram, sketching out a set of classes and their relationships, for the next tier of a system I am developing. On there I spot a box, about one inch wide, with a class name written inside of it, along with a few public functions. I notice the class does not depend on anything. And I suddenly realize, again, that all I have to do for now is write only as many tests that will establish the functionality contained in this one-inch box.</p>

<p>A few months ago I read a few pretty much life-changing words in David Allen's <a href="http://www.amazon.com/gp/product/0143034545/qid=1143403252/sr=2-1/ref=pd_bbs_b_2_1/104-3961450-8471104?s=books&amp;v=glance&amp;n=283155">"Ready for Anything"</a>. They go something like this: You can not <b>do</b> a project. You can only <b>do</b> the next small physical action in a long list of actions that will eventually produce the result. I suggest you read that again. This is huge.
Is this not liberating? You can not do it. You can not write a complex software system. You do not have to resolve the huge tension that seems to exist between nothing and the end result of a big project. You can only resolve the tension between now and a few minutes later, while you look through your little picture frame, and write your test.
For me, this is the mode of work that makes me most productive, focused and passionate. I make it my primary activity to ask myself the following question, over and over again: "What is the smallest thing I can do now to advance the given assignment a little further?". Or, more to the point: "What is the smallest thing I can test that will tell me a little bit more about the system I am developing".</p>

<p>When I have neglected to pose and answer this question for a certain amount of time, I am becoming less efficient, I am stressed, and irritated. There are a few things I watch out for, to notice this as soon as possible (because after all, it does happen):</p>

<p><b>I have changed too many files since the last checkin:</b> More than, say 5 files checked out already make me a little nervous. It means I am working on something that is probably too big for my current short assignment.</p>

<p><b>I start to comment out tests:</b> It is ok to comment out a test to work on a <i>single</i> other one that has to be fixed first. It means I went too fast, but there is a threshold. If a test stays commented out for a long time, I probably became obsessive with some other aspect of the system, and I am most likely pressing my frame against my eye very hard to spot the "dead angles" that unnecessarily bother me.</p>

<p><b>I have "ok to fail tests":</b> I run my tests, and there is a list of tests that are "ok to fail" at the moment, and they "will be fixed as soon as...."
This is not only tedious, as I have to check the failing tests and check if those are the "ok to fail" ones or "not ok to fail" ones. Again this indicates a burst of compulsive obsession with something I have spotted at the corner of my current 2-inch vision.</p>

<p>Luckily, my body faithfully reminds me of what I am doing. My neck gets tense, my stress level rises, my breath becomes shallow and I become a miserable person. Until an angel dutyfully and gently taps me on the shoulder and hands me my one-inch picture frame.</p>

<p>The beautiful thing when writing tests is that you usually don't have to look back. When I look through my one-inch picture frame, it helps to not have to look back. How else would I be able to focus? It helps to feel safe and not threatened. It helps to stand on a solid foundation. In my daily work, testing and test driven develpopment provides me with exactly that. If I remembered to look through my one-inch picture frame often enough, I have built up a system test by test, layer by layer, every layer as solid as the next. It is like building my own staircase, and I can safely and faithfully step on to the very next step that I have just built, and I know it will not break down. This bundles all my abilities and experience to a narrowly defined problem, and I can rise to excellence in ways I never thought possible, because I am simply not bothered with anything else. When you were last in the flow, how many thoughts were in your mind?</p>

<p>There is, of course, a caveat to all of this. In order to work in little tiny steps, you must disconnect emotionally from the outcome of your big project. You must also make peace with the fact that you are not doing anything special. So many people want to build that cool engine, or that cool AI or that cool system that does whatever, and they want do feel like they are doing exactly that. With attempting to do that, there comes recognition from your peers, overtime, stress, tension, broken relationships and probably an unhealthy diet. But it <em>is</em> special. All those things are making it special! Your suffering is your special invitation to the party. On the other hand, writing a little test that might test one or two functions of a new, say, string class does not sound all that glamorous. And it is not. It is nothing special. If you connect emotionally with your activity and with the outcome of your work, that is, if it means life and death for you if you write the 3D engine of your game over a year or two, or just a small test over 30 minutes, you will have trouble with what I have written here. The fact of the matter is, we all do nothing special. And it certainly does not make us special. All we do is little things, and we string them together, and sometimes something interesting appears. Everything else is a delusion.</p>

<p>For those of you who are now angry, or just delusioned, maybe this quote from <a href="http://www.amazon.com/gp/product/156414738X/qid=1143403377/sr=1-1/ref=sr_1_1/104-3961450-8471104?s=books&amp;v=glance&amp;n=283155">"The 15 Seconds Principle"</a> will comfort you:</p>

<blockquote>
You must be more interested in producing pleasurable and specific actions in present time and less concerned with past failures or future results. Your mission is just to live and create with relaxation, energy, focus and faith. In this masterful state, there is no past or future. There is just creating in the now.
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/03/26/test-by-test-some-instructions-on-programming-and-life/feed/</wfw:commentRss>
		</item>
		<item>
		<title>30 Minutes, first thing in the morning</title>
		<link>http://op.closedformodification.com/2006/03/15/30-minutes-first-thing-in-the-morning/</link>
		<comments>http://op.closedformodification.com/2006/03/15/30-minutes-first-thing-in-the-morning/#comments</comments>
		<pubDate>Wed, 15 Mar 2006 11:34:18 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Procrastination]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/?p=11</guid>
		<description><![CDATA[I used to have the habit of coming into work, and as the first thing make some coffee and grab something to eat in the kitchen. I would hang out there, chatting with whoever was available, for a varying amount of time. I started noticing that, when doing this, it became harder and harder for [...]]]></description>
			<content:encoded><![CDATA[<p>I used to have the habit of coming into work, and as the first thing make some coffee and grab something to eat in the kitchen. I would hang out there, chatting with whoever was available, for a varying amount of time. I started noticing that, when doing this, it became harder and harder for me to convince me to do some work, and remarkably hard to snap out of this mode for the whole day.</p>

<p>I think the problem there was that my body and mind got into "day off mode". When I have a day off, I lazyly go about making breakfast, check news, read my blogs, all those "lazy day off things". Which is perfectly ok, when I DO have the day off, and it is fine to go into that mode.
At work, this is a problem, and my emotional state quickly makes friends with all the guilt feelings of not getting anything done, or dramatically less. In this mode I am also much more prone to procrastination and goofing off.</p>

<p>So fairly recently I am doing this: I come into work, grab a water, and do 30 minutes of work as soon as my computer is started up. I look at my lists of tests to write, or I have left a failing test from yesterday, or even a <a href="http://op.closedformodification.com/?p=7">not-compiling one</a>, and I start to work. Two factors are important: Quickly find a point to pick up from yesterday, and timebox it to 30 minutes, so your mind can not really convince you that you will be working forever without even grabbing a coffee.</p>

<p>After this little routine I grab some breakfast, some coffee, and chat with people, but usually the coding part of my mind has started up sufficiently, and I can't wait to get back to the problem at hand. Remarkably, it is equally hard to snap out of THIS mode the whole day.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/03/15/30-minutes-first-thing-in-the-morning/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Testing Practice: Use your eyes, when necessary</title>
		<link>http://op.closedformodification.com/2006/03/07/testing-practice-use-your-eyes-when-necessary/</link>
		<comments>http://op.closedformodification.com/2006/03/07/testing-practice-use-your-eyes-when-necessary/#comments</comments>
		<pubDate>Wed, 08 Mar 2006 06:14:09 +0000</pubDate>
		<dc:creator>Wolfgang</dc:creator>
		
		<category><![CDATA[Programming]]></category>

		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://op.closedformodification.com/?p=9</guid>
		<description><![CDATA[Imagine the situation:  You have a third party system, and you would like to query it for some information. You are not sure yet how this information will look like, and it will also change over the course of exploring and discovering said system in your test. Let's also say, the output is a [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine the situation:  You have a third party system, and you would like to query it for some information. You are not sure yet how this information will look like, and it will also change over the course of exploring and discovering said system in your test. Let's also say, the output is a somewhat lengthy string, with lots of details that you are not even interested in. You just want to confirm that you got the right answer.
It would be tedious to immediately use an automatic check for that, because everytime you change the way you interface with the system, it might give you a different string, and you have to adapt your test. 
So here is what I normally do: I use my eyes on the early results of my queries to the system, i.e. I print out the results on the console or whatever output device. I do that until I can visually confirm to the best of my knowledge, that the result is what I want, or that the result indicates correct operation of the system. Only then I take the result, and write some automatic checks for it. Maybe a simple string compare, or maybe I look for certain substrings.
Don't delete the code that displays the results, though. You might need it again, when things change, and you need your visual checks again. It helps to have a flag to turn verbosity on and off. It does not need to be fancy at all. Just comment out the line that turns verbosity on when you are done.</p>
]]></content:encoded>
			<wfw:commentRss>http://op.closedformodification.com/2006/03/07/testing-practice-use-your-eyes-when-necessary/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.125 seconds -->
