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

Commit 1b5d9fb

Browse files
committed
Fix System.Text.Encoding tests to use non-static RNG
1 parent a322dba commit 1b5d9fb

32 files changed

+199
-576
lines changed

src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingGetByteCount1.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class ASCIIEncodingGetByteCount1
1212
{
1313
private const int c_MIN_STRING_LENGTH = 2;
1414
private const int c_MAX_STRING_LENGTH = 260;
15+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
1516

1617
// PosTest1: The specified string is string.Empty.
1718
[Fact]
@@ -27,7 +28,7 @@ public void PosTest2()
2728
string source;
2829
int expectedValue;
2930

30-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
31+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
3132

3233
expectedValue = source.Length;
3334
DoPosTest(source, expectedValue);

src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingGetByteCount2.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace System.Text.EncodingTests
1212
public class ASCIIEncodingGetByteCount2
1313
{
1414
private const int c_MAX_ARRAY_LENGTH = 260;
15+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
1516

1617
// PosTest1: The specified character array is zero-length array.
1718
[Fact]
@@ -29,8 +30,8 @@ public void PosTest2()
2930
int expectedValue;
3031

3132
InitializeCharacterArray(out chars);
32-
startIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
33-
count = TestLibrary.Generator.GetInt32(-55) % (chars.Length - startIndex) + 1;
33+
startIndex = _generator.GetInt32(-55) % chars.Length;
34+
count = _generator.GetInt32(-55) % (chars.Length - startIndex) + 1;
3435
expectedValue = count;
3536
DoPosTest(chars, startIndex, count,
3637
expectedValue);
@@ -94,8 +95,8 @@ public void NegTest2()
9495
int startIndex, count;
9596

9697
InitializeCharacterArray(out chars);
97-
startIndex = -1 * TestLibrary.Generator.GetInt32(-55) - 1;
98-
count = TestLibrary.Generator.GetInt32(-55) % chars.Length + 1;
98+
startIndex = -1 * _generator.GetInt32(-55) - 1;
99+
count = _generator.GetInt32(-55) % chars.Length + 1;
99100

100101
DoNegAOORTest(chars, startIndex, count);
101102
}
@@ -108,8 +109,8 @@ public void NegTest3()
108109
int startIndex, count;
109110

110111
InitializeCharacterArray(out chars);
111-
startIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
112-
count = -1 * TestLibrary.Generator.GetInt32(-55) - 1;
112+
startIndex = _generator.GetInt32(-55) % chars.Length;
113+
count = -1 * _generator.GetInt32(-55) - 1;
113114

114115
DoNegAOORTest(chars, startIndex, count);
115116
}
@@ -122,8 +123,8 @@ public void NegTest4()
122123
int startIndex, count;
123124

124125
InitializeCharacterArray(out chars);
125-
startIndex = chars.Length + TestLibrary.Generator.GetInt32(-55) % (Int32.MaxValue - chars.Length);
126-
count = TestLibrary.Generator.GetInt32(-55) % chars.Length + 1;
126+
startIndex = chars.Length + _generator.GetInt32(-55) % (Int32.MaxValue - chars.Length);
127+
count = _generator.GetInt32(-55) % chars.Length + 1;
127128

128129
DoNegAOORTest(chars, startIndex, count);
129130
}
@@ -136,8 +137,8 @@ public void NegTest5()
136137
int startIndex, count;
137138

138139
InitializeCharacterArray(out chars);
139-
startIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
140-
count = chars.Length + 1 + TestLibrary.Generator.GetInt32(-55) % (Int32.MaxValue - chars.Length);
140+
startIndex = _generator.GetInt32(-55) % chars.Length;
141+
count = chars.Length + 1 + _generator.GetInt32(-55) % (Int32.MaxValue - chars.Length);
141142

142143
DoNegAOORTest(chars, startIndex, count);
143144
}
@@ -155,11 +156,11 @@ private void DoNegAOORTest(char[] chars, int startIndex, int count)
155156
private void InitializeCharacterArray(out char[] chars)
156157
{
157158
//Get a character array whose length is beween 1 and c_MAX_ARRAY_LENGTH
158-
int length = TestLibrary.Generator.GetInt32(-55) % c_MAX_ARRAY_LENGTH + 1;
159+
int length = _generator.GetInt32(-55) % c_MAX_ARRAY_LENGTH + 1;
159160
chars = new char[length];
160161
for (int i = 0; i < chars.Length; ++i)
161162
{
162-
chars[i] = TestLibrary.Generator.GetChar(-55);
163+
chars[i] = _generator.GetChar(-55);
163164
}
164165
}
165166
}

