Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a322dba

Browse files
committed
Fix System.Globalization.Calendar tests to use non-static RNG
1 parent d361504 commit a322dba

32 files changed

+391
-337
lines changed

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarAddMonths.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class GregorianCalendarAddMonths
1212
{
1313
private const int c_MIN_MONTHS_NUMBER = -120000;
1414
private const int c_MAX_MONTHS_NUMBER = 120000;
15+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
1516

1617
#region Positive tests
1718
// PosTest1: Add zero month to the specified date time
@@ -37,7 +38,7 @@ public void PosTest2()
3738
int months;
3839
DateTime resultingTime;
3940
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
40-
months = TestLibrary.Generator.GetInt32(-55) % c_MAX_MONTHS_NUMBER + 1;
41+
months = _generator.GetInt32(-55) % c_MAX_MONTHS_NUMBER + 1;
4142
initialTime = myCalendar.MinSupportedDateTime;
4243
resultingTime = myCalendar.AddMonths(initialTime, months);
4344

@@ -52,7 +53,7 @@ public void PosTest3()
5253
int months;
5354
DateTime resultingTime;
5455
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
55-
months = -1 * TestLibrary.Generator.GetInt32(-55) % c_MAX_MONTHS_NUMBER - 1;
56+
months = -1 * _generator.GetInt32(-55) % c_MAX_MONTHS_NUMBER - 1;
5657
initialTime = myCalendar.MaxSupportedDateTime;
5758
resultingTime = myCalendar.AddMonths(initialTime, months);
5859
VerifyAddMonthsResult(myCalendar, initialTime, resultingTime, months);
@@ -69,7 +70,7 @@ public void PosTest4()
6970
months = -1;
7071
long maxTimeInTicks = myCalendar.MaxSupportedDateTime.Ticks;
7172
long minTimeInTikcs = myCalendar.MinSupportedDateTime.Ticks;
72-
initialTime = new DateTime(TestLibrary.Generator.GetInt64(-55) % (maxTimeInTicks + 1));
73+
initialTime = new DateTime(_generator.GetInt64(-55) % (maxTimeInTicks + 1));
7374
resultingTime = myCalendar.AddMonths(initialTime, months);
7475
VerifyAddMonthsResult(myCalendar, initialTime, resultingTime, months);
7576
}
@@ -157,7 +158,7 @@ public void NegTest1()
157158
int months;
158159
System.Globalization.Calendar myCalendar;
159160
time = DateTime.Now;
160-
months = -1 * (c_MAX_MONTHS_NUMBER + 1 + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - c_MAX_MONTHS_NUMBER));
161+
months = -1 * (c_MAX_MONTHS_NUMBER + 1 + _generator.GetInt32(-55) % (int.MaxValue - c_MAX_MONTHS_NUMBER));
161162
myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
162163
Assert.Throws<ArgumentOutOfRangeException>(() =>
163164
{
@@ -173,7 +174,7 @@ public void NegTest2()
173174
int months;
174175
System.Globalization.Calendar myCalendar;
175176
time = DateTime.Now;
176-
months = c_MAX_MONTHS_NUMBER + 1 + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - c_MAX_MONTHS_NUMBER);
177+
months = c_MAX_MONTHS_NUMBER + 1 + _generator.GetInt32(-55) % (int.MaxValue - c_MAX_MONTHS_NUMBER);
177178
myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
178179
Assert.Throws<ArgumentOutOfRangeException>(() =>
179180
{

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarAddYears.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace System.Globalization.CalendarsTests
1010
// GregorianCalendar.AddYears(DateTime, int)
1111
public class GregorianCalendarAddYears
1212
{
13+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
14+
1315
#region Positive tests
1416
// PosTest1: Add zero year to the specified date time
1517
[Fact]
@@ -64,7 +66,7 @@ public void PosTest4()
6466
years = 1;
6567
long maxTimeInTicks = myCalendar.MaxSupportedDateTime.Ticks;
6668
long minTimeInTikcs = myCalendar.MinSupportedDateTime.Ticks;
67-
initialTime = new DateTime(TestLibrary.Generator.GetInt64(-55) % maxTimeInTicks);
69+
initialTime = new DateTime(_generator.GetInt64(-55) % maxTimeInTicks);
6870
resultingTime = myCalendar.AddYears(initialTime, years);
6971
VerifyAddyearsResult(myCalendar, initialTime, resultingTime, years);
7072
}

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarGetDaysInMonth.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace System.Globalization.CalendarTests
1010
// GregorianCalendar.GetDaysInMonth(Int32, Int32)
1111
public class GregorianCalendarGetDaysInMonth
1212
{
13+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
14+
1315
private static readonly int[] s_daysInMonth365 =
1416
{
1517
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
@@ -32,7 +34,7 @@ public void PosTest1()
3234
//Get a random value beween 1 and 12 not including 2.
3335
do
3436
{
35-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
37+
month = _generator.GetInt32(-55) % 12 + 1;
3638
} while (2 == month);
3739
expectedDays = s_daysInMonth366[month];
3840

@@ -79,7 +81,7 @@ public void PosTest4()
7981
//Get a random value beween 1 and 12 not including 2.
8082
do
8183
{
82-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
84+
month = _generator.GetInt32(-55) % 12 + 1;
8385
} while (2 == month);
8486
expectedDays = s_daysInMonth365[month];
8587
actualDays = myCalendar.GetDaysInMonth(year, month);
@@ -95,7 +97,7 @@ public void PosTest5()
9597
int expectedDays, actualDays;
9698
year = myCalendar.MaxSupportedDateTime.Year;
9799
//Get a random month whose value is beween 1 and 12
98-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
100+
month = _generator.GetInt32(-55) % 12 + 1;
99101
expectedDays = (IsLeapYear(year)) ? s_daysInMonth366[month] : s_daysInMonth365[month];
100102
actualDays = myCalendar.GetDaysInMonth(year, month);
101103
Assert.Equal(expectedDays, actualDays);
@@ -110,7 +112,7 @@ public void PosTest6()
110112
int expectedDays, actualDays;
111113
year = myCalendar.MaxSupportedDateTime.Year;
112114
//Get a random month whose value is beween 1 and 12
113-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
115+
month = _generator.GetInt32(-55) % 12 + 1;
114116
expectedDays = (IsLeapYear(year)) ? s_daysInMonth366[month] : s_daysInMonth365[month];
115117
actualDays = myCalendar.GetDaysInMonth(year, month);
116118
Assert.Equal(expectedDays, actualDays);
@@ -125,7 +127,7 @@ public void PosTest7()
125127
int expectedDays, actualDays;
126128
year = GetAYear(myCalendar);
127129
//Get a random month whose value is beween 1 and 12
128-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
130+
month = _generator.GetInt32(-55) % 12 + 1;
129131
expectedDays = (IsLeapYear(year)) ? s_daysInMonth366[month] : s_daysInMonth365[month];
130132
actualDays = myCalendar.GetDaysInMonth(year, month);
131133
Assert.Equal(expectedDays, actualDays);
@@ -168,7 +170,7 @@ public void NegTest1()
168170
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
169171
int year, month;
170172
year = myCalendar.MaxSupportedDateTime.Year + 100;
171-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
173+
month = _generator.GetInt32(-55) % 12 + 1;
172174
Assert.Throws<ArgumentOutOfRangeException>(() =>
173175
{
174176
myCalendar.GetDaysInMonth(year, month);
@@ -182,7 +184,7 @@ public void NegTest2()
182184
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
183185
int year, month;
184186
year = myCalendar.MinSupportedDateTime.Year - 100;
185-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
187+
month = _generator.GetInt32(-55) % 12 + 1;
186188
Assert.Throws<ArgumentOutOfRangeException>(() =>
187189
{
188190
myCalendar.GetDaysInMonth(year, month);
@@ -196,7 +198,7 @@ public void NegTest3()
196198
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
197199
int year, month;
198200
year = GetAYear(myCalendar);
199-
month = 13 + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - 12);
201+
month = 13 + _generator.GetInt32(-55) % (int.MaxValue - 12);
200202
Assert.Throws<ArgumentOutOfRangeException>(() =>
201203
{
202204
myCalendar.GetDaysInMonth(year, month);
@@ -210,7 +212,7 @@ public void NegTest4()
210212
System.Globalization.Calendar myCalendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish);
211213
int year, month;
212214
year = myCalendar.MinSupportedDateTime.Year - 100;
213-
month = -1 * TestLibrary.Generator.GetInt32(-55);
215+
month = -1 * _generator.GetInt32(-55);
214216
Assert.Throws<ArgumentOutOfRangeException>(() =>
215217
{
216218
myCalendar.GetDaysInMonth(year, month);
@@ -238,7 +240,7 @@ private int GetAYear(Calendar calendar)
238240
int maxYear, minYear;
239241
maxYear = calendar.MaxSupportedDateTime.Year;
240242
minYear = calendar.MinSupportedDateTime.Year;
241-
retVal = minYear + TestLibrary.Generator.GetInt32(-55) % (maxYear + 1 - minYear);
243+
retVal = minYear + _generator.GetInt32(-55) % (maxYear + 1 - minYear);
242244
return retVal;
243245
}
244246

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarGetDaysInYear.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class GregorianCalendarGetDaysInYear
1212
{
1313
private const int c_DAYS_IN_LEAP_YEAR = 366;
1414
private const int c_DAYS_IN_COMMON_YEAR = 365;
15+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
16+
1517
#region Positive tests
1618
// PosTest1: leap year
1719
[Fact]
@@ -126,7 +128,7 @@ private int GetAYear(Calendar calendar)
126128
int maxYear, minYear;
127129
maxYear = calendar.MaxSupportedDateTime.Year;
128130
minYear = calendar.MinSupportedDateTime.Year;
129-
retVal = minYear + TestLibrary.Generator.GetInt32(-55) % (maxYear + 1 - minYear);
131+
retVal = minYear + _generator.GetInt32(-55) % (maxYear + 1 - minYear);
130132
return retVal;
131133
}
132134

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarGetEra.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace System.Globalization.CalendarTests
1010
// GregorianCalendar.GetEra(DateTime)
1111
public class GregorianCalendarGetEra
1212
{
13+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
14+
1315
private const int c_DAYS_IN_LEAP_YEAR = 366;
1416
private const int c_DAYS_IN_COMMON_YEAR = 365;
1517
private const int c_AD_ERA = 1;
@@ -44,7 +46,7 @@ public void PosTest2()
4446
//Get a random value beween 1 and 12 not including 2.
4547
do
4648
{
47-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
49+
month = _generator.GetInt32(-55) % 12 + 1;
4850
} while (2 == month);
4951
time = myCalendar.ToDateTime(year, month, 28, 10, 30, 20, 0);
5052
expectedEra = c_AD_ERA;
@@ -80,7 +82,7 @@ public void PosTest4()
8082
//Get a random value beween 1 and 12 not including 2.
8183
do
8284
{
83-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
85+
month = _generator.GetInt32(-55) % 12 + 1;
8486
} while (2 == month);
8587
time = myCalendar.ToDateTime(year, month, 28, 10, 30, 20, 0);
8688
expectedEra = c_AD_ERA;
@@ -97,7 +99,7 @@ public void PosTest5()
9799
DateTime time;
98100
int expectedEra, actualEra;
99101
year = myCalendar.MinSupportedDateTime.Year;
100-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
102+
month = _generator.GetInt32(-55) % 12 + 1;
101103
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
102104
expectedEra = c_AD_ERA;
103105
actualEra = myCalendar.GetEra(time);
@@ -113,7 +115,7 @@ public void PosTest6()
113115
DateTime time;
114116
int expectedEra, actualEra;
115117
year = myCalendar.MaxSupportedDateTime.Year;
116-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
118+
month = _generator.GetInt32(-55) % 12 + 1;
117119
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
118120
expectedEra = c_AD_ERA;
119121
actualEra = myCalendar.GetEra(time);
@@ -161,7 +163,7 @@ public void PosTest9()
161163
DateTime time;
162164
int expectedEra, actualEra;
163165
year = GetAYear(myCalendar);
164-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
166+
month = _generator.GetInt32(-55) % 12 + 1;
165167
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
166168
expectedEra = c_AD_ERA;
167169
actualEra = myCalendar.GetEra(time);
@@ -188,7 +190,7 @@ private int GetAYear(Calendar calendar)
188190
int maxYear, minYear;
189191
maxYear = calendar.MaxSupportedDateTime.Year;
190192
minYear = calendar.MinSupportedDateTime.Year;
191-
retVal = minYear + TestLibrary.Generator.GetInt32(-55) % (maxYear + 1 - minYear);
193+
retVal = minYear + _generator.GetInt32(-55) % (maxYear + 1 - minYear);
192194
return retVal;
193195
}
194196

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarGetMonth.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class GregorianCalendarGetMonth
1212
{
1313
private const int c_DAYS_IN_LEAP_YEAR = 366;
1414
private const int c_DAYS_IN_COMMON_YEAR = 365;
15+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
1516

1617
#region Positive tests
1718
// PosTest1: the speicified time is in leap year, February
@@ -42,7 +43,7 @@ public void PosTest2()
4243
//Get a random value beween 1 and 12 not including 2.
4344
do
4445
{
45-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
46+
month = _generator.GetInt32(-55) % 12 + 1;
4647
} while (2 == month);
4748
time = myCalendar.ToDateTime(year, month, 28, 10, 30, 20, 0);
4849
expectedMonth = month;
@@ -78,7 +79,7 @@ public void PosTest4()
7879
//Get a random value beween 1 and 12 not including 2.
7980
do
8081
{
81-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
82+
month = _generator.GetInt32(-55) % 12 + 1;
8283
} while (2 == month);
8384
time = myCalendar.ToDateTime(year, month, 28, 10, 30, 20, 0);
8485
expectedMonth = month;
@@ -95,7 +96,7 @@ public void PosTest5()
9596
DateTime time;
9697
int expectedMonth, actualMonth;
9798
year = myCalendar.MinSupportedDateTime.Year;
98-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
99+
month = _generator.GetInt32(-55) % 12 + 1;
99100
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
100101
expectedMonth = month;
101102
actualMonth = myCalendar.GetMonth(time);
@@ -111,7 +112,7 @@ public void PosTest6()
111112
DateTime time;
112113
int expectedMonth, actualMonth;
113114
year = myCalendar.MaxSupportedDateTime.Year;
114-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
115+
month = _generator.GetInt32(-55) % 12 + 1;
115116
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
116117
expectedMonth = month;
117118
actualMonth = myCalendar.GetMonth(time);
@@ -159,7 +160,7 @@ public void PosTest9()
159160
DateTime time;
160161
int expectedMonth, actualMonth;
161162
year = GetAYear(myCalendar);
162-
month = TestLibrary.Generator.GetInt32(-55) % 12 + 1;
163+
month = _generator.GetInt32(-55) % 12 + 1;
163164
time = myCalendar.ToDateTime(year, month, 20, 8, 20, 30, 0);
164165
expectedMonth = month;
165166
actualMonth = myCalendar.GetMonth(time);
@@ -186,7 +187,7 @@ private int GetAYear(Calendar calendar)
186187
int maxYear, minYear;
187188
maxYear = calendar.MaxSupportedDateTime.Year;
188189
minYear = calendar.MinSupportedDateTime.Year;
189-
retVal = minYear + TestLibrary.Generator.GetInt32(-55) % (maxYear + 1 - minYear);
190+
retVal = minYear + _generator.GetInt32(-55) % (maxYear + 1 - minYear);
190191
return retVal;
191192
}
192193

src/System.Globalization.Calendars/tests/GregorianCalendar/GregorianCalendarGetMonthsInYear.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class GregorianCalendarGetMonthsInYear
1313
private const int c_MONTHS_IN_YEAR = 12;
1414
private const int c_AD_ERA = 1;
1515
private const int c_CURRENT_ERA = 0;
16+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
1617

1718
#region Positive tests
1819
// PosTest2: leap year, era is anno Domini
@@ -236,7 +237,7 @@ private int GetAYear(Calendar calendar)
236237
int maxYear, minYear;
237238
maxYear = calendar.MaxSupportedDateTime.Year;
238239
minYear = calendar.MinSupportedDateTime.Year;
239-
retVal = minYear + TestLibrary.Generator.GetInt32(-55) % (maxYear + 1 - minYear);
240+
retVal = minYear + _generator.GetInt32(-55) % (maxYear + 1 - minYear);
240241
return retVal;
241242
}
242243

0 commit comments

Comments
 (0)