{"id":1186,"date":"2013-08-09T01:00:00","date_gmt":"2013-08-08T23:00:00","guid":{"rendered":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1186"},"modified":"2013-08-09T14:23:03","modified_gmt":"2013-08-09T13:23:03","slug":"fizz-buzz-testing","status":"publish","type":"post","link":"https:\/\/www.fussylogic.co.uk\/blog\/?p=1186","title":{"rendered":"Fizz Buzz Testing"},"content":{"rendered":"<p>Unit testing is the name we give to the idea of writing code that tests our application code automatically. Essentially, its job is to exercise each piece of your application in isolation, and ensure that any data dependent bugs you might introduce accidentally, never make it into production releases. Personally, I also find it makes programming more enjoyable; it takes your large application and lets you play with just a little piece of it \u00e2\u20ac\u201c programming becomes more like a little bite sized puzzle. That being said, it\u00e2\u20ac\u2122s not always easy to write test code, especially in large intricate libraries you will find you have to start <a href=\"http:\/\/en.wikipedia.org\/wiki\/Mock_object\">faking<\/a> parts of the system to feed to your test code. I\u00e2\u20ac\u2122m not going to go into that today.<\/p>\n<p>I used to do my unit testing like this:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"ot\">#ifdef UNITTEST<\/span>\n<span class=\"dt\">int<\/span> main()\n{\n    try {\n        <span class=\"co\">\/* tests *\/<\/span>\n    } catch( exception &amp;e ) {\n        log() &lt;&lt; e.what() &lt;&lt; endl;\n        <span class=\"kw\">return<\/span> <span class=\"dv\">255<\/span>;\n    }\n}\n<span class=\"ot\">#endif<\/span><\/code><\/pre>\n<p>This is certainly workable; and has the advantage of being very simple. However, I recently got pointed at <a href=\"http:\/\/sourceforge.net\/apps\/mediawiki\/cppunit\/index.php?title=Main_Page\">cppunit<\/a> by a posting on stackoverflow, and I like the structure it imposes on tests. After a bit of reading and messing I got enough boilerplate code that I find I\u00e2\u20ac\u2122d rather use <code>cppunit<\/code> for unit tests, over my simple method. It\u00e2\u20ac\u2122s got the advantage that I can create a single unit test executable covering all modules, or test individual modules as I wish. It\u00e2\u20ac\u2122s got some nice output formats and allows easy separation and isolation of individual tests.<\/p>\n<p>Now, I could just give you my boilerplate template, but I thought it would be nice to actually show you it in use on something real. So, let\u00e2\u20ac\u2122s attack the problem that <a href=\"http:\/\/weblog.raganwald.com\/2007\/01\/dont-overthink-fizzbuzz.html\">199 out of 200 applicants for developer positions<\/a> can\u00e2\u20ac\u2122t do (incidentally that doesn\u00e2\u20ac\u2122t mean if you <em>can<\/em> do this you\u00e2\u20ac\u2122re in <a href=\"http:\/\/www.joelonsoftware.com\/items\/2005\/01\/27.html\">the top 0.5%<\/a>) and unit test the result with cppunit. That problem is <a href=\"http:\/\/en.wikipedia.org\/wiki\/Fizz_buzz\">FizzBuzz<\/a>.<\/p>\n<blockquote>\n<p>Write a program that prints the numbers from 1 to 100. But for multiples of three print \u00e2\u20ac\u0153Fizz\u00e2\u20ac\u009d instead of the number and for the multiples of five print \u00e2\u20ac\u0153Buzz\u00e2\u20ac\u009d. For numbers which are multiples of both three and five print \u00e2\u20ac\u0153FizzBuzz\u00e2\u20ac\u009d.<\/p>\n<\/blockquote>\n<p>First let me say this: a lot of good programmers, who can do this test, for a bit of fun coded up elaborate and clever solutions. For fun, that\u00e2\u20ac\u2122s fine, but clever solutions make for poor, unreadable, code. I\u00e2\u20ac\u2122ll not bother with anything clever then, I\u00e2\u20ac\u2122ll just do a work horse solution.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">void<\/span> FizzBuzz() {\n    <span class=\"kw\">for<\/span>(<span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt;= <span class=\"dv\">100<\/span>; i++) {\n        bool f = (i % <span class=\"dv\">3<\/span>) == <span class=\"dv\">0<\/span>;\n        bool b = (i % <span class=\"dv\">5<\/span>) == <span class=\"dv\">0<\/span>;\n        <span class=\"kw\">if<\/span>(f)\n            cout &lt;&lt; <span class=\"st\">&quot;Fizz&quot;<\/span>;\n        <span class=\"kw\">if<\/span>(b)\n            cout &lt;&lt; <span class=\"st\">&quot;Buzz&quot;<\/span>;\n        <span class=\"kw\">if<\/span>(!b &amp;&amp; !f)\n            cout &lt;&lt; i;\n        cout &lt;&lt; endl;\n    }\n}<\/code><\/pre>\n<p>This solution is rubbish. It\u00e2\u20ac\u2122s not rubbish because it doesn\u00e2\u20ac\u2122t work, or because it\u00e2\u20ac\u2122s not a clever solution, it\u00e2\u20ac\u2122s rubbish because to test it we have to run it then manually eyeball 100 lines of output \u00e2\u20ac\u201c worse, what if it was 1000 lines of output? That means it\u00e2\u20ac\u2122s difficult to test.<\/p>\n<p>That difficulty represents a smell from our code. Well written code <em>will<\/em> be testable. Let\u00e2\u20ac\u2122s first split it into something that\u00e2\u20ac\u2122s testable by separating our maths from our loop.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"kw\">enum<\/span> class eFizzBuzz {\n    Null,\n    Fizz,\n    Buzz,\n    FizzBuzz,\n};\n\nostream &amp;operator&lt;&lt;(ostream &amp;s, eFizzBuzz fb)\n{\n    <span class=\"kw\">switch<\/span>(fb) {\n        <span class=\"kw\">case<\/span> eFizzBuzz::Fizz:\n            <span class=\"kw\">return<\/span> s &lt;&lt; <span class=\"st\">&quot;Fizz&quot;<\/span>;\n        <span class=\"kw\">case<\/span> eFizzBuzz::Buzz:\n            <span class=\"kw\">return<\/span> s &lt;&lt; <span class=\"st\">&quot;Buzz&quot;<\/span>;\n        <span class=\"kw\">case<\/span> eFizzBuzz::FizzBuzz:\n            <span class=\"kw\">return<\/span> s &lt;&lt; <span class=\"st\">&quot;FizzBuzz&quot;<\/span>;\n        <span class=\"kw\">case<\/span> eFizzBuzz::Null:\n            <span class=\"kw\">return<\/span> s &lt;&lt; <span class=\"st\">&quot;Null&quot;<\/span>;\n    }\n}\n\neFizzBuzz FizzBuzz(<span class=\"dt\">int<\/span> n)\n{\n    bool f = (i % <span class=\"dv\">3<\/span>) == <span class=\"dv\">0<\/span>;\n    bool b = (i % <span class=\"dv\">5<\/span>) == <span class=\"dv\">0<\/span>;\n    <span class=\"kw\">if<\/span>(f &amp;&amp; b)\n        <span class=\"kw\">return<\/span> eFizzBuzz::FizzBuzz;\n    <span class=\"kw\">if<\/span>(f)\n        <span class=\"kw\">return<\/span> eFizzBuzz::Fizz;\n    <span class=\"kw\">if<\/span>(b)\n        <span class=\"kw\">return<\/span> eFizzBuzz::Buzz;\n    <span class=\"kw\">return<\/span> eFizzBuzz::Null;\n}\n\n<span class=\"dt\">void<\/span> FizzBuzz() {\n    <span class=\"kw\">for<\/span>(<span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt;= <span class=\"dv\">100<\/span>; i++) {\n        <span class=\"dt\">auto<\/span> fb = FizzBuzz(i);\n        <span class=\"kw\">if<\/span>(fb == eFizzBuzz::Null) {\n            cout &lt;&lt; i &lt;&lt; endl;\n        } <span class=\"kw\">else<\/span> {\n            cout &lt;&lt; fb &lt;&lt; endl;\n        }\n    }\n}<\/code><\/pre>\n<p>A lot more code; and you might say \u00e2\u20ac\u0153how is more code better, if it does the same job?\u00e2\u20ac\u009d. The answer is that it does the same job only superficially. Writing testable code has forced us to write cleaner, more general code. We\u00e2\u20ac\u2122ve separated our algorithm from our application, and we\u00e2\u20ac\u2122ve also generalised our output.<\/p>\n<p>This is now in a form that we can write a unit test. Let\u00e2\u20ac\u2122s do so. Make sure you have the <code>cppunit<\/code> library available (<code>apt-get install libcppunit-dev<\/code> on Debian).<\/p>\n<p>First the test case, note there is absolutely no requirement that this go in the same module as the code it\u00e2\u20ac\u2122s testing. I like to do that and keep my test code wrapped in <code>#ifdef UNITTEST<\/code>, but you can do as you wish.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"ot\">#include &lt;cppunit\/extensions\/HelperMacros.h&gt;<\/span>\n\nclass FizzBuzzTest : public CppUnit::TestFixture\n{\n  public:\n    <span class=\"dt\">void<\/span> setUp(<span class=\"dt\">void<\/span>) {}\n    <span class=\"dt\">void<\/span> tearDown(<span class=\"dt\">void<\/span>) {}\n\n    <span class=\"dt\">void<\/span> test_fizzbuzz() {\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(<span class=\"dv\">3<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(<span class=\"dv\">5<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::FizzBuzz, FizzBuzz(<span class=\"dv\">15<\/span>));\n    }\n\n    <span class=\"co\">\/\/ -----------------------<\/span>\n\n    <span class=\"co\">\/\/ HelperMacros to manufacture `static CppUnit::Test *suite()`<\/span>\n    CPPUNIT_TEST_SUITE(FizzBuzzTest);\n    CPPUNIT_TEST(test_fizzbuzz);\n    CPPUNIT_TEST_SUITE_END();\n};\n<span class=\"co\">\/\/ Add manufactured FizzBuzzTest::suite() to the global test<\/span>\n<span class=\"co\">\/\/ register<\/span>\nCPPUNIT_TEST_SUITE_REGISTRATION(FizzBuzzTest);<\/code><\/pre>\n<p><code>cppunit<\/code>\u00e2\u20ac\u2122s helper macros, while slightly unnerving looking, are straight-forward to use. Everything in the top half is just a standard class derived from <code>CppUnit::TestFixture<\/code>, <code>setUp()<\/code> and <code>tearDown()<\/code> are called before and after each test \u00e2\u20ac\u201c these supply a clean environment for each test to run in; we\u00e2\u20ac\u2122re not using them here. <code>test_fizzbuzz()<\/code> is arbitrarily named, and is added to the list of tests (<code>cppunit<\/code> calls it the test suite) in this class with the <code>CPPUNIT_TEST(test_fizzbuzz)<\/code> line. The test suite represented by this class is then scheduled for test with <code>CPPUNIT_TEST_SUITE_REGISTRATION(FizzBuzzTest)<\/code>.<\/p>\n<p>Now we just need the code to run the tests.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"ot\">#include &lt;cppunit\/ui\/text\/TestRunner.h&gt;<\/span>\n<span class=\"ot\">#include &lt;cppunit\/TextOutputter.h&gt;<\/span>\n<span class=\"ot\">#include &lt;iostream&gt;<\/span>\n<span class=\"ot\">#include &lt;stdexcept&gt;<\/span>\n<span class=\"dt\">int<\/span> main()\n{\n    <span class=\"co\">\/\/ --- Boilerplate cppunit code<\/span>\n    <span class=\"co\">\/\/ Set up runner to run all test in registry<\/span>\n    CppUnit::TextUi::TestRunner runner;\n    runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );\n    <span class=\"co\">\/\/ Redirect output to clog,<\/span>\n    runner.setOutputter(new CppUnit::TextOutputter(&amp;runner.result(), std::clog));\n    <span class=\"co\">\/\/ Run all and give success indiciation<\/span>\n    <span class=\"kw\">return<\/span> runner.run() ? <span class=\"dv\">0<\/span> : <span class=\"dv\">1<\/span>;\n}<\/code><\/pre>\n<p>Thanks to the use of the test registry there is absolutely no code in this block specific to our tests \u00e2\u20ac\u201c you can use this without change in every single project you have. It runs every test suite registered, outputs to <code>std::clog<\/code> (stderr) and returns 0 for test success, and 1 for test failure \u00e2\u20ac\u201c this is typical for command line programs.<\/p>\n<p>The meat of our unit test is in <code>test_fizzbuzz()<\/code>, and we are free to do anything we wish. The most useful thing we can do though is to use the <code>cppunit<\/code> assertions to perform some tests.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">void<\/span> test_fizzbuzz() {\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(<span class=\"dv\">3<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(<span class=\"dv\">5<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::FizzBuzz, FizzBuzz(<span class=\"dv\">15<\/span>));\n}<\/code><\/pre>\n<p><code>CPPUNIT_ASSERT_EQUAL()<\/code> takes two arguments, <code>expected<\/code>, and <code>result<\/code>. <code>cppunit<\/code> expects to be able to output both of these to a stream should the test fail, so our <code>operator&lt;&lt;()<\/code> implementation for <code>eFizzBuzz<\/code> types is a necessity here.<\/p>\n<p>Here\u00e2\u20ac\u2122s my compile and build:<\/p>\n<pre><code>clang++ -ggdb3 -O2 -Wall -Wextra -std=c++11 -Wfatal-errors   -c -o fizzbuzz.o fizzbuzz.cc\nclang++ -pthread -rdynamic -lcppunit   -o fizzbuzz fizzbuzz.o\n.\n\n\nOK (1 tests)<\/code><\/pre>\n<p><code>cppunit<\/code> output is terse when there are no failures. It\u00e2\u20ac\u2122s expected that there will be a great many tests for a project, and it\u00e2\u20ac\u2122s far better to have only the failures show any verbose output. The \u00e2\u20ac\u0153.\u00e2\u20ac\u009d indicates a test ran, and the summary tells us everything was okay with <code>1 tests<\/code> run.<\/p>\n<p>Our tests as they stand are not very good. We\u00e2\u20ac\u2122ve made the classic mistake of only testing for the match cases, not for the non-match cases.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">void<\/span> test_fizzbuzz() {\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">0<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">1<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">2<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(<span class=\"dv\">3<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">4<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(<span class=\"dv\">5<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">6<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">7<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">8<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">9<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">10<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">11<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">12<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">13<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">14<\/span>));\n    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::FizzBuzz, FizzBuzz(<span class=\"dv\">15<\/span>));\n}<\/code><\/pre>\n<p>This might look odd, as programmers we\u00e2\u20ac\u2122re used to turning sequences like this into a loop; but remember what we\u00e2\u20ac\u2122re testing is pretty fundamental \u00e2\u20ac\u201c whether we\u00e2\u20ac\u2122ve used C++\u00e2\u20ac\u2122s modulo operator correctly. It would be a pretty bad test then if we used the very thing we were testing in our test \u00e2\u20ac\u201c that\u00e2\u20ac\u2122s like saying \u00e2\u20ac\u0153<code>if(testresult == testresult)<\/code>\u00e2\u20ac\u009d. Our only option then is to hard code tests without using \u00e2\u20ac\u0153<code>%<\/code>\u00e2\u20ac\u009d and a loop counter. This is the equivalent of us eyeballing every line of output from the first example.<\/p>\n<p>Let\u00e2\u20ac\u2122s run our test again\u00e2\u20ac\u00a6<\/p>\n<pre><code>clang++ -ggdb3 -O2 -Wall -Wextra -std=c++11 -Wfatal-errors   -c -o fizzbuzz.o fizzbuzz.cc\nclang++ -pthread -rdynamic -lcppunit   -o fizzbuzz fizzbuzz.o\nrm fizzbuzz.o\n.F\n\n\n!!!FAILURES!!!\nTest Results:\nRun:  1   Failures: 1   Errors: 0\n\n\n1) test: FizzBuzzTest::test_fizzbuzz (F) line: 97 fizzbuzz.cc\nequality assertion failed\n- Expected: Null\n- Actual  : FizzBuzz<\/code><\/pre>\n<p>Very useful, we\u00e2\u20ac\u2122ve already caught something. A test failure when \u00e2\u20ac\u0153null\u00e2\u20ac\u009d was expected but the result was \u00e2\u20ac\u0153fizzbuzz\u00e2\u20ac\u009d (we\u00e2\u20ac\u2122re seeing the advantage of our <code>operator&lt;&lt;()<\/code> in this output). You can\u00e2\u20ac\u2122t see my line numbers of course, but this is the faulty line:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(<span class=\"dv\">0<\/span>));<\/code><\/pre>\n<p>Spotted the fault? It\u00e2\u20ac\u2122s actually a fault in our test. Zero is divisible by 3 and by 5 and by everything in fact. The expected output is therefore \u00e2\u20ac\u0153FizzBuzz\u00e2\u20ac\u009d not \u00e2\u20ac\u0153Null\u00e2\u20ac\u009d.<\/p>\n<p>We\u00e2\u20ac\u2122ve also only tested from 0 to 15 so far; we should do more:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">void<\/span> test_fizzbuzz() {\n    <span class=\"kw\">for<\/span>( <span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt; <span class=\"dv\">1000<\/span>; i += <span class=\"dv\">15<\/span> ) {\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::FizzBuzz, FizzBuzz(i + <span class=\"dv\">0<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">1<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">2<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(i + <span class=\"dv\">3<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">4<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(i + <span class=\"dv\">5<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">6<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">7<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">8<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">9<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">10<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">11<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">12<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">13<\/span>));\n        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">14<\/span>));\n    }\n}<\/code><\/pre>\n<p>Rerun:<\/p>\n<pre><code>!!!FAILURES!!!\nTest Results:\nRun:  1   Failures: 1   Errors: 0\n\n\n1) test: FizzBuzzTest::test_fizzbuzz (F) line: 104 fizzbuzz.cc\nequality assertion failed\n- Expected: Null\n- Actual  : Fizz<\/code><\/pre>\n<p>Line 104:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">        CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">6<\/span>));<\/code><\/pre>\n<p>Another test case fault; 6 is divisible by 3. Similarly 9 and 12. Similarly 10 is divisible by 5. Our test case has listed them all as \u00e2\u20ac\u0153Null\u00e2\u20ac\u009d.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    <span class=\"dt\">void<\/span> test_fizzbuzz() {\n        <span class=\"kw\">for<\/span>( <span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt; <span class=\"dv\">100<\/span>*<span class=\"dv\">15<\/span>; i += <span class=\"dv\">15<\/span> ) {\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::FizzBuzz, FizzBuzz(i + <span class=\"dv\">0<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">1<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">2<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(i + <span class=\"dv\">3<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">4<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(i + <span class=\"dv\">5<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(i + <span class=\"dv\">6<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">7<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">8<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(i + <span class=\"dv\">9<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Buzz, FizzBuzz(i + <span class=\"dv\">10<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">11<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Fizz, FizzBuzz(i + <span class=\"dv\">12<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">13<\/span>));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(i + <span class=\"dv\">14<\/span>));\n        }\n    }<\/code><\/pre>\n<p>This will give us zero errors finally. We can be pretty confident in our maths now. Or can we? What about the edge cases? We\u00e2\u20ac\u2122ve done zero, but what about <code>int_max<\/code>? What about negative numbers? Remember when you\u00e2\u20ac\u2122re testing, you\u00e2\u20ac\u2122re trying to find faults, you should test for every circumstance \u00e2\u20ac\u201c overflow, underflow, off-by-one errors, sign errors. I\u00e2\u20ac\u2122ll leave those for you (or you can check out my <a href=\"https:\/\/github.com\/andyparkins\/cpp11\">cpp11<\/a> repository and look at the <a href=\"https:\/\/github.com\/andyparkins\/cpp11\/blob\/master\/fizzbuzz.cc\">fizzbuzz<\/a> example). The limit test is interesting though because it shows the use of C++\u00e2\u20ac\u2122s <code>numeric_limits<\/code> to check the boundary cases.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    <span class=\"dt\">int<\/span> digits = numeric_limits&lt;<span class=\"dt\">int<\/span>&gt;::digits;\n    <span class=\"dt\">int<\/span> max = numeric_limits&lt;<span class=\"dt\">int<\/span>&gt;::max();\n    <span class=\"dt\">int<\/span> min = numeric_limits&lt;<span class=\"dt\">int<\/span>&gt;::min();\n    <span class=\"kw\">switch<\/span>( digits ) {\n        <span class=\"co\">\/\/ max = (2^n-1) ; min = -(2^n)<\/span>\n        <span class=\"co\">\/\/ All happen to be identically, Null<\/span>\n        <span class=\"kw\">case<\/span> <span class=\"dv\">15<\/span>:\n        <span class=\"kw\">case<\/span> <span class=\"dv\">23<\/span>:\n        <span class=\"kw\">case<\/span> <span class=\"dv\">31<\/span>:\n        <span class=\"kw\">case<\/span> <span class=\"dv\">63<\/span>:\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(min));\n            CPPUNIT_ASSERT_EQUAL(eFizzBuzz::Null, FizzBuzz(max));\n            <span class=\"kw\">break<\/span>;\n        <span class=\"kw\">default<\/span>:\n            CPPUNIT_ASSERT_EQUAL(<span class=\"dv\">0<\/span>, digits);\n            throw logic_error(<span class=\"st\">&quot;unsupported architecture&quot;<\/span>);\n    }<\/code><\/pre>\n<p>Are we done then? No. We\u00e2\u20ac\u2122ve used a facility in our main application without testing it.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\"><span class=\"dt\">void<\/span> FizzBuzz() {\n    <span class=\"kw\">for<\/span>(<span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt;= <span class=\"dv\">100<\/span>; i++) {\n        <span class=\"dt\">auto<\/span> fb = FizzBuzz(i);\n        <span class=\"kw\">if<\/span>(fb == eFizzBuzz::Null) {\n            cout &lt;&lt; i &lt;&lt; endl;\n        } <span class=\"kw\">else<\/span> {\n            cout &lt;&lt; fb &lt;&lt; endl;\n        }\n    }\n}<\/code><\/pre>\n<p>Our <code>operator&lt;&lt;<\/code> implementation. Testing it is easy though because we\u00e2\u20ac\u2122ve generalised it to work on all streams, we can output to a string instead of the terminal and have <code>cppunit<\/code> check those strings.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    <span class=\"dt\">void<\/span> test_fizzbuzz_operator() {\n        ostringstream null, fizz, buzz, fizzbuzz;\n        null &lt;&lt; eFizzBuzz::Null;\n        CPPUNIT_ASSERT_EQUAL(string(<span class=\"st\">&quot;Null&quot;<\/span>), null.str());\n        fizz &lt;&lt; eFizzBuzz::Fizz;\n        CPPUNIT_ASSERT_EQUAL(string(<span class=\"st\">&quot;Fizz&quot;<\/span>), fizz.str());\n        buzz &lt;&lt; eFizzBuzz::Buzz;\n        CPPUNIT_ASSERT_EQUAL(string(<span class=\"st\">&quot;Buzz&quot;<\/span>), buzz.str());\n        fizzbuzz &lt;&lt; eFizzBuzz::FizzBuzz;\n        CPPUNIT_ASSERT_EQUAL(string(<span class=\"st\">&quot;Fizzbuzz&quot;<\/span>), fizzbuzz.str());\n    }\n\n    <span class=\"co\">\/\/ ...<\/span>\n\n    CPPUNIT_TEST_SUITE(FizzBuzzTest);\n    CPPUNIT_TEST(test_fizzbuzz_operator);\n    CPPUNIT_TEST(test_fizzbuzz);\n    CPPUNIT_TEST_SUITE_END();<\/code><\/pre>\n<p>Output:<\/p>\n<pre><code>!!!FAILURES!!!\nTest Results:\nRun:  2   Failures: 1   Errors: 0\n\n\n1) test: FizzBuzzTest::test_fizzbuzz_operator (F) line: 107 fizzbuzz.cc\nequality assertion failed\n- Expected: Fizzbuzz\n- Actual  : FizzBuzz<\/code><\/pre>\n<p>Hopefully the fault in line 107 is obvious:<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">    CPPUNIT_ASSERT_EQUAL(string(<span class=\"st\">&quot;Fizzbuzz&quot;<\/span>), fizzbuzz.str());<\/code><\/pre>\n<p>I\u00e2\u20ac\u2122ve left the \u00e2\u20ac\u0153buzz\u00e2\u20ac\u009d lowercase.<\/p>\n<p>With that fixed we\u00e2\u20ac\u2122re in a position to be confident about our FizzBuzz implementation. By using a unit test system we\u00e2\u20ac\u2122ve also forced ourselves to not only write a working FizzBuzz, but also a well designed one. You should notice that, strangely, we haven\u00e2\u20ac\u2122t actually tested the actual <code>FizzBuzz()<\/code> function.<\/p>\n<p>As written, this isn\u00e2\u20ac\u2122t testable. I\u00e2\u20ac\u2122m not going to present that because it\u00e2\u20ac\u2122s just another, much larger, string comparison; after a change to make <code>FizzBuzz()<\/code> able to output to a string; and we\u00e2\u20ac\u2122ve already seen how to do that, so it becomes a typing exercise.<\/p>\n<pre class=\"sourceCode C\"><code class=\"sourceCode c\">ostream &amp;FizzBuzz(ostream &amp;s, <span class=\"dt\">int<\/span> n)\n{\n    <span class=\"kw\">for<\/span>(<span class=\"dt\">int<\/span> i = <span class=\"dv\">0<\/span>; i &lt;= n; i++) {\n        <span class=\"dt\">auto<\/span> fb = FizzBuzz(i);\n        <span class=\"kw\">if<\/span>(fb == eFizzBuzz::Null) {\n            s &lt;&lt; i &lt;&lt; endl;\n        } <span class=\"kw\">else<\/span> {\n            s &lt;&lt; fb &lt;&lt; endl;\n        }\n    }\n    <span class=\"kw\">return<\/span> s;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Unit testing is the name we give to the idea of writing code that tests our application code automatically. Essentially, its job is to exercise each piece of your application in isolation, and ensure that any data dependent bugs you might introduce accidentally, never make it into production releases. Personally, I also find it makes\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.fussylogic.co.uk\/blog\/?p=1186\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[66,107,6,106],"_links":{"self":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1186"}],"collection":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1186"}],"version-history":[{"count":3,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1186\/revisions"}],"predecessor-version":[{"id":1189,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1186\/revisions\/1189"}],"wp:attachment":[{"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fussylogic.co.uk\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}