測試代碼:
using System;
using NUnit.Framework;
namespace Yyw
{
[TestFixture]
public class DateTimeHelperFixture
{
/**//// <summary>
/// 邊界測試
/// </summary>
[Test]
public void TestGetRandomTime()
{
DateTime minTime = DateTime.MinValue;
DateTime maxTime = DateTime.MaxValue;
Random random = new Random();
for (int i = 0; i < 10000; i++)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(maxTime, minTime, random);
CheckResult(minTime, maxTime, randomTime);
}
for (int i = 0; i < 10000; i++)
{
DateTime randomTime = DateTimeHelper.GetRandomTime(minTime, maxTime, random);
CheckResult(minTime, maxTime, randomTime);
}
}
private void CheckResult(DateTime minTime, DateTime maxTime, DateTime randomTime)
{
System.TimeSpan ts1 = new System.TimeSpan(randomTime.Ticks - minTime.Ticks);
System.TimeSpan ts2 = new System.TimeSpan(randomTime.Ticks - maxTime.Ticks);
Assert.IsTrue(ts1.Seconds >= 0 && ts2.Seconds <= 0);
}
}
}