title: Factorial function - design and test
body: <p>I'm trying to nail down some interview questions, so I stared with a simple one.</p>
  
  <p><strong>Design the factorial function.</strong></p>
  
  <p>This function is a leaf (no dependencies - easly testable), so I made it static inside the helper class.</p>
  
  <pre><code>public static class MathHelper
  {
      public static int Factorial(int n)
      {
          Debug.Assert(n &gt;= 0);
          if (n &lt; 0)
          {
              throw new ArgumentException("n cannot be lower that 0");
          }
  
          Debug.Assert(n &lt;= 12);
          if (n &gt; 12)
          {
              throw new OverflowException("Overflow occurs above 12 factorial");
          }
  
          int factorialOfN = 1;
          for (int i = 1; i &lt;= n; ++i)
          {
              //checked
              //{
                  factorialOfN *= i;   
              //}
          }
          return factorialOfN;
      }
  }
  </code></pre>
  
  <p>Testing:</p>
  
  <pre><code>    [TestMethod]
      [ExpectedException(typeof(OverflowException))]
      public void Overflow()
      {
        int temp = FactorialHelper.MathHelper.Factorial(40);
      }
  
      [TestMethod]
      public void ZeroTest()
      {
          int factorialOfZero = FactorialHelper.MathHelper.Factorial(0);
          Assert.AreEqual(1, factorialOfZero);
      }
  
      [TestMethod]
      public void FactorialOf5()
      {
         int factOf5 = FactorialHelper.MathHelper.Factorial(5);
         Assert.AreEqual(5*4*3*2*1,factOf5);
      }
  
      [TestMethod]
      [ExpectedException(typeof(ArgumentException))]
      public void NegativeTest()
      {
          int factOfMinus5 = FactorialHelper.MathHelper.Factorial(-5);
      }
  </code></pre>
  
  <p>I have a few questions:</p>
  
  <ol>
  <li>Is it correct? (I hope so ;) )</li>
  <li>Does it throw right exceptions?</li>
  <li>Should I use <em>checked</em> context or this trick ( n > 12 ) is ok? </li>
  <li>Is it better to use uint istead of checking for negative values?</li>
  <li>Future improving: Overload for long, decimal, BigInteger or maybe generic method?</li>
  </ol>
  
  <p>Thank you</p>