<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Feroze Daud's WebLog : Miscellaneous</title><link>http://blogs.msdn.com/feroze_daud/archive/tags/Miscellaneous/default.aspx</link><description>Tags: Miscellaneous</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Encapsulating Enumeration</title><link>http://blogs.msdn.com/feroze_daud/archive/2008/11/03/encapsulating-enumeration.aspx</link><pubDate>Tue, 04 Nov 2008 04:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9036434</guid><dc:creator>Feroze Daud</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/feroze_daud/comments/9036434.aspx</comments><wfw:commentRss>http://blogs.msdn.com/feroze_daud/commentrss.aspx?PostID=9036434</wfw:commentRss><description>&lt;P&gt;While writing a unit test for a particular feature, I was faced with an interesting problem. My unit test has different scenarios. These scenarios test that a certain data item is propagated all the way down from the called function.&lt;/P&gt;
&lt;P&gt;For eg, I have a method that I need to test - lets call it CFoo:DomSometing().&lt;/P&gt;
&lt;P&gt;We pass in a data item to this method - so as part of the changes I change the method to CFoo::DoSomething(int data)&lt;/P&gt;
&lt;P&gt;I need to make sure, in my tests, that the 'data' item is properly consumed by the function.&lt;/P&gt;
&lt;P&gt;It turns out that for different functions that I call as part of each of my scenario, the way of checking is different. For eg, for one scenario, I have to call the function 'n' times, whereas for another scenario, I have to call the function for a particular duration.&lt;/P&gt;
&lt;P&gt;I want to encapsulate all of this, so that I can write one test function that does the following&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;bool RunTest ( RunTestDelegate testMethod, int data, ILoopIterator iterator) &lt;BR&gt;{ &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (int i in iterator.GetValues()) &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // call the test method &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; testMethod (data); &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;} &lt;BR&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here, the RunTestDelegate will be used to encapsulate each method that I need to test, so that I dont have to write one RunTestXXX method per each method I want to test.&lt;/P&gt;
&lt;P&gt;The question is: how do you implement the ILoopIterator? Clearly, the first requirement is that it should be returning an IEnumerable&amp;lt;int&amp;gt; because that is what the foreach loop is iterating over.&lt;/P&gt;
&lt;P&gt;Also, as discussed above, we need it so that we can iterate over both a range of values, as well as for a time duration.&lt;/P&gt;
&lt;P&gt;In order to solve this, I implemented the interface as follows:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;BR&gt;interface ILoopIterator &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IEnumerable&amp;lt;int&amp;gt; GetValues(); &lt;BR&gt;} 
&lt;P&gt;class LoopIterator : ILoopIterator &lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int count; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private TimeSpan duration; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private bool isCounted; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private LoopIterator (TimeSpan duration)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.duration = duration;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.count = -1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.isCounted = false;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private LoopIterator (int iterationCount)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.count = iterationCount;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.isCounted = true;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.duration = TimeSpan.MaxValue;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static LoopIterator CreateTimed(TimeSpan duration)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new LoopIterator (duration);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static LoopIterator CreateCounted(int iterationCount)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return new LoopIterator (iterationCount);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } 
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public IEnumerable&amp;lt;int&amp;gt; GetValues()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (this.isCounted)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; this.count; i++)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield return i;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i = 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DateTime end = DateTime.Now + this.duration;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (DateTime.Now &amp;lt; end)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield return i;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ++i;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This code used the .NET/2.0 C# features - i.e generics &amp;amp; yield statement for implementing enumerators. 
&lt;P&gt;The caller will use a different factory method, depending on how he wants the iteration to be performed. 
&lt;P&gt;For duration based iteration, use LoopIterator .CreateTimed(TimeSpan duration) 
&lt;P&gt;For normal iteration (like a for loop) use LoopIterator .CreateCounted(int count) 
&lt;P&gt;The utility of this pattern, is that the client (which is using ILoopIterator ) does not need to change!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9036434" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/feroze_daud/archive/tags/Miscellaneous/default.aspx">Miscellaneous</category><category domain="http://blogs.msdn.com/feroze_daud/archive/tags/Design+Patterns/default.aspx">Design Patterns</category></item><item><title>Attaching VS to a process on startup.</title><link>http://blogs.msdn.com/feroze_daud/archive/2006/04/04/Using-ImageFileExecutionOptions.aspx</link><pubDate>Tue, 04 Apr 2006 22:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:568369</guid><dc:creator>Feroze Daud</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/feroze_daud/comments/568369.aspx</comments><wfw:commentRss>http://blogs.msdn.com/feroze_daud/commentrss.aspx?PostID=568369</wfw:commentRss><description>&lt;P&gt;I have been stumped from time to time on how to attach VS debuggers to a process on process startup. I knew how to do it with Windbg, by setting ImageFileExecutionOptions for the target process. However I did not know how to do it for VS.&lt;/P&gt;
&lt;P&gt;Well, I need to fret no more. A colleague forwarded me th is blog entry by Greg, in which he has some good info on how to use ImageFileExecutionOptions to attach to a process on startup.&lt;/P&gt;
&lt;P&gt;&lt;A HREF="/greggm/archive/2005/02/21/377663.aspx"&gt;http://blogs.msdn.com/greggm/archive/2005/02/21/377663.aspx&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=568369" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/feroze_daud/archive/tags/Miscellaneous/default.aspx">Miscellaneous</category></item><item><title>Printers are watching you!</title><link>http://blogs.msdn.com/feroze_daud/archive/2004/11/22/268270.aspx</link><pubDate>Tue, 23 Nov 2004 03:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:268270</guid><dc:creator>Feroze Daud</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/feroze_daud/comments/268270.aspx</comments><wfw:commentRss>http://blogs.msdn.com/feroze_daud/commentrss.aspx?PostID=268270</wfw:commentRss><description>&lt;p&gt;Someone sent me a link which talks about how printers are embedding tracking information in pages that they print. Spooky!&lt;/p&gt; &lt;p&gt;&lt;a href="http://story.news.yahoo.com/news?tmpl=story&amp;amp;cid=1093&amp;amp;e=4&amp;amp;u=/pcworld/20041122/tc_pcworld/118664"&gt;http://story.news.yahoo.com/news?tmpl=story&amp;amp;cid=1093&amp;amp;e=4&amp;amp;u=/pcworld/20041122/tc_pcworld/118664&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=268270" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/feroze_daud/archive/tags/Miscellaneous/default.aspx">Miscellaneous</category></item></channel></rss>