Skip to content

Commit ba0b795

Browse files
committed
SampleUses added for CombinatorialRandomAttribute
1 parent de35615 commit ba0b795

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/Xunit.Combinatorial.Tests/CombinatorialRandomAttributeTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,29 @@ public void ConstructorCountMinMaxValues(int count, int minValue, int maxValue)
3535
[InlineData(1, 25, 35, 234)]
3636
[InlineData(3, 100, 200, 1343)]
3737
[InlineData(24, 1000, 3000, 56732)]
38+
[InlineData(12, 576, 765, CombinatorialRandomAttribute.NoSeed)]
3839
public void ConstructorCountMinMaxValuesSeed(int count, int minValue, int maxValue, int seed)
3940
=> Check(new CombinatorialRandomAttribute(count, minValue, maxValue, seed), count, minValue, maxValue, seed);
4041

4142
[Theory]
4243
[InlineData(0)]
4344
[InlineData(-1)]
4445
[InlineData(int.MinValue)]
45-
public void ConstructorNegativeCount(int count)
46+
public void ConstructorOutOfRangeCount(int count)
4647
{
4748
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count));
4849
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count, 100));
4950
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count, 0, 100));
50-
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count, 0, 100, null));
5151
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count, 0, 100, 325));
52+
Assert.Throws<ArgumentOutOfRangeException>(() => new CombinatorialRandomAttribute(count, 0, 100, CombinatorialRandomAttribute.NoSeed));
5253
}
5354

5455
internal void Check(
5556
CombinatorialRandomAttribute attribute,
5657
int count = CombinatorialRandomAttribute.DefaultCount,
5758
int minValue = CombinatorialRandomAttribute.DefaultMinValue,
5859
int maxValue = CombinatorialRandomAttribute.DefaultMaxValue,
59-
int? seed = null)
60+
int seed = CombinatorialRandomAttribute.NoSeed)
6061
{
6162
Assert.NotNull(attribute.Values);
6263
Assert.Equal(count, attribute.Values.Length);
@@ -68,9 +69,9 @@ internal void Check(
6869
Assert.InRange(intValue, minValue, maxValue - 1);
6970
});
7071

71-
if (seed.HasValue)
72+
if (seed != CombinatorialRandomAttribute.NoSeed)
7273
{
73-
Assert.Equal(RandomIterator(count, new Random(seed.Value), minValue, maxValue), attribute.Values.Cast<int>());
74+
Assert.Equal(RandomIterator(count, new Random(seed), minValue, maxValue), attribute.Values.Cast<int>());
7475
}
7576
}
7677

src/Xunit.Combinatorial.Tests/SampleUses.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,36 @@ public void CombinatorialCustomRange([CombinatorialRange(0, 5)] int p1, [Combina
6868
Assert.True(p2 == 0 || p2 == 2);
6969
}
7070

71+
[Theory, CombinatorialData]
72+
public void CombinatorialRandomValuesDefault([CombinatorialRandom()] int p1)
73+
{
74+
Assert.InRange(p1, 0, int.MaxValue - 1);
75+
}
76+
77+
[Theory, CombinatorialData]
78+
public void CombinatorialRandomValuesCount([CombinatorialRandom(10)] int p1)
79+
{
80+
Assert.InRange(p1, 0, int.MaxValue - 1);
81+
}
82+
83+
[Theory, CombinatorialData]
84+
public void CombinatorialRandomValuesCountMaxValue([CombinatorialRandom(10, 35)] int p1)
85+
{
86+
Assert.InRange(p1, 0, 35 - 1);
87+
}
88+
89+
[Theory, CombinatorialData]
90+
public void CombinatorialRandomValuesCountMinMaxValues([CombinatorialRandom(10, -20, -5)] int p1)
91+
{
92+
Assert.InRange(p1, -20, -5 - 1);
93+
}
94+
95+
[Theory, CombinatorialData]
96+
public void CombinatorialRandomValuesCountMinMaxValuesSeed([CombinatorialRandom(10, -5, 6, 567)] int p1)
97+
{
98+
Assert.InRange(p1, -5, 6 - 1);
99+
}
100+
71101
[AttributeUsage(AttributeTargets.Parameter)]
72102
private class CustomValuesAttribute : CombinatorialValuesAttribute
73103
{

src/Xunit.Combinatorial/CombinatorialRandomAttribute.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ public class CombinatorialRandomAttribute : Attribute
2525
/// </summary>
2626
public const int DefaultMaxValue = int.MaxValue;
2727

28+
/// <summary>
29+
/// Special seed value to create System.Random class without seed.
30+
/// </summary>
31+
public const int NoSeed = int.MinValue;
32+
2833
/// <summary>
2934
/// Initializes a new instance of the <see cref="CombinatorialRandomAttribute"/> class.
3035
/// </summary>
3136
public CombinatorialRandomAttribute()
32-
: this(DefaultCount, DefaultMinValue, DefaultMaxValue, null)
37+
: this(DefaultCount, DefaultMinValue, DefaultMaxValue, NoSeed)
3338
{
3439
}
3540

@@ -41,7 +46,7 @@ public CombinatorialRandomAttribute()
4146
/// Cannot be less than 1, which would conceptually result in zero test cases.
4247
/// </param>
4348
public CombinatorialRandomAttribute(int count)
44-
: this(count, DefaultMinValue, DefaultMaxValue, null)
49+
: this(count, DefaultMinValue, DefaultMaxValue, NoSeed)
4550
{
4651
}
4752

@@ -56,7 +61,7 @@ public CombinatorialRandomAttribute(int count)
5661
/// maxValue for System.Random.Next method.
5762
/// </param>
5863
public CombinatorialRandomAttribute(int count, int maxValue)
59-
: this(count, DefaultMinValue, maxValue, null)
64+
: this(count, DefaultMinValue, maxValue, NoSeed)
6065
{
6166
}
6267

@@ -74,7 +79,7 @@ public CombinatorialRandomAttribute(int count, int maxValue)
7479
/// maxValue for System.Random.Next method.
7580
/// </param>
7681
public CombinatorialRandomAttribute(int count, int minValue, int maxValue)
77-
: this(count, minValue, maxValue, null)
82+
: this(count, minValue, maxValue, NoSeed)
7883
{
7984
}
8085

@@ -92,16 +97,16 @@ public CombinatorialRandomAttribute(int count, int minValue, int maxValue)
9297
/// maxValue for System.Random.Next method.
9398
/// </param>
9499
/// <param name="seed">
95-
/// seed for System.Random constructor.
100+
/// Seed for System.Random constructor. If Seed equal to NoSeed value then System.Random constructor whithout seed used.
96101
/// </param>
97-
public CombinatorialRandomAttribute(int count, int minValue, int maxValue, int? seed)
102+
public CombinatorialRandomAttribute(int count, int minValue, int maxValue, int seed)
98103
{
99104
if (count < 1)
100105
{
101106
throw new ArgumentOutOfRangeException(nameof(count));
102107
}
103108

104-
var random = seed.HasValue ? new Random(seed.Value) : new Random();
109+
var random = seed == NoSeed ? new Random() : new Random(seed);
105110

106111
object[] values = new object[count];
107112
for (int i = 0; i < count; i++)

0 commit comments

Comments
 (0)