src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingGetBytes1.cs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class ASCIIEncodingGetBytes1
1616
private const char c_MIN_ASCII_CHAR = (char)0x0;
1717
private const char c_MAX_ASCII_CHAR = (char)0x7f;
1818

19+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
20+
1921
[Fact]
2022
public void PosTest1()
2123
{
@@ -32,18 +34,18 @@ public void PosTest2()
3234
int byteIndex;
3335

3436
ascii = new ASCIIEncoding();
35-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
36-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
37-
count = TestLibrary.Generator.GetInt32(-55) % (source.Length - charIndex) + 1;
37+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
38+
charIndex = _generator.GetInt32(-55) % source.Length;
39+
count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1;
3840

3941
int minLength = ascii.GetByteCount(source.Substring(charIndex, count));
40-
int length = minLength + TestLibrary.Generator.GetInt32(-55) % (Int16.MaxValue - minLength);
42+
int length = minLength + _generator.GetInt32(-55) % (Int16.MaxValue - minLength);
4143
bytes = new byte[length];
4244
for (int i = 0; i < bytes.Length; ++i)
4345
{
4446
bytes[i] = 0;
4547
}
46-
byteIndex = TestLibrary.Generator.GetInt32(-55) % (bytes.Length - minLength + 1);
48+
byteIndex = _generator.GetInt32(-55) % (bytes.Length - minLength + 1);
4749

4850
DoPosTest(ascii, source, charIndex, count, bytes, byteIndex);
4951
}
@@ -114,9 +116,9 @@ public void NegTest2()
114116
int byteIndex;
115117

116118
ascii = new ASCIIEncoding();
117-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
118-
charIndex = -1 * TestLibrary.Generator.GetInt32(-55) - 1;
119-
count = TestLibrary.Generator.GetInt32(-55) % source.Length + 1;
119+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
120+
charIndex = -1 * _generator.GetInt32(-55) - 1;
121+
count = _generator.GetInt32(-55) % source.Length + 1;
120122
bytes = new byte[count];
121123
byteIndex = 0;
122124

@@ -134,9 +136,9 @@ public void NegTest3()
134136
int byteIndex;
135137

136138
ascii = new ASCIIEncoding();
137-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
138-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
139-
count = -1 * TestLibrary.Generator.GetInt32(-55) % source.Length - 1;
139+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
140+
charIndex = _generator.GetInt32(-55) % source.Length;
141+
count = -1 * _generator.GetInt32(-55) % source.Length - 1;
140142
bytes = new byte[source.Length];
141143
byteIndex = 0;
142144

@@ -154,11 +156,11 @@ public void NegTest4()
154156
int byteIndex;
155157

156158
ascii = new ASCIIEncoding();
157-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
158-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
159-
count = TestLibrary.Generator.GetInt32(-55) % (source.Length - charIndex) + 1;
159+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
160+
charIndex = _generator.GetInt32(-55) % source.Length;
161+
count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1;
160162
bytes = new byte[count];
161-
byteIndex = -1 * TestLibrary.Generator.GetInt32(-55) % source.Length - 1;
163+
byteIndex = -1 * _generator.GetInt32(-55) % source.Length - 1;
162164

163165
DoNegAOORTest(ascii, source, charIndex, count, bytes, byteIndex);
164166
}
@@ -174,8 +176,8 @@ public void NegTest5()
174176
int byteIndex;
175177

176178
ascii = new ASCIIEncoding();
177-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
178-
charIndex = source.Length + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - source.Length);
179+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
180+
charIndex = source.Length + _generator.GetInt32(-55) % (int.MaxValue - source.Length);
179181
count = 0;
180182
bytes = new byte[0];
181183
byteIndex = 0;
@@ -194,10 +196,10 @@ public void NegTest6()
194196
int byteIndex;
195197

196198
ascii = new ASCIIEncoding();
197-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
198-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
199+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
200+
charIndex = _generator.GetInt32(-55) % source.Length;
199201
count = source.Length - charIndex + 1 +
200-
TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - source.Length + charIndex);
202+
_generator.GetInt32(-55) % (int.MaxValue - source.Length + charIndex);
201203
bytes = new byte[1];
202204
byteIndex = 0;
203205

