{"id":4236,"date":"2010-11-05T09:29:03","date_gmt":"2010-11-05T15:29:03","guid":{"rendered":"http:\/\/bobbeaty.com\/wp\/?p=4236"},"modified":"2010-11-07T08:55:28","modified_gmt":"2010-11-07T14:55:28","slug":"proper-unit-testing-is-valuable-but-painful","status":"publish","type":"post","link":"https:\/\/bobbeaty.com\/wp\/archives\/4236","title":{"rendered":"Proper Unit Testing is Valuable &#8211; But Painful"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/bobbeaty.com\/wp\/wp-content\/uploads\/2008\/03\/generaldev.jpg\" alt=\"GeneralDev.jpg\" title=\"GeneralDev.jpg\" border=\"0\" width=\"128\" height=\"128\" style=\"float:right;\" \/><\/p>\n<p>One of the things I really think is important in large systems work is <em>proper<\/em> unit testing. Now I'm not talking about the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Test_first\">\"Test First\"<\/a> idea, I'm talking about building some code - a class in C++ or Java, and it contains something other than mindless data containers, and <em>those<\/em> methods needing testing. If it's a cache, then throwing things in, counting them, and getting them out again. <strong>That<\/strong> kind of testing. Stuff where you need to be <em>certain<\/em> that the code is working before you go on.<\/p>\n<p>The problem with a lot of this testing is the same thing you see in testing ICs - test vectors often require additional instrumentation on the class in order to test things properly. For example, I was testing parts of my NBBO engine where I needed to save it to a persistence system, and read it back out, and I needed to make <em>sure<\/em> it was correct. This means that I have to put <tt>operator==()<\/tt> on everything - even the data structures, because I can't be sure I'm not missing a component in the larger picture.<\/p>\n<p>Then in order to test the larger components, I have to really add a <tt>clone()<\/tt> method to all the objects in the data structure so that I can be assured that once these elemental objects are equal, placing them in the larger data structure is also equivalent.<\/p>\n<p>Now the code is simple:<\/p>\n<pre class=\"cpp\" style=\"font-family:monospace;\">  LeafNode <span style=\"color: #000040;\">&amp;<\/span> LeafNode<span style=\"color: #008080;\">::<\/span><span style=\"color: #007788;\">clone<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n  <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0000ff;\">return<\/span> <span style=\"color: #0000dd;\">new<\/span> LeafNode<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000040;\">*<\/span><span style=\"color: #0000dd;\">this<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008080;\">;<\/span>\n  <span style=\"color: #008000;\">&#125;<\/span><\/pre>\n<p>because I <strong>always<\/strong> have the copy constructor on all my objects (to keep the compiler from creating one for me with behaviors I did not intend). So the code is really just a few lines - but it's this added instrumentation that makes efficient testing possible that makes the process all the more tiresome.<\/p>\n<p>However, It finds bugs. It benchmarks performance. It's useful. It's just not very fun. Painful, in fact, at times.<\/p>\n<p>But it needs to be done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things I really think is important in large systems work is proper unit testing. Now I&#8217;m not talking about the &#8220;Test First&#8221; idea, I&#8217;m talking about building some code &#8211; a class in C++ or Java, and it contains something other than mindless data containers, and those methods needing testing. If it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[],"class_list":["post-4236","post","type-post","status-publish","format-standard","hentry","category-coding","category-cube-life"],"_links":{"self":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/comments?post=4236"}],"version-history":[{"count":1,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4236\/revisions"}],"predecessor-version":[{"id":4237,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/posts\/4236\/revisions\/4237"}],"wp:attachment":[{"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/media?parent=4236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/categories?post=4236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bobbeaty.com\/wp\/wp-json\/wp\/v2\/tags?post=4236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}