AutoFixture: Creating dictionaries with relatively small key type fails ocasionally

There is a problem with generating dictionaries that have a relatively small key type (like System.Byte). Sometimes unit tests fail because of duplicate keys:

System.ArgumentException : An item with the same key has already been added.

So even though AutoFixture creates dictionaries that have only 3 elements (well at least that’s what I can observe), it doesn’t check whether the generated keys are unique.

In order to reproduce it, please run the following test:

    [Fact]
    public void Duplicate_entries()
    {
        var fixture = new Fixture();
        for (int i = 0; i < 1000; ++i)
        {
            fixture.Create<IDictionary<byte, string>>();
        }
    }

The question is: is it a bug that can be fixed, or is it a corner case where AutoFixture is not supposed to be used?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 30 (28 by maintainers)

Commits related to this issue

Most upvoted comments

Here’s what I think RandomNumericSequenceGenerator ought to do:

Bytes

Draw values from the set [1 .. 255], making sure that each value is drawn only once. The 256th time a value is drawn, it would have to reset the set to [1 .. 255] and start over.

Shorts

Draw values from the set [1 .. 255], making sure that each value is drawn only once. The 256th time a value is drawn, it should draw values from the set [256 .. 32767], making sure that each value is drawn only once. The 32768th time a value is drawn, it would have to reset the set to [1 .. 255] and start over.

Ints

Draw values from the set [1 .. 255], making sure that each value is drawn only once. The 256th time a value is drawn, it should draw values from the set [256 .. 32767], making sure that each value is drawn only once. The 32768th time a value is drawn, it should draw values from the set [32768 .. 2147483647], making sure that each value is drawn only once. The 2147483648th time a value is drawn, it would have to reset the set to [1 .. 255] and start over.

I’m sure you can extrapolate the algorithm for long integers 😉

If it doesn’t do this ATM, I would consider that a bug. The above was the original specification.