Are integers created with AutoFixture 3 unique?

Are integers generated with IFixture.Create<int>() unique?

The wiki says the numbers are random, but it also tells us that.

The first numbers are generated in the range from [1, 255], since this is a set of values ​​valid for all numeric data types. the smallest numeric data type in .NET is System.Byte, which matches this Range.

When the first 255 integers were used, the numbers were subsequently selected from the range [256, 32767], which corresponds to the remaining positive numbers available for System.Int16.

Two things on github:

https://github.com/AutoFixture/AutoFixture/issues/2

https://github.com/AutoFixture/AutoFixture/pull/7

What about those unit tests?

https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L33

 [Theory, ClassData(typeof(CountTestCases))] public void StronglyTypedEnumerationYieldsUniqueValues(int count) { // Fixture setup var sut = new Generator<T>(new Fixture()); // Exercise system var actual = sut.Take(count); // Verify outcome Assert.Equal(count, actual.Distinct().Count()); // Teardown } 

https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoFixtureUnitTest/GeneratorTest.cs#L57

 [Theory, ClassData(typeof(CountTestCases))] public void WeaklyTypedEnumerationYieldsUniqueValues(int count) { // Fixture setup IEnumerable sut = new Generator<T>(new Fixture()); // Exercise system var actual = sut.OfType<T>().Take(count); // Verify outcome Assert.Equal(count, actual.Distinct().Count()); // Teardown } 

I did not find an operator that says that the generated numbers are unique, only those bits of information that could offer, but I could be wrong.

+7
c # autofixture
source share
2 answers

AutoFixture currently seeks to create unique numbers, but does not guarantee it . For example, you can exhaust the range that is likely to happen for byte values. If, for example, you request 300 byte values, you will get duplicates, because there are only 256 values ​​to choose from.

AutoFixture will happily use the values ​​after the original set has been exhausted; an alternative would be an exception.

If it is important for the test case that the numbers are unique, I would recommend making this explicit in the test case itself. You can combine Generator<T> with Distinct to do this:

 var uniqueIntegers = new Generator<int>(new Fixture()).Distinct().Take(10); 

If you use AutoFixture.Xunit2 , you can query Generator<T> using the argument of the test method:

 [Theory, AutoData] public void MyTest(Generator<int> g, string foo) { var uniqueIntegers = g.Distinct().Take(10); // more test code goes here... } 
+8
source share

The first numbers are generated in the range from [1, 255] and there are no duplicates. Above this range, it is currently possible to get duplicates.

This issue contains useful information on how to improve the built-in algorithm.

+7
source share

All Articles