VS2005
. .
. , .
1.
1.1.
. VisualStudio 2005 Team Suite.
. , , .
:
1.
2.
3.
4.
.
1.2.
.
:
( )
, .
0! 0!!
.
, . . ( )
.
1.3.
VisualStudio 2005 Team Suite.
VisualStudio 2005 Express Edition VisualStudio 2005 Professional .
1.4.
MathLib. . , .
TDD, . , .
2.
2.1.
File -> New -> Project
( , Test) .
.
(. ). : , .
2.2.
1. Solution Explorer -> TestMathLib -> -> Add Reference
2. Projects
3. MathLib
3.
:
: , ?
.
?
. .
?
. : . . .
3.1.
SimpleFactorial.sc
using MathLib;
#region Additional test attributes. .
:
5!==120
:
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MathLib;
namespace TestMathLib
{
[TestClass]
public class SimpleFactorial
{
public SimpleFactorial()
{
}
[TestMethod]
public void TestSimpleCalc()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(120L, factorial.Calculate(5));
}
}
}
:
[TestMethod] , , .
Assert.AreEqual() - .
3.1.1.
. Shift+Alt+X. : Test-> Start Selected Test Project with Debugger .
3.1.2.
. .
, Code Coverage Results ( ).
, . , SimpleFactorial.cs
, . number < 0 , number > 15 .
:
if(number < 0 || number > 15) 1
throw new ArgumentOutOfRangeException(”number”); - 2
return 1L; - 1
3.2.
3.2.1.
.
Solution Explorer -> TestMathLib -> Add -> New test -> Unit Test
: SimpleFactorialBoundTest.cs
( ).
.. , . , . :
[ExpectedException(typeof(ArgumentOutOfRangeException))]
.
3.2.2.
, .
1. Test -> Windows -> Test Manager
. , , .
.
1.
2. , ( ).
3. drag&drop.
.
1. , . List of Tests
2. Ctrl+Shift+X
: , .
3.3.
. . , .
:
0 ( )
1-
2
3
, 3 , 2 1 . .
, . [TestMethod] [Priority(2), TestMethod]. - , .
4.
. , . , . , (, ), - .
SimpleFactorial.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MathLib;
namespace TestMathLib
{
[TestClass]
public class SimpleFactorial
{
public SimpleFactorial()
{
}
[Priority(3), TestMethod]
public void TestSimpleCalc()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(120L, factorial.Calculate(5));
}
}
}
SimpleFactorialBoundTest.cs
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MathLib;
namespace TestMathLib
{
[TestClass]
public class SimpleFactorialBoundTest
{
public SimpleFactorialBoundTest()
{
}
[Priority(1), TestMethod]
public void TestBottomBound()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(1L, factorial.Calculate(0));
}
[Priority(2), TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestBottomBoundOut()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
factorial.Calculate(-1);
}
[Priority(1), TestMethod]
public void TestBottomBoundIn()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(1L, factorial.Calculate(1));
}
[Priority(1), TestMethod]
public void TestTopBound()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(1307674368000L, factorial.Calculate(15));
}
[Priority(1), TestMethod]
public void TestTopBoundIn()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
Assert.AreEqual(87178291200L, factorial.Calculate(14));
}
[Priority(2), TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestTopBoundOut()
{
IFactorial factorial = FactorialProvider.CreateInstance(FactorialType.Simple);
factorial.Calculate(16);
}
}
}
SimpleFactorial.cs
using System;
namespace MathLib
{
internal class SimpleFactorial : IFactorial
{
internal SimpleFactorial()
{
}
long IFactorial.Calculate(int number)
{
if(number < 0 || number > 15)
throw new ArgumentOutOfRangeException(”number”);
if(number == 0)
return 1L;
long result = 1;
for(int i = 1; i <= number; i++)
{
result *= i;
}
return result;
}
}
}
OddFactorial.cs
using System;
namespace MathLib
{
internal class OddFactorial : IFactorial
{
internal OddFactorial()
{
}
long IFactorial.Calculate(int number)
{
if (number < 0 || number > 15)
throw new ArgumentOutOfRangeException(”number”);
if (number == 0 || number == 1)
return 1L;
long result = 1;
for (int i = number; i > 0; i -= 2)
{
result *= i;
}
return result;
}
}
}
IFactorial.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace MathLib
{
///
/// Calculates factorial from 0 to 25.
///
public interface IFactorial
{
///
///
///
///
long Calculate(int number);
}
}
FactorialProvider.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace MathLib
{
public enum FactorialType
{
Simple,
Odd,
Test
}
public static class FactorialProvider
{
public static IFactorial CreateInstance(FactorialType type)
{
switch(type)
{
case FactorialType.Odd :
return new OddFactorial();
case FactorialType.Simple:
return new SimpleFactorial();
default :
return new SimpleFactorial();
}
}
}
}
21 2007 14:19
[] . . . , . ? , . , ftp , . []