Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert that a large enumerable contains only the very same item n times? #914

Closed
drauch opened this issue Sep 16, 2018 · 8 comments
Closed

Assert that a large enumerable contains only the very same item n times? #914

drauch opened this issue Sep 16, 2018 · 8 comments
Labels

Comments

@drauch
Copy link

@drauch drauch commented Sep 16, 2018

I have a collection of 100K sample numbers. In one test case I want to assert that all of them are of the same value. I tried:

values.Should().AllBeEquivalentTo(5);

This takes quite some time (around five seconds). Which is WAY to long. Is this a bug in the implementation? Why is it taking so long? I think it's the wrong function, but I couldn't find a .AllBe(5) for which I was looking for originally.

My workaround now is values.Distinct().Should().BeEquivalentTo(new[]{5}); but I don't like it because it doesn't express my intent.

Ideas on how to do that properly?

@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Sep 16, 2018

It's a performance bug. The current implementation is using a very naive approach.

@krajek

This comment has been minimized.

Copy link
Contributor

@krajek krajek commented Sep 16, 2018

@dennisdoomen Are you going to fix it or can I jump in?

@dennisdoomen

This comment has been minimized.

Copy link
Member

@dennisdoomen dennisdoomen commented Sep 17, 2018

By all means, jump in

@krajek

This comment has been minimized.

Copy link
Contributor

@krajek krajek commented Sep 19, 2018

@drauch I have just tried to reproduce the problem with following test.

[Fact] public void When_some_subject_items_are_not_equivalent_to_expectation_for_huge_table_execution_time_should_still_be_short()
{
	//-----------------------------------------------------------------------------------------------------------
	// Arrange
	//-----------------------------------------------------------------------------------------------------------
	const int N = 1000000;
	var subject = new List<int>(N) {1};
	for (int i = 1; i < N; i++)
	{
		subject.Add(2);
	}

	//-----------------------------------------------------------------------------------------------------------
	// Act
	//-----------------------------------------------------------------------------------------------------------
	Action action = () =>
	{
		try { subject.Should().AllBeEquivalentTo(1); }
		catch (Exception)
		{
			// ignored, we only care about execution time
		}
	};

	////-----------------------------------------------------------------------------------------------------------
	//// Assert
	////-----------------------------------------------------------------------------------------------------------
	action.ExecutionTime().Should().BeLessThan(150.Milliseconds());
}

On my machine it always passes, even for more elements than 100K. Could you provide minimal, complete and verifiable example?

@krajek

This comment has been minimized.

Copy link
Contributor

@krajek krajek commented Sep 19, 2018

@drauch Forget my last comment, I was looking at the wrong test after all. I confirm that execution time gets excessively long for larger collections.

@krajek krajek mentioned this issue Sep 19, 2018
3 of 3 tasks complete
@drauch

This comment has been minimized.

Copy link
Author

@drauch drauch commented Sep 26, 2018

Thanks!

@drauch

This comment has been minimized.

Copy link
Author

@drauch drauch commented Oct 21, 2018

Are you going to release this soon? :)

@jnyrup

This comment has been minimized.

Copy link
Collaborator

@jnyrup jnyrup commented Oct 21, 2018

Yes, we're fixing up the last things to get 5.5.0 ready.
https://github.com/fluentassertions/fluentassertions/milestone/12

So stay tuned, we're also eager to get out a new release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
You can’t perform that action at this time.