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

<channel>
	<title>The Many Hats of Jason Specland &#187; c</title>
	<atom:link href="http://www.jasonspecland.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jasonspecland.com</link>
	<description>The mostly self-deprecating story of a programmer, performer, and daddy.</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:51:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Unit Testing: Rhino Mocks, Ordered Mocks, and Events</title>
		<link>http://www.jasonspecland.com/2010/09/16/unit-testing-rhino-mocks-ordered-mocks-and-events/</link>
		<comments>http://www.jasonspecland.com/2010/09/16/unit-testing-rhino-mocks-ordered-mocks-and-events/#comments</comments>
		<pubDate>Thu, 16 Sep 2010 16:23:11 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[rhino mocks]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.jasonspecland.com/?p=188</guid>
		<description><![CDATA[Tired of all that improv show talk? Well, now it&#8217;s time for me to get my geek on. Non-computer geeks feel free to tune out. This is actually more along the lines of &#8220;put it out there for the sake &#8230; <a href="http://www.jasonspecland.com/2010/09/16/unit-testing-rhino-mocks-ordered-mocks-and-events/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Tired of all that improv show talk?  Well, now it&#8217;s time for me to get my geek on.  Non-computer geeks feel free to tune out.  This is actually more along the lines of &#8220;put it out there for the sake of future Googlers&#8221; than a serious discussion.</p>
<p>I&#8217;ve had test-first religion for some time now, but deadlines have made me something of a lapsed tester.  After finding a pretty bad-ass bug in my code, I got religion again, admitted I was a sinner, and went forward in Testing.  I am redeeming myself by writing a few hundred unit tests of already existing code.</p>
<p>My preferred mock framework is Rhino Mocks.  Now I&#8217;m all about the AAA (Arrange, Act, Assert) syntax.  99.9% of my tests (seriously, I&#8217;ve got over a thousand of them) are AAA.  Most of my tests are unordered.  I just want to test that something was called, and I don&#8217;t care what was called before or after it.</p>
<p>But you can&#8217;t do AAA syntax when you&#8217;re doing ordered tests.  It hasn&#8217;t been a problem thus far, but I ran into a problem when I started testing objects that hooked up events in the constructor: So my code looked like this:</p>
<pre class="brush: c#; ">

namespace RhinoMocksOrdredWithEvents {
    public interface IOrderedThing {
        void FirstThing();
        void SecondThing();
        void SomethingElse();
        event EventHandler&lt;EventArgs&gt; OnThingHappened;
    }

    public class MyTestClass {

        private IOrderedThing MyOrderedThing { get; set; }

        public MyTestClass(IOrderedThing orderedThing) {
            MyOrderedThing = orderedThing;
            MyOrderedThing.OnThingHappened += new EventHandler&lt;EventArgs&gt;(MyOrderedThing_OnThingHappened);
            MyOrderedThing.SomethingElse();
        }

        public void DoThingsInOrder() {

            MyOrderedThing.SomethingElse();

            MyOrderedThing.FirstThing();
            MyOrderedThing.SecondThing();
        }

        private void MyOrderedThing_OnThingHappened(object sender, EventArgs e) {
            return;
        }

    }
}
</pre>
<p>And here&#8217;s the test to make sure that FirstThing() and SecondThing() are called in order:</p>
<pre class="brush: c#; ">

namespace RhinoMocksOrdredWithEvents {
    [TestClass]
    public class OrderedMockTests { 

        [TestMethod]
        public void TestThingsAreRunInOrder() {
            // Arrange
            MockRepository mocks = new MockRepository(); 

            IOrderedThing mockOrderedThing = mocks.DynamicMock&lt;IOrderedThing&gt;(); 

            MyTestClass classUnderTest = new MyTestClass(mockOrderedThing); 

            using (mocks.Record()) {
                using (mocks.Ordered()) {
                    mockOrderedThing.Expect(thing =&gt; thing.FirstThing());
                    mockOrderedThing.Expect(thing =&gt; thing.SecondThing());
                }
            } 

            mockOrderedThing.Replay(); 

            // Act
            classUnderTest.DoThingsInOrder(); 

            // Assert
            mockOrderedThing.VerifyAllExpectations();
        }
    }
} 
</pre>
<p>Be not fooled by my spurious use of the //Arange, //Act, and //Assert comments.  This is most definitely not the AAA syntax.</p>
<p>So when I tried to run this test, I got the following error:</p>
<p><code><br />
Test method<br />
RhinoMocksOrdredWithEvents.OrderedMockTests.TestThingsAreRunInOrder<br />
threw exception:<br />
Rhino.Mocks.Exceptions.ExpectationViolationException:<br />
IOrderedThing.add_OnThingHappened(System.EventHandler`1[System.EventArgs]);<br />
Expected #1, Actual #0..<br />
</code></p>
<p>What what what!?  I never told Rhino Mocks that I expected the event to be wired!  And anyway, it <i>is</i> wired, right there in the constructor, see?  Then I comment out the event hookup, and the test works!  So off I sauntered to the Rhino Mocks mailing list all like, &#8220;Hey guys, I don&#8217;t mean to be all smug and stuff, but all y&#8217;all got a bug.&#8221;</p>
<p>Thankfully, <a href="http://devlicio.us/blogs/tim_barcz/">Tim Barcz</a> on the mailing list set me straight.</p>
<p>Yes, Virginia, you <i>can</i> do the AAA syntax in an ordered mock, and this is how you do it:</p>
<pre class="brush: c#; ">

namespace RhinoMocksOrdredWithEvents {
    [TestClass]
    public class OrderedMockTests {

        [TestMethod]
        public void TestThingsAreRunInOrder() {
            // Arrange
            //MockRepository mocks = new MockRepository();

            IOrderedThing mockOrderedThing = MockRepository.GenerateMock&lt;IOrderedThing&gt;();

            MyTestClass classUnderTest = new MyTestClass(mockOrderedThing);

            mockOrderedThing.GetMockRepository().Ordered();

            mockOrderedThing.Expect(thing =&gt; thing.FirstThing());
            mockOrderedThing.Expect(thing =&gt; thing.SecondThing());

            // Act
            classUnderTest.DoThingsInOrder();

            // Assert
            mockOrderedThing.VerifyAllExpectations();

        }
    }
}
</pre>
<p>Which works no matter what strange stuff you want to do in the constructor.</p>
<p>Much thanks to Tim Barcz for setting me straight.  Here is <a href="http://groups.google.com/group/rhinomocks/browse_thread/thread/7bfe0b3e2fc59baf/2dbbdc920e78f10a#2dbbdc920e78f10a">the original thread on the Rhino Mocks mailing list</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonspecland.com/2010/09/16/unit-testing-rhino-mocks-ordered-mocks-and-events/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Praxis: Goldbach&#8217;s Conjecture</title>
		<link>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/</link>
		<comments>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 03:26:26 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[praxis]]></category>

		<guid isPermaLink="false">http://www.jasonspecland.com/?p=72</guid>
		<description><![CDATA[Since I mentioned in my last post that I live in fear of the tech interview, I decided to do something about it rather than live in fear. To that end, I subscribed to a website called Programming Praxis, which &#8230; <a href="http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since I mentioned in my last post that I live in fear of the tech interview, I decided to do something about it rather than live in fear.  To that end, I subscribed to a website called Programming Praxis, which publishes short programming problems to sharpen your saw on.  (Well, I&#8217;d actually been a subscriber for a while, but it&#8217;s just now that I&#8217;ve actually took the time to do my own saw sharpening.)</p>
<p>Geek Content Ahead.  Feel Free to Ignore.</p>
<p><a href="http://programmingpraxis.com/2010/03/02/goldbachs-conjecture/">Today&#8217;s Praxis</a> is on the <a href="http://en.wikipedia.org/wiki/Goldbach_conjecture">Goldbach Conjecture</a> which states that any even number greater than 2 can be expressed as the sum of two primes.  The challenge is to write a program that will take in an even number, and spit out the two primes that can be added together to make it.</p>
<p>I realize my solution is extremely naive, but it&#8217;s the first time in a long time that I&#8217;ve actually worked on programming problems other than &#8220;get data from database a, transform it somehow, and put it in database b.&#8221;  Also, for extra difficulty (and speed, although my algorithm is the real speed hangup in this case) I wrote it in C.</p>
<p>For the one or two people still reading, here is my extremely naive solution, cobbled together while watching West Side Story on TCM.</p>
<pre class="brush: cpp; ">

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void show_usage();
unsigned long *create_sieve_to_number(unsigned long number);

int main (int argc, const char * argv[]) {

    if (argc != 2) {
		show_usage();
		return -1;
	}

	unsigned long number = atol(argv[1]);

	if ((number % 2) != 0) {
		show_usage();
		return -1;
	}

	unsigned long *sieve = create_sieve_to_number(number);

	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i; j &lt; number; j++) {
				if (sieve[j] == 1) {
					if (i + j == number) {
						printf(&quot;Solution found: %d + %d\n&quot;, i, j);
						return 0;
					}
				}
			}
		}
	}

	printf(&quot;no solution found!  pick up your Fields Medal!\n&quot;);

	return 0;

}

void show_usage(void) {
	printf(&quot;usage: goldbach [even number]\n&quot;);
}

unsigned long *create_sieve_to_number(unsigned long number) {
	unsigned long *sieve;

	sieve = (unsigned long *)malloc(sizeof(unsigned long) * (number + 1));

	for (int i = 0; i &lt; number; i++) {
		sieve[i] = 1;
	}

	for (unsigned long i = 2; i &lt; number; i++) {
		if (sieve[i] == 1) {
			for (unsigned long j = i * i; j &lt; number; j = j + i) {
				sieve[j] = 0;
			}
		}
	}

	return sieve;
}
</pre>
<p><b>Edit:</b> See what happens when you&#8217;re watching TV when you&#8217;re doing your homework?  The flags marking numbers as prime can be a bool (or even a bit) and most certainly does <i>not</i> have to be an unsigned long.  Yay for massive, massive wastes of memory!  (But hey, I <i>was</i> watching West Side Story&#8230;)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jasonspecland.com/2010/03/02/programming-praxis-goldbachs-conjecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

