1
// ***********************************************************************
2
// Copyright (c) 2009 Charlie Poole
4
// Permission is hereby granted, free of charge, to any person obtaining
5
// a copy of this software and associated documentation files (the
6
// "Software"), to deal in the Software without restriction, including
7
// without limitation the rights to use, copy, modify, merge, publish,
8
// distribute, sublicense, and/or sell copies of the Software, and to
9
// permit persons to whom the Software is furnished to do so, subject to
10
// the following conditions:
12
// The above copyright notice and this permission notice shall be
13
// included in all copies or substantial portions of the Software.
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
// ***********************************************************************
25
using System.Reflection;
27
namespace NUnit.Framework.Internal
29
public class RandomizerTests
32
public void RandomSeedsAreUnique()
34
int[] seeds = new int[10];
35
for (int i = 0; i < 10; i++)
36
seeds[i] = Randomizer.RandomSeed;
38
Assert.That(seeds, Is.Unique);
42
public void RandomIntsAreUnique()
44
Randomizer r = new Randomizer();
46
int[] values = new int[10];
47
for (int i = 0; i < 10; i++)
50
Assert.That(values, Is.Unique);
54
public void RandomDoublesAreUnique()
56
Randomizer r = new Randomizer();
58
double[] values = new double[10];
59
for (int i = 0; i < 10; i++)
60
values[i] = r.NextDouble();
62
Assert.That(values, Is.Unique);
66
public void CanGetArrayOfRandomInts()
68
Randomizer r = new Randomizer();
70
int[] ints = r.GetInts(1, 100, 10);
71
Assert.That(ints.Length, Is.EqualTo(10));
72
foreach (int i in ints)
73
Assert.That(i, Is.InRange(1, 100));
77
public void CanGetArrayOfRandomDoubles()
79
Randomizer r = new Randomizer();
81
double[] doubles = r.GetDoubles(0.5, 1.5, 10);
82
Assert.That(doubles.Length, Is.EqualTo(10));
83
foreach (double d in doubles)
84
Assert.That(d, Is.InRange(0.5, 1.5));
86
// Heuristic: Could fail occasionally
87
Assert.That(doubles, Is.Unique);
91
public void CanGetArrayOfRandomEnums()
93
Randomizer r = new Randomizer();
95
object[] enums = r.GetEnums(10, typeof(AttributeTargets));
96
Assert.That(enums.Length, Is.EqualTo(10));
97
foreach (object e in enums)
98
Assert.That(e, Is.TypeOf(typeof(AttributeTargets)));
102
public void RandomizersWithSameSeedsReturnSameValues()
104
Randomizer r1 = new Randomizer(1234);
105
Randomizer r2 = new Randomizer(1234);
107
for (int i = 0; i < 10; i++)
108
Assert.That(r1.NextDouble(), Is.EqualTo(r2.NextDouble()));
112
public void RandomizersWithDifferentSeedsReturnDifferentValues()
114
Randomizer r1 = new Randomizer(1234);
115
Randomizer r2 = new Randomizer(4321);
117
for (int i = 0; i < 10; i++)
118
Assert.That(r1.NextDouble(), Is.Not.EqualTo(r2.NextDouble()));
122
public void ReturnsSameRandomizerForSameParameter()
124
ParameterInfo p = testMethod1.GetParameters()[0];
125
Randomizer r1 = Randomizer.GetRandomizer(p);
126
Randomizer r2 = Randomizer.GetRandomizer(p);
127
Assert.That(r1, Is.SameAs(r2));
131
public void ReturnsSameRandomizerForDifferentParametersOfSameMethod()
133
ParameterInfo p1 = testMethod1.GetParameters()[0];
134
ParameterInfo p2 = testMethod1.GetParameters()[1];
135
Randomizer r1 = Randomizer.GetRandomizer(p1);
136
Randomizer r2 = Randomizer.GetRandomizer(p2);
137
Assert.That(r1, Is.SameAs(r2));
141
public void ReturnsSameRandomizerForSameMethod()
143
Randomizer r1 = Randomizer.GetRandomizer(testMethod1);
144
Randomizer r2 = Randomizer.GetRandomizer(testMethod1);
145
Assert.That(r1, Is.SameAs(r2));
149
public void ReturnsDifferentRandomizersForDifferentMethods()
151
Randomizer r1 = Randomizer.GetRandomizer(testMethod1);
152
Randomizer r2 = Randomizer.GetRandomizer(testMethod2);
153
Assert.That(r1, Is.Not.SameAs(r2));
156
static readonly MethodInfo testMethod1 =
157
typeof(RandomizerTests).GetMethod("TestMethod1", BindingFlags.NonPublic | BindingFlags.Instance);
158
private void TestMethod1(int x, int y)
162
static readonly MethodInfo testMethod2 =
163
typeof(RandomizerTests).GetMethod("TestMethod2", BindingFlags.NonPublic | BindingFlags.Instance);
164
private void TestMethod2(int x, int y)
168
private int CountUniqueValues(Array array)
172
for (int index = 0; index < array.Length; index++)
174
bool isUnique = true;
175
for (int index2 = 0; index2 < index; index2++)
176
if (array.GetValue(index).Equals(array.GetValue(index2)))