@@ -215,12 +217,12 @@ public void NegTest7()
215217
int byteIndex;
216218

217219
ascii = new ASCIIEncoding();
218-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
219-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
220-
count = TestLibrary.Generator.GetInt32(-55) % (source.Length - charIndex) + 1;
220+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
221+
charIndex = _generator.GetInt32(-55) % source.Length;
222+
count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1;
221223
int minLength = ascii.GetByteCount(source.Substring(charIndex, count));
222224
bytes = new byte[minLength];
223-
byteIndex = bytes.Length + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - bytes.Length);
225+
byteIndex = bytes.Length + _generator.GetInt32(-55) % (int.MaxValue - bytes.Length);
224226

225227
DoNegAOORTest(ascii, source, charIndex, count, bytes, byteIndex);
226228
}
@@ -236,11 +238,11 @@ public void NegTest8()
236238
int byteIndex;
237239

238240
ascii = new ASCIIEncoding();
239-
source = TestLibrary.Generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
240-
charIndex = TestLibrary.Generator.GetInt32(-55) % source.Length;
241-
count = TestLibrary.Generator.GetInt32(-55) % (source.Length - charIndex) + 1;
241+
source = _generator.GetString(-55, false, c_MIN_STRING_LENGTH, c_MAX_STRING_LENGTH);
242+
charIndex = _generator.GetInt32(-55) % source.Length;
243+
count = _generator.GetInt32(-55) % (source.Length - charIndex) + 1;
242244
int minLength = ascii.GetByteCount(source.Substring(charIndex, count));
243-
bytes = new byte[TestLibrary.Generator.GetInt32(-55) % minLength];
245+
bytes = new byte[_generator.GetInt32(-55) % minLength];
244246
byteIndex = 0;
245247

246248
Assert.Throws<ArgumentException>(() =>

src/System.Text.Encoding/tests/ASCIIEncoding/ASCIIEncodingGetBytes2.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class ASCIIEncodingGetBytes2
1919

2020
private const int c_MAX_ARRAY_LENGTH = 260;
2121

22+
private readonly RandomDataGenerator _generator = new RandomDataGenerator();
23+
2224
// PosTest1: The specified string is string.Empty.
2325
[Fact]
2426
public void PosTest1()
@@ -38,17 +40,17 @@ public void PosTest2()
3840

3941
ascii = new ASCIIEncoding();
4042
InitializeCharacterArray(out chars);
41-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
42-
count = TestLibrary.Generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
43+
charIndex = _generator.GetInt32(-55) % chars.Length;
44+
count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
4345

4446
int minLength = ascii.GetByteCount(chars, charIndex, count);
45-
int length = minLength + TestLibrary.Generator.GetInt32(-55) % (Int16.MaxValue - minLength);
47+
int length = minLength + _generator.GetInt32(-55) % (Int16.MaxValue - minLength);
4648
bytes = new byte[length];
4749
for (int i = 0; i < bytes.Length; ++i)
4850
{
4951
bytes[i] = 0;
5052
}
51-
byteIndex = TestLibrary.Generator.GetInt32(-55) % (bytes.Length - minLength + 1);
53+
byteIndex = _generator.GetInt32(-55) % (bytes.Length - minLength + 1);
5254

5355
DoPosTest(ascii, chars, charIndex, count, bytes, byteIndex);
5456
}
@@ -117,8 +119,8 @@ public void NegTest2()
117119

118120
ascii = new ASCIIEncoding();
119121
InitializeCharacterArray(out chars);
120-
charIndex = -1 * TestLibrary.Generator.GetInt32(-55) - 1;
121-
count = TestLibrary.Generator.GetInt32(-55) % chars.Length + 1;
122+
charIndex = -1 * _generator.GetInt32(-55) - 1;
123+
count = _generator.GetInt32(-55) % chars.Length + 1;
122124
bytes = new byte[count];
123125
byteIndex = 0;
124126

@@ -137,8 +139,8 @@ public void NegTest3()
137139

