1+ using System . Numerics ;
2+ using Algorithms . Numeric ;
3+
4+ namespace Algorithms . Tests . Numeric ;
5+
6+ /// <summary>
7+ /// Tests for the DoubleFactorial class methods.
8+ /// </summary>
9+ public static class DoubleFactorialTests
10+ {
11+ /// <summary>
12+ /// Tests the calculation of double factorial for non-negative integers.
13+ /// Includes base cases (0, 1), odd numbers (5, 11), even numbers (6, 12),
14+ /// and a large number (20) that benefits from BigInteger.
15+ /// </summary>
16+ /// <param name="input">The number N to calculate N!!.</param>
17+ /// <param name="expected">The expected result as a string (for BigInteger parsing).</param>
18+ [ TestCase ( 0 , "1" ) ] // Base Case: 0!! = 1
19+ [ TestCase ( 1 , "1" ) ] // Base Case: 1!! = 1
20+ [ TestCase ( 5 , "15" ) ] // Odd: 5 * 3 * 1 = 15
21+ [ TestCase ( 6 , "48" ) ] // Even: 6 * 4 * 2 = 48
22+ [ TestCase ( 11 , "10395" ) ] // Larger Odd: 11 * 9 * 7 * 5 * 3 * 1 = 10395
23+ [ TestCase ( 12 , "46080" ) ] // Larger Even: 12 * 10 * 8 * 6 * 4 * 2 = 46080
24+ [ TestCase ( 20 , "3715891200" ) ] // Large Even
25+ public static void GetsDoubleFactorial ( int input , string expected )
26+ {
27+ // Arrange
28+ BigInteger expectedBigInt = BigInteger . Parse ( expected ) ;
29+
30+ // Act
31+ var result = DoubleFactorial . Calculate ( input ) ;
32+
33+ // Assert
34+ Assert . That ( result , Is . EqualTo ( expectedBigInt ) ) ;
35+ }
36+
37+ /// <summary>
38+ /// Tests that calculating double factorial for negative numbers throws an ArgumentException.
39+ /// </summary>
40+ /// <param name="num">A negative integer input.</param>
41+ [ TestCase ( - 1 ) ]
42+ [ TestCase ( - 5 ) ]
43+ [ TestCase ( - 10 ) ]
44+ public static void GetsDoubleFactorialExceptionForNegativeNumbers ( int num )
45+ {
46+ // Arrange
47+
48+ // Act
49+ void Act ( ) => DoubleFactorial . Calculate ( num ) ;
50+
51+ // Assert
52+ _ = Assert . Throws < ArgumentException > ( Act ) ;
53+ }
54+ }
0 commit comments