c# – 使用AutoFixture 3生成的整数是唯一的吗?

使用IFixture.Create< int>()生成的整数是唯一的吗?

The Wiki says这些数字是随机的,但它也告诉我们这一点

The first numbers are generated within the range of [1,255],as this
is a set of values that are valid for all numeric data types. The
smallest numeric data type in .NET is System.Byte,which fits in this
range.

When the first 255 integers have been used,numbers are subsequently
picked from the range [256,32767],which corresponds to the remaining
positive numbers available for system.int16.

GitHub上的两件相关事情:

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

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

那些单元测试怎么样?

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
}

我还没有找到一个声明,说明生成的数字是唯一的,只有那些可能暗示它的信息,但我可能错了.

解决方法

目前,AutoFixture努力创造独特的数字,但它并不能保证.例如,您可以耗尽范围,这最有可能发生在字节值上.例如,如果您请求300字节值,您将获得重复项,因为只有256个值可供选择.

一旦初始设置耗尽,AutoFixture将很乐意重用值;另一种方法是抛出异常.

如果对于测试用例而言数字是唯一的很重要,我建议在测试用例本身中明确这一点.您可以组合Generator< T>与众不同:

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

如果您使用的是AutoFixture.Xunit2,则可以请求生成器< T>通过测试方法参数:

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

相关文章

原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么