138140
ascii = new ASCIIEncoding();
139141
InitializeCharacterArray(out chars);
140-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
141-
count = -1 * TestLibrary.Generator.GetInt32(-55) % chars.Length - 1;
142+
charIndex = _generator.GetInt32(-55) % chars.Length;
143+
count = -1 * _generator.GetInt32(-55) % chars.Length - 1;
142144
bytes = new byte[chars.Length];
143145
byteIndex = 0;
144146

@@ -157,10 +159,10 @@ public void NegTest4()
157159

158160
ascii = new ASCIIEncoding();
159161
InitializeCharacterArray(out chars);
160-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
161-
count = TestLibrary.Generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
162+
charIndex = _generator.GetInt32(-55) % chars.Length;
163+
count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
162164
bytes = new byte[count];
163-
byteIndex = -1 * TestLibrary.Generator.GetInt32(-55) % chars.Length - 1;
165+
byteIndex = -1 * _generator.GetInt32(-55) % chars.Length - 1;
164166

165167
DoNegAOORTest(ascii, chars, charIndex, count, bytes, byteIndex);
166168
}
@@ -177,7 +179,7 @@ public void NegTest5()
177179

178180
ascii = new ASCIIEncoding();
179181
InitializeCharacterArray(out chars);
180-
charIndex = chars.Length + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - chars.Length);
182+
charIndex = chars.Length + _generator.GetInt32(-55) % (int.MaxValue - chars.Length);
181183
count = 0;
182184
bytes = new byte[0];
183185
byteIndex = 0;
@@ -197,9 +199,9 @@ public void NegTest6()
197199

198200
ascii = new ASCIIEncoding();
199201
InitializeCharacterArray(out chars);
200-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
202+
charIndex = _generator.GetInt32(-55) % chars.Length;
201203
count = chars.Length - charIndex + 1 +
202-
TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - chars.Length + charIndex);
204+
_generator.GetInt32(-55) % (int.MaxValue - chars.Length + charIndex);
203205
bytes = new byte[1];
204206
byteIndex = 0;
205207

@@ -218,11 +220,11 @@ public void NegTest7()
218220

219221
ascii = new ASCIIEncoding();
220222
InitializeCharacterArray(out chars);
221-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
222-
count = TestLibrary.Generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
223+
charIndex = _generator.GetInt32(-55) % chars.Length;
224+
count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
223225
int minLength = ascii.GetByteCount(chars, charIndex, count);
224226
bytes = new byte[minLength];
225-
byteIndex = bytes.Length + TestLibrary.Generator.GetInt32(-55) % (int.MaxValue - bytes.Length);
227+
byteIndex = bytes.Length + _generator.GetInt32(-55) % (int.MaxValue - bytes.Length);
226228

227229
DoNegAOORTest(ascii, chars, charIndex, count, bytes, byteIndex);
228230
}
@@ -239,10 +241,10 @@ public void NegTest8()
239241

240242
ascii = new ASCIIEncoding();
241243
InitializeCharacterArray(out chars);
242-
charIndex = TestLibrary.Generator.GetInt32(-55) % chars.Length;
243-
count = TestLibrary.Generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
244+
charIndex = _generator.GetInt32(-55) % chars.Length;
245+
count = _generator.GetInt32(-55) % (chars.Length - charIndex) + 1;
244246
int minLength = ascii.GetByteCount(chars, charIndex, count);
245-
bytes = new byte[TestLibrary.Generator.GetInt32(-55) % minLength];
247+
bytes = new byte[_generator.GetInt32(-55) % minLength];
246248
byteIndex = 0;
247249

248250
Assert.Throws<ArgumentException>(() =>
@@ -264,11 +266,11 @@ private void DoNegAOORTest(ASCIIEncoding ascii, char[] chars, int charIndex, int
264266
private void InitializeCharacterArray(out char[] chars)
265267
{
266268
//Get a character array whose length is beween 1 and c_MAX_ARRAY_LENGTH
267-
int length = TestLibrary.Generator.GetInt32(-55) % c_MAX_ARRAY_LENGTH + 1;
269+
int length = _generator.GetInt32(-55) % c_MAX_ARRAY_LENGTH + 1;
268270
chars = new char[length];
269271
for (int i = 0; i < chars.Length; ++i)
270272
{
271-
chars[i] = TestLibrary.Generator.GetChar(-55);
273+
chars[i] = _generator.GetChar(-55);
272274
}
273275
}
274276
}

0 commit comments

Comments
 (0)