Skip to content

Commit fddf1a0

Browse files
committed
Merge pull request #49 from Tynamix/BUGFIX_DateRange
DateTime Range fix, random generator for long implemented
2 parents f37aa42 + 9f545e2 commit fddf1a0

File tree

5 files changed

+99
-9
lines changed

5 files changed

+99
-9
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace ObjectFiller.Test
2+
{
3+
using System;
4+
using System.Linq;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
using Tynamix.ObjectFiller;
9+
10+
public class DateRangeTestClass
11+
{
12+
public DateTime Date { get; set; }
13+
}
14+
15+
[TestClass]
16+
public class DateTimeRangeTest
17+
{
18+
[TestMethod]
19+
public void WhenGettingDatesBetweenNowAnd31DaysAgo()
20+
{
21+
Filler<DateRangeTestClass> filler = new Filler<DateRangeTestClass>();
22+
23+
filler.Setup().OnType<DateTime>().Use(
24+
new DateTimeRange(DateTime.Now.AddDays(-31)));
25+
26+
var d = filler.Create(1000);
27+
28+
Assert.IsTrue(d.All(x => x.Date < DateTime.Now && x.Date > DateTime.Now.AddDays(-31)));
29+
}
30+
31+
[TestMethod]
32+
public void WhenStartDateIsBiggerThenEndDateTheDatesShouldBeSwitched()
33+
{
34+
Filler<DateRangeTestClass> filler = new Filler<DateRangeTestClass>();
35+
36+
filler.Setup().OnType<DateTime>().Use(
37+
new DateTimeRange(DateTime.Now, DateTime.Now.AddDays(-31)));
38+
39+
var d = filler.Create(1000);
40+
41+
Assert.IsTrue(d.All(x => x.Date < DateTime.Now && x.Date > DateTime.Now.AddDays(-31)));
42+
}
43+
44+
[TestMethod]
45+
public void WhenStartDateAndEndDateIsSetItShouldFindOnlyDatesInBetweenThisTwoDates()
46+
{
47+
var startDate = new DateTime(2000, 11, 10);
48+
var endDate = new DateTime(2006, 1, 30);
49+
50+
Filler<DateRangeTestClass> filler = new Filler<DateRangeTestClass>();
51+
52+
filler.Setup().OnType<DateTime>().Use(new DateTimeRange(startDate, endDate));
53+
54+
var d = filler.Create(1000);
55+
d.ToList().ForEach(x => Console.WriteLine(x.Date));
56+
Assert.IsTrue(d.All(x => x.Date < endDate && x.Date > startDate));
57+
}
58+
}
59+
}

ObjectFiller.Test/ObjectFiller.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<Compile Include="CountryNamesPlugin.cs" />
5050
<Compile Include="CityNamesPluginTest.cs" />
5151
<Compile Include="CreateInstanceTest.cs" />
52+
<Compile Include="DateTimeRangeTest.cs" />
5253
<Compile Include="DefaultDatatypeMappingsTest.cs" />
5354
<Compile Include="EnumTest.cs" />
5455
<Compile Include="StreetNamesPluginTest.cs" />

ObjectFiller/Plugins/DateTime/DateTimeRange.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ public DateTimeRange(DateTime earliestDate)
1414

1515
public DateTimeRange(DateTime earliestDate, DateTime latestDate)
1616
{
17+
if (earliestDate > latestDate)
18+
{
19+
var latestD = latestDate;
20+
latestDate = earliestDate;
21+
earliestDate = latestD;
22+
}
1723
_earliestDate = earliestDate;
1824
_latestDate = latestDate;
1925
}
2026

2127
public DateTime GetValue()
2228
{
23-
int seconds = Random.Next(0, 60);
24-
int minute = Random.Next(0, 60);
25-
int hour = Random.Next(0, 24);
26-
int month = Random.Next(_earliestDate.Month, _latestDate.Month + 1);
27-
int day = Random.Next(_earliestDate.Day, month == 2 && _latestDate.Day > 28 ? 29 : _latestDate.Day);
28-
int year = Random.Next(_earliestDate.Year, _latestDate.Year + 1);
29+
var timeSpan = _latestDate.Subtract(_earliestDate);
2930

30-
return new DateTime(year, month, day, hour, minute, seconds);
31+
var diff = Random.NextLong(0, timeSpan.Ticks);
32+
33+
return _latestDate.AddTicks(diff * -1);
3134
}
3235

3336
DateTime? IRandomizerPlugin<DateTime?>.GetValue()

ObjectFiller/Random.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Tynamix.ObjectFiller
1212
{
13+
using System;
14+
1315
/// <summary>
1416
/// Class wraps the <see cref="System.Random"/> class.
1517
/// This is to have a static instance of the random class
@@ -92,5 +94,30 @@ internal static void NextByte(byte[] buffer)
9294
{
9395
Rnd.NextBytes(buffer);
9496
}
97+
98+
/// <summary>
99+
/// Gets a random value between to source long values
100+
/// </summary>
101+
/// <param name="min">Min long value</param>
102+
/// <param name="max">Max long value</param>
103+
/// <returns>A Long between <see cref="min"/> and <see cref="max"/></returns>
104+
internal static long NextLong(long min, long max)
105+
{
106+
long longRand = NextLong();
107+
return (Math.Abs(longRand % (max - min)) + min);
108+
}
109+
110+
/// <summary>
111+
/// Gets a random value between to source long values
112+
/// </summary>
113+
/// <param name="min">Min long value</param>
114+
/// <param name="max">Max long value</param>
115+
/// <returns>A Long between <see cref="min"/> and <see cref="max"/></returns>
116+
internal static long NextLong()
117+
{
118+
byte[] buf = new byte[8];
119+
Rnd.NextBytes(buf);
120+
return BitConverter.ToInt64(buf, 0);
121+
}
95122
}
96123
}

ObjectFiller/Setup/FillerSetupItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ private void SetDefaultRandomizer()
120120
this.TypeToRandomFunc[typeof(short?)] = () => (short)Random.Next(-32767, 32767);
121121
this.TypeToRandomFunc[typeof(int)] = () => Random.Next();
122122
this.TypeToRandomFunc[typeof(int?)] = () => Random.Next();
123-
this.TypeToRandomFunc[typeof(long)] = () => (long)Random.Next();
124-
this.TypeToRandomFunc[typeof(long?)] = () => (long)Random.Next();
123+
this.TypeToRandomFunc[typeof(long)] = () => Random.NextLong();
124+
this.TypeToRandomFunc[typeof(long?)] = () => Random.NextLong();
125125
this.TypeToRandomFunc[typeof(float)] = () => (float)doublePlugin.GetValue();
126126
this.TypeToRandomFunc[typeof(float?)] = () => (float?)doublePlugin.GetValue();
127127
this.TypeToRandomFunc[typeof(double)] = () => doublePlugin.GetValue();

0 commit comments

Comments
 